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.

39 lines
1.1 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package main
// bff backend for frontend
import (
"github.com/dtm-labs/client/dtmcli"
"log"
)
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"
type Req struct {
Quantity int `json:"quantity,omitempty"`
Id int `json:"id,omitempty"`
TxId string `json:"tx_id,omitempty"`
}
// 确认订单
func main() {
// dtmcli 客户端
// 生成分布式事务id利用 dtm 接口
gid := dtmcli.MustGenGid(dtmServer) // global id
// 启动 saga 事务
// 提供操作的必要数据
// 1 订单管理,正向业务逻辑,逆向补偿操作
// 2 库存管理,正向扣减库存,逆向补偿操作追回库存
req := Req{20, 3, gid}
saga := dtmcli.NewSaga(dtmServer, gid).
Add(orderServer+"/order-create", orderServer+"/order-create-compensate", req). // 增加一个order本地事务
Add(storageServer+"/deduct", storageServer+"/deduct-compensate", req)
// 提交saga事务
err := saga.Submit()
if err != nil {
log.Fatalln(err)
}
log.Println("Ensure Order Completed.")
}