1.下载seata源代码,地址github仓库
2.server目录下registry.conf,当前使用的注册中心是eureka,只修改其中带注释的两处地方
registry {
# file 、nacos 、eureka、redis、zk、consul、etcd3、sofa
**type = "file"** #修改为eureka,然后修改响应eureka的设置
nacos {
serverAddr = "localhost"
namespace = ""
cluster = "default"
username = ""
password = ""
}
**eureka {
serviceUrl = "http://localhost:8761/eureka" #你的注册中心地址
application = "default"
weight = "1"
}**
redis {
serverAddr = "localhost:6379"
db = "0"
}
zk {
cluster = "default"
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
consul {
cluster = "default"
serverAddr = "127.0.0.1:8500"
}
etcd3 {
cluster = "default"
serverAddr = "http://localhost:2379"
}
sofa {
serverAddr = "127.0.0.1:9603"
application = "default"
region = "DEFAULT_ZONE"
datacenter = "DefaultDataCenter"
cluster = "default"
group = "SEATA_GROUP"
addressWaitTime = "3000"
}
file {
name = "file.conf"
}
}
config {
# file、nacos 、apollo、zk、consul、etcd3
type = "file"
nacos {
serverAddr = "localhost"
namespace = ""
group = "SEATA_GROUP"
username = ""
password = ""
}
consul {
serverAddr = "127.0.0.1:8500"
}
apollo {
appId = "seata-server"
apolloMeta = "http://192.168.1.204:8801"
namespace = "application"
}
zk {
serverAddr = "127.0.0.1:2181"
sessionTimeout = 6000
connectTimeout = 2000
username = ""
password = ""
}
etcd3 {
serverAddr = "http://localhost:2379"
}
file {
name = "file.conf"
}
}
- serverA中加入maven依赖,目前最高版本的starter使用的seata版本过低,与spring boot 2.x冲突,所以我们要引入高版本的seata-all依赖1.1.0版本
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-spring-boot-starter</artifactId>
<version>${seata.versioin}</version>
</dependency>
<dependency>
<groupId>io.seata</groupId>
<artifactId>seata-all</artifactId>
<version>${seata.versioin}</version>
</dependency>
- serverA加入配置
seata:
registry:
type: eureka #当前你使用的注册中心类型
eureka:
service-url: ${eureka.client.service-url.defaultZone} #注册中心地址
service:
vgroup-mapping:
user-service-seata-service-group: seata-server #这个很重要,默认该属性名为${spring.application.name}-seata-service-group,属性值为seata服务的服务名称
5.加入seata配置文件,该配置是为了代理当前服务的数据库
@Configuration
public class SeataDatasourceProxy {
/**
* autowired datasource config
*/
@Autowired
private DataSourceProperties dataSourceProperties;
@Autowired
private MybatisPlusProperties mybatisPlusProperties;
/**
* init durid datasource
*
* @Return: druidDataSource datasource instance
*/
@Bean
@Primary
public DruidDataSource druidDataSource(){
DruidDataSource druidDataSource = new DruidDataSource();
druidDataSource.setUrl(dataSourceProperties.getUrl());
druidDataSource.setUsername(dataSourceProperties.getUsername());
druidDataSource.setPassword(dataSourceProperties.getPassword());
druidDataSource.setDriverClassName(dataSourceProperties.getDriverClassName());
return druidDataSource;
}
/**
* init mybatis sqlSessionFactory
* @Param: dataSourceProxy datasource proxy
* @Return: DataSourceProxy datasource proxy
* mybatis使用SqlSessionFactory,但是mybatis plus必须使用MybatisSqlSessionFactoryBean
*/
@Bean
public MybatisSqlSessionFactoryBean mybatisSqlSessionFactoryBean(DataSource dataSource) throws Exception {
MybatisSqlSessionFactoryBean factoryBean = new MybatisSqlSessionFactoryBean();
factoryBean.setDataSource(dataSource);
return factoryBean;
}
}
5.在需要控制使用的方法上加上注册@GlobalTransacation注解 just ok