
Seata 分布式事务配置实战基于 Nacos 的高可用部署方案1. 引言在微服务架构中分布式事务一直是绕不开的难题。SeataSimple Extensible Autonomous Transaction Architecture是阿里巴巴开源的分布式事务解决方案它提供了AT、TCC、SAGA、XA等多种事务模式能够以零侵入的方式解决跨数据库、跨服务的分布式数据一致性问题。本文将以Seata 1.5.2版本为例手把手教你如何配置 Seata Server并集成Nacos作为注册中心和配置中心实现 Seata 的高可用部署。通过本文你将掌握✅ Seata 的核心角色TC、TM、RM与工作流程✅ 如何为 Seata Server 创建数据库表结构✅ 如何配置 Nacos 作为 Seata 的注册中心和配置中心✅ 如何启动并验证 Seata Server2. Seata 核心架构概览在开始配置之前先了解一下 Seata 的三个核心组件微服务架构开启/提交/回滚注册/报告分支注册/报告分支操作数据库操作数据库TM - 事务管理器发起全局事务RM - 资源管理器Branch1 分支事务RM - 资源管理器Branch2 分支事务TC - 事务协调器Seata Server数据库1数据库2TC (Transaction Coordinator)Seata Server 独立部署负责全局事务的协调与管理。TM (Transaction Manager)嵌入在业务应用中负责开启全局事务、提交或回滚。RM (Resource Manager)嵌入在数据访问层负责分支事务的注册、状态报告以及数据库操作。Seata 通过 TC 来统一调度TM 和 RM 通过注册中心如 Nacos感知 TC 的地址。3. 环境准备组件版本说明Seata Server1.5.2事务协调器Nacos2.x注册中心 配置中心MySQL5.7 / 8.0Seata Server 存储事务日志Spring Cloud Alibaba2021.x 或更高客户端集成参考注意本文配置基于 Seata 1.5.2不同版本的配置文件路径和参数可能有所差异请以实际下载版本为准。4. 第一步创建 Seata 数据库表Seata Server 需要将全局事务会话、分支事务会话、锁等信息持久化到数据库中。官方提供了 MySQL 建表脚本。4.1 下载脚本访问 Seata 官方 GitHub 仓库国内可使用 Gitee 镜像GitHub 地址https://github.com/seata/seata/tree/v1.5.2/script/server/dbGitee 地址推荐https://gitee.com/seata-io/seata/tree/v1.5.2/script/server/db需要下载以下 SQL 文件以 MySQL 为例mysql.sql包含global_table、branch_table、lock_tablemysql_distributed_lock.sql包含distributed_lock4.2 执行建表登录 MySQL创建一个专门用于 Seata 的数据库例如seataCREATEDATABASEIFNOTEXISTSseataDEFAULTCHARSETutf8mb4;USEseata;然后执行下载的 SQL 脚本。执行完成后你会看到以下四张表表名作用global_table存储全局事务信息branch_table存储分支事务信息lock_table存储全局锁信息AT 模式distributed_lock分布式锁表用于 Server 集群5. 第二步配置 Seata Server 的注册中心Seata Server 启动后需要注册到 Nacos以便客户端TM/RM能够发现它。修改 Seata Server 配置文件conf/application.ymlSeata 1.5 统一使用 YAML 配置。5.1 配置 registry注册中心seata:registry:type:nacos# 注册中心类型nacos:application:seata-server# 服务名server-addr:127.0.0.1:8848# Nacos 地址group:SEATA_GROUP# 分组需与客户端一致namespace:# 命名空间可选留空为 publiccluster:default# 集群名称username:# Nacos 用户名如有password:# Nacos 密码如有⚠️重要提示客户端微服务中的注册中心配置必须与 Seata Server 配置在同一个 namespace 和 group否则客户端将无法找到 Seata Server。6. 第三步配置 Seata Server 的配置中心Seata Server 本身的配置事务超时、存储模式、锁策略等可以存放在 Nacos 中实现动态刷新。这里我们将seataServer.properties文件的内容托管到 Nacos。6.1 修改配置中心类型在conf/application.yml中添加config部分seata:config:type:nacos# 配置中心类型nacos:server-addr:127.0.0.1:8848namespace:# 与 registry 保持一致group:SEATA_GROUPdata-id:seataServer.properties# 配置 dataIdusername:password:注意data-id是 Nacos 中配置文件的 dataId后面需要在 Nacos 中创建同名的配置。6.2 完整的 application.yml 示例以下是整合了 server 端口、日志、安全、注册中心和配置中心的完整配置基于你提供的原文整理server:port:7091# Seata Console 访问端口spring:application:name:seata-serverlogging:config:classpath:logback-spring.xmlfile:path:${user.home}/logs/seataconsole:user:username:seatapassword:seataseata:config:type:nacosnacos:server-addr:127.0.0.1:8848namespace:group:SEATA_GROUPdata-id:seataServer.propertiesusername:password:registry:type:nacosnacos:application:seata-serverserver-addr:127.0.0.1:8848group:SEATA_GROUPnamespace:cluster:defaultsecurity:secretKey:SeataSecretkey0c382ef121d778043159209298fd40bf3850a017tokenValidityInMilliseconds:1800000ignore:urls:-/**/*.css-/**/*.js-/**/*.html-/**/*.map-/**/*.svg-/**/*.png-/**/*.ico-/console-fe/public/**-/api/v1/auth/login注意security.secretKey用于生成 JWT token生产环境请修改为自定义密钥。7. 第四步在 Nacos 中创建配置seataServer.propertiesSeata Server 启动时会从 Nacos 读取data-id为seataServer.properties的配置。你需要将以下内容根据你的环境微调添加到 Nacos 配置中。7.1 登录 Nacos 控制台访问http://127.0.0.1:8848/nacos在配置管理 → 配置列表中选择命名空间public或你指定的 namespace点击新建配置。Data IDseataServer.propertiesGroupSEATA_GROUP配置格式Properties7.2 配置内容优化版以下配置包含了事务协调、存储模式DB、锁策略、客户端参数等核心项并修正了原文中的拼写错误# 传输配置 transport.typeTCP transport.serverNIO transport.heartbeattrue transport.enableTcClientBatchSendRequestfalse transport.enableRmClientBatchSendRequesttrue transport.enableTcServerBatchSendResponsefalse transport.rpcRmRequestTimeout30000 transport.rpcTmRequestTimeout30000 transport.rpcTcRequestTimeout30000 transport.serializationseata transport.compressornone # 事务分组映射重要客户端使用时需要匹配 service.vgroupMapping.my_test_tx_groupdefault # 客户端 RM 配置 client.rm.asyncCommitBufferLimit10000 client.rm.lock.retryInterval10 client.rm.lock.retryTimes30 client.rm.lock.retryPolicyBranchRollbackOnConflicttrue client.rm.reportRetryCount5 client.rm.tableMetaCheckEnabletrue client.rm.tableMetaCheckerInterval60000 client.rm.sqlParserTypedruid client.rm.reportSuccessEnablefalse # 客户端 TM 配置 client.tm.commitRetryCount5 client.tm.rollbackRetryCount5 client.tm.defaultGlobalTransactionTimeout60000 client.tm.degradeCheckfalse client.tm.degradeCheckAllowTimes10 client.tm.degradeCheckPeriod2000 # 撤销日志配置 client.undo.dataValidationtrue client.undo.logSerializationjackson client.undo.onlyCareUpdateColumnstrue client.undo.logTableundo_log client.undo.compress.enabletrue client.undo.compress.typezip client.undo.compress.threshold64k # 存储模式DB 模式 store.modedb store.lock.modedb store.session.modedb # 数据库连接池配置 store.db.datasourcedruid store.db.dbTypemysql store.db.driverClassNamecom.mysql.jdbc.Driver store.db.urljdbc:mysql://localhost:3306/seata?useUnicodetruerewriteBatchedStatementstrueuseLocalSessionStatetrue store.db.userroot store.db.passwordroot store.db.minConn5 store.db.maxConn30 store.db.globalTableglobal_table store.db.branchTablebranch_table store.db.distributedLockTabledistributed_lock store.db.queryLimit100 store.db.lockTablelock_table store.db.maxWait5000 # 服务器恢复策略 server.recovery.committingRetryPeriod1000 server.recovery.asynCommittingRetryPeriod1000 server.recovery.rollbackingRetryPeriod1000 server.recovery.timeoutRetryPeriod1000 server.maxCommitRetryTimeout-1 server.maxRollbackRetryTimeout-1 server.rollbackRetryTimeoutUnlockEnablefalse server.distributedLockExpireTime10000 # 会话管理 server.session.branchAsyncQueueSize5000 server.session.enableBranchAsyncRemovefalse # 指标监控 metrics.enabledfalse metrics.registryTypecompact metrics.exporterListprometheus metrics.exporterPrometheusPort9898关键修改说明修正了service.vgroupMapping.mal1_order_tx_group为示例名称my_test_tx_group你可按需修改。修正了store.db.url中的拼写错误useuni code→useUnicode。补充了缺失的store.db.distributedLockTable配置对应第四张表。8. 第五步启动 Seata Server8.1 启动命令进入 Seata Server 的bin目录执行启动脚本Linux/Mac./seata-server.shWindowsseata-server.bat启动成功后你会看到日志中出现Server started, listen port: 8091 Console started, listen port: 70918091TC 服务端口用于与客户端TM/RM通信并注册到 Nacos7091Seata Console可视化控制台访问端口提供 Web UI8.2 访问控制台打开浏览器访问http://localhost:7091默认登录账号seata/seata登录后你可以查看全局事务、事务统计等信息。8.3 在 Nacos 中验证注册登录 Nacos 控制台进入服务管理 → 服务列表选择命名空间与配置一致应能看到seata-server服务以及一个健康的实例。9. 整体流程回顾准备 MySQL执行建表脚本下载 Seata Server修改 application.yml配置 registry config 为 Nacos在 Nacos 创建 seataServer.properties配置存储模式、事务分组等启动 Seata Server验证Nacos 服务注册成功Console 可登录客户端微服务集成引入 seata-spring-boot-starter客户端配置 registry 同 server并设置事务分组10. 常见问题与排障问题可能原因解决方案Seata Server 启动报错Failed to connect to NacosNacos 地址或端口错误检查application.yml中server-addr是否正确Nacos 是否已启动服务注册成功但客户端无法获取 TC 地址客户端与 Server 的 namespace/group 不一致确保客户端配置中的registry.nacos.group和namespace与 Server 完全相同data-id未找到配置Nacos 中未创建seataServer.properties或 group/dataId 不匹配检查 Nacos 配置列表确认 dataId 和 group 与application.yml一致客户端事务执行失败日志显示no available service事务分组映射错误检查客户端seata.tx-service-group与 Server 端service.vgroupMapping.xxx是否对应数据库连接失败store.db.url配置错误或数据库未启动确认 MySQL 可访问且用户名密码正确11. 总结本文详细介绍了 Seata 1.5.2 基于 Nacos 的配置全过程包括✅ 数据库表结构初始化✅ Seata Server 注册中心Nacos配置✅ Seata Server 配置中心Nacos配置✅ 将核心参数存储在 Nacos 中实现动态刷新✅ 启动验证与控制台使用通过这套配置Seata Server 可以集群化部署多实例通过 Nacos 实现服务发现达到高可用效果。后续的文章中我们将进一步介绍如何在 Spring Cloud 微服务中集成 Seata 客户端并实战演示 AT 模式的分布式事务。下个问题Spring Cloud Alibaba Seata 分布式事务实战AT模式