gen gid only in dtm
This commit is contained in:
parent
2dc5e7ce88
commit
89879aba1a
@ -37,10 +37,9 @@ DTM 是一款跨语言的分布式事务管理方案,在各类微服务架构
|
|||||||
``` go
|
``` go
|
||||||
const DtmServer = "http://localhost:8080/api/dtmsvr"
|
const DtmServer = "http://localhost:8080/api/dtmsvr"
|
||||||
const startBusi = "http://localhost:8081/api/busi_saga"
|
const startBusi = "http://localhost:8081/api/busi_saga"
|
||||||
gid := common.GenGid() // 生成事务id
|
|
||||||
req := &gin.H{"amount": 30} // 微服务的负荷
|
req := &gin.H{"amount": 30} // 微服务的负荷
|
||||||
// 生成dtm的saga对象
|
// 生成dtm的saga对象
|
||||||
saga := dtm.SagaNew(DtmServer, gid).
|
saga := dtm.SagaNew(DtmServer).
|
||||||
// 添加两个子事务
|
// 添加两个子事务
|
||||||
Add(startBusi+"/TransOut", startBusi+"/TransOutCompensate", req).
|
Add(startBusi+"/TransOut", startBusi+"/TransOutCompensate", req).
|
||||||
Add(startBusi+"/TransIn", startBusi+"/TransInCompensate", req)
|
Add(startBusi+"/TransIn", startBusi+"/TransInCompensate", req)
|
||||||
|
|||||||
@ -22,7 +22,7 @@ func Prepare(c *gin.Context) (interface{}, error) {
|
|||||||
m := TransFromContext(c)
|
m := TransFromContext(c)
|
||||||
m.Status = "prepared"
|
m.Status = "prepared"
|
||||||
m.SaveNew(dbGet())
|
m.SaveNew(dbGet())
|
||||||
return M{"message": "SUCCESS"}, nil
|
return M{"message": "SUCCESS", "gid": m.Gid}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Commit(c *gin.Context) (interface{}, error) {
|
func Commit(c *gin.Context) (interface{}, error) {
|
||||||
@ -31,7 +31,7 @@ func Commit(c *gin.Context) (interface{}, error) {
|
|||||||
m.Status = "committed"
|
m.Status = "committed"
|
||||||
m.SaveNew(db)
|
m.SaveNew(db)
|
||||||
go m.Process(db)
|
go m.Process(db)
|
||||||
return M{"message": "SUCCESS"}, nil
|
return M{"message": "SUCCESS", "gid": m.Gid}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func Rollback(c *gin.Context) (interface{}, error) {
|
func Rollback(c *gin.Context) (interface{}, error) {
|
||||||
|
|||||||
@ -209,29 +209,32 @@ func sagaCommittedPending(t *testing.T) {
|
|||||||
|
|
||||||
func genMsg(gid string) *dtm.Msg {
|
func genMsg(gid string) *dtm.Msg {
|
||||||
logrus.Printf("beginning a msg test ---------------- %s", gid)
|
logrus.Printf("beginning a msg test ---------------- %s", gid)
|
||||||
msg := dtm.MsgNew(examples.DtmServer, gid)
|
msg := dtm.MsgNew(examples.DtmServer)
|
||||||
msg.QueryPrepared = examples.MsgBusi + "/TransQuery"
|
msg.QueryPrepared = examples.MsgBusi + "/TransQuery"
|
||||||
req := examples.GenTransReq(30, false, false)
|
req := examples.GenTransReq(30, false, false)
|
||||||
msg.Add(examples.MsgBusi+"/TransOut", &req)
|
msg.Add(examples.MsgBusi+"/TransOut", &req)
|
||||||
msg.Add(examples.MsgBusi+"/TransIn", &req)
|
msg.Add(examples.MsgBusi+"/TransIn", &req)
|
||||||
|
msg.Gid = gid
|
||||||
return msg
|
return msg
|
||||||
}
|
}
|
||||||
|
|
||||||
func genSaga(gid string, outFailed bool, inFailed bool) *dtm.Saga {
|
func genSaga(gid string, outFailed bool, inFailed bool) *dtm.Saga {
|
||||||
logrus.Printf("beginning a saga test ---------------- %s", gid)
|
logrus.Printf("beginning a saga test ---------------- %s", gid)
|
||||||
saga := dtm.SagaNew(examples.DtmServer, gid)
|
saga := dtm.SagaNew(examples.DtmServer)
|
||||||
req := examples.GenTransReq(30, outFailed, inFailed)
|
req := examples.GenTransReq(30, outFailed, inFailed)
|
||||||
saga.Add(examples.SagaBusi+"/TransOut", examples.SagaBusi+"/TransOutCompensate", &req)
|
saga.Add(examples.SagaBusi+"/TransOut", examples.SagaBusi+"/TransOutCompensate", &req)
|
||||||
saga.Add(examples.SagaBusi+"/TransIn", examples.SagaBusi+"/TransInCompensate", &req)
|
saga.Add(examples.SagaBusi+"/TransIn", examples.SagaBusi+"/TransInCompensate", &req)
|
||||||
|
saga.Gid = gid
|
||||||
return saga
|
return saga
|
||||||
}
|
}
|
||||||
|
|
||||||
func genTcc(gid string, outFailed bool, inFailed bool) *dtm.Tcc {
|
func genTcc(gid string, outFailed bool, inFailed bool) *dtm.Tcc {
|
||||||
logrus.Printf("beginning a tcc test ---------------- %s", gid)
|
logrus.Printf("beginning a tcc test ---------------- %s", gid)
|
||||||
tcc := dtm.TccNew(examples.DtmServer, gid)
|
tcc := dtm.TccNew(examples.DtmServer)
|
||||||
req := examples.GenTransReq(30, outFailed, inFailed)
|
req := examples.GenTransReq(30, outFailed, inFailed)
|
||||||
tcc.Add(examples.TccBusi+"/TransOutTry", examples.TccBusi+"/TransOutConfirm", examples.TccBusi+"/TransOutCancel", &req)
|
tcc.Add(examples.TccBusi+"/TransOutTry", examples.TccBusi+"/TransOutConfirm", examples.TccBusi+"/TransOutCancel", &req)
|
||||||
tcc.Add(examples.TccBusi+"/TransInTry", examples.TccBusi+"/TransInConfirm", examples.TccBusi+"/TransInCancel", &req)
|
tcc.Add(examples.TccBusi+"/TransInTry", examples.TccBusi+"/TransInConfirm", examples.TccBusi+"/TransInCancel", &req)
|
||||||
|
tcc.Gid = gid
|
||||||
return tcc
|
return tcc
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -125,6 +125,9 @@ func (t *TransGlobal) setNextCron(expireIn int64) []string {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (t *TransGlobal) SaveNew(db *common.DB) {
|
func (t *TransGlobal) SaveNew(db *common.DB) {
|
||||||
|
if t.Gid == "" {
|
||||||
|
t.Gid = common.GenGid()
|
||||||
|
}
|
||||||
err := db.Transaction(func(db1 *gorm.DB) error {
|
err := db.Transaction(func(db1 *gorm.DB) error {
|
||||||
db := &common.DB{DB: db1}
|
db := &common.DB{DB: db1}
|
||||||
updates := t.setNextCron(config.TransCronInterval)
|
updates := t.setNextCron(config.TransCronInterval)
|
||||||
|
|||||||
@ -29,14 +29,13 @@ func MsgStartSvr() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func MsgFireRequest() {
|
func MsgFireRequest() {
|
||||||
gid := common.GenGid()
|
logrus.Printf("a busi transaction begin")
|
||||||
logrus.Printf("busi transaction begin: %s", gid)
|
|
||||||
req := &TransReq{
|
req := &TransReq{
|
||||||
Amount: 30,
|
Amount: 30,
|
||||||
TransInResult: "SUCCESS",
|
TransInResult: "SUCCESS",
|
||||||
TransOutResult: "SUCCESS",
|
TransOutResult: "SUCCESS",
|
||||||
}
|
}
|
||||||
msg := dtm.MsgNew(DtmServer, gid).
|
msg := dtm.MsgNew(DtmServer).
|
||||||
Add(MsgBusi+"/TransOut", req).
|
Add(MsgBusi+"/TransOut", req).
|
||||||
Add(MsgBusi+"/TransIn", req)
|
Add(MsgBusi+"/TransIn", req)
|
||||||
err := msg.Prepare(MsgBusi + "/TransQuery")
|
err := msg.Prepare(MsgBusi + "/TransQuery")
|
||||||
|
|||||||
@ -29,18 +29,18 @@ func SagaStartSvr() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func SagaFireRequest() {
|
func SagaFireRequest() {
|
||||||
gid := common.GenGid()
|
logrus.Printf("a busi transaction begin")
|
||||||
logrus.Printf("busi transaction begin: %s", gid)
|
|
||||||
req := &TransReq{
|
req := &TransReq{
|
||||||
Amount: 30,
|
Amount: 30,
|
||||||
TransInResult: "SUCCESS",
|
TransInResult: "SUCCESS",
|
||||||
TransOutResult: "SUCCESS",
|
TransOutResult: "SUCCESS",
|
||||||
}
|
}
|
||||||
saga := dtm.SagaNew(DtmServer, gid).
|
saga := dtm.SagaNew(DtmServer).
|
||||||
Add(SagaBusi+"/TransOut", SagaBusi+"/TransOutCompensate", req).
|
Add(SagaBusi+"/TransOut", SagaBusi+"/TransOutCompensate", req).
|
||||||
Add(SagaBusi+"/TransIn", SagaBusi+"/TransInCompensate", req)
|
Add(SagaBusi+"/TransIn", SagaBusi+"/TransInCompensate", req)
|
||||||
logrus.Printf("busi trans commit")
|
logrus.Printf("busi trans commit")
|
||||||
err := saga.Commit()
|
err := saga.Commit()
|
||||||
|
logrus.Printf("result gid is: %s", saga.Gid)
|
||||||
e2p(err)
|
e2p(err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -29,14 +29,13 @@ func TccStartSvr() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func TccFireRequest() {
|
func TccFireRequest() {
|
||||||
gid := common.GenGid()
|
logrus.Printf("a busi transaction begin")
|
||||||
logrus.Printf("busi transaction begin: %s", gid)
|
|
||||||
req := &TransReq{
|
req := &TransReq{
|
||||||
Amount: 30,
|
Amount: 30,
|
||||||
TransInResult: "SUCCESS",
|
TransInResult: "SUCCESS",
|
||||||
TransOutResult: "SUCCESS",
|
TransOutResult: "SUCCESS",
|
||||||
}
|
}
|
||||||
tcc := dtm.TccNew(DtmServer, gid).
|
tcc := dtm.TccNew(DtmServer).
|
||||||
Add(TccBusi+"/TransOutTry", TccBusi+"/TransOutConfirm", TccBusi+"/TransOutCancel", req).
|
Add(TccBusi+"/TransOutTry", TccBusi+"/TransOutConfirm", TccBusi+"/TransOutCancel", req).
|
||||||
Add(TccBusi+"/TransInTry", TccBusi+"/TransInConfirm", TccBusi+"/TransOutCancel", req)
|
Add(TccBusi+"/TransInTry", TccBusi+"/TransInConfirm", TccBusi+"/TransOutCancel", req)
|
||||||
logrus.Printf("busi trans commit")
|
logrus.Printf("busi trans commit")
|
||||||
|
|||||||
@ -29,9 +29,8 @@ func startStartSvr() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func startFireRequest() {
|
func startFireRequest() {
|
||||||
gid := common.GenGid()
|
|
||||||
req := &gin.H{"amount": 30}
|
req := &gin.H{"amount": 30}
|
||||||
saga := dtm.SagaNew(DtmServer, gid).
|
saga := dtm.SagaNew(DtmServer).
|
||||||
Add(startBusi+"/TransOut", startBusi+"/TransOutCompensate", req).
|
Add(startBusi+"/TransOut", startBusi+"/TransOutCompensate", req).
|
||||||
Add(startBusi+"/TransIn", startBusi+"/TransInCompensate", req)
|
Add(startBusi+"/TransIn", startBusi+"/TransInCompensate", req)
|
||||||
err := saga.Commit()
|
err := saga.Commit()
|
||||||
|
|||||||
2
go.mod
2
go.mod
@ -10,7 +10,7 @@ require (
|
|||||||
github.com/go-resty/resty/v2 v2.6.0
|
github.com/go-resty/resty/v2 v2.6.0
|
||||||
github.com/golang/protobuf v1.4.2 // indirect
|
github.com/golang/protobuf v1.4.2 // indirect
|
||||||
github.com/google/go-cmp v0.5.5 // indirect
|
github.com/google/go-cmp v0.5.5 // indirect
|
||||||
github.com/json-iterator/go v1.1.10 // indirect
|
github.com/json-iterator/go v1.1.10
|
||||||
github.com/sirupsen/logrus v1.7.0
|
github.com/sirupsen/logrus v1.7.0
|
||||||
github.com/spf13/viper v1.7.1
|
github.com/spf13/viper v1.7.1
|
||||||
github.com/stretchr/testify v1.7.0 // indirect
|
github.com/stretchr/testify v1.7.0 // indirect
|
||||||
|
|||||||
@ -3,6 +3,7 @@ package dtm
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
jsonitor "github.com/json-iterator/go"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
)
|
)
|
||||||
@ -23,10 +24,9 @@ type MsgStep struct {
|
|||||||
Data string `json:"data"`
|
Data string `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func MsgNew(server string, gid string) *Msg {
|
func MsgNew(server string) *Msg {
|
||||||
return &Msg{
|
return &Msg{
|
||||||
MsgData: MsgData{
|
MsgData: MsgData{
|
||||||
Gid: gid,
|
|
||||||
TransType: "msg",
|
TransType: "msg",
|
||||||
},
|
},
|
||||||
Server: server,
|
Server: server,
|
||||||
@ -51,6 +51,7 @@ func (s *Msg) Commit() error {
|
|||||||
if resp.StatusCode() != 200 {
|
if resp.StatusCode() != 200 {
|
||||||
return fmt.Errorf("commit failed: %v", resp.Body())
|
return fmt.Errorf("commit failed: %v", resp.Body())
|
||||||
}
|
}
|
||||||
|
s.Gid = jsonitor.Get(resp.Body(), "gid").ToString()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -64,5 +65,6 @@ func (s *Msg) Prepare(queryPrepared string) error {
|
|||||||
if resp.StatusCode() != 200 {
|
if resp.StatusCode() != 200 {
|
||||||
return fmt.Errorf("prepare failed: %v", resp.Body())
|
return fmt.Errorf("prepare failed: %v", resp.Body())
|
||||||
}
|
}
|
||||||
|
s.Gid = jsonitor.Get(resp.Body(), "gid").ToString()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
5
saga.go
5
saga.go
@ -3,6 +3,7 @@ package dtm
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
jsonitor "github.com/json-iterator/go"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
)
|
)
|
||||||
@ -23,10 +24,9 @@ type SagaStep struct {
|
|||||||
Data string `json:"data"`
|
Data string `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func SagaNew(server string, gid string) *Saga {
|
func SagaNew(server string) *Saga {
|
||||||
return &Saga{
|
return &Saga{
|
||||||
SagaData: SagaData{
|
SagaData: SagaData{
|
||||||
Gid: gid,
|
|
||||||
TransType: "saga",
|
TransType: "saga",
|
||||||
},
|
},
|
||||||
Server: server,
|
Server: server,
|
||||||
@ -52,5 +52,6 @@ func (s *Saga) Commit() error {
|
|||||||
if resp.StatusCode() != 200 {
|
if resp.StatusCode() != 200 {
|
||||||
return fmt.Errorf("commit failed: %v", resp.Body())
|
return fmt.Errorf("commit failed: %v", resp.Body())
|
||||||
}
|
}
|
||||||
|
s.Gid = jsonitor.Get(resp.Body(), "gid").ToString()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
5
tcc.go
5
tcc.go
@ -3,6 +3,7 @@ package dtm
|
|||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
|
||||||
|
jsonitor "github.com/json-iterator/go"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/yedf/dtm/common"
|
"github.com/yedf/dtm/common"
|
||||||
)
|
)
|
||||||
@ -24,10 +25,9 @@ type TccStep struct {
|
|||||||
Data string `json:"data"`
|
Data string `json:"data"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func TccNew(server string, gid string) *Tcc {
|
func TccNew(server string) *Tcc {
|
||||||
return &Tcc{
|
return &Tcc{
|
||||||
TccData: TccData{
|
TccData: TccData{
|
||||||
Gid: gid,
|
|
||||||
TransType: "tcc",
|
TransType: "tcc",
|
||||||
},
|
},
|
||||||
Server: server,
|
Server: server,
|
||||||
@ -54,5 +54,6 @@ func (s *Tcc) Commit() error {
|
|||||||
if resp.StatusCode() != 200 {
|
if resp.StatusCode() != 200 {
|
||||||
return fmt.Errorf("commit failed: %v", resp.Body())
|
return fmt.Errorf("commit failed: %v", resp.Body())
|
||||||
}
|
}
|
||||||
|
s.Gid = jsonitor.Get(resp.Body(), "gid").ToString()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user