这里我来记录一下我在项目中使用分布式事务seata来解决微服务中数据一致性的问题。
Seata服务端我部署在docker中,具体操作请移步《docker(十八)docker-compose部署seata》
下边我在项目中集成seata。
一:添加POM依赖
<!--seata-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
<exclusions>
<!-- 要与seata服务端版本一直,所以把自带的替换掉 -->
<exclusion>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>1.5.2</version>
</dependency>
二:YML配置
seata:
enabled: true
application-id: ${spring.application.name}
#tx-service-group: ${spring.application.name}-tx-group #it-fccar-service-driver-tx-group
# 指定事务分组至集群映射关系,集群名default需要与seata-server注册到Nacos的cluster保持一致 需要配置到nacos配置中心里边
service:
vgroup-mapping:
default-tx-group: default
# 事务分组配置
tx-service-group: default-tx-group
#data-source-proxy-mode: XA # XA / AT / TCC / SAGA
config:
type: nacos
nacos:
server-addr: 127.0.0.1:8848
namespace: 60f677d3-3d41-4ffb-ae1f-931af47f93bb
group: DEFAULT_GROUP
username: xxx
password: xxx
registry:
typ