Apache Seata- 是一款开源的分布式事务解决方案,致力于在微服务架构下提供高性能和简单易用的分布式事务服务。(是处理分布式事务的,可以保证数据一致性)
数据库中创建undo_log表,用于记录事务回滚信息
-- 注意此处0.3.0+ 增加唯一索引 ux_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;
2.安装seata-server-2.0.0.zip
3.配置zookeeper,在seata\conf\application.yml文件中配置信息
seata:
registry:
type: zk
zk:
server-addr: 127.0.0.1:2181
# 有关事务分组,请参考 https://seata.apache.org/zh-cn/docs/user/txgroup/transaction-group
session-timeout: 6000
connect-timeout: 2000
username:
password:
tx-service-group: default_tx_group
service:
# 事务分组与集群映射关系
vgroup-mapping:
default_tx_group: default
4.启动seata,打开seata\bin\seata-server.bat
5.配置在分布式框架的父服务器中引入seata的相关依赖
<!--分布式事务seata-->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>${seata.version}</version>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.11</version>
</dependency>
6.在需要加事务的方法上添加注解