dtm源码编译安装-运行客户端demo

第一步:dtm源码编译安装
git clone https://github.com/dtm-labs/dtm && cd dtm
或手动下载https://github.com/dtm-labs/dtm

编译器:goland
运行:
同级目录下有main.go 执行编译(go run main.go),第一次需要下载依赖git包
编译:
windows下默认编译生成.exe的可执行程序,linux下默认编译生成二进制可执行程序,mac同理不同环境的可执行程序不同
执行(go build main.go)
修改不同环境下可执行程序:
Linux
CGO_ENABLED=0 GOOS=darwin GOARCH=amd64 go install test
CGO_ENABLED=0 GOOS=windows GOARCH=amd64 go install test
Windows
SET CGO_ENABLED=0 SET GOOS=darwin SET GOARCH=amd64 go install test
SET CGO_ENABLED=0 SET GOOS=darwin SET GOARCH=amd64 go install test
SET CGO_ENABLED=0 SET GOOS=windows SET GOARCH=amd64 go install test

存储引擎:
默认boltdb:
boltdb是一种内嵌的kv存储,常被etcd用作存储引擎,支持ACID。详细配置参考conf.sample.yml中Store.Driver:"boltdb"部分
Mysql:
采用关系数据库进行存储,性能测试报告显示:2.6wIOPS磁盘上的的Mysql数据库,能够提供900+事务每秒,能够满足绝大部分公司的分布式事务需求。
如果上述性能不够,建议考虑下面Redis存储方案。详细配置参考conf.sample.yml中Store.Driver:"mysql"部分
Redis:
详细配置参考conf.sample.yml中Store.Driver:"redis"部分

第二步:运行,其他语言官网参考:https://dtm.pub/ref/sdk.html
dotnet:客户端示例: https://github.com/dtm-labs/dtmcli-csharp
示例demo: https://github.com/dtm-labs/dtmcli-csharp-sample

Java: 客户端示例:https://github.com/dtm-labs/dtmcli-java
示例demo: https://github.com/dtm-labs/dtmcli-java-sample
node
客户端示例: https://github.com/dtm-labs/dtmcli-node
示例demo: https://github.com/dtm-labs/dtmcli-node-sample

AP-TCC方式调用api参考:
这部分接口的路径前缀为/api/dtmsvr。例如后面介绍的newGid,实际路径为/api/dtmsvr/newGid
1、newGid 获取Gid:
此接口用于获取gid。dtm的gid生成采用ip+snowflake。如果发生短时间内ip重用的情况,有极低概率发生gid重复情况。
请求示例:curl ‘localhost:36789/api/dtmsvr/newGid’
响应示例:{“dtm_result”:“SUCCESS”,“gid”:“c0a8038b_4nxAcyxSX8N”}
2、prepare 准备事务
此接口用于准备事务,正常情况下,后续将提交事务。
请求示例:
curl --location --request POST ‘localhost:36789/api/dtmsvr/prepare’
–header ‘Content-Type: application/json’
–data-raw ‘{
“gid”: “xxx”,
“trans_type”: “tcc”
}’
响应示例:{“dtm_result”:“SUCCESS”}
3、submit 提交事务
此接口用于提交全局事务
请求示例:
curl --location --request POST ‘localhost:36789/api/dtmsvr/submit’
–header ‘Content-Type: application/json’
–data-raw ‘{
“gid”: “xxx”,
“trans_type”: “tcc”
}’
响应示例:{“dtm_result”:“SUCCESS”}
4、abort 回滚事务
此接口用于回滚全局事务
请求示例:
curl --location --request POST ‘localhost:36789/api/dtmsvr/abort’
–header ‘Content-Type: application/json’
–data-raw ‘{
“gid”: “xxx”,
“trans_type”: “tcc”
}’
响应示例:{“dtm_result”:“SUCCESS”}
5、registerBranch
此接口用于TCC, XA事务模式中,注册事务分支
请求示例:
curl --location --request POST ‘localhost:36789/api/dtmsvr/registerTccBranch’
–header ‘Content-Type: application/json’
–data-raw ‘{
“branch_id”:“01”,
“cancel”:“http://localhost:8081/api/busi/TransOutRevert”,
“confirm”:“http://localhost:8081/api/busi/TransOutConfirm”,
“data”:“{“amount”:30,“transInResult”:”“,“transOutResult”:”“}”,
“gid”:“c0a8038b_4nxEEGy7W5h”,
“trans_type”:“tcc”
}’
cancel:指定cancel操作的url
confirm:指定confirm操作的url
data:调用cancel/confirm时需要传递的body
响应示例:{“dtm_result”:“SUCCESS”}
6、query 查询事务
此接口用于查询指定gid的事务。
请求示例:curl ‘localhost:36789/api/dtmsvr/query?gid=xxx’
响应示例:{“branches”:[],“transaction”:{“ID”:69,“CreateTime”:“2021-10-25T08:51:27+08:00”,“UpdateTime”:“2021-10-25T08:51:39+08:00”,“gid”:“xxx”,“trans_type”:“tcc”,“data”:“”,“status”:“failed”,“query_prepared”:“”,“protocol”:“http”,“CommitTime”:null,“FinishTime”:null,“RollbackTime”:“2021-10-25T08:51:39+08:00”,“Options”:“{}”,“NextCronInterval”:10,“NextCronTime”:“2021-10-25T08:51:49+08:00”}}
7、all 批量查询事务
此接口用于批量查询事务。dtm将返回id小于last_id,按照id从大到小排序的100条数据
请求示例:curl ‘localhost:36789/api/dtmsvr/all?last_id=’
响应示例:{“transactions”:[{“ID”:69,“CreateTime”:“2021-10-25T08:51:27+08:00”,“UpdateTime”:“2021-10-25T08:51:39+08:00”,“gid”:“xxx”,“trans_type”:“tcc”,“data”:“”,“status”:“failed”,“query_prepared”:“”,“protocol”:“http”,“CommitTime”:null,“FinishTime”:null,“RollbackTime”:“2021-10-25T08:51:39+08:00”,“Options”:“{}”,“NextCronInterval”:10,“NextCronTime”:“2021-10-25T08:51:49+08:00”}]}
8、事务状态通知选项
wait_result: 等待事务结果
retry_interval: 重试间隔
timeout_to_fail: 超时失败时间
branch_headers: 自定义header
这些事务选项可以在prepare/submit请求中,发送给TM,请求示例
curl --location --request POST ‘localhost:36789/api/dtmsvr/prepare’
–header ‘Content-Type: application/json’
–data-raw ‘{
“gid”: “xxx”,
“trans_type”: “tcc”,
“wait_result”: true,
“retry_interval”: 15,
“branch_headers”:{“test_header”:“test”},
“timeout_to_fail”: 60
}’
响应示例:{“dtm_result”:“SUCCESS”}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值