springboot 项目集成 seate 分布式事务TCC使用nacos完整配置步骤及说明

在 Spring Boot 项目中集成 Seata 的 TCC 模式 ,并使用 Nacos 作为注册中心和配置中心,是一个典型的微服务分布式事务解决方案。以下是完整的配置步骤与详细说明(基于 Seata 2.x + Nacos 2.2+)。


🧩 一、前置条件

  1. Nacos 已启动(建议使用 standalone 模式)

    sh nacos/bin/startup.sh -m standalone

  2. MySQL 数据库已准备(用于 Seata Server 存储事务日志)

  3. Seata Server 版本 ≥ 2.0.0

  4. Spring Boot 项目使用 Spring Cloud Alibaba


🛠️ 二、Seata Server 配置(服务端)

1. 创建数据库表(Seata Server 专用)

创建名为 seata 的数据库,并执行 官方 SQL 脚本:

  • global_table
  • branch_table
  • lock_table

注意:这些表仅用于 TC(事务协调器),不是业务库

2. 配置 Nacos 中的 seataServer.properties

在 Nacos 控制台(http://localhost:8848/nacos)中:

  • Data ID : seataServer.properties

  • Group : SEATA_GROUP

  • 配置内容

    事务组映射(关键!客户端 tx-service-group 必须匹配)

    service.vgroupMapping.my_tcc_tx_group=default

    存储模式

    store.mode=db
    store.lock.mode=db
    store.session.mode=db

    DB 配置

    store.db.datasource=druid
    store.db.dbType=mysql
    store.db.driverClassName=com.mysql.cj.jdbc.Driver
    store.db.url=jdbc:mysql://localhost:3306/seata?useUnicode=true&rewriteBatchedStatements=true&serverTimezone=Asia/Shanghai
    store.db.user=root
    store.db.password=123456

    客户端相关

    client.undo.logTable=undo_log

⚠️ my_tcc_tx_group 是你在客户端配置的 tx-service-group 名称,必须一致!

3. 启动 Seata Server

修改 conf/application.yml(或使用默认,通过 Nacos 加载配置):

复制代码
seata:
  registry:
    type: nacos
    nacos:
      application: seata-server
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace: ""  # 默认 public
      cluster: default
      username: nacos
      password: nacos
  config:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace: ""
      username: nacos
      password: nacos

启动命令:

复制代码
sh bin/seata-server.sh -h 127.0.0.1 -p 8091 -m db

✅ 启动后,在 Nacos 服务列表 中应看到 seata-server


📦 三、Spring Boot 客户端配置(TCC 模式)

1. 添加 Maven 依赖

复制代码
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-seata</artifactId>
    <version>2022.0.0.0</version> <!-- 对应 Spring Boot 3.x / 2.x -->
</dependency>

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-discovery</artifactId>
</dependency>

<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-starter-alibaba-nacos-config</artifactId>
</dependency>

注意:Seata Starter 版本需与 Seata Server 一致(如 2.0.0)。


2. 配置 application.yml

复制代码
spring:
  application:
    name: order-service
  cloud:
    nacos:
      discovery:
        server-addr: 127.0.0.1:8848
      config:
        server-addr: 127.0.0.1:8848
        file-extension: properties

seata:
  enabled: true
  application-id: ${spring.application.name}
  # 事务组名称,必须与 Nacos 中 service.vgroupMapping.xxx 的 key 匹配
  tx-service-group: my_tcc_tx_group
  registry:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace: ""
      cluster: default
      username: nacos
      password: nacos
  config:
    type: nacos
    nacos:
      server-addr: 127.0.0.1:8848
      group: SEATA_GROUP
      namespace: ""
      username: nacos
      password: nacos

tx-service-group: my_tcc_tx_group 必须与 Nacos 中 service.vgroupMapping.my_tcc_tx_group=default 一致。


3. 编写 TCC 接口(LocalTCC 模式)

复制代码
@LocalTCC
public interface OrderTccService {

    @TwoPhaseBusinessAction(
        name = "createOrder", 
        commitMethod = "commit", 
        rollbackMethod = "rollback"
    )
    boolean prepareCreateOrder(BusinessActionContext context, String orderId, int amount);

    boolean commit(BusinessActionContext context);

    boolean rollback(BusinessActionContext context);
}

4. 实现 TCC 接口

复制代码
@Service
public class OrderTccServiceImpl implements OrderTccService {

    @Override
    public boolean prepareCreateOrder(BusinessActionContext context, String orderId, int amount) {
        // Try 阶段:预留资源(如冻结库存、预扣余额)
        System.out.println("Try: 预留订单资源 " + orderId);
        return true;
    }

    @Override
    public boolean commit(BusinessActionContext context) {
        // Confirm 阶段:确认提交
        System.out.println("Confirm: 确认订单 " + context.getActionContext("orderId"));
        return true;
    }

    @Override
    public boolean rollback(BusinessActionContext context) {
        // Cancel 阶段:回滚释放资源
        System.out.println("Cancel: 取消订单 " + context.getActionContext("orderId"));
        return true;
    }
}

5. 在业务方法上启用全局事务

复制代码
@RestController
public class OrderController {

    @Autowired
    private OrderTccService orderTccService;

    @GlobalTransactional
    @PostMapping("/order")
    public String createOrder(@RequestParam String orderId, @RequestParam int amount) {
        // 调用 TCC 接口(Try 阶段)
        boolean success = orderTccService.prepareCreateOrder(null, orderId, amount);
        if (!success) {
            throw new RuntimeException("订单预留失败");
        }
        // 其他微服务调用...
        return "下单成功";
    }
}

@GlobalTransactional 注解开启全局事务,Seata 会自动协调 TCC 的 Try/Confirm/Cancel。


🔍 四、验证与调试

  1. 启动成功标志(查看日志):

    register TM success
    register RM success

  2. Nacos 控制台 → 服务列表:能看到 order-serviceseata-server

  3. 模拟异常 :在业务方法中抛出异常,观察是否触发 rollback


📌 五、关键注意事项

项目 说明
tx-service-group 客户端必须与 Nacos 中 service.vgroupMapping.xxx 的 key 一致
cluster 客户端与 Server 的 cluster 必须相同(默认 default
TCC 接口 必须加 @LocalTCC,方法参数需包含 BusinessActionContext
幂等性 Confirm 和 Cancel 方法必须幂等(可能重复调用)
网络隔离 Seata Server 的 -h 参数建议用内网 IP,避免 127.0.0.1 导致跨机器无法访问

✅ 总结

通过以上步骤,你已完成:

  • Seata Server 注册到 Nacos
  • Spring Boot 客户端通过 Nacos 发现 Seata
  • 使用 TCC 模式实现分布式事务
  • 全局事务由 @GlobalTransactional 自动管理

💡 TCC 模式适合对一致性要求高、业务逻辑清晰的场景(如支付、库存)。若追求简单,可考虑 AT 模式(需 undo_log 表)。

如需完整示例代码,可参考 GitHub:seata-samples/tcc-sample。

相关推荐
枫叶林FYL20 小时前
【强化学习】长上下文可验证奖励强化学习:原理推导与系统架构
人工智能·系统架构
roman_日积跬步-终至千里1 天前
【系统架构师-综合题(2)】项目管理知识点整理
系统架构
tedcloud1231 天前
agent-skills部署教程:打造工程化AI Agent系统
服务器·人工智能·系统架构·powerpoint·dreamweaver
一几文1 天前
系统架构设计师案例分析终极冲刺预测汇总(临考版)
系统架构
SuniaWang1 天前
AgentX 专栏-00前言:一个Java开发者的Agent实践之路
java·人工智能·spring boot·langchain·系统架构
2601_957786771 天前
企业级矩阵系统架构深度解析:从冷启动到规模化增长的技术演进
矩阵·系统架构·内容矩阵
逍遥德1 天前
Java编程高频的“踩坑点”-01:fastjson.JSON 转换时泛型擦除问题
java·spring boot·spring·系统架构·json
数据与后端架构提升之路1 天前
软考系统架构设计师实战论文集:自动驾驶与AI云端架构演进
人工智能·系统架构·自动驾驶
枫叶林FYL1 天前
【强化学习】5 异构机器人数据集的跨具身离线强化学习:形态感知分组与梯度冲突消解
人工智能·系统架构·机器人
Sam_Deep_Thinking2 天前
连锁门店的外卖订单平台对接
java·微服务·架构·系统架构