for new docs
This commit is contained in:
parent
677fcc27c4
commit
2e6b3987d1
@ -24,7 +24,7 @@ func main() {
|
|||||||
wait()
|
wait()
|
||||||
}
|
}
|
||||||
// 下面都是运行示例,因此首先把服务器的数据重新准备好
|
// 下面都是运行示例,因此首先把服务器的数据重新准备好
|
||||||
dtmsvr.PopulateMysql()
|
dtmsvr.PopulateMysql(true)
|
||||||
dtmsvr.MainStart()
|
dtmsvr.MainStart()
|
||||||
if len(os.Args) == 1 { // 默认没有参数的情况下,准备好数据并启动dtmsvr即可
|
if len(os.Args) == 1 { // 默认没有参数的情况下,准备好数据并启动dtmsvr即可
|
||||||
wait()
|
wait()
|
||||||
@ -37,7 +37,7 @@ func main() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// 下面是各类的例子
|
// 下面是各类的例子
|
||||||
examples.PopulateMysql()
|
examples.PopulateMysql(true)
|
||||||
app := examples.BaseAppStartup()
|
app := examples.BaseAppStartup()
|
||||||
if os.Args[1] == "xa" { // 启动xa示例
|
if os.Args[1] == "xa" { // 启动xa示例
|
||||||
examples.XaSetup(app)
|
examples.XaSetup(app)
|
||||||
@ -47,7 +47,7 @@ func main() {
|
|||||||
examples.SagaFireRequest()
|
examples.SagaFireRequest()
|
||||||
} else if os.Args[1] == "tcc" { // 启动tcc示例
|
} else if os.Args[1] == "tcc" { // 启动tcc示例
|
||||||
examples.TccSetup(app)
|
examples.TccSetup(app)
|
||||||
examples.TccFireRequest()
|
examples.TccFireRequestNested()
|
||||||
} else if os.Args[1] == "msg" { // 启动msg示例
|
} else if os.Args[1] == "msg" { // 启动msg示例
|
||||||
examples.MsgSetup(app)
|
examples.MsgSetup(app)
|
||||||
examples.MsgFireRequest()
|
examples.MsgFireRequest()
|
||||||
@ -57,7 +57,7 @@ func main() {
|
|||||||
examples.XaSetup(app)
|
examples.XaSetup(app)
|
||||||
examples.MsgSetup(app)
|
examples.MsgSetup(app)
|
||||||
examples.SagaFireRequest()
|
examples.SagaFireRequest()
|
||||||
examples.TccFireRequest()
|
examples.TccFireRequestNested()
|
||||||
examples.XaFireRequest()
|
examples.XaFireRequest()
|
||||||
examples.MsgFireRequest()
|
examples.MsgFireRequest()
|
||||||
} else if os.Args[1] == "saga_barrier" {
|
} else if os.Args[1] == "saga_barrier" {
|
||||||
|
|||||||
@ -21,6 +21,9 @@ type Tcc struct {
|
|||||||
type TccGlobalFunc func(tcc *Tcc) error
|
type TccGlobalFunc func(tcc *Tcc) error
|
||||||
|
|
||||||
// TccGlobalTransaction begin a tcc global transaction
|
// TccGlobalTransaction begin a tcc global transaction
|
||||||
|
// dtm dtm服务器地址
|
||||||
|
// gid 全局事务id
|
||||||
|
// tccFunc tcc事务函数,里面会定义全局事务的分支
|
||||||
func TccGlobalTransaction(dtm string, gid string, tccFunc TccGlobalFunc) (rerr error) {
|
func TccGlobalTransaction(dtm string, gid string, tccFunc TccGlobalFunc) (rerr error) {
|
||||||
data := &M{
|
data := &M{
|
||||||
"gid": gid,
|
"gid": gid,
|
||||||
@ -67,6 +70,7 @@ func TccFromReq(c *gin.Context) (*Tcc, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// CallBranch call a tcc branch
|
// CallBranch call a tcc branch
|
||||||
|
// 函数首先注册子事务的所有分支,成功后调用try分支,返回try分支的调用结果
|
||||||
func (t *Tcc) CallBranch(body interface{}, tryURL string, confirmURL string, cancelURL string) (*resty.Response, error) {
|
func (t *Tcc) CallBranch(body interface{}, tryURL string, confirmURL string, cancelURL string) (*resty.Response, error) {
|
||||||
branchID := t.NewBranchID()
|
branchID := t.NewBranchID()
|
||||||
resp, err := common.RestyClient.R().
|
resp, err := common.RestyClient.R().
|
||||||
|
|||||||
@ -21,8 +21,8 @@ func TestMain(m *testing.M) {
|
|||||||
TransProcessedTestChan = make(chan string, 1)
|
TransProcessedTestChan = make(chan string, 1)
|
||||||
common.InitApp(common.GetProjectDir(), &config)
|
common.InitApp(common.GetProjectDir(), &config)
|
||||||
config.Mysql["database"] = dbName
|
config.Mysql["database"] = dbName
|
||||||
PopulateMysql()
|
PopulateMysql(false)
|
||||||
examples.PopulateMysql()
|
examples.PopulateMysql(false)
|
||||||
// 启动组件
|
// 启动组件
|
||||||
go StartSvr()
|
go StartSvr()
|
||||||
app = examples.BaseAppStartup()
|
app = examples.BaseAppStartup()
|
||||||
@ -33,10 +33,6 @@ func TestMain(m *testing.M) {
|
|||||||
examples.TccBarrierAddRoute(app)
|
examples.TccBarrierAddRoute(app)
|
||||||
examples.SagaBarrierAddRoute(app)
|
examples.SagaBarrierAddRoute(app)
|
||||||
|
|
||||||
// 清理数据
|
|
||||||
e2p(dbGet().Exec("truncate trans_global").Error)
|
|
||||||
e2p(dbGet().Exec("truncate trans_branch").Error)
|
|
||||||
e2p(dbGet().Exec("truncate trans_log").Error)
|
|
||||||
examples.ResetXaData()
|
examples.ResetXaData()
|
||||||
m.Run()
|
m.Run()
|
||||||
}
|
}
|
||||||
|
|||||||
@ -15,5 +15,6 @@ func TestExamples(t *testing.T) {
|
|||||||
assertSucceed(t, examples.SagaFireRequest())
|
assertSucceed(t, examples.SagaFireRequest())
|
||||||
assertSucceed(t, examples.TccBarrierFireRequest())
|
assertSucceed(t, examples.TccBarrierFireRequest())
|
||||||
assertSucceed(t, examples.TccFireRequest())
|
assertSucceed(t, examples.TccFireRequest())
|
||||||
|
assertSucceed(t, examples.TccFireRequestNested())
|
||||||
assertSucceed(t, examples.XaFireRequest())
|
assertSucceed(t, examples.XaFireRequest())
|
||||||
}
|
}
|
||||||
|
|||||||
@ -30,8 +30,8 @@ func StartSvr() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PopulateMysql setup mysql data
|
// PopulateMysql setup mysql data
|
||||||
func PopulateMysql() {
|
func PopulateMysql(skipDrop bool) {
|
||||||
common.InitApp(common.GetProjectDir(), &config)
|
common.InitApp(common.GetProjectDir(), &config)
|
||||||
config.Mysql["database"] = ""
|
config.Mysql["database"] = ""
|
||||||
examples.RunSQLScript(config.Mysql, common.GetCurrentCodeDir()+"/dtmsvr.sql")
|
examples.RunSQLScript(config.Mysql, common.GetCurrentCodeDir()+"/dtmsvr.sql", skipDrop)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -9,7 +9,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
// RunSQLScript 1
|
// RunSQLScript 1
|
||||||
func RunSQLScript(mysql map[string]string, script string) {
|
func RunSQLScript(mysql map[string]string, script string, skipDrop bool) {
|
||||||
conf := map[string]string{}
|
conf := map[string]string{}
|
||||||
common.MustRemarshal(mysql, &conf)
|
common.MustRemarshal(mysql, &conf)
|
||||||
conf["database"] = ""
|
conf["database"] = ""
|
||||||
@ -22,7 +22,7 @@ func RunSQLScript(mysql map[string]string, script string) {
|
|||||||
sqls := strings.Split(string(content), ";")
|
sqls := strings.Split(string(content), ";")
|
||||||
for _, sql := range sqls {
|
for _, sql := range sqls {
|
||||||
s := strings.TrimSpace(sql)
|
s := strings.TrimSpace(sql)
|
||||||
if s == "" {
|
if s == "" || skipDrop && strings.Contains(s, "drop") {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
logrus.Printf("executing: '%s'", s)
|
logrus.Printf("executing: '%s'", s)
|
||||||
@ -31,8 +31,8 @@ func RunSQLScript(mysql map[string]string, script string) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// PopulateMysql populate example mysql data
|
// PopulateMysql populate example mysql data
|
||||||
func PopulateMysql() {
|
func PopulateMysql(skipDrop bool) {
|
||||||
common.InitApp(common.GetProjectDir(), &config)
|
common.InitApp(common.GetProjectDir(), &config)
|
||||||
config.Mysql["database"] = dbName
|
config.Mysql["database"] = dbName
|
||||||
RunSQLScript(config.Mysql, common.GetCurrentCodeDir()+"/examples.sql")
|
RunSQLScript(config.Mysql, common.GetCurrentCodeDir()+"/examples.sql", skipDrop)
|
||||||
}
|
}
|
||||||
|
|||||||
@ -21,8 +21,8 @@ func TccSetup(app *gin.Engine) {
|
|||||||
}))
|
}))
|
||||||
}
|
}
|
||||||
|
|
||||||
// TccFireRequest 1
|
// TccFireRequestNested 1
|
||||||
func TccFireRequest() string {
|
func TccFireRequestNested() string {
|
||||||
logrus.Printf("tcc transaction begin")
|
logrus.Printf("tcc transaction begin")
|
||||||
gid := dtmcli.MustGenGid(DtmServer)
|
gid := dtmcli.MustGenGid(DtmServer)
|
||||||
err := dtmcli.TccGlobalTransaction(DtmServer, gid, func(tcc *dtmcli.Tcc) (rerr error) {
|
err := dtmcli.TccGlobalTransaction(DtmServer, gid, func(tcc *dtmcli.Tcc) (rerr error) {
|
||||||
@ -36,3 +36,18 @@ func TccFireRequest() string {
|
|||||||
e2p(err)
|
e2p(err)
|
||||||
return gid
|
return gid
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TccFireRequest 1
|
||||||
|
func TccFireRequest() string {
|
||||||
|
logrus.Printf("tcc simple transaction begin")
|
||||||
|
gid := dtmcli.MustGenGid(DtmServer)
|
||||||
|
err := dtmcli.TccGlobalTransaction(DtmServer, gid, func(tcc *dtmcli.Tcc) (rerr error) {
|
||||||
|
res1, rerr := tcc.CallBranch(&TransReq{Amount: 30}, Busi+"/TransOut", Busi+"/TransOutConfirm", Busi+"/TransOutRevert")
|
||||||
|
e2p(rerr)
|
||||||
|
res2, rerr := tcc.CallBranch(&TransReq{Amount: 30}, Busi+"/TransIn", Busi+"/TransInConfirm", Busi+"/TransInRevert")
|
||||||
|
logrus.Printf("tcc returns: %s, %s", res1.String(), res2.String())
|
||||||
|
return
|
||||||
|
})
|
||||||
|
e2p(err)
|
||||||
|
return gid
|
||||||
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user