You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

main.go 1.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "context"
  4. "flag"
  5. "time"
  6. "github.com/parnurzeal/gorequest"
  7. "github.com/seata/seata-go/pkg/client"
  8. "github.com/seata/seata-go/pkg/common"
  9. "github.com/seata/seata-go/pkg/common/log"
  10. "github.com/seata/seata-go/pkg/tm"
  11. )
  12. func main() {
  13. client.Init()
  14. flag.Parse()
  15. ctx, cancel := context.WithTimeout(context.Background(), time.Minute*10)
  16. defer cancel()
  17. var err error
  18. // GlobalTransactional is starting
  19. log.Infof("global transaction begin")
  20. ctx = tm.Begin(ctx, "TestTCCServiceBusiness")
  21. defer func() {
  22. resp := tm.CommitOrRollback(ctx, err == nil)
  23. log.Infof("tx result %v", resp)
  24. <-make(chan bool)
  25. }()
  26. var serverIpPort = "http://127.0.0.1:8080"
  27. request := gorequest.New()
  28. var xid string
  29. if tm.IsSeataContext(ctx) {
  30. xid = tm.GetXID(ctx)
  31. }
  32. log.Infof("branch transaction begin")
  33. request.Post(serverIpPort+"/prepare").
  34. Set(common.XidKey, xid).
  35. End(func(response gorequest.Response, body string, errs []error) {
  36. if len(errs) != 0 {
  37. err = errs[0]
  38. }
  39. })
  40. }