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 2.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package main
  18. import (
  19. "context"
  20. "flag"
  21. "fmt"
  22. "net/http"
  23. "time"
  24. "github.com/parnurzeal/gorequest"
  25. "github.com/seata/seata-go/pkg/client"
  26. "github.com/seata/seata-go/pkg/constant"
  27. "github.com/seata/seata-go/pkg/tm"
  28. "github.com/seata/seata-go/pkg/util/log"
  29. )
  30. var (
  31. serverIpPort = "http://127.0.0.1:8080"
  32. serverIpPort2 = "http://127.0.0.1:8081"
  33. )
  34. func main() {
  35. flag.Parse()
  36. client.InitPath("./sample/conf/seatago.yml")
  37. bgCtx, cancel := context.WithTimeout(context.Background(), time.Minute*10)
  38. defer cancel()
  39. transInfo := &tm.GtxConfig{
  40. Name: "ATSampleLocalGlobalTx",
  41. Timeout: time.Second * 30,
  42. }
  43. if err := tm.WithGlobalTx(bgCtx, transInfo, updateData); err != nil {
  44. panic(fmt.Sprintf("tm update data err, %v", err))
  45. }
  46. }
  47. func updateData(ctx context.Context) (re error) {
  48. request := gorequest.New()
  49. log.Infof("branch transaction begin")
  50. request.Post(serverIpPort+"/updateDataSuccess").
  51. Set(constant.XidKey, tm.GetXID(ctx)).
  52. End(func(response gorequest.Response, body string, errs []error) {
  53. if response.StatusCode != http.StatusOK {
  54. re = fmt.Errorf("update data fail")
  55. }
  56. })
  57. request.Post(serverIpPort2+"/updateDataFail").
  58. Set(constant.XidKey, tm.GetXID(ctx)).
  59. End(func(response gorequest.Response, body string, errs1 []error) {
  60. if response.StatusCode != http.StatusOK {
  61. re = fmt.Errorf("update data fail")
  62. }
  63. })
  64. return
  65. }