8 changed files with 179 additions and 150 deletions
@ -1,89 +0,0 @@ |
|||||
package examples |
|
||||
|
|
||||
import ( |
|
||||
"github.com/gin-gonic/gin" |
|
||||
"github.com/sirupsen/logrus" |
|
||||
"github.com/yedf/dtm/common" |
|
||||
) |
|
||||
|
|
||||
func AddRoute(app *gin.Engine) { |
|
||||
app.POST(BusiApi+"/TransIn", common.WrapHandler(TransIn)) |
|
||||
app.POST(BusiApi+"/TransInCompensate", common.WrapHandler(TransInCompensate)) |
|
||||
app.POST(BusiApi+"/TransOut", common.WrapHandler(TransOut)) |
|
||||
app.POST(BusiApi+"/TransOutCompensate", common.WrapHandler(TransOutCompensate)) |
|
||||
app.GET(BusiApi+"/TransQuery", common.WrapHandler(TransQuery)) |
|
||||
logrus.Printf("examples istening at %d", BusiPort) |
|
||||
} |
|
||||
|
|
||||
type M = map[string]interface{} |
|
||||
|
|
||||
var TransInResult = "" |
|
||||
var TransOutResult = "" |
|
||||
var TransInCompensateResult = "" |
|
||||
var TransOutCompensateResult = "" |
|
||||
var TransQueryResult = "" |
|
||||
|
|
||||
type TransReq struct { |
|
||||
Amount int `json:"amount"` |
|
||||
TransInFailed bool `json:"transInFailed"` |
|
||||
TransOutFailed bool `json:"transOutFailed"` |
|
||||
} |
|
||||
|
|
||||
func TransIn(c *gin.Context) (interface{}, error) { |
|
||||
gid := c.Query("gid") |
|
||||
req := TransReq{} |
|
||||
if err := c.BindJSON(&req); err != nil { |
|
||||
return nil, err |
|
||||
} |
|
||||
if req.TransInFailed { |
|
||||
logrus.Printf("%s TransIn %v failed", gid, req) |
|
||||
return M{"result": "FAIL"}, nil |
|
||||
} |
|
||||
res := common.OrString(TransInResult, "SUCCESS") |
|
||||
logrus.Printf("%s TransIn: %v result: %s", gid, req, res) |
|
||||
return M{"result": res}, nil |
|
||||
} |
|
||||
|
|
||||
func TransInCompensate(c *gin.Context) (interface{}, error) { |
|
||||
gid := c.Query("gid") |
|
||||
req := TransReq{} |
|
||||
if err := c.BindJSON(&req); err != nil { |
|
||||
return nil, err |
|
||||
} |
|
||||
res := common.OrString(TransInCompensateResult, "SUCCESS") |
|
||||
logrus.Printf("%s TransInCompensate: %v result: %s", gid, req, res) |
|
||||
return M{"result": res}, nil |
|
||||
} |
|
||||
|
|
||||
func TransOut(c *gin.Context) (interface{}, error) { |
|
||||
gid := c.Query("gid") |
|
||||
req := TransReq{} |
|
||||
if err := c.BindJSON(&req); err != nil { |
|
||||
return nil, err |
|
||||
} |
|
||||
if req.TransOutFailed { |
|
||||
logrus.Printf("%s TransOut %v failed", gid, req) |
|
||||
return M{"result": "FAIL"}, nil |
|
||||
} |
|
||||
res := common.OrString(TransOutResult, "SUCCESS") |
|
||||
logrus.Printf("%s TransOut: %v result: %s", gid, req, res) |
|
||||
return M{"result": res}, nil |
|
||||
} |
|
||||
|
|
||||
func TransOutCompensate(c *gin.Context) (interface{}, error) { |
|
||||
gid := c.Query("gid") |
|
||||
req := TransReq{} |
|
||||
if err := c.BindJSON(&req); err != nil { |
|
||||
return nil, err |
|
||||
} |
|
||||
res := common.OrString(TransOutCompensateResult, "SUCCESS") |
|
||||
logrus.Printf("%s TransOutCompensate: %v result: %s", gid, req, res) |
|
||||
return M{"result": res}, nil |
|
||||
} |
|
||||
|
|
||||
func TransQuery(c *gin.Context) (interface{}, error) { |
|
||||
gid := c.Query("gid") |
|
||||
logrus.Printf("%s TransQuery", gid) |
|
||||
res := common.OrString(TransQueryResult, "SUCCESS") |
|
||||
return M{"result": res}, nil |
|
||||
} |
|
||||
@ -1,41 +0,0 @@ |
|||||
package examples |
|
||||
|
|
||||
import ( |
|
||||
"time" |
|
||||
|
|
||||
"github.com/sirupsen/logrus" |
|
||||
"github.com/yedf/dtm" |
|
||||
"github.com/yedf/dtm/common" |
|
||||
) |
|
||||
|
|
||||
func Main() { |
|
||||
go StartSvr() |
|
||||
FireRequest() |
|
||||
time.Sleep(1000 * time.Second) |
|
||||
} |
|
||||
|
|
||||
func FireRequest() { |
|
||||
gid := common.GenGid() |
|
||||
logrus.Printf("busi transaction begin: %s", gid) |
|
||||
req := &TransReq{ |
|
||||
Amount: 30, |
|
||||
TransInFailed: false, |
|
||||
TransOutFailed: false, |
|
||||
} |
|
||||
saga := dtm.SagaNew(DtmServer, gid, Busi+"/TransQuery") |
|
||||
|
|
||||
saga.Add(Busi+"/TransIn", Busi+"/TransInCompensate", req) |
|
||||
saga.Add(Busi+"/TransOut", Busi+"/TransOutCompensate", req) |
|
||||
err := saga.Prepare() |
|
||||
common.PanicIfError(err) |
|
||||
logrus.Printf("busi trans commit") |
|
||||
err = saga.Commit() |
|
||||
common.PanicIfError(err) |
|
||||
} |
|
||||
|
|
||||
func StartSvr() { |
|
||||
logrus.Printf("examples starting") |
|
||||
app := common.GetGinApp() |
|
||||
AddRoute(app) |
|
||||
app.Run(":8081") |
|
||||
} |
|
||||
@ -0,0 +1,120 @@ |
|||||
|
package examples |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"time" |
||||
|
|
||||
|
"github.com/gin-gonic/gin" |
||||
|
"github.com/sirupsen/logrus" |
||||
|
"github.com/yedf/dtm" |
||||
|
"github.com/yedf/dtm/common" |
||||
|
) |
||||
|
|
||||
|
// 事务参与者的服务地址
|
||||
|
const SagaBusiPort = 8081 |
||||
|
const SagaBusiApi = "/api/busi_saga" |
||||
|
|
||||
|
var SagaBusi = fmt.Sprintf("http://localhost:%d%s", SagaBusiPort, SagaBusiApi) |
||||
|
|
||||
|
func SagaMain() { |
||||
|
go SagaStartSvr() |
||||
|
sagaFireRequest() |
||||
|
time.Sleep(1000 * time.Second) |
||||
|
} |
||||
|
|
||||
|
func SagaStartSvr() { |
||||
|
logrus.Printf("saga examples starting") |
||||
|
app := common.GetGinApp() |
||||
|
AddRoute(app) |
||||
|
app.Run(":8081") |
||||
|
} |
||||
|
|
||||
|
func sagaFireRequest() { |
||||
|
gid := common.GenGid() |
||||
|
logrus.Printf("busi transaction begin: %s", gid) |
||||
|
req := &TransReq{ |
||||
|
Amount: 30, |
||||
|
TransInResult: "SUCCESS", |
||||
|
TransOutResult: "SUCCESS", |
||||
|
} |
||||
|
saga := dtm.SagaNew(DtmServer, gid, SagaBusi+"/TransQuery") |
||||
|
|
||||
|
saga.Add(SagaBusi+"/TransIn", SagaBusi+"/TransInCompensate", req) |
||||
|
saga.Add(SagaBusi+"/TransOut", SagaBusi+"/TransOutCompensate", req) |
||||
|
err := saga.Prepare() |
||||
|
common.PanicIfError(err) |
||||
|
logrus.Printf("busi trans commit") |
||||
|
err = saga.Commit() |
||||
|
common.PanicIfError(err) |
||||
|
} |
||||
|
|
||||
|
// api
|
||||
|
|
||||
|
func AddRoute(app *gin.Engine) { |
||||
|
app.POST(SagaBusiApi+"/TransIn", common.WrapHandler(TransIn)) |
||||
|
app.POST(SagaBusiApi+"/TransInCompensate", common.WrapHandler(TransInCompensate)) |
||||
|
app.POST(SagaBusiApi+"/TransOut", common.WrapHandler(TransOut)) |
||||
|
app.POST(SagaBusiApi+"/TransOutCompensate", common.WrapHandler(TransOutCompensate)) |
||||
|
app.GET(SagaBusiApi+"/TransQuery", common.WrapHandler(TransQuery)) |
||||
|
logrus.Printf("examples listening at %d", SagaBusiPort) |
||||
|
} |
||||
|
|
||||
|
type M = map[string]interface{} |
||||
|
|
||||
|
var TransInResult = "" |
||||
|
var TransOutResult = "" |
||||
|
var TransInCompensateResult = "" |
||||
|
var TransOutCompensateResult = "" |
||||
|
var TransQueryResult = "" |
||||
|
|
||||
|
type TransReq struct { |
||||
|
Amount int `json:"amount"` |
||||
|
TransInResult string `json:"transInResult"` |
||||
|
TransOutResult string `json:"transOutResult"` |
||||
|
} |
||||
|
|
||||
|
func transReqFromContext(c *gin.Context) *TransReq { |
||||
|
req := TransReq{} |
||||
|
err := c.BindJSON(&req) |
||||
|
common.PanicIfError(err) |
||||
|
return &req |
||||
|
} |
||||
|
|
||||
|
func TransIn(c *gin.Context) (interface{}, error) { |
||||
|
gid := c.Query("gid") |
||||
|
req := transReqFromContext(c) |
||||
|
res := common.OrString(TransInResult, req.TransInResult, "SUCCESS") |
||||
|
logrus.Printf("%s TransIn: %v result: %s", gid, req, res) |
||||
|
return M{"result": res}, nil |
||||
|
} |
||||
|
|
||||
|
func TransInCompensate(c *gin.Context) (interface{}, error) { |
||||
|
gid := c.Query("gid") |
||||
|
req := transReqFromContext(c) |
||||
|
res := common.OrString(TransInCompensateResult, "SUCCESS") |
||||
|
logrus.Printf("%s TransInCompensate: %v result: %s", gid, req, res) |
||||
|
return M{"result": res}, nil |
||||
|
} |
||||
|
|
||||
|
func TransOut(c *gin.Context) (interface{}, error) { |
||||
|
gid := c.Query("gid") |
||||
|
req := transReqFromContext(c) |
||||
|
res := common.OrString(TransOutResult, req.TransOutResult, "SUCCESS") |
||||
|
logrus.Printf("%s TransOut: %v result: %s", gid, req, res) |
||||
|
return M{"result": res}, nil |
||||
|
} |
||||
|
|
||||
|
func TransOutCompensate(c *gin.Context) (interface{}, error) { |
||||
|
gid := c.Query("gid") |
||||
|
req := transReqFromContext(c) |
||||
|
res := common.OrString(TransOutCompensateResult, "SUCCESS") |
||||
|
logrus.Printf("%s TransOutCompensate: %v result: %s", gid, req, res) |
||||
|
return M{"result": res}, nil |
||||
|
} |
||||
|
|
||||
|
func TransQuery(c *gin.Context) (interface{}, error) { |
||||
|
gid := c.Query("gid") |
||||
|
logrus.Printf("%s TransQuery", gid) |
||||
|
res := common.OrString(TransQueryResult, "SUCCESS") |
||||
|
return M{"result": res}, nil |
||||
|
} |
||||
@ -0,0 +1,34 @@ |
|||||
|
package examples |
||||
|
|
||||
|
import ( |
||||
|
"fmt" |
||||
|
"time" |
||||
|
|
||||
|
"github.com/sirupsen/logrus" |
||||
|
"github.com/yedf/dtm/common" |
||||
|
) |
||||
|
|
||||
|
// 事务参与者的服务地址
|
||||
|
const XaBusiPort = 8082 |
||||
|
const XaBusiApi = "/api/busi_xa" |
||||
|
|
||||
|
var XaBusi = fmt.Sprintf("http://localhost:%d%s", XaBusiPort, XaBusiApi) |
||||
|
|
||||
|
func XaMain() { |
||||
|
go XaStartSvr() |
||||
|
xaFireRequest() |
||||
|
time.Sleep(1000 * time.Second) |
||||
|
} |
||||
|
|
||||
|
func XaStartSvr() { |
||||
|
logrus.Printf("xa examples starting") |
||||
|
app := common.GetGinApp() |
||||
|
AddRoute(app) |
||||
|
app.Run(":8081") |
||||
|
} |
||||
|
|
||||
|
func xaFireRequest() { |
||||
|
|
||||
|
} |
||||
|
|
||||
|
// api
|
||||
Loading…
Reference in new issue