您的位置:  首页 > 技术杂谈 > 正文

seata整合多数据源

2021-09-28 11:00 https://my.oschina.net/huanoschina/blog/5270617 燚-焱 次阅读 条评论

一、背景

在这篇文章中,我们使用Seata整合一下多数据源的场景。多数据源切换的功能我们使用dynamic-datasource-spring-boot-starter来完成,并且这个组件还可以和Seata进行整合,实现数据源的代理。

此篇文章 依赖之前的 seata整合nacos完成分布式的部署

二、整合步骤

1、seata server的搭建

seata整合nacos完成分布式的部署

2、引入数据源切换组件

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>

3、引入seata组件

<dependency>
   <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.4.2</version>
</dependency>
<!-- 此处seata的注册中心和配置中心使用的都是nacos,索引需要引入这个 -->
<dependency>
    <groupId>com.alibaba.nacos</groupId>
    <artifactId>nacos-client</artifactId>
    <version>1.3.2</version>
</dependency>

4、配置多数据源

  1. 此处配置2个数据源,account和order并且设置和 seata进行整合
  2. 需要注册此切面的位置
  3. 设置默认的数据源
spring:
  datasource:
    dynamic:
      # 启用 seata
      seata: true
      # 模式是 at 模式
      seata-mode: at
      # 主数据源是 account 数据源
      primary: account
      # 不启用严格模式
      strict: false
      # 配置数据源切面的位置
      order: "-2147483648"
      # 每一个数据源
      datasource:
        # account 库的数据源
        account:
          url: jdbc:mysql://127.0.0.1:3306/seata_account?useUnicode=true&characterEncoding=utf8&autoReconnectForPools=true&useSSL=false
          username: root
          password: root
          driver-class-name: com.mysql.cj.jdbc.Driver
        # 订单库的数据源
        order:
          url: jdbc:mysql://127.0.0.1:3306/seata_order?useUnicode=true&characterEncoding=utf8&autoReconnectForPools=true&useSSL=false
          username: root
          password: root
          driver-class-name: com.mysql.cj.jdbc.Driver

5、关闭seata自己默认的数据源代理

seata:
  # 是否自动开启数据源代理
  enable-auto-data-source-proxy: false

6、配置seata事物分组

seata:
  enabled: true
  tx-service-group: tx_multiple_datasource_group 
  # 该分组需要在seata server的配置中心中存在,即在 seata server 的配置中心中需要存在service.vgroupMapping.tx_multiple_datasource_group 配置项

7、业务库创建undo_log表

CREATE TABLE IF NOT EXISTS `undo_log`
(
    `branch_id`     BIGINT       NOT NULL COMMENT 'branch transaction id',
    `xid`           VARCHAR(128) NOT NULL COMMENT 'global transaction id',
    `context`       VARCHAR(128) NOT NULL COMMENT 'undo_log context,such as serialization',
    `rollback_info` LONGBLOB     NOT NULL COMMENT 'rollback info',
    `log_status`    INT(11)      NOT NULL COMMENT '0:normal status,1:defense status',
    `log_created`   DATETIME(6)  NOT NULL COMMENT 'create datetime',
    `log_modified`  DATETIME(6)  NOT NULL COMMENT 'modify datetime',
    UNIQUE KEY `ux_undo_log` (`xid`, `branch_id`)
) ENGINE = InnoDB COMMENT ='AT transaction mode undo table';

8、xid的传递

xid传递

9、代码中使用数据源切换

代码中使用数据源切换

10、业务方法开启分布式事物

业务方法开启分布式事物 到此就整合完了。

三、注意事项

1、开启事物,是需要获取一个数据库连接的,那么我们的 @DS 注解切换数据源必须要在 @Transaction 之前执行。

四、完整代码

https://gitee.com/huan1993/spring-cloud-parent/tree/master/seata/seata-multiple-datasource

  • 0
    感动
  • 0
    路过
  • 0
    高兴
  • 0
    难过
  • 0
    搞笑
  • 0
    无聊
  • 0
    愤怒
  • 0
    同情
热度排行
友情链接