Seata的使用简介
一,引入依赖
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-starter-alibaba-seata</artifactId>
</dependency>
二,seata官网下载压缩包
1,修改conf目录下的registry.conf文件
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
type = "nacos"
nacos {
application = "seata-server"
serverAddr = "127.0.0.1:8848"
group = "DEFAULT_GROUP"
namespace = ""
cluster = "default"
username = ""
password = ""
}
2,修改file.conf文件
1.3.0版本没有service块,需要再老版本中复制
service {
#vgroup->rgroup
vgroupMapping.gulimall-seata-service-group = "default"
#only support single node
default.grouplist = "localhost:8091"
#degrade current not support
enableDegrade = false
#disable
disable = false
#unit ms,s,m,h,d represents milliseconds, seconds, minutes, hours, days, default permanent
max.commit.retry.timeout = "-1"
max.rollback.retry.timeout = "-1"
}
三,复制registry.conf和file.conf到项目的resource目录中
四,微服务项目配置
1,需要事务控制的微服务中增加配置数据源配置文件,内容如下:
@Configuration
public class SeataConfig {
@Autowired
DataSourceProperties dataSourceProperties;
@Primary
@Bean
public DataSource dataSource(DataSourceProperties dataSourceProperties) {
HikariDataSource dataSource = dataSourceProperties.initializeDataSourceBuilder().type(HikariDataSource.class).build();
if (StringUtils.hasText(dataSourceProperties.getName())) {
dataSource.setPoolName(dataSourceProperties.getName());
}
return new DataSourceProxy(dataSource);
}
}
2,application.properties文件中增加配置,需要和file.conf中的 vgroupMapping.xx保持一致
spring.cloud.alibaba.seata.tx-service-group=gulimall-seata-service-group
五,启动seata-server,并在业务方法上使用@GlobalTransactional 注解即可