在完成seata服务端的配置和部署之后,就可以将seata客户端集成到需要实现seata分布式服务的应用服务中。
1. 在每个参与seata事务的服务中添加依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.5.RELEASE</version>
</dependency>
2. 在参与seata事务的数据库中添加undo log表
CREATE TABLE `undo_log` (
`id` bigint(20) NOT NULL AUTO_INCREMENT,
`branch_id` bigint(20) NOT NULL,
`xid` varchar(100) NOT NULL,
`context` varchar(128) NOT NULL,
`rollback_info` longblob NOT NULL,
`log_status` int(11) NOT NULL,
`log_created` datetime NOT NULL,
`log_modified` datetime NOT NULL,
`ext` varchar(100) DEFAULT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `ux_undo_log` (`xid`,`branch_id`)
) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;
3. 每个参与seata事务的服务添加配置事务分组
spring:
cloud:
alibaba:
seata:
tx-service-group: seataserver-demo
这里的参数需要和seata服务器端的配置保持一致。
4. 在每个事务参与者配置seata事务的协调者
# seata配置
seata:
config:
type: nacos
nacos:
serverAddr: 127.0.0.1:8848
group: SEATA_GROUP
namespace:
username:
password:
registry:
type: nacos
nacos:
application: seata-server # seata server 的服务名
server-addr: 127.0.0.1:8848 # seata server 所在nacos服务地址
group: SEATA_GROUP
username:
password:
5. 将需要全局事务的方法添加全局事务注解。
@GlobalTransactional