1 . 下载seata(事务协调器TC)服务器
下载地址: https://github.com/seata/seata/releases/dowrpoadv0.7.1/seata-server-0.7.1.zip
也可以直接解压:资料lseata-server-0.7.1.zip
2 . 解压并 启动
cdm 打开 start F:\seata\seata-server-0.7.1\bin\seata-server.bat -p 8888 -m file
**注**:其中8888为服务端口号; file为启动模式,这里指seata服务将采用文件的方式存储信息。
3 . 父工程导入alibaba框架(使用seata工具必须基于alibaba框架)
<!--alibaba框架配置-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-dependencies</artifactId>
<version>2.1.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
4 . 配置TM和RM
- 1 。服务消费者 和 服务提供者 导入seata架包
<!--seata配置-->
<dependency>
<groupId>com.alibaba.cloud</groupId>
<artifactId>spring-cloud-alibaba-seata</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
- 2。服务消费者 和 服务提供者 配置数据源
spring:
application:
name: order-service #服务名称
datasource: #seata数据源代理配置
ds0:
url: jdbc:mysql://localhost:3306/mb?useUnicode=true&characterEncoding=utf8&serverTimezone=UTC
username: root
password: root
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
- 3。服务消费者 和 服务提供者 获取数据源
@Configuration
public class DatabaseConfiguration {
private final ApplicationContext applicationContext;
public DatabaseConfiguration(ApplicationContext applicationContext){
this.applicationContext = applicationContext;
}
@Bean
@ConfigurationProperties(prefix ="spring.datasource.ds0" )
public DruidDataSource ds0(){
DruidDataSource druidDataSource = new DruidDataSource();
return druidDataSource;
}
@Primary
@Bean
public DataSource dataSource(DruidDataSource ds0) {
DataSourceProxy pds0 = new DataSourceProxy(ds0);
return pds0;
}
}
- 4。 导入 file.conf 和 registry.conf( 在seata-server-0.7.1\conf目录下 )
file.conf ,registry.conf 及 数据源获取 放置路径如下:
- 设置file.conf 更改服务名 和 端口号 即可
6. 开启分布式事务事务
全局事务发起者
分支事务
注意:全局事务 和 分支事务配置一样(导入seata架包,配置数据源,获取数据源,导入file.conf 和 registry.conffile.conf 更改服务名 和 端口号即可)。。。。。项目运行必须开启事务协调器TC(打开cdm输入: start (seata在电脑中的解压路径)/seata-server-0.7.1\bin\seata-server.bat -p 8888 -m file )