Seata TCC 模式下解决幂等、悬挂、空回滚问题 | Spring Cloud56

一、前言

通过以下系列章节:

docker-compose 实现Seata Server高可用部署 | Spring Cloud 51

Seata AT 模式理论学习、事务隔离及部分源码解析 | Spring Cloud 52

Spring Boot集成Seata利用AT模式分布式事务示例 | Spring Cloud 53

Seata XA 模式理论学习、使用及注意事项 | Spring Cloud54

Seata TCC 模式理论学习、生产级使用示例搭建及注意事项 | Spring Cloud55

我们对Seata及其ATXATCC事务模式的理论、使用有了深入的了解,今天继续学习SeataTCC事务模式下是如何解决的空回滚、防悬挂、幂等控制问题。

理论部分来自Seata官网:

  • 2
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Spring Boot + Spring Cloud Alibaba Seata 中配置 TCC 模式,需要进行以下步骤: 1. 引入 Seata TCC 的依赖: ```xml <dependency> <groupId>com.alibaba.cloud</groupId> <artifactId>spring-cloud-starter-alibaba-seata</artifactId> <version>2.0.3.RELEASE</version> </dependency> ``` 2. 配置 Seata TCC 的相关参数: ```yaml spring: application: name: demo-service # 应用名称 seata: service: group: my_test_tx_group # 事务分组名称 vgroup-mapping.my_test_tx_group: default # 分组所在的虚拟组 enable-degrade: false # 是否开启降级模式,默认为 false use-jdk-proxy: false # 是否使用 JDK 代理,默认为 false config: type: nacos # 配置中心类型,可以是 file、nacos、apollo、zk nacos: namespace: seata # 命名间 server-addr: localhost:8848 # Nacos 服务地址 group: SEATA_GROUP # 配置组 username: nacos # 用户名 password: nacos # 密码 file: name: file.conf # 配置文件名 registry: type: nacos # 注册中心类型,可以是 file、nacos、eureka、consul、zk nacos: server-addr: localhost:8848 # Nacos 服务地址 namespace: seata # 命名间 group: SEATA_GROUP # 注册组 username: nacos # 用户名 password: nacos # 密码 tx-service-group: my_test_tx_group # 事务分组名称 ``` 3. 在需要使用 TCC 模式的方法上使用 @Tcc 注解: ```java @Service public class DemoServiceImpl implements DemoService { @Autowired private AccountService accountService; @Autowired private StorageService storageService; /** * TCC 模式下的分布式事务实现 */ @Override @GlobalTransactional(timeoutMills = 300000, name = "demo-service-tx") public void tccTransaction(String userId, String commodityCode, Integer count) { // 第一步:减少库存 storageService.decrease(commodityCode, count); // 第二步:扣除账户余额 accountService.decrease(userId, count); // 第三步:确认(提交) // do nothing // 第四步:取消() // do nothing } } ``` 4. 在 Seata 控制台中配置相应的 TCC 事务分组。 以上就是在 Spring Boot + Spring Cloud Alibaba Seata 中配置 TCC 模式的步骤。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

gmHappy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值