springCloud集成seata2.x

发布时间:2026/6/30 4:32:19

springCloud集成seata2.x 下载安装seata上面一章已经说了这里不多余介绍我这里用的注册中心是nacos如果你对seata不太了解可以直接按照我的步骤把配置文件复制过去改成你自己的就ok了否则会踩坑1.修改下载的seata配置文件2.x都是以ymlconfig下叫application.yml下面的文件db相关配置使用到了文件seataServer.properties这个文件在nacos中创建表示启动读取seataServer.properties中的配置内容到seata中放nacos主要是方便管理server: port: 7091 spring: application: name: seata-server logging: config: classpath:logback-spring.xml file: path: ${log.home:${user.home}/logs/seata} extend: logstash-appender: destination: 127.0.0.1:4560 kafka-appender: bootstrap-servers: 127.0.0.1:9092 topic: logback_to_logstash console: user: username: seata password: seata seata: config: type: nacos nacos: server-addr: 127.0.0.1:8848 namespace: 539fe715-08e4-4614-8941-059cc5c37497 username: nacos password: nacos group: SEATA_GROUP # 指定Nacos里的配置文件dataId /># 客户端事务超时、锁重试 client.rm.asyncCommitBufferLimit10000 client.rm.lock.retryInterval10 client.rm.lock.retryTimes30 client.rm.lock.retryPolicyBranchTimeoutfalse client.tm.defaultGlobalTransactionTimeout60000 client.tm.degradeCheckfalse client.tm.degradeCheckAllowTimes10 client.tm.degradeCheckPeriod2000 # Undo日志回滚日志 client.undo.logTableundo_log client.undo.logDeletePeriod86400000 client.undo.compress.enabletrue client.undo.compress.typezip client.undo.compress.threshold64k # TCC模式默认开启 tcc.fence.logTableNametcc_fence_log tcc.fence.cleanPeriod1h log.exceptionRate100 # 存储模式DB解决你驱动缺失报错核心段 store.modedb store.lock.modedb store.session.modedb # 数据库连接 MySQL8必须用cj驱动 store.db.datasourcedruid store.db.dbTypemysql store.db.driverClassNamecom.mysql.cj.jdbc.Driver # 修改为你的ry-seata库地址、账号密码 store.db.urljdbc:mysql://127.0.0.1:3306/ry-seata?serverTimezoneAsia/ShanghaiuseUnicodetruecharacterEncodingutf-8useSSLfalseallowMultiQueriestrue store.db.userroot store.db.password123456 # 连接池参数 store.db.minConn5 store.db.maxConn30 store.db.maxWait5000 # Seata事务表ry-seata.sql自动生成不要修改表名 store.db.globalTableglobal_table store.db.branchTablebranch_table store.db.lockTablelock_table store.db.distributedLockTabledistributed_lock store.db.queryLimit1003.springboot工程配置我以若依的system file两个工程来实现事务测试每个工程把下面的依赖和配置写到Yml里面就行了tx-service-group对应的值每个工程以工程名称自定义就行每一个工程都要引入下面的内容;注意在springboot工程中 seata下面的nacos必须要单独写为什么不用spring.cloud.nacos还要多写一次因为seata本身没有和springcloud做nacos的集成所以必须单独去写# seata配置 seata: enabled: true # Seata 应用编号默认为 ${spring.application.name} application-id: ${spring.application.name} # Seata 事务组编号用于 TC 集群名 tx-service-group: ruoyi-file-group # 关闭自动代理 enable-auto-data-source-proxy: true # 服务配置项 service: # 虚拟组和分组的映射 vgroup-mapping: # 默认组cluser 集群 ruoyi-file-group: default registry: type: nacos nacos: application: seata-server server-addr: 127.0.0.1:8848 group: SEATA_GROUP namespace: 539fe715-08e4-4614-8941-059cc5c37497 username: nacos password: nacos!-- SpringBoot Seata -- dependency groupIdcom.alibaba.cloud/groupId artifactIdspring-cloud-starter-alibaba-seata/artifactId /dependency4.分布式事务用到的表1.x和2.x对应的可能不一样注意区分直接去官网拉就行了undo_log表每一个业务数据库加入这张表https://github.com/apache/incubator-seata/blob/2.x/script/client/at/db/mysql.sql下面是分布式锁用到的表新建一个ry-seata数据库单独存放这几张表对应上面的配置文件https://github.com/apache/incubator-seata/tree/v2.3.0/script/server/db如果你的网络慢直接复制下面的sql去执行就行了CREATE TABLE IF NOT EXISTS undo_log ( branch_id BIGINT NOT NULL COMMENT branch transaction id, xid VARCHAR(128) NOT NULL COMMENT global transaction id, context VARCHAR(128) NOT NULL COMMENT undo_log context,such as serialization, rollback_info LONGBLOB NOT NULL COMMENT rollback info, log_status INT(11) NOT NULL COMMENT 0:normal status,1:defense status, log_created DATETIME(6) NOT NULL COMMENT create datetime, log_modified DATETIME(6) NOT NULL COMMENT modify datetime, UNIQUE KEY ux_undo_log (xid, branch_id) ) ENGINE InnoDB AUTO_INCREMENT 1 DEFAULT CHARSET utf8mb4 COMMENT AT transaction mode undo table; ALTER TABLE undo_log ADD INDEX ix_log_created (log_created); -- -- Licensed to the Apache Software Foundation (ASF) under one or more -- contributor license agreements. See the NOTICE file distributed with -- this work for additional information regarding copyright ownership. -- The ASF licenses this file to You under the Apache License, Version 2.0 -- (the License); you may not use this file except in compliance with -- the License. You may obtain a copy of the License at -- -- http://www.apache.org/licenses/LICENSE-2.0 -- -- Unless required by applicable law or agreed to in writing, software -- distributed under the License is distributed on an AS IS BASIS, -- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -- See the License for the specific language governing permissions and -- limitations under the License. -- -- -------------------------------- The script used when storeMode is db -------------------------------- -- the table to store GlobalSession data CREATE TABLE IF NOT EXISTS global_table ( xid VARCHAR(128) NOT NULL, transaction_id BIGINT, status TINYINT NOT NULL, application_id VARCHAR(32), transaction_service_group VARCHAR(32), transaction_name VARCHAR(128), timeout INT, begin_time BIGINT, application_data VARCHAR(2000), gmt_create DATETIME, gmt_modified DATETIME, PRIMARY KEY (xid), KEY idx_status_gmt_modified (status , gmt_modified), KEY idx_transaction_id (transaction_id) ) ENGINE InnoDB DEFAULT CHARSET utf8mb4; -- the table to store BranchSession data CREATE TABLE IF NOT EXISTS branch_table ( branch_id BIGINT NOT NULL, xid VARCHAR(128) NOT NULL, transaction_id BIGINT, resource_group_id VARCHAR(32), resource_id VARCHAR(256), branch_type VARCHAR(8), status TINYINT, client_id VARCHAR(64), application_data VARCHAR(2000), gmt_create DATETIME(6), gmt_modified DATETIME(6), PRIMARY KEY (branch_id), KEY idx_xid (xid) ) ENGINE InnoDB DEFAULT CHARSET utf8mb4; -- the table to store lock data CREATE TABLE IF NOT EXISTS lock_table ( row_key VARCHAR(128) NOT NULL, xid VARCHAR(128), transaction_id BIGINT, branch_id BIGINT NOT NULL, resource_id VARCHAR(256), table_name VARCHAR(32), pk VARCHAR(36), status TINYINT NOT NULL DEFAULT 0 COMMENT 0:locked ,1:rollbacking, gmt_create DATETIME, gmt_modified DATETIME, PRIMARY KEY (row_key), KEY idx_status (status), KEY idx_branch_id (branch_id), KEY idx_xid (xid) ) ENGINE InnoDB DEFAULT CHARSET utf8mb4; CREATE TABLE IF NOT EXISTS distributed_lock ( lock_key CHAR(20) NOT NULL, lock_value VARCHAR(20) NOT NULL, expire BIGINT, primary key (lock_key) ) ENGINE InnoDB DEFAULT CHARSET utf8mb4; INSERT INTO distributed_lock (lock_key, lock_value, expire) VALUES (AsyncCommitting, , 0); INSERT INTO distributed_lock (lock_key, lock_value, expire) VALUES (RetryCommitting, , 0); INSERT INTO distributed_lock (lock_key, lock_value, expire) VALUES (RetryRollbacking, , 0); INSERT INTO distributed_lock (lock_key, lock_value, expire) VALUES (TxTimeoutCheck, , 0); CREATE TABLE IF NOT EXISTS vgroup_table ( vGroup VARCHAR(255), namespace VARCHAR(255), cluster VARCHAR(255), UNIQUE KEY idx_vgroup_namespace_cluster (vGroup,namespace,cluster) ) ENGINE InnoDB DEFAULT CHARSET utf8mb4;最后写一下简单的java测试代码, system中openfeign 调用file工程如果报错则数据库中保存会失败,下面就简写了能看到效果就行了RestController public class SysTestController { Autowired private RemoteFileService remoteFileService; GlobalTransactional GetMapping(/api/test) public String test() { SysFile sysFile new SysFile(); sysFile.setName(test); sysFile.setUrl(http://localhost/1.png); remoteFileService.insertFile(sysFile); int i 1/0; return success; } }file工程代码只做保存

相关新闻