mirror of https://github.com/dtm-labs/dtm.git
csharpjavadistributed-transactionsdtmgogolangmicroservicenodejsphpdatabasesagaseatatcctransactiontransactionsxapythondistributed
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.
75 lines
2.0 KiB
75 lines
2.0 KiB
package busi
|
|
|
|
import (
|
|
"fmt"
|
|
"log"
|
|
"time"
|
|
|
|
"github.com/dtm-labs/dtm/client/dtmcli"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/lithammer/shortuuid/v3"
|
|
)
|
|
|
|
// busi address
|
|
const qsBusiAPI = "/api/busi_start"
|
|
const qsBusiPort = 8082
|
|
|
|
var qsBusi = fmt.Sprintf("http://localhost:%d%s", qsBusiPort, qsBusiAPI)
|
|
|
|
// QsMain will be call from dtm/qs
|
|
func QsMain() {
|
|
QsStartSvr()
|
|
QsFireRequest()
|
|
select {}
|
|
}
|
|
|
|
// QsStartSvr quick start: start server
|
|
func QsStartSvr() {
|
|
app := gin.New()
|
|
qsAddRoute(app)
|
|
log.Printf("quick start examples listening at %d", qsBusiPort)
|
|
go func() {
|
|
_ = app.Run(fmt.Sprintf(":%d", qsBusiPort))
|
|
}()
|
|
time.Sleep(100 * time.Millisecond)
|
|
}
|
|
|
|
func qsAddRoute(app *gin.Engine) {
|
|
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
|
|
})
|
|
app.POST(qsBusiAPI+"/TransInCompensate", func(c *gin.Context) {
|
|
log.Printf("TransInCompensate")
|
|
c.JSON(200, "")
|
|
})
|
|
app.POST(qsBusiAPI+"/TransOut", func(c *gin.Context) {
|
|
log.Printf("TransOut")
|
|
c.JSON(200, "")
|
|
})
|
|
app.POST(qsBusiAPI+"/TransOutCompensate", func(c *gin.Context) {
|
|
log.Printf("TransOutCompensate")
|
|
c.JSON(200, "")
|
|
})
|
|
}
|
|
|
|
const dtmServer = "http://localhost:36789/api/dtmsvr"
|
|
|
|
// QsFireRequest quick start: fire request
|
|
func QsFireRequest() string {
|
|
req := &gin.H{"amount": 30} // load of micro-service
|
|
// DtmServer is the url of dtm
|
|
saga := dtmcli.NewSaga(dtmServer, shortuuid.New()).
|
|
// add a TransOut sub-transaction,forward operation with url: qsBusi+"/TransOut", reverse compensation operation with url: qsBusi+"/TransOutCompensate"
|
|
Add(qsBusi+"/TransOut", qsBusi+"/TransOutCompensate", req).
|
|
// add a TransIn sub-transaction, forward operation with url: qsBusi+"/TransIn", reverse compensation operation with url: qsBusi+"/TransInCompensate"
|
|
Add(qsBusi+"/TransIn", qsBusi+"/TransInCompensate", req)
|
|
// submit the created saga transaction,dtm ensures all sub-transactions either complete or get revoked
|
|
err := saga.Submit()
|
|
|
|
if err != nil {
|
|
panic(err)
|
|
}
|
|
return saga.Gid
|
|
}
|
|
|