经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
SpringBoot使用GTS的示例详解
来源:jb51  时间:2021/10/11 17:56:47  对本文有异议

1. 依赖类库txc-client.jar, txt-client-spring-cloud-2.0.1.jar

2. 使用TxcDataSource代理源数据源【注意:dbcp2.BasicDataSource不支持,可以使用DruidDataSource】

3. 添加自动配置类文件

  1. package com.bodytrack.restapi;
  2. import com.taobao.txc.client.aop.TxcTransactionScaner;
  3. import com.taobao.txc.client.boot.TxcSpringBootProperties;
  4. import org.springframework.beans.factory.annotation.Autowired;
  5. import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
  6. import org.springframework.boot.context.properties.EnableConfigurationProperties;
  7. import org.springframework.context.ApplicationContext;
  8. import org.springframework.context.annotation.Bean;
  9. import org.springframework.context.annotation.Configuration;
  10. import org.springframework.context.annotation.DependsOn;
  11. @Configuration
  12. @EnableConfigurationProperties({TxcSpringBootProperties.class})
  13. public class TxcSpringBootAutoConfiguration {
  14. @Autowired
  15. private TxcSpringBootProperties txcSpringBootProperties;
  16. @Autowired
  17. private ApplicationContext applicationContext;
  18. private static boolean isEmpty(String str) {
  19. return str == null || str.length() == 0;
  20. }
  21. @Bean(name = "txcScanner")
  22. @ConditionalOnProperty(
  23. prefix = "spring.boot.txc",
  24. name = {"txcServerGroup"}
  25. )
  26. //定义声明式事务,要想让事务annotation感知的话,要在这里定义一下
  27. public TxcTransactionScaner txcTransactionScaner() {
  28. String appName = this.txcSpringBootProperties.getTxcAppName() == null ? this.applicationContext.getEnvironment().getProperty("spring.application.name") : this.txcSpringBootProperties.getTxcAppName();
  29. String txServiceGroup = this.txcSpringBootProperties.getTxcServerGroup();
  30. int mode = this.txcSpringBootProperties.getMode() == 0 ? 1 : this.txcSpringBootProperties.getMode();
  31. TxcTransactionScaner txcTransactionScanner = new TxcTransactionScaner(appName, txServiceGroup, mode, this.txcSpringBootProperties.getUrl());
  32. if (!isEmpty(this.txcSpringBootProperties.getAccessKey())) {
  33. txcTransactionScanner.setAccessKey(this.txcSpringBootProperties.getAccessKey());
  34. }
  35. if (!isEmpty(this.txcSpringBootProperties.getSecretKey())) {
  36. txcTransactionScanner.setSecretKey(this.txcSpringBootProperties.getSecretKey());
  37. }
  38. return txcTransactionScanner;
  39. }
  40. }

4. 添加GTS配置

  1. spring:
  2. boot:
  3. txc:
  4. txcAppName: demo
  5. txcServerGroup: txc_test_public.1129361738553704.QD #公网测试的专用事务分组
  6. url: https://test-cs-gts.aliyuncs.com #公网测试url
  7. accessKey: xxx #非测试时需提供
  8. secretKey: xxxx #非测试时需提供

5. 发送rest请求时,请求添加header(TXC_XID,BEGIN_COUNT,COMMIT_COUNT)

  1. public String callTestTxc() {
  2. HttpHeaders requestHeaders = new HttpHeaders();
  3. requestHeaders.set("TXC_XID", String.valueOf(TxcContext.getCurrentXid()));
  4. requestHeaders.set("BEGIN_COUNT", String.valueOf(TxcContext.getBeginCount()));
  5. requestHeaders.set("COMMIT_COUNT", String.valueOf(TxcContext.getCommitCount()));
  6. HttpEntity<String> entity = new HttpEntity<>("parameters", requestHeaders);
  7. String restUrl = String.format("%s/api/scoreService/testTxc", "http://10.0.0.5:8762");
  8. ResponseEntity<String> restData = restTemplate.exchange(restUrl, HttpMethod.GET, entity, String.class);
  9. return restData.toString();
  10. }

6. 发起全局事务使用注解@TxcTransaction

到此这篇关于SpringBoot使用GTS的示例详解的文章就介绍到这了,更多相关SpringBoot使用GTS内容请搜索w3xue以前的文章或继续浏览下面的相关文章希望大家以后多多支持w3xue!

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号