SpringCloud整合分布式事务Seata
版本说明 spring-cloud-alibaba 2022.0.0.0-RC1,jdk17,seata 1.6.1
接入步骤
在dependencyManagement添加spring-cloud-alibaba的依赖管理
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-alibaba-dependencies</artifactId><version>${spring.cloud.alibaba.version}</version><scope>import</scope><type>pom</type>
</dependency>
引入seata依赖和我们的适配器依赖
内置依赖的seata的包,只需要引入一个即可,seata版本不对的话需要另外引入,这里我自己项目做的配置,一般用mysql使用第二个依赖。
<dependencies><dependency><groupId>com.bda.common</groupId><artifactId>datacenter-commons-seata-adapter</artifactId><version>1.1.230713.0-SNAPSHOT</version></dependency>
</dependencies>
无需适配的依赖
<dependency><groupId>com.alibaba.cloud</groupId><artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
初始化表结构
达梦的表结构如下
CREATE TABLE "UNDO_LOG"
("ID" BIGINT IDENTITY(1, 1) NOT NULL,"BRANCH_ID" BIGINT NOT NULL,"XID" VARCHAR2(100) NOT NULL,"CONTEXT_VAL" VARCHAR2(500) NOT NULL,"ROLLBACK_INFO" BLOB NOT NULL,"LOG_STATUS" INT NOT NULL,"LOG_CREATED" DATETIME(6) NOT NULL,"LOG_MODIFIED" DATETIME(6) NOT NULL,NOT CLUSTER PRIMARY KEY("ID"),CONSTRAINT "UX_UNDO_LOG" UNIQUE("XID", "BRANCH_ID")) STORAGE(ON "MAIN", CLUSTERBTR) ;
mysql的表结构
create table if not exists undo_log
(id bigint auto_increment primary key,branch_id bigint not null,xid varchar(100) not null,context_val varchar(500) not null,rollback_info longblob not null,log_status int not null,log_created datetime not null,log_modified datetime not null,ext varchar(100) null,constraint ux_undo_logunique (xid, branch_id)
);
客户端TC配置seata信息
配置seata相关信息,根据实际配置即可
seata.enabled=true
seata.application-id=${spring.application.name}
seata.tx-service-group=default_tx_group
seata.config.type=nacos
seata.config.nacos.server-addr=${my.cloud.nacos.server-addr}
seata.config.nacos.group=SEATA_GROUP
seata.config.nacos.dataId=seataServer.properties
seata.config.nacos.username=nacos
seata.config.nacos.password=nacos
## 和seata的 nacos 2.0.3以前,public的时候不需要写,不然找不到
seata.config.nacos.namespace=
seata.client.rm.sql-parser-type=druid# get seata-server by nacos
seata.registry.type=nacos
seata.registry.nacos.application=seata-server
seata.registry.nacos.group=SEATA_GROUP
seata.registry.nacos.username=nacos
seata.registry.nacos.password=nacos
seata.registry.nacos.server-addr=${my.cloud.nacos.server-addr}
使用说明
at模式只需要添加一个注释即可@GlobalTransactional
//添加注解@GlobalTransactional@Transactional(rollbackFor = Exception.class)public void placeOrder(String userId, String commodityCode, Integer count) {BigDecimal orderMoney = new BigDecimal(count).multiply(new BigDecimal(5));Order order = new Order().setUserId(userId).setCommodityCode(commodityCode).setCount(count).setMoney(orderMoney);orderDAO.insert(order);stockFeignClient.deduct(commodityCode, count);}
术语
-
TC (Transaction Coordinator) - 事务协调者
维护全局和分支事务的状态,驱动全局事务提交或回滚,就是我们部署的seata服务器 -
TM (Transaction Manager) - 事务管理器
定义全局事务的范围:开始全局事务、提交或回滚全局事务。 -
RM (Resource Manager) - 资源管理器
管理分支事务处理的资源,与TC交谈以注册分支事务和报告分支事务的状态,并驱动分支事务提交或回滚。
Seata分TC、TM和RM三个角色,TC(Server端)为单独服务端部署,TM和RM(Client端)由业务系统集成。
client
存放client端sql脚本 (包含 undo_log表) ,参数配置config-center
各个配置中心参数导入脚本,config.txt(包含server和client,原名nacos-config.txt)为通用参数文件server
server端数据库脚本 (包含 lock_table、branch_table 与 global_table) 及各个容器配置