You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

38 lines
880 B

package main
import (
"github.com/dtm-labs/client/dtmcli"
"log"
)
// 业务请求数据对象
type Req struct {
Quantity int `json:"quantity"`
Id int `json:"id"`
TxId string `json:"tx_id"`
}
// 模拟创建订单的聚合业务逻辑
func main() {
// dtm 服务器地址
const dtmServer = "http://192.168.177.131:36789/api/dtmsvr"
// 关联的两个服务的地址
const orderServer = "http://192.168.177.1:8081"
const storageServer = "http://192.168.177.1:8082"
// dtm 生成 事务id
gid := dtmcli.MustGenGid(dtmServer)
// 伪造请求数据
req := Req{20, 3, gid}
// 启动 Saga 事务
saga := dtmcli.NewSaga(dtmServer, gid).
Add(orderServer+"/order-create", orderServer+"/order-create-compensate", req).
Add(storageServer+"/deduct", storageServer+"/deduct-compensate", req)
// 事务提交
err := saga.Submit()
log.Fatalln(err)
}