From d1a6359410f8b4638b77d6a0ebbac93be93850c2 Mon Sep 17 00:00:00 2001 From: yedongfu Date: Fri, 4 Jun 2021 19:28:37 +0800 Subject: [PATCH] readme cn ok --- README-cn.md | 42 ----------------------------------------- README-en.md | 6 ++++++ README.md | 31 ++++++++++++++++++++++++++---- app/main.go | 8 ++++++-- conf.sample.yml | 2 +- examples/main_tcc.go | 4 ++-- examples/main_xa.go | 4 ++-- examples/quick_start.go | 18 +++--------------- 8 files changed, 47 insertions(+), 68 deletions(-) delete mode 100644 README-cn.md create mode 100644 README-en.md diff --git a/README-cn.md b/README-cn.md deleted file mode 100644 index 7d03075..0000000 --- a/README-cn.md +++ /dev/null @@ -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 - - 其他 diff --git a/README-en.md b/README-en.md new file mode 100644 index 0000000..3f7f7fe --- /dev/null +++ b/README-en.md @@ -0,0 +1,6 @@ +# a lightweight distributed transaction management service + +### 英文[] +### 中文 + +近期频繁更新中 diff --git a/README.md b/README.md index 3f7f7fe..8b7d321 100644 --- a/README.md +++ b/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 diff --git a/app/main.go b/app/main.go index 18f5ad0..9997588 100644 --- a/app/main.go +++ b/app/main.go @@ -18,15 +18,19 @@ func main() { go examples.SagaStartSvr() time.Sleep(100 * time.Millisecond) examples.SagaFireRequest() - } else if os.Args[1] == "dtmsvr" { + } else if os.Args[1] == "dtmsvr" { // 只启动dtmsvr go dtmsvr.StartSvr() - } else if os.Args[1] == "all" { + } else if os.Args[1] == "all" { // 运行所有示例 dtmsvr.PopulateMysql() examples.PopulateMysql() go dtmsvr.StartSvr() go examples.SagaStartSvr() go examples.TccStartSvr() go examples.XaStartSvr() + time.Sleep(100 * time.Millisecond) + examples.SagaFireRequest() + examples.TccFireRequest() + examples.XaFireRequest() } else { logrus.Fatalf("unknown arg: %s", os.Args[1]) } diff --git a/conf.sample.yml b/conf.sample.yml index 2f91b07..b2cb95b 100644 --- a/conf.sample.yml +++ b/conf.sample.yml @@ -5,4 +5,4 @@ Mysql: port: '3306' PreparedExpire: 90 # 单位秒,处于prepared中的任务,过了这个时间,查询结果还是PENDING的话,则会被cancel -TransCronInterval: 20 # 单位秒 当事务等待这个时间之后,还没有变化,则进行一轮处理,包括prepared中的任务和commited的任务 +TransCronInterval: 20 # 单位秒 当事务等待这个时间之后,还没有变化,则进行一轮重试处理,包括prepared中的任务和commited的任务 diff --git a/examples/main_tcc.go b/examples/main_tcc.go index a81dc39..774b9de 100644 --- a/examples/main_tcc.go +++ b/examples/main_tcc.go @@ -18,7 +18,7 @@ var TccBusi = fmt.Sprintf("http://localhost:%d%s", TccBusiPort, TccBusiApi) func TccMain() { go TccStartSvr() - tccFireRequest() + TccFireRequest() time.Sleep(1000 * time.Second) } @@ -29,7 +29,7 @@ func TccStartSvr() { app.Run(fmt.Sprintf(":%d", TccBusiPort)) } -func tccFireRequest() { +func TccFireRequest() { gid := common.GenGid() logrus.Printf("busi transaction begin: %s", gid) req := &TransReq{ diff --git a/examples/main_xa.go b/examples/main_xa.go index beafbf1..80af58b 100644 --- a/examples/main_xa.go +++ b/examples/main_xa.go @@ -34,7 +34,7 @@ func dbGet() *common.MyDb { func XaMain() { go XaStartSvr() time.Sleep(100 * time.Millisecond) - xaFireRequest() + XaFireRequest() time.Sleep(1000 * time.Second) } @@ -48,7 +48,7 @@ func XaStartSvr() { app.Run(fmt.Sprintf(":%d", XaBusiPort)) } -func xaFireRequest() { +func XaFireRequest() { gid := common.GenGid() err := XaClient.XaGlobalTransaction(gid, func() (rerr error) { defer common.P2E(&rerr) diff --git a/examples/quick_start.go b/examples/quick_start.go index 888c3af..251d4f4 100644 --- a/examples/quick_start.go +++ b/examples/quick_start.go @@ -31,16 +31,10 @@ func startStartSvr() { func startFireRequest() { gid := common.GenGid() - logrus.Printf("busi transaction begin: %s", gid) - req := &TransReq{ - Amount: 30, - TransInResult: "SUCCESS", - TransOutResult: "SUCCESS", - } + req := &gin.H{"amount": 30} saga := dtm.SagaNew(DtmServer, gid). Add(startBusi+"/TransOut", startBusi+"/TransOutCompensate", req). Add(startBusi+"/TransIn", startBusi+"/TransInCompensate", req) - logrus.Printf("busi trans commit") err := saga.Commit() e2p(err) } @@ -54,10 +48,7 @@ func startAddRoute(app *gin.Engine) { } func startTransIn(c *gin.Context) (interface{}, error) { - gid := c.Query("gid") - req := transReqFromContext(c) - logrus.Printf("%s TransIn: %v result: %s", gid, req, req.TransInResult) - return M{"result": req.TransInResult}, nil + return M{"result": "SUCCESS"}, nil } 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) { - gid := c.Query("gid") - req := transReqFromContext(c) - logrus.Printf("%s TransOut: %v result: %s", gid, req, req.TransOutResult) - return M{"result": req.TransOutResult}, nil + return M{"result": "SUCCESS"}, nil } func startTransOutCompensate(c *gin.Context) (interface{}, error) {