依赖的相关环境及组件为了方便像我这样的小白快速上手我只能踩着巨人的肩膀前进本文中相关demo基本参考ruoyi-cloud官方文档中的示例能参考别人的代码千万别自己动手写只做了部分改造方便验证同时微服务环境框架也是直接用的ruoyi-cloud感谢每一位开源前辈无私无畏的奉献。组件版本ruoyi-cloudv3.6.6Seata1.4.0Nacos2.5.0部署Seata-server集成Nacos下载Seata-server可以从GitHub仓库https://github.com/apache/incubator-seata/releases下载各版本的Seata-server直达链接Seata-server各Releases版本。Windows下载解压后.zip直接点击bin/seata-server.bat就可以启动。配置Seata-server集成Nacos使用Nacos作为注册中心及配置中心需要修改Seata目录下conf/registry.con中的相关配置。由于使用Nacos作为注册中心所以conf目录下的file.conf无需理会。主要修改两个地方一个是registry.type改为nacos(默认是file)另一个是config.type改为nacos默认是file当然registry.nacos和config.nacos中需要修改成自己的环境例如Nacos地址127.0.0.1:8848以及username和password修改完成后Seata-server就会使用Nacos作为注册中心和配置中心。registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type nacos loadBalance RandomLoadBalance loadBalanceVirtualNodes 10 nacos { application seata-server serverAddr 127.0.0.1:8848 group SEATA_GROUP namespace cluster default username password } eureka { serviceUrl http://localhost:8761/eureka application default weight 1 } redis { serverAddr localhost:6379 db 0 password cluster default timeout 0 } zk { cluster default serverAddr 127.0.0.1:2181 sessionTimeout 6000 connectTimeout 2000 username password } consul { cluster default serverAddr 127.0.0.1:8500 } etcd3 { cluster default serverAddr http://localhost:2379 } sofa { serverAddr 127.0.0.1:9603 application default region DEFAULT_ZONE datacenter DefaultDataCenter cluster default group SEATA_GROUP addressWaitTime 3000 } file { name file.conf } } config { # file、nacos 、apollo、zk、consul、etcd3 type nacos nacos { serverAddr 127.0.0.1:8848 namespace group SEATA_GROUP username password } consul { serverAddr 127.0.0.1:8500 } apollo { appId seata-server apolloMeta http://192.168.1.204:8801 namespace application apolloAccesskeySecret } zk { serverAddr 127.0.0.1:2181 sessionTimeout 6000 connectTimeout 2000 username password } etcd3 { serverAddr http://localhost:2379 } file { name file.conf } }上传配置至Nacos配置中心在Nacos中新建配置dataId为seataServer.propertiesgroup为SEATA_GROUP与Seata-server中config.nacos.group一致配置内容参考https://github.com/apache/incubator-seata/tree/develop/script/config-center的config.txt并按需修改保存直达链接配置中心内容。若不想手动复制也可使用该链接目录下/nacos/nacos-config.sh或/nacos/nacos-config.py脚本导入到Nacos这里主要修改两个地方同时由于后边将会搭建三个测试微服这里需增加事务分组的配置事务分组的具体解释请参考Seata官网 https://seata.apache.org/zh-cn/docs/v1.4/user/txgroup/transaction-group 如下修改为db,分布式事务的核心数据存储至数据库store.modedbstore.lock.modedbstore.session.modedb以及配置数据库连接信息store.db.driverClassNamestore.db.urlstore.db.userstore.db.password增加事务分组配置每个服务一个一般都采用将key 值设置为服务端的服务名有多少个微服务就添加多少行。#后边会搭建账户、商品、订单三个测试微服务这配置每个微服务的事务分组service.vgroupMapping.ruoyi-account-groupdefaultservice.vgroupMapping.ruoyi-order-groupdefaultservice.vgroupMapping.ruoyi-product-groupdefault#事务分组配置 service.vgroupMapping.ruoyi-account-groupdefault service.vgroupMapping.ruoyi-order-groupdefault service.vgroupMapping.ruoyi-product-groupdefault #For details about configuration items, see https://seata.io/zh-cn/docs/user/configurations.html #Transport configuration, for client and server transport.typeTCP transport.serverNIO transport.heartbeattrue transport.enableTmClientBatchSendRequestfalse transport.enableRmClientBatchSendRequesttrue transport.enableTcServerBatchSendResponsefalse transport.rpcRmRequestTimeout30000 transport.rpcTmRequestTimeout30000 transport.rpcTcRequestTimeout30000 transport.threadFactory.bossThreadPrefixNettyBoss transport.threadFactory.workerThreadPrefixNettyServerNIOWorker transport.threadFactory.serverExecutorThreadPrefixNettyServerBizHandler transport.threadFactory.shareBossWorkerfalse transport.threadFactory.clientSelectorThreadPrefixNettyClientSelector transport.threadFactory.clientSelectorThreadSize1 transport.threadFactory.clientWorkerThreadPrefixNettyClientWorkerThread transport.threadFactory.bossThreadSize1 transport.threadFactory.workerThreadSizedefault transport.shutdown.wait3 transport.serializationseata transport.compressornone #Transaction routing rules configuration, only for the client service.vgroupMapping.default_tx_groupdefault #If you use a registry, you can ignore it service.default.grouplist127.0.0.1:8091 service.enableDegradefalse service.disableGlobalTransactionfalse #Transaction rule configuration, only for the client 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 client.rm.sagaBranchRegisterEnablefalse client.rm.sagaJsonParserfastjson client.rm.tccActionInterceptorOrder-2147482648 client.tm.commitRetryCount5 client.tm.rollbackRetryCount5 client.tm.defaultGlobalTransactionTimeout60000 client.tm.degradeCheckfalse client.tm.degradeCheckAllowTimes10 client.tm.degradeCheckPeriod2000 client.tm.interceptorOrder-2147482648 client.undo.dataValidationtrue client.undo.logSerializationjackson client.undo.onlyCareUpdateColumnstrue server.undo.logSaveDays7 server.undo.logDeletePeriod86400000 client.undo.logTableundo_log client.undo.compress.enabletrue client.undo.compress.typezip client.undo.compress.threshold64k #For TCC transaction mode tcc.fence.logTableNametcc_fence_log tcc.fence.cleanPeriod1h #Log rule configuration, for client and server log.exceptionRate100 #Transaction storage configuration, only for the server. The file, db, and redis configuration values are optional. # 修改为db,分布式事务的核心数据存储至数据库 store.modedb store.lock.modedb store.session.modedb #Used for password encryption store.publicKey #If store.mode,store.lock.mode,store.session.mode are not equal to file, you can remove the configuration block. store.file.dirfile_store/data store.file.maxBranchSessionSize16384 store.file.maxGlobalSessionSize512 store.file.fileWriteBufferCacheSize16384 store.file.flushDiskModeasync store.file.sessionReloadReadSize100 #These configurations are required if the store mode is db. If store.mode,store.lock.mode,store.session.mode are not equal to db, you can remove the configuration block. store.db.datasourcedruid store.db.dbTypemysql store.db.driverClassNamecom.mysql.jdbc.Driver # 配置数据库连接信息 store.db.urljdbc:mysql://127.0.0.1:3306/seata?useUnicodetruerewriteBatchedStatementstrue store.db.userusername store.db.passwordpassword 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 #These configurations are required if the store mode is redis. If store.mode,store.lock.mode,store.session.mode are not equal to redis, you can remove the configuration block. store.redis.modesingle store.redis.single.host127.0.0.1 store.redis.single.port6379 store.redis.sentinel.masterName store.redis.sentinel.sentinelHosts store.redis.sentinel.sentinelPassword store.redis.maxConn10 store.redis.minConn1 store.redis.maxTotal100 store.redis.database0 store.redis.password store.redis.queryLimit100 #Transaction rule configuration, only for the server server.recovery.committingRetryPeriod1000 server.recovery.asynCommittingRetryPeriod1000 server.recovery.rollbackingRetryPeriod1000 server.recovery.timeoutRetryPeriod1000 server.maxCommitRetryTimeout-1 server.maxRollbackRetryTimeout-1 server.rollbackFailedUnlockEnablefalse server.distributedLockExpireTime10000 server.xaerNotaRetryTimeout60000 server.session.branchAsyncQueueSize5000 server.session.enableBranchAsyncRemovefalse server.enableParallelRequestHandlefalse #Metrics configuration, only for the server metrics.enabledfalse metrics.registryTypecompact metrics.exporterListprometheus metrics.exporterPrometheusPort9898
Spring Cloud分布式事务快速上手(基于Seata AT模式,集成Nacos)--学习版
发布时间:2026/7/1 2:39:00
依赖的相关环境及组件为了方便像我这样的小白快速上手我只能踩着巨人的肩膀前进本文中相关demo基本参考ruoyi-cloud官方文档中的示例能参考别人的代码千万别自己动手写只做了部分改造方便验证同时微服务环境框架也是直接用的ruoyi-cloud感谢每一位开源前辈无私无畏的奉献。组件版本ruoyi-cloudv3.6.6Seata1.4.0Nacos2.5.0部署Seata-server集成Nacos下载Seata-server可以从GitHub仓库https://github.com/apache/incubator-seata/releases下载各版本的Seata-server直达链接Seata-server各Releases版本。Windows下载解压后.zip直接点击bin/seata-server.bat就可以启动。配置Seata-server集成Nacos使用Nacos作为注册中心及配置中心需要修改Seata目录下conf/registry.con中的相关配置。由于使用Nacos作为注册中心所以conf目录下的file.conf无需理会。主要修改两个地方一个是registry.type改为nacos(默认是file)另一个是config.type改为nacos默认是file当然registry.nacos和config.nacos中需要修改成自己的环境例如Nacos地址127.0.0.1:8848以及username和password修改完成后Seata-server就会使用Nacos作为注册中心和配置中心。registry { # file 、nacos 、eureka、redis、zk、consul、etcd3、sofa type nacos loadBalance RandomLoadBalance loadBalanceVirtualNodes 10 nacos { application seata-server serverAddr 127.0.0.1:8848 group SEATA_GROUP namespace cluster default username password } eureka { serviceUrl http://localhost:8761/eureka application default weight 1 } redis { serverAddr localhost:6379 db 0 password cluster default timeout 0 } zk { cluster default serverAddr 127.0.0.1:2181 sessionTimeout 6000 connectTimeout 2000 username password } consul { cluster default serverAddr 127.0.0.1:8500 } etcd3 { cluster default serverAddr http://localhost:2379 } sofa { serverAddr 127.0.0.1:9603 application default region DEFAULT_ZONE datacenter DefaultDataCenter cluster default group SEATA_GROUP addressWaitTime 3000 } file { name file.conf } } config { # file、nacos 、apollo、zk、consul、etcd3 type nacos nacos { serverAddr 127.0.0.1:8848 namespace group SEATA_GROUP username password } consul { serverAddr 127.0.0.1:8500 } apollo { appId seata-server apolloMeta http://192.168.1.204:8801 namespace application apolloAccesskeySecret } zk { serverAddr 127.0.0.1:2181 sessionTimeout 6000 connectTimeout 2000 username password } etcd3 { serverAddr http://localhost:2379 } file { name file.conf } }上传配置至Nacos配置中心在Nacos中新建配置dataId为seataServer.propertiesgroup为SEATA_GROUP与Seata-server中config.nacos.group一致配置内容参考https://github.com/apache/incubator-seata/tree/develop/script/config-center的config.txt并按需修改保存直达链接配置中心内容。若不想手动复制也可使用该链接目录下/nacos/nacos-config.sh或/nacos/nacos-config.py脚本导入到Nacos这里主要修改两个地方同时由于后边将会搭建三个测试微服这里需增加事务分组的配置事务分组的具体解释请参考Seata官网 https://seata.apache.org/zh-cn/docs/v1.4/user/txgroup/transaction-group 如下修改为db,分布式事务的核心数据存储至数据库store.modedbstore.lock.modedbstore.session.modedb以及配置数据库连接信息store.db.driverClassNamestore.db.urlstore.db.userstore.db.password增加事务分组配置每个服务一个一般都采用将key 值设置为服务端的服务名有多少个微服务就添加多少行。#后边会搭建账户、商品、订单三个测试微服务这配置每个微服务的事务分组service.vgroupMapping.ruoyi-account-groupdefaultservice.vgroupMapping.ruoyi-order-groupdefaultservice.vgroupMapping.ruoyi-product-groupdefault#事务分组配置 service.vgroupMapping.ruoyi-account-groupdefault service.vgroupMapping.ruoyi-order-groupdefault service.vgroupMapping.ruoyi-product-groupdefault #For details about configuration items, see https://seata.io/zh-cn/docs/user/configurations.html #Transport configuration, for client and server transport.typeTCP transport.serverNIO transport.heartbeattrue transport.enableTmClientBatchSendRequestfalse transport.enableRmClientBatchSendRequesttrue transport.enableTcServerBatchSendResponsefalse transport.rpcRmRequestTimeout30000 transport.rpcTmRequestTimeout30000 transport.rpcTcRequestTimeout30000 transport.threadFactory.bossThreadPrefixNettyBoss transport.threadFactory.workerThreadPrefixNettyServerNIOWorker transport.threadFactory.serverExecutorThreadPrefixNettyServerBizHandler transport.threadFactory.shareBossWorkerfalse transport.threadFactory.clientSelectorThreadPrefixNettyClientSelector transport.threadFactory.clientSelectorThreadSize1 transport.threadFactory.clientWorkerThreadPrefixNettyClientWorkerThread transport.threadFactory.bossThreadSize1 transport.threadFactory.workerThreadSizedefault transport.shutdown.wait3 transport.serializationseata transport.compressornone #Transaction routing rules configuration, only for the client service.vgroupMapping.default_tx_groupdefault #If you use a registry, you can ignore it service.default.grouplist127.0.0.1:8091 service.enableDegradefalse service.disableGlobalTransactionfalse #Transaction rule configuration, only for the client 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 client.rm.sagaBranchRegisterEnablefalse client.rm.sagaJsonParserfastjson client.rm.tccActionInterceptorOrder-2147482648 client.tm.commitRetryCount5 client.tm.rollbackRetryCount5 client.tm.defaultGlobalTransactionTimeout60000 client.tm.degradeCheckfalse client.tm.degradeCheckAllowTimes10 client.tm.degradeCheckPeriod2000 client.tm.interceptorOrder-2147482648 client.undo.dataValidationtrue client.undo.logSerializationjackson client.undo.onlyCareUpdateColumnstrue server.undo.logSaveDays7 server.undo.logDeletePeriod86400000 client.undo.logTableundo_log client.undo.compress.enabletrue client.undo.compress.typezip client.undo.compress.threshold64k #For TCC transaction mode tcc.fence.logTableNametcc_fence_log tcc.fence.cleanPeriod1h #Log rule configuration, for client and server log.exceptionRate100 #Transaction storage configuration, only for the server. The file, db, and redis configuration values are optional. # 修改为db,分布式事务的核心数据存储至数据库 store.modedb store.lock.modedb store.session.modedb #Used for password encryption store.publicKey #If store.mode,store.lock.mode,store.session.mode are not equal to file, you can remove the configuration block. store.file.dirfile_store/data store.file.maxBranchSessionSize16384 store.file.maxGlobalSessionSize512 store.file.fileWriteBufferCacheSize16384 store.file.flushDiskModeasync store.file.sessionReloadReadSize100 #These configurations are required if the store mode is db. If store.mode,store.lock.mode,store.session.mode are not equal to db, you can remove the configuration block. store.db.datasourcedruid store.db.dbTypemysql store.db.driverClassNamecom.mysql.jdbc.Driver # 配置数据库连接信息 store.db.urljdbc:mysql://127.0.0.1:3306/seata?useUnicodetruerewriteBatchedStatementstrue store.db.userusername store.db.passwordpassword 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 #These configurations are required if the store mode is redis. If store.mode,store.lock.mode,store.session.mode are not equal to redis, you can remove the configuration block. store.redis.modesingle store.redis.single.host127.0.0.1 store.redis.single.port6379 store.redis.sentinel.masterName store.redis.sentinel.sentinelHosts store.redis.sentinel.sentinelPassword store.redis.maxConn10 store.redis.minConn1 store.redis.maxTotal100 store.redis.database0 store.redis.password store.redis.queryLimit100 #Transaction rule configuration, only for the server server.recovery.committingRetryPeriod1000 server.recovery.asynCommittingRetryPeriod1000 server.recovery.rollbackingRetryPeriod1000 server.recovery.timeoutRetryPeriod1000 server.maxCommitRetryTimeout-1 server.maxRollbackRetryTimeout-1 server.rollbackFailedUnlockEnablefalse server.distributedLockExpireTime10000 server.xaerNotaRetryTimeout60000 server.session.branchAsyncQueueSize5000 server.session.enableBranchAsyncRemovefalse server.enableParallelRequestHandlefalse #Metrics configuration, only for the server metrics.enabledfalse metrics.registryTypecompact metrics.exporterListprometheus metrics.exporterPrometheusPort9898