refactor main
This commit is contained in:
parent
91b1ccdc46
commit
7aabd05871
42
app/main.go
42
app/main.go
@ -12,54 +12,48 @@ import (
|
|||||||
type M = map[string]interface{}
|
type M = map[string]interface{}
|
||||||
|
|
||||||
func wait() {
|
func wait() {
|
||||||
|
for {
|
||||||
time.Sleep(10000 * time.Second)
|
time.Sleep(10000 * time.Second)
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
if len(os.Args) > 1 && (os.Args[1] == "quick_start" || os.Args[1] == "qs") {
|
if len(os.Args) == 1 || os.Args[1] == "dtmsvr" { // 只启动dtmsvr
|
||||||
dtmsvr.PopulateMysql()
|
|
||||||
dtmsvr.MainStart()
|
dtmsvr.MainStart()
|
||||||
examples.StartMain()
|
|
||||||
wait()
|
wait()
|
||||||
}
|
}
|
||||||
app := examples.BaseAppNew()
|
// 下面都是运行示例,因此首先把服务器的数据重新准备好
|
||||||
examples.BaseAppSetup(app)
|
|
||||||
if len(os.Args) == 1 || os.Args[1] == "dtmsvr" { // 只启动dtmsvr
|
|
||||||
go dtmsvr.MainStart()
|
|
||||||
} else if os.Args[1] == "xa" { // 启动xa示例
|
|
||||||
dtmsvr.PopulateMysql()
|
dtmsvr.PopulateMysql()
|
||||||
dtmsvr.MainStart()
|
dtmsvr.MainStart()
|
||||||
|
|
||||||
|
// quick_start 比较独立,单独作为一个例子运行,方便新人上手
|
||||||
|
if len(os.Args) > 1 && (os.Args[1] == "quick_start" || os.Args[1] == "qs") {
|
||||||
|
examples.QuickStarMain()
|
||||||
|
wait()
|
||||||
|
}
|
||||||
|
|
||||||
|
// 下面是各类的例子
|
||||||
examples.PopulateMysql()
|
examples.PopulateMysql()
|
||||||
|
app := examples.BaseAppStartup()
|
||||||
|
if os.Args[1] == "xa" { // 启动xa示例
|
||||||
examples.XaSetup(app)
|
examples.XaSetup(app)
|
||||||
examples.BaseAppStart(app)
|
|
||||||
examples.XaFireRequest()
|
examples.XaFireRequest()
|
||||||
} else if os.Args[1] == "saga" { // 启动saga示例
|
} else if os.Args[1] == "saga" { // 启动saga示例
|
||||||
dtmsvr.PopulateMysql()
|
|
||||||
dtmsvr.MainStart()
|
|
||||||
examples.SagaSetup(app)
|
examples.SagaSetup(app)
|
||||||
examples.BaseAppStart(app)
|
|
||||||
examples.SagaFireRequest()
|
examples.SagaFireRequest()
|
||||||
} else if os.Args[1] == "all" { // 运行所有示例
|
} else if os.Args[1] == "all" { // 运行所有示例
|
||||||
dtmsvr.PopulateMysql()
|
|
||||||
examples.PopulateMysql()
|
|
||||||
dtmsvr.MainStart()
|
|
||||||
examples.SagaSetup(app)
|
examples.SagaSetup(app)
|
||||||
examples.TccSetup(app)
|
examples.TccSetup(app)
|
||||||
examples.XaSetup(app)
|
examples.XaSetup(app)
|
||||||
examples.BaseAppStart(app)
|
|
||||||
examples.SagaFireRequest()
|
examples.SagaFireRequest()
|
||||||
examples.TccFireRequest()
|
examples.TccFireRequest()
|
||||||
examples.XaFireRequest()
|
examples.XaFireRequest()
|
||||||
} else if os.Args[1] == "saga_barrier" {
|
} else if os.Args[1] == "saga_barrier" {
|
||||||
dtmsvr.PopulateMysql()
|
examples.SagaBarrierAddRoute(app)
|
||||||
dtmsvr.MainStart()
|
examples.SagaBarrierFireRequest()
|
||||||
examples.PopulateMysql()
|
|
||||||
examples.SagaBarrierMainStart()
|
|
||||||
} else if os.Args[1] == "tcc_barrier" {
|
} else if os.Args[1] == "tcc_barrier" {
|
||||||
dtmsvr.PopulateMysql()
|
examples.TccBarrierAddRoute(app)
|
||||||
dtmsvr.MainStart()
|
examples.TccBarrierFireRequest()
|
||||||
examples.PopulateMysql()
|
|
||||||
examples.TccBarrierMainStart()
|
|
||||||
} else {
|
} else {
|
||||||
logrus.Fatalf("unknown arg: %s", os.Args[1])
|
logrus.Fatalf("unknown arg: %s", os.Args[1])
|
||||||
}
|
}
|
||||||
|
|||||||
@ -33,13 +33,11 @@ func TestDtmSvr(t *testing.T) {
|
|||||||
TransProcessedTestChan = make(chan string, 1)
|
TransProcessedTestChan = make(chan string, 1)
|
||||||
// 启动组件
|
// 启动组件
|
||||||
go StartSvr()
|
go StartSvr()
|
||||||
app := examples.BaseAppNew()
|
app := examples.BaseAppStartup()
|
||||||
examples.BaseAppSetup(app)
|
|
||||||
examples.SagaSetup(app)
|
examples.SagaSetup(app)
|
||||||
examples.TccSetup(app)
|
examples.TccSetup(app)
|
||||||
examples.XaSetup(app)
|
examples.XaSetup(app)
|
||||||
examples.MsgSetup(app)
|
examples.MsgSetup(app)
|
||||||
examples.BaseAppStart(app)
|
|
||||||
|
|
||||||
// 清理数据
|
// 清理数据
|
||||||
e2p(dbGet().Exec("truncate trans_global").Error)
|
e2p(dbGet().Exec("truncate trans_global").Error)
|
||||||
|
|||||||
@ -16,16 +16,14 @@ const (
|
|||||||
|
|
||||||
var Busi string = fmt.Sprintf("http://localhost:%d%s", BusiPort, BusiApi)
|
var Busi string = fmt.Sprintf("http://localhost:%d%s", BusiPort, BusiApi)
|
||||||
|
|
||||||
func BaseAppNew() *gin.Engine {
|
func BaseAppStartup() *gin.Engine {
|
||||||
logrus.Printf("examples starting")
|
logrus.Printf("examples starting")
|
||||||
app := common.GetGinApp()
|
app := common.GetGinApp()
|
||||||
return app
|
BaseAddRoute(app)
|
||||||
}
|
|
||||||
|
|
||||||
func BaseAppStart(app *gin.Engine) {
|
|
||||||
logrus.Printf("Starting busi at: %d", BusiPort)
|
logrus.Printf("Starting busi at: %d", BusiPort)
|
||||||
go app.Run(fmt.Sprintf(":%d", BusiPort))
|
go app.Run(fmt.Sprintf(":%d", BusiPort))
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
return app
|
||||||
}
|
}
|
||||||
|
|
||||||
type AutoEmptyString struct {
|
type AutoEmptyString struct {
|
||||||
@ -62,7 +60,7 @@ func handleGeneralBusiness(c *gin.Context, result1 string, result2 string, busi
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func BaseAppSetup(app *gin.Engine) {
|
func BaseAddRoute(app *gin.Engine) {
|
||||||
app.POST(BusiApi+"/TransIn", common.WrapHandler(func(c *gin.Context) (interface{}, error) {
|
app.POST(BusiApi+"/TransIn", common.WrapHandler(func(c *gin.Context) (interface{}, error) {
|
||||||
return handleGeneralBusiness(c, MainSwitch.TransInResult.Fetch(), reqFrom(c).TransInResult, "transIn")
|
return handleGeneralBusiness(c, MainSwitch.TransInResult.Fetch(), reqFrom(c).TransInResult, "transIn")
|
||||||
}))
|
}))
|
||||||
|
|||||||
@ -1,21 +1,11 @@
|
|||||||
package examples
|
package examples
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/yedf/dtm/dtmcli"
|
"github.com/yedf/dtm/dtmcli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func MsgMain() {
|
|
||||||
app := BaseAppNew()
|
|
||||||
BaseAppSetup(app)
|
|
||||||
BaseAppStart(app)
|
|
||||||
MsgFireRequest()
|
|
||||||
time.Sleep(1000 * time.Second)
|
|
||||||
}
|
|
||||||
|
|
||||||
func MsgSetup(app *gin.Engine) {
|
func MsgSetup(app *gin.Engine) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -1,23 +1,11 @@
|
|||||||
package examples
|
package examples
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/yedf/dtm/dtmcli"
|
"github.com/yedf/dtm/dtmcli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func SagaMain() {
|
|
||||||
app := BaseAppNew()
|
|
||||||
BaseAppSetup(app)
|
|
||||||
TccSetup(app)
|
|
||||||
go BaseAppStart(app)
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
TccFireRequest()
|
|
||||||
time.Sleep(1000 * time.Second)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SagaSetup(app *gin.Engine) {
|
func SagaSetup(app *gin.Engine) {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -2,8 +2,6 @@ package examples
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -12,30 +10,12 @@ import (
|
|||||||
"gorm.io/gorm"
|
"gorm.io/gorm"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 事务参与者的服务地址
|
|
||||||
const SagaBarrierBusiApi = "/api/busi_saga_barrier"
|
|
||||||
|
|
||||||
var SagaBarrierBusi = fmt.Sprintf("http://localhost:%d%s", SagaBarrierBusiPort, SagaBarrierBusiApi)
|
|
||||||
|
|
||||||
func SagaBarrierMainStart() {
|
|
||||||
SagaBarrierStartSvr()
|
|
||||||
SagaBarrierFireRequest()
|
|
||||||
}
|
|
||||||
|
|
||||||
func SagaBarrierStartSvr() {
|
|
||||||
logrus.Printf("saga barrier examples starting")
|
|
||||||
app := common.GetGinApp()
|
|
||||||
SagaBarrierAddRoute(app)
|
|
||||||
go app.Run(fmt.Sprintf(":%d", SagaBarrierBusiPort))
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
}
|
|
||||||
|
|
||||||
func SagaBarrierFireRequest() {
|
func SagaBarrierFireRequest() {
|
||||||
logrus.Printf("a busi transaction begin")
|
logrus.Printf("a busi transaction begin")
|
||||||
req := &TransReq{Amount: 30}
|
req := &TransReq{Amount: 30}
|
||||||
saga := dtmcli.NewSaga(DtmServer).
|
saga := dtmcli.NewSaga(DtmServer).
|
||||||
Add(SagaBarrierBusi+"/TransOut", SagaBarrierBusi+"/TransOutCompensate", req).
|
Add(Busi+"/SagaBTransOut", Busi+"/SagaBTransOutCompensate", req).
|
||||||
Add(SagaBarrierBusi+"/TransIn", SagaBarrierBusi+"/TransInCompensate", req)
|
Add(Busi+"/SagaBTransIn", Busi+"/SagaBTransInCompensate", req)
|
||||||
logrus.Printf("busi trans submit")
|
logrus.Printf("busi trans submit")
|
||||||
err := saga.Submit()
|
err := saga.Submit()
|
||||||
e2p(err)
|
e2p(err)
|
||||||
@ -44,49 +24,40 @@ func SagaBarrierFireRequest() {
|
|||||||
// api
|
// api
|
||||||
|
|
||||||
func SagaBarrierAddRoute(app *gin.Engine) {
|
func SagaBarrierAddRoute(app *gin.Engine) {
|
||||||
app.POST(SagaBarrierBusiApi+"/TransIn", common.WrapHandler(sagaBarrierTransIn))
|
app.POST(BusiApi+"/SagaBTransIn", common.WrapHandler(sagaBarrierTransIn))
|
||||||
app.POST(SagaBarrierBusiApi+"/TransInCompensate", common.WrapHandler(sagaBarrierTransInCompensate))
|
app.POST(BusiApi+"/SagaBTransInCompensate", common.WrapHandler(sagaBarrierTransInCompensate))
|
||||||
app.POST(SagaBarrierBusiApi+"/TransOut", common.WrapHandler(sagaBarrierTransOut))
|
app.POST(BusiApi+"/SagaBTransOut", common.WrapHandler(sagaBarrierTransOut))
|
||||||
app.POST(SagaBarrierBusiApi+"/TransOutCompensate", common.WrapHandler(sagaBarrierTransOutCompensate))
|
app.POST(BusiApi+"/SagaBTransOutCompensate", common.WrapHandler(sagaBarrierTransOutCompensate))
|
||||||
logrus.Printf("examples listening at %d", SagaBarrierBusiPort)
|
logrus.Printf("examples listening at %d", BusiPort)
|
||||||
|
}
|
||||||
|
|
||||||
|
func sagaBarrierAdjustBalance(sdb *sql.DB, uid int, amount int) (interface{}, error) {
|
||||||
|
db := common.SqlDB2DB(sdb)
|
||||||
|
dbr := db.Model(&UserAccount{}).Where("user_id = ?", 1).Update("balance", gorm.Expr("balance + ?", amount))
|
||||||
|
return "SUCCESS", dbr.Error
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func sagaBarrierTransIn(c *gin.Context) (interface{}, error) {
|
func sagaBarrierTransIn(c *gin.Context) (interface{}, error) {
|
||||||
req := reqFrom(c)
|
|
||||||
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
||||||
db := common.SqlDB2DB(sdb)
|
return sagaBarrierAdjustBalance(sdb, 1, reqFrom(c).Amount)
|
||||||
dbr := db.Model(&UserAccount{}).Where("user_id = ?", 1).
|
|
||||||
Update("balance", gorm.Expr("balance + ?", req.Amount))
|
|
||||||
return "SUCCESS", dbr.Error
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func sagaBarrierTransInCompensate(c *gin.Context) (interface{}, error) {
|
func sagaBarrierTransInCompensate(c *gin.Context) (interface{}, error) {
|
||||||
req := reqFrom(c)
|
|
||||||
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
||||||
db := common.SqlDB2DB(sdb)
|
return sagaBarrierAdjustBalance(sdb, 1, -reqFrom(c).Amount)
|
||||||
dbr := db.Model(&UserAccount{}).Where("user_id = ?", 1).
|
|
||||||
Update("balance", gorm.Expr("balance - ?", req.Amount))
|
|
||||||
return "SUCCESS", dbr.Error
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func sagaBarrierTransOut(c *gin.Context) (interface{}, error) {
|
func sagaBarrierTransOut(c *gin.Context) (interface{}, error) {
|
||||||
req := reqFrom(c)
|
|
||||||
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
||||||
db := common.SqlDB2DB(sdb)
|
return sagaBarrierAdjustBalance(sdb, 2, -reqFrom(c).Amount)
|
||||||
dbr := db.Model(&UserAccount{}).Where("user_id = ?", 2).
|
|
||||||
Update("balance", gorm.Expr("balance - ?", req.Amount))
|
|
||||||
return "SUCCESS", dbr.Error
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func sagaBarrierTransOutCompensate(c *gin.Context) (interface{}, error) {
|
func sagaBarrierTransOutCompensate(c *gin.Context) (interface{}, error) {
|
||||||
req := reqFrom(c)
|
|
||||||
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
||||||
db := common.SqlDB2DB(sdb)
|
return sagaBarrierAdjustBalance(sdb, 2, reqFrom(c).Amount)
|
||||||
dbr := db.Model(&UserAccount{}).Where("user_id = ?", 2).
|
|
||||||
Update("balance", gorm.Expr("balance + ?", req.Amount))
|
|
||||||
return "SUCCESS", dbr.Error
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -1,24 +1,12 @@
|
|||||||
package examples
|
package examples
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
"github.com/yedf/dtm/dtmcli"
|
"github.com/yedf/dtm/dtmcli"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TccMain() {
|
|
||||||
app := BaseAppNew()
|
|
||||||
BaseAppSetup(app)
|
|
||||||
TccSetup(app)
|
|
||||||
go BaseAppStart(app)
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
TccFireRequest()
|
|
||||||
time.Sleep(1000 * time.Second)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TccSetup(app *gin.Engine) {
|
func TccSetup(app *gin.Engine) {
|
||||||
app.POST(BusiApi+"/TransInTcc", common.WrapHandler(func(c *gin.Context) (interface{}, error) {
|
app.POST(BusiApi+"/TransInTcc", common.WrapHandler(func(c *gin.Context) (interface{}, error) {
|
||||||
tcc, err := dtmcli.TccFromReq(c)
|
tcc, err := dtmcli.TccFromReq(c)
|
||||||
|
|||||||
@ -3,7 +3,6 @@ package examples
|
|||||||
import (
|
import (
|
||||||
"database/sql"
|
"database/sql"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -11,35 +10,17 @@ import (
|
|||||||
"github.com/yedf/dtm/dtmcli"
|
"github.com/yedf/dtm/dtmcli"
|
||||||
)
|
)
|
||||||
|
|
||||||
// 事务参与者的服务地址
|
|
||||||
const TccBarrierBusiApi = "/api/busi_saga_barrier"
|
|
||||||
|
|
||||||
var TccBarrierBusi = fmt.Sprintf("http://localhost:%d%s", TccBarrierBusiPort, TccBarrierBusiApi)
|
|
||||||
|
|
||||||
func TccBarrierMainStart() {
|
|
||||||
TccBarrierStartSvr()
|
|
||||||
TccBarrierFireRequest()
|
|
||||||
}
|
|
||||||
|
|
||||||
func TccBarrierStartSvr() {
|
|
||||||
logrus.Printf("saga barrier examples starting")
|
|
||||||
app := common.GetGinApp()
|
|
||||||
TccBarrierAddRoute(app)
|
|
||||||
go app.Run(fmt.Sprintf(":%d", TccBarrierBusiPort))
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
}
|
|
||||||
|
|
||||||
func TccBarrierFireRequest() {
|
func TccBarrierFireRequest() {
|
||||||
logrus.Printf("tcc transaction begin")
|
logrus.Printf("tcc transaction begin")
|
||||||
_, err := dtmcli.TccGlobalTransaction(DtmServer, func(tcc *dtmcli.Tcc) (rerr error) {
|
_, err := dtmcli.TccGlobalTransaction(DtmServer, func(tcc *dtmcli.Tcc) (rerr error) {
|
||||||
res1, rerr := tcc.CallBranch(&TransReq{Amount: 30}, TccBarrierBusi+"/TransOutTry", TccBarrierBusi+"/TransOutConfirm", TccBarrierBusi+"/TransOutRevert")
|
res1, rerr := tcc.CallBranch(&TransReq{Amount: 30}, Busi+"/TccBTransOutTry", Busi+"/TccBTransOutConfirm", Busi+"/TccBTransOutRevert")
|
||||||
if rerr != nil {
|
if rerr != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
if res1.StatusCode() != 200 {
|
if res1.StatusCode() != 200 {
|
||||||
return fmt.Errorf("bad status code: %d", res1.StatusCode())
|
return fmt.Errorf("bad status code: %d", res1.StatusCode())
|
||||||
}
|
}
|
||||||
res2, rerr := tcc.CallBranch(&TransReq{Amount: 30}, TccBarrierBusi+"/TransInTry", TccBarrierBusi+"/TransInConfirm", TccBarrierBusi+"/TransInRevert")
|
res2, rerr := tcc.CallBranch(&TransReq{Amount: 30}, Busi+"/TccBTransInTry", Busi+"/TccBTransInConfirm", Busi+"/TccBTransInRevert")
|
||||||
if rerr != nil {
|
if rerr != nil {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -55,13 +36,13 @@ func TccBarrierFireRequest() {
|
|||||||
// api
|
// api
|
||||||
|
|
||||||
func TccBarrierAddRoute(app *gin.Engine) {
|
func TccBarrierAddRoute(app *gin.Engine) {
|
||||||
app.POST(TccBarrierBusiApi+"/TransInTry", common.WrapHandler(tccBarrierTransInTry))
|
app.POST(BusiApi+"/TccBTransInTry", common.WrapHandler(tccBarrierTransInTry))
|
||||||
app.POST(TccBarrierBusiApi+"/TransInConfirm", common.WrapHandler(tccBarrierTransInConfirm))
|
app.POST(BusiApi+"/TccBTransInConfirm", common.WrapHandler(tccBarrierTransInConfirm))
|
||||||
app.POST(TccBarrierBusiApi+"/TransInCancel", common.WrapHandler(tccBarrierTransInCancel))
|
app.POST(BusiApi+"/TccBTransInCancel", common.WrapHandler(tccBarrierTransInCancel))
|
||||||
app.POST(TccBarrierBusiApi+"/TransOutTry", common.WrapHandler(tccBarrierTransOutTry))
|
app.POST(BusiApi+"/TccBTransOutTry", common.WrapHandler(tccBarrierTransOutTry))
|
||||||
app.POST(TccBarrierBusiApi+"/TransOutConfirm", common.WrapHandler(tccBarrierTransOutConfirm))
|
app.POST(BusiApi+"/TccBTransOutConfirm", common.WrapHandler(tccBarrierTransOutConfirm))
|
||||||
app.POST(TccBarrierBusiApi+"/TransOutCancel", common.WrapHandler(tccBarrierTransOutCancel))
|
app.POST(BusiApi+"/TccBTransOutCancel", common.WrapHandler(tccBarrierTransOutCancel))
|
||||||
logrus.Printf("examples listening at %d", TccBarrierBusiPort)
|
logrus.Printf("examples listening at %d", BusiPort)
|
||||||
}
|
}
|
||||||
|
|
||||||
const transInUid = 1
|
const transInUid = 1
|
||||||
@ -93,43 +74,37 @@ func adjustBalance(sdb *sql.DB, uid int, amount int) (interface{}, error) {
|
|||||||
|
|
||||||
// TCC下,转入
|
// TCC下,转入
|
||||||
func tccBarrierTransInTry(c *gin.Context) (interface{}, error) {
|
func tccBarrierTransInTry(c *gin.Context) (interface{}, error) {
|
||||||
req := reqFrom(c)
|
|
||||||
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
||||||
return adjustTrading(sdb, transInUid, req.Amount)
|
return adjustTrading(sdb, transInUid, reqFrom(c).Amount)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func tccBarrierTransInConfirm(c *gin.Context) (interface{}, error) {
|
func tccBarrierTransInConfirm(c *gin.Context) (interface{}, error) {
|
||||||
req := reqFrom(c)
|
|
||||||
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
||||||
return adjustBalance(sdb, transInUid, req.Amount)
|
return adjustBalance(sdb, transInUid, reqFrom(c).Amount)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func tccBarrierTransInCancel(c *gin.Context) (interface{}, error) {
|
func tccBarrierTransInCancel(c *gin.Context) (interface{}, error) {
|
||||||
req := reqFrom(c)
|
|
||||||
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
||||||
return adjustTrading(sdb, transInUid, -req.Amount)
|
return adjustTrading(sdb, transInUid, -reqFrom(c).Amount)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func tccBarrierTransOutTry(c *gin.Context) (interface{}, error) {
|
func tccBarrierTransOutTry(c *gin.Context) (interface{}, error) {
|
||||||
req := reqFrom(c)
|
|
||||||
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
||||||
return adjustTrading(sdb, transOutUid, -req.Amount)
|
return adjustTrading(sdb, transOutUid, -reqFrom(c).Amount)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func tccBarrierTransOutConfirm(c *gin.Context) (interface{}, error) {
|
func tccBarrierTransOutConfirm(c *gin.Context) (interface{}, error) {
|
||||||
req := reqFrom(c)
|
|
||||||
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
||||||
return adjustBalance(sdb, transOutUid, -req.Amount)
|
return adjustBalance(sdb, transOutUid, -reqFrom(c).Amount)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
func tccBarrierTransOutCancel(c *gin.Context) (interface{}, error) {
|
func tccBarrierTransOutCancel(c *gin.Context) (interface{}, error) {
|
||||||
req := reqFrom(c)
|
|
||||||
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
return dtmcli.ThroughBarrierCall(dbGet().ToSqlDB(), dtmcli.TransInfoFromReq(c), func(sdb *sql.DB) (interface{}, error) {
|
||||||
return adjustTrading(sdb, transOutUid, req.Amount)
|
return adjustTrading(sdb, transOutUid, reqFrom(c).Amount)
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|||||||
@ -2,7 +2,6 @@ package examples
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/gin-gonic/gin"
|
"github.com/gin-gonic/gin"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
@ -32,15 +31,6 @@ func dbGet() *common.DB {
|
|||||||
return common.DbGet(Config.Mysql)
|
return common.DbGet(Config.Mysql)
|
||||||
}
|
}
|
||||||
|
|
||||||
func XaMain() {
|
|
||||||
app := BaseAppNew()
|
|
||||||
XaSetup(app)
|
|
||||||
go BaseAppStart(app)
|
|
||||||
time.Sleep(100 * time.Millisecond)
|
|
||||||
XaFireRequest()
|
|
||||||
time.Sleep(1000 * time.Second)
|
|
||||||
}
|
|
||||||
|
|
||||||
func XaFireRequest() {
|
func XaFireRequest() {
|
||||||
_, err := XaClient.XaGlobalTransaction(func(gid string) (rerr error) {
|
_, err := XaClient.XaGlobalTransaction(func(gid string) (rerr error) {
|
||||||
defer common.P2E(&rerr)
|
defer common.P2E(&rerr)
|
||||||
|
|||||||
@ -19,7 +19,7 @@ const qsBusiPort = 8082
|
|||||||
var qsBusi = fmt.Sprintf("http://localhost:%d%s", qsBusiPort, qsBusiApi)
|
var qsBusi = fmt.Sprintf("http://localhost:%d%s", qsBusiPort, qsBusiApi)
|
||||||
|
|
||||||
// 被app/main.go调用,启动服务并运行示例
|
// 被app/main.go调用,启动服务并运行示例
|
||||||
func StartMain() {
|
func QuickStarMain() {
|
||||||
qsStartSvr()
|
qsStartSvr()
|
||||||
qsFireRequest()
|
qsFireRequest()
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user