分布式事务解决方案之Seata

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. 1 。服务消费者 和 服务提供者 导入seata架包
<!--seata配置-->
<dependency>
    <groupId>com.alibaba.cloud</groupId>
    <artifactId>spring-cloud-alibaba-seata</artifactId>
    <version>2.1.0.RELEASE</version>
</dependency>
  1. 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
  1. 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;
    }

}
  1. 4。 导入 file.conf 和 registry.conf( 在seata-server-0.7.1\conf目录下 )
    file.conf ,registry.conf数据源获取 放置路径如下:

在这里插入图片描述

  1. 设置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 )

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值