nacos整合seata实现分布式事务
seata-server端配置
- 下载seata启动包
- 建表(db):sql脚本链接
- 修改file.conf中store.mode的信息

- 修改file.conf中store.db的信息

- 在nacos新建配置,dataId为seataServer.properties
- 修改seataServer.properties中数据库相关信息

- 启动seata-server:sh seata-server.sh
seata-client端配置
- 增加 Maven 依赖
<!-- 建议使用 Seata 1.4.0+ -->
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.4.2</version>
</dependency>
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<version>2.2.1.RELEASE</version>
<exclusions>
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
- 在 application.yml 中加入对应的配置中心
seata:
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
group : "SEATA_GROUP"
namespace: ""
dataId: "seataServer.properties"
username: "nacos"
password: "nacos"
- SEATA AT 模式需要 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;
- 添加@GlobalTransactional注解使用
@GlobalTransactional
@Override
public String insertPayment(Payment payment) {
return "下单成功";
}