dtm/examples/grpc_xa.go
2021-08-11 19:12:05 +08:00

41 lines
1.2 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

package examples
import (
context "context"
"github.com/gin-gonic/gin"
"github.com/yedf/dtm/dtmcli"
"github.com/yedf/dtm/dtmgrpc"
emptypb "google.golang.org/protobuf/types/known/emptypb"
)
// 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")
}
}
func (s *busiServer) XaNotify(ctx context.Context, in *dtmgrpc.BusiRequest) (*emptypb.Empty, error) {
err := XaGrpcClient.HandleCallback(in.Info.Gid, in.Info.BranchID, in.Info.BranchType)
return &emptypb.Empty{}, dtmgrpc.Result2Error(nil, err)
}
// XaGrpcFireRequest 注册全局XA事务调用XA的分支
func XaGrpcFireRequest() string {
gid := dtmcli.MustGenGid(DtmServer)
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
}