dtmcli-go-sample

command module
v1.8.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 29, 2021 License: BSD-3-Clause Imports: 6 Imported by: ? modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

README

dtmcli-go-sample

dtmcli的最简go使用示例

快速开始

部署启动dtm

需要docker版本20.04以上

git clone https://github.com/dtm-labs/dtm
cd dtm && git checkout v1.7.5
docker-compose up
启动示例
go run main.go
输出

可以从dtmcli-go-sample的日志里看到执行的顺序如下:

  • TransOutTry
  • TransInTry
  • TransInConfirm
  • TransOutConfirm

整个tcc事务执行成功

示例解读

核心代码如下,示例开启一个tcc全局事务,然后在事务内部注册并调用TransOut、TransIn分支,完成后返回,剩下的二阶段Confirm,会由DTM完成

	// TccGlobalTransaction 开启一个TCC全局事务,第一个参数为dtm的地址,第二个参数是gid,第三个参数是回调函数
	err := dtmcli.TccGlobalTransaction(dtm, gid, func(tcc *dtmcli.Tcc) (resp *resty.Response, rerr error) {
		// 调用TransOut分支,三个参数分别为post的body,tryUrl,confirmUrl,cancelUrl
		// res1 为try执行的结果
		resp, rerr = tcc.CallBranch(gin.H{"amount": 30}, tccBusi+"/TransOut", tccBusi+"/TransOutConfirm", tccBusi+"/TransOutCancel")
		if rerr != nil {
			return
		}
		// 调用TransIn分支
		resp, rerr = tcc.CallBranch(gin.H{"amount": 30}, tccBusi+"/TransIn", tccBusi+"/TransInConfirm", tccBusi+"/TransInCancel")
		if rerr != nil {
			return
		}
		// 返回后,tcc会把全局事务提交,DTM会调用个分支的Confirm
		return
	})
更多示例,详见dtm-examples

Documentation

The Go Gopher

There is no documentation for this package.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL
⌂ 换网址