38 lines
1.1 KiB
Go
38 lines
1.1 KiB
Go
package examples
|
|
|
|
import (
|
|
context "context"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/yedf/dtm/dtmcli"
|
|
"github.com/yedf/dtm/dtmgrpc"
|
|
)
|
|
|
|
// XaGrpcClient XA client connection
|
|
var XaGrpcClient *dtmgrpc.XaGrpcClient = nil
|
|
|
|
func init() {
|
|
setupFuncs["XaGrpcSetup"] = func(app *gin.Engine) {
|
|
XaGrpcClient = dtmgrpc.NewXaGrpcClient(DtmGrpcServer, config.DB, BusiGrpc+"/examples.Busi/XaNotify")
|
|
}
|
|
addSample("grpc_xa", func() string {
|
|
gid := dtmgrpc.MustGenGid(DtmGrpcServer)
|
|
busiData := dtmcli.MustMarshal(&TransReq{Amount: 30})
|
|
err := XaGrpcClient.XaGlobalTransaction(gid, func(xa *dtmgrpc.XaGrpc) error {
|
|
_, err := xa.CallBranch(busiData, BusiGrpc+"/examples.Busi/TransOutXa")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
_, err = xa.CallBranch(busiData, BusiGrpc+"/examples.Busi/TransInXa")
|
|
return err
|
|
})
|
|
dtmcli.FatalIfError(err)
|
|
return gid
|
|
})
|
|
}
|
|
|
|
func (s *busiServer) XaNotify(ctx context.Context, in *dtmgrpc.BusiRequest) (*dtmgrpc.BusiReply, error) {
|
|
err := XaGrpcClient.HandleCallback(in.Info.Gid, in.Info.BranchID, in.Info.BranchType)
|
|
return &dtmgrpc.BusiReply{}, dtmgrpc.Result2Error(nil, err)
|
|
}
|