Dynamic data source Mysql and Sqlserver are realized by synchronous datasource spring boot starter

scene

If you build a local development environment and run the tutorial of the project according to the previous back-end separate version:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/108465662

If you follow the instructions of microservices to build a local environment and run front-end and back-end projects:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/109363303

After the project is built using the front-end and back-end separated architecture and micro service architecture, the default connection is

Is a Mysql database.

Then, if you have built-in support for multiple data sources,

However, after configuring multiple data sources in the front and back-end separate version, you need to add annotations on each method to distinguish it

Using the microservice version, you can separate a service and connect the whole service to another data source.

However, in the same service, except for a special business that requires a separate connection to the Sqlserver database, other

Request the data of Mysql main database normally.

Dynamic datasource spring starter can be used to support distributed transactions based on SpringBoot multi data source dynamic data source master-slave separation fast initiator

Its Gitee address is:

https://gitee.com/baomidou/dynamic-datasource-spring-boot-starter

appointment

Note:

Blog:
https://blog.csdn.net/badao_liumang_qizhi
Official account
Domineering procedural ape
Get programming related e-books, tutorial push and free download.

realization

First, introduce the dependency in the pom file of the service to be used

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

Because the slave database data source here needs to connect to Sqlserver, you need to add the dependency of Sqlserver

        <!-- sqlserver-->
        <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <version>7.4.1.jre8</version>
        </dependency>

Then find the configuration file of the configuration data source. The front-end and back-end separation is the yml file, and the micro service is in Nacos.

Take a look at the official configuration example

spring:
  datasource:
    dynamic:
      primary: master #Set the default data source or data source group. The default value is master
      strict: false #Strictly match the data source. The default is false True throws an exception when it does not match the specified data source, and false uses the default data source
      datasource:
        master:
          url: jdbc:mysql://xx.xx.xx.xx:3306/dynamic
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver # 3.2.0 SPI support can be omitted
        slave_1:
          url: jdbc:mysql://xx.xx.xx.xx:3307/dynamic
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
        slave_2:
          url: ENC(xxxxx) # Built in encryption, please check the detailed document
          username: ENC(xxxxx)
          password: ENC(xxxxx)
          driver-class-name: com.mysql.jdbc.Driver
         
       #...... ellipsis
       #The above will configure a default library master, and there are two sub libraries under a group slave_ 1,slave_ two

This is modified according to your own needs. Here is

# Spring
spring:
  datasource:
    dynamic:
      primary: mysql
      datasource:
        #mysql database
        mysql:
          driver-class-name: com.mysql.cj.jdbc.Driver
         url:jdbc:mysql://Mysql database IP:3306 / database name? useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
          username: root
          password: password
        #sqlserver
        sqlserver:
          driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
          url: jdbc:sqlserver://IP of Sqlserver database: 1433; Databasename = database name
          username: root
          password: password

The main database here is still Mysql data, and only the data of the slave database Sqlserver needs to be queried under a certain business

How to use data source switching after the above configuration

Official example

Just use @ DS annotation.

If there is no annotation, the default data source is.

Therefore, the @ DS annotation is added to the implementation of the Service that needs to query the Sqlserver database. The parameter is the above

The node name of the slave library data source of the configuration file

In this way, the configured Mysql master database data source can be used except for this business. Only those annotated can use the slave database data source.

Added by |Adam| on Tue, 08 Feb 2022 09:53:38 +0200