The background,

In this article, we use Seata to consolidate a multi-data source scenario. We use dynamic-datasource-spring-boot-starter to switch multiple data sources, and this component can be integrated with Seata to realize the proxy of data sources.

This article relies on the previous SeATA integration nacOS for distributed deployment

Second, integration steps

1. Setup of SEATA Server

Seata integrates NACOS for distributed deployment

2. Introduce the data source switching component

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

3. Introduce seATA components

<dependency>
   <groupId>io.seata</groupId>
    <artifactId>seata-spring-boot-starter</artifactId>
    <version>1.4.2</version>
</dependency>
<! -- SeATA's registry and configuration center use NACOS.
<dependency>
    <groupId>com.alibaba.nacos</groupId>
    <artifactId>nacos-client</artifactId>
    <version>1.3.2</version>
</dependency>

Copy the code

4. Configure multiple data sources

  1. Configure two data sources, Account and Order, and set up integration with SEATA
  2. The location of this section needs to be registered
  3. Set the default data source
spring:
  datasource:
    dynamic:
      # enable seata
      seata: true
      The mode is at mode
      seata-mode: at
      The primary data source is the account data source
      primary: account
      # Disable strict mode
      strict: false
      Configure the location of the data source facet
      order: "2147483648"
      # Every data source
      datasource:
        # Data source for the account library
        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 library data source
        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
Copy the code

5. Turn off seata’s own default datasource agent

seata:
  Whether to enable the data source agent automatically
  enable-auto-data-source-proxy: false
Copy the code

6. Configure seATA transaction grouping

seata:
  enabled: true
  tx-service-group: tx_multiple_datasource_group 
  Vgroupmapping.tx_multiple_datasource_group The service. Vgroupmapping.tx_multiple_datasource_group must be configured in the seata server configuration center
Copy the code

Create table 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';
Copy the code

8. Xid passing

9. Data source switching is used in the code

10. Business approach unlocks distributed things

That’s where the integration ends.

Three, notes

To open a Transaction, we need to obtain a database connection, so our @DS annotation to switch the data source must be performed before @Transaction.

4. Complete code

Gitee.com/huan1993/sp…