readme cn ok
This commit is contained in:
parent
ab90c40094
commit
d1a6359410
42
README-cn.md
42
README-cn.md
@ -1,42 +0,0 @@
|
|||||||
### 轻量级分布式事务管理服务
|
|
||||||
跨语言--语言无关,基于http协议
|
|
||||||
支持xa、tcc、saga
|
|
||||||
## 快速开始
|
|
||||||
dtm依赖于mysql
|
|
||||||
使用已有的mysql:
|
|
||||||
参考conf.yml.sample将mysql信息填写到conf.yml
|
|
||||||
或者通过docker安装mysql
|
|
||||||
docker-compose up -f docker-compose.mysql.yml
|
|
||||||
启动并运行saga示例
|
|
||||||
go run app/main.go
|
|
||||||
|
|
||||||
场景描述:
|
|
||||||
假设您实现了一个转账功能,分为两个微服务:转入、转出
|
|
||||||
转出:服务地址为 http://example.com/api/busi_saga/transOut?gid=xxx POST 参数为 {"uid": 2, "amount":30}
|
|
||||||
转入:服务地址为 http://example.com/api/busi_saga/transIn?gid=xxx POST 参数为 {"uid": 1, "amount":30}
|
|
||||||
在saga模式下,有对应的补偿微服务
|
|
||||||
转出:服务地址为 http://example.com/api/busi_saga/transOutCompensate?gid=xxx POST 参数为 {"uid": 2, "amount":30}
|
|
||||||
转入:服务地址为 http://example.com/api/busi_saga/transInCompensate?gid=xxx POST 参数为 {"uid": 1, "amount":30}
|
|
||||||
HTTP协议方式
|
|
||||||
curl -d '{"gid":"xxx","trans_type":"saga","steps":[{"action":"http://example.com/api/busi_saga/TransOut","compensate":"http://example.com/api/busi_saga/TransOutCompensate","data":"{\"amount\":30}"},{"action":"http://localhost:8081/api/busi_saga/TransIn","compensate":"http://localhost:8081/api/busi_saga/TransInCompensate","data":"{\"amount\":30}"}]}' 8.140.124.252/api/dtm/commit
|
|
||||||
此请求向dtm提交了一个saga事务,dtm会按照saga模式,请求transIn/transOut,并且在出错情况下,保证抵用相关的补偿api
|
|
||||||
go客户端方式
|
|
||||||
// 事务参与者的服务地址
|
|
||||||
const startBusiPort = 8084
|
|
||||||
const startBusiApi = "/api/busi_start"
|
|
||||||
|
|
||||||
var startBusi = fmt.Sprintf("http://localhost:%d%s", startBusiPort, startBusiApi)
|
|
||||||
err := dtm.SagaNew(DtmServer, gid).Add(startBusi+"/TransOut", startBusi+"/TransOutCompensate", &gin.H{
|
|
||||||
"amount": 30,
|
|
||||||
"uid": 2,
|
|
||||||
}).Add(startBusi+"/TransIn", startBusi+"/TransInCompensate", &gin.H{
|
|
||||||
"amount": 30,
|
|
||||||
"uid": 1
|
|
||||||
}).Commit()
|
|
||||||
|
|
||||||
本地启动方式
|
|
||||||
需要安装docker,和docker-compose
|
|
||||||
curl localhost:8080/api/initMysql
|
|
||||||
go run examples/app/main saga
|
|
||||||
|
|
||||||
其他
|
|
||||||
6
README-en.md
Normal file
6
README-en.md
Normal file
@ -0,0 +1,6 @@
|
|||||||
|
# a lightweight distributed transaction management service
|
||||||
|
|
||||||
|
### 英文[]
|
||||||
|
### 中文
|
||||||
|
|
||||||
|
近期频繁更新中
|
||||||
31
README.md
31
README.md
@ -1,6 +1,29 @@
|
|||||||
# a lightweight distributed transaction management service
|
## 轻量级分布式事务管理服务
|
||||||
|
* 跨语言
|
||||||
|
- 语言无关,基于http协议
|
||||||
|
* 多种协议支持
|
||||||
|
- 支持xa、tcc、saga
|
||||||
|
## 运行示例
|
||||||
|
### dtm依赖于mysql
|
||||||
|
使用已有的mysql:
|
||||||
|
`cp conf.sample.yml conf.yml # 修改conf.yml`
|
||||||
|
或者通过docker安装mysql
|
||||||
|
`docker-compose up -f compose.mysql.yml`
|
||||||
|
### 启动并运行saga示例
|
||||||
|
`go run app/main.go`
|
||||||
|
|
||||||
### 英文[]
|
## 开始使用
|
||||||
### 中文
|
|
||||||
|
|
||||||
近期频繁更新中
|
### 安装
|
||||||
|
`go get github.com/yedf/dtm`
|
||||||
|
### 使用
|
||||||
|
``` go
|
||||||
|
gid := common.GenGid()
|
||||||
|
req := &gin.H{"amount": 30}
|
||||||
|
saga := dtm.SagaNew(DtmServer, gid).
|
||||||
|
Add(startBusi+"/TransOut", startBusi+"/TransOutCompensate", req).
|
||||||
|
Add(startBusi+"/TransIn", startBusi+"/TransInCompensate", req)
|
||||||
|
err := saga.Commit()
|
||||||
|
```
|
||||||
|
### 完整示例
|
||||||
|
参考examples/quick_start.go
|
||||||
|
|||||||
@ -18,15 +18,19 @@ func main() {
|
|||||||
go examples.SagaStartSvr()
|
go examples.SagaStartSvr()
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
examples.SagaFireRequest()
|
examples.SagaFireRequest()
|
||||||
} else if os.Args[1] == "dtmsvr" {
|
} else if os.Args[1] == "dtmsvr" { // 只启动dtmsvr
|
||||||
go dtmsvr.StartSvr()
|
go dtmsvr.StartSvr()
|
||||||
} else if os.Args[1] == "all" {
|
} else if os.Args[1] == "all" { // 运行所有示例
|
||||||
dtmsvr.PopulateMysql()
|
dtmsvr.PopulateMysql()
|
||||||
examples.PopulateMysql()
|
examples.PopulateMysql()
|
||||||
go dtmsvr.StartSvr()
|
go dtmsvr.StartSvr()
|
||||||
go examples.SagaStartSvr()
|
go examples.SagaStartSvr()
|
||||||
go examples.TccStartSvr()
|
go examples.TccStartSvr()
|
||||||
go examples.XaStartSvr()
|
go examples.XaStartSvr()
|
||||||
|
time.Sleep(100 * time.Millisecond)
|
||||||
|
examples.SagaFireRequest()
|
||||||
|
examples.TccFireRequest()
|
||||||
|
examples.XaFireRequest()
|
||||||
} else {
|
} else {
|
||||||
logrus.Fatalf("unknown arg: %s", os.Args[1])
|
logrus.Fatalf("unknown arg: %s", os.Args[1])
|
||||||
}
|
}
|
||||||
|
|||||||
@ -5,4 +5,4 @@ Mysql:
|
|||||||
port: '3306'
|
port: '3306'
|
||||||
|
|
||||||
PreparedExpire: 90 # 单位秒,处于prepared中的任务,过了这个时间,查询结果还是PENDING的话,则会被cancel
|
PreparedExpire: 90 # 单位秒,处于prepared中的任务,过了这个时间,查询结果还是PENDING的话,则会被cancel
|
||||||
TransCronInterval: 20 # 单位秒 当事务等待这个时间之后,还没有变化,则进行一轮处理,包括prepared中的任务和commited的任务
|
TransCronInterval: 20 # 单位秒 当事务等待这个时间之后,还没有变化,则进行一轮重试处理,包括prepared中的任务和commited的任务
|
||||||
|
|||||||
@ -18,7 +18,7 @@ var TccBusi = fmt.Sprintf("http://localhost:%d%s", TccBusiPort, TccBusiApi)
|
|||||||
|
|
||||||
func TccMain() {
|
func TccMain() {
|
||||||
go TccStartSvr()
|
go TccStartSvr()
|
||||||
tccFireRequest()
|
TccFireRequest()
|
||||||
time.Sleep(1000 * time.Second)
|
time.Sleep(1000 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ func TccStartSvr() {
|
|||||||
app.Run(fmt.Sprintf(":%d", TccBusiPort))
|
app.Run(fmt.Sprintf(":%d", TccBusiPort))
|
||||||
}
|
}
|
||||||
|
|
||||||
func tccFireRequest() {
|
func TccFireRequest() {
|
||||||
gid := common.GenGid()
|
gid := common.GenGid()
|
||||||
logrus.Printf("busi transaction begin: %s", gid)
|
logrus.Printf("busi transaction begin: %s", gid)
|
||||||
req := &TransReq{
|
req := &TransReq{
|
||||||
|
|||||||
@ -34,7 +34,7 @@ func dbGet() *common.MyDb {
|
|||||||
func XaMain() {
|
func XaMain() {
|
||||||
go XaStartSvr()
|
go XaStartSvr()
|
||||||
time.Sleep(100 * time.Millisecond)
|
time.Sleep(100 * time.Millisecond)
|
||||||
xaFireRequest()
|
XaFireRequest()
|
||||||
time.Sleep(1000 * time.Second)
|
time.Sleep(1000 * time.Second)
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,7 +48,7 @@ func XaStartSvr() {
|
|||||||
app.Run(fmt.Sprintf(":%d", XaBusiPort))
|
app.Run(fmt.Sprintf(":%d", XaBusiPort))
|
||||||
}
|
}
|
||||||
|
|
||||||
func xaFireRequest() {
|
func XaFireRequest() {
|
||||||
gid := common.GenGid()
|
gid := common.GenGid()
|
||||||
err := XaClient.XaGlobalTransaction(gid, func() (rerr error) {
|
err := XaClient.XaGlobalTransaction(gid, func() (rerr error) {
|
||||||
defer common.P2E(&rerr)
|
defer common.P2E(&rerr)
|
||||||
|
|||||||
@ -31,16 +31,10 @@ func startStartSvr() {
|
|||||||
|
|
||||||
func startFireRequest() {
|
func startFireRequest() {
|
||||||
gid := common.GenGid()
|
gid := common.GenGid()
|
||||||
logrus.Printf("busi transaction begin: %s", gid)
|
req := &gin.H{"amount": 30}
|
||||||
req := &TransReq{
|
|
||||||
Amount: 30,
|
|
||||||
TransInResult: "SUCCESS",
|
|
||||||
TransOutResult: "SUCCESS",
|
|
||||||
}
|
|
||||||
saga := dtm.SagaNew(DtmServer, gid).
|
saga := dtm.SagaNew(DtmServer, gid).
|
||||||
Add(startBusi+"/TransOut", startBusi+"/TransOutCompensate", req).
|
Add(startBusi+"/TransOut", startBusi+"/TransOutCompensate", req).
|
||||||
Add(startBusi+"/TransIn", startBusi+"/TransInCompensate", req)
|
Add(startBusi+"/TransIn", startBusi+"/TransInCompensate", req)
|
||||||
logrus.Printf("busi trans commit")
|
|
||||||
err := saga.Commit()
|
err := saga.Commit()
|
||||||
e2p(err)
|
e2p(err)
|
||||||
}
|
}
|
||||||
@ -54,10 +48,7 @@ func startAddRoute(app *gin.Engine) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func startTransIn(c *gin.Context) (interface{}, error) {
|
func startTransIn(c *gin.Context) (interface{}, error) {
|
||||||
gid := c.Query("gid")
|
return M{"result": "SUCCESS"}, nil
|
||||||
req := transReqFromContext(c)
|
|
||||||
logrus.Printf("%s TransIn: %v result: %s", gid, req, req.TransInResult)
|
|
||||||
return M{"result": req.TransInResult}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func startTransInCompensate(c *gin.Context) (interface{}, error) {
|
func startTransInCompensate(c *gin.Context) (interface{}, error) {
|
||||||
@ -65,10 +56,7 @@ func startTransInCompensate(c *gin.Context) (interface{}, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func startTransOut(c *gin.Context) (interface{}, error) {
|
func startTransOut(c *gin.Context) (interface{}, error) {
|
||||||
gid := c.Query("gid")
|
return M{"result": "SUCCESS"}, nil
|
||||||
req := transReqFromContext(c)
|
|
||||||
logrus.Printf("%s TransOut: %v result: %s", gid, req, req.TransOutResult)
|
|
||||||
return M{"result": req.TransOutResult}, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func startTransOutCompensate(c *gin.Context) (interface{}, error) {
|
func startTransOutCompensate(c *gin.Context) (interface{}, error) {
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user