seata整合多數據源


一、背景

在這篇文章中,我們使用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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM