mirror of https://github.com/dtm-labs/dtm.git
committed by
GitHub
1 changed files with 1 additions and 100 deletions
@ -1,100 +1 @@ |
|||||
 |
此仓库应常青藤爸爸公司要求,已停止维护。请移步 [https://github.com/dtm-labs/dtf](https://github.com/dtm-labs/dtf) |
||||
 |
|
||||
[](https://codecov.io/gh/dtm-labs/dtm) |
|
||||
[](https://goreportcard.com/report/github.com/dtm-labs/dtm) |
|
||||
[](https://pkg.go.dev/github.com/dtm-labs/dtm) |
|
||||
[](https://github.com/avelino/awesome-go#database) |
|
||||
|
|
||||
English | [简体中文](https://github.com/dtm-labs/dtm/blob/main/helper/README-cn.md) |
|
||||
|
|
||||
# Distributed Transactions Manager |
|
||||
|
|
||||
## What is DTM |
|
||||
|
|
||||
DTM is a distributed transaction framework which provides cross-service eventual data consistency. It provides saga, tcc, xa, 2-phase message strategies for a variety of application scenarios. It also supports multiple languages and multiple store engine to form up a transaction as following: |
|
||||
|
|
||||
<img alt="function-picture" src="https://en.dtm.pub/assets/function.7d5618f8.png" height=250 /> |
|
||||
|
|
||||
## Who's using DTM (partial) |
|
||||
|
|
||||
[Tencent](https://www.tencent.com/) |
|
||||
|
|
||||
[Ivydad](https://ivydad.com) |
|
||||
|
|
||||
[Eglass](https://epeijing.cn) |
|
||||
|
|
||||
## [Cook Book](https://en.dtm.pub) |
|
||||
|
|
||||
## Quick start |
|
||||
|
|
||||
### run dtm |
|
||||
|
|
||||
``` bash |
|
||||
git clone https://github.com/dtm-labs/dtm && cd dtm |
|
||||
go run main.go |
|
||||
``` |
|
||||
|
|
||||
### Start an example |
|
||||
Suppose we want to perform an inter-bank transfer. The operations of transfer out (TransOut) and transfer in (TransIn) are coded in separate micro-services. |
|
||||
|
|
||||
Here is an example to illustrate a solution of dtm to this problem: |
|
||||
|
|
||||
``` bash |
|
||||
git clone https://github.com/dtm-labs/dtmcli-go-sample && cd dtmcli-go-sample |
|
||||
go run main.go |
|
||||
``` |
|
||||
|
|
||||
## Code |
|
||||
|
|
||||
### Use |
|
||||
``` go |
|
||||
// business micro-service address |
|
||||
const qsBusi = "http://localhost:8081/api/busi_saga" |
|
||||
// The address where DtmServer serves DTM, which is a url |
|
||||
DtmServer := "http://localhost:36789/api/dtmsvr" |
|
||||
req := &gin.H{"amount": 30} // micro-service payload |
|
||||
// DtmServer is the address of DTM micro-service |
|
||||
saga := dtmcli.NewSaga(DtmServer, dtmcli.MustGenGid(DtmServer)). |
|
||||
// add a TransOut subtraction,forward operation with url: qsBusi+"/TransOut", reverse compensation operation with url: qsBusi+"/TransOutCom" |
|
||||
Add(qsBusi+"/TransOut", qsBusi+"/TransOutCom", req). |
|
||||
// add a TransIn subtraction, forward operation with url: qsBusi+"/TransIn", reverse compensation operation with url: qsBusi+"/TransInCom" |
|
||||
Add(qsBusi+"/TransIn", qsBusi+"/TransInCom", req) |
|
||||
// submit the created saga transaction,dtm ensures all subtractions either complete or get revoked |
|
||||
err := saga.Submit() |
|
||||
``` |
|
||||
|
|
||||
When the above code runs, we can see in the console that services TransOut, TransIn has been called. |
|
||||
|
|
||||
#### Timing diagram |
|
||||
A timing diagram for a successfully completed SAGA transaction would be as follows: |
|
||||
|
|
||||
<img alt="saga-success" src="https://en.dtm.pub/assets/saga_normal.59a75c01.jpg" height=450/> |
|
||||
|
|
||||
#### Rollback upon failure |
|
||||
If any forward operation fails, DTM invokes the corresponding compensating operation of each sub-transaction to roll back, after which the transaction is successfully rolled back. |
|
||||
|
|
||||
Let's purposely fail the forward operation of the second sub-transaction and watch what happens |
|
||||
|
|
||||
``` go |
|
||||
app.POST(qsBusiAPI+"/TransIn", func(c *gin.Context) { |
|
||||
log.Printf("TransIn") |
|
||||
// c.JSON(200, "") |
|
||||
c.JSON(409, "") // Status 409 for Failure. Won't be retried |
|
||||
}) |
|
||||
``` |
|
||||
|
|
||||
The timing diagram for the intended failure is as follows: |
|
||||
|
|
||||
<img alt="saga-failed" src="https://en.dtm.pub/assets/saga_rollback.7989c866.jpg" height=550> |
|
||||
|
|
||||
## More examples |
|
||||
|
|
||||
Refer to [dtm-examples](https://github.com/dtm-labs/dtm-examples) for more examples. |
|
||||
|
|
||||
## Slack |
|
||||
|
|
||||
You can join the [DTM slack channel here](https://join.slack.com/t/dtm-w6k9662/shared_invite/zt-vkrph4k1-eFqEFnMkbmlXqfUo5GWHWw). |
|
||||
|
|
||||
## Give a star! ⭐ |
|
||||
|
|
||||
If you think this project is interesting, or helpful to you, please give a star! |
|
||||
|
|||||
Loading…
Reference in new issue