springBoot calls dynamic-datasource from multiple data sources

Keywords: MySQL JDBC Druid Database

Introduction to 1 dynamic-datasource

  • This framework only does the core thing of switching data sources and does not restrict your specific operations.
  • Highly recommended in master-slave mode
  • Follow the general rules so that others can understand your code more easily.
  • The primary database recommends that only INSERT UPDATE DELETE operations be performed.
  • It is recommended that only SELECT operations be performed from the database.

2 Usage

2.1 Introducing dynamic-datasource-spring-boot-starter

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

2.2 spring boot configuration (yml)

Master-slave scheme

spring:
  datasource:
    dynamic:
      primary: master #Set the default data source or data source group, the default value is master, which is not defined if the name of your master library is master by default.
      datasource:
        master:
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://47.xxx.xx.xxx:3306/dynamic?characterEncoding=utf8&useSSL=false
        slave_1:
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://47.xxx.xx.xxx:3307/dynamic?characterEncoding=utf8&useSSL=false
        slave_2:
          username: root
          password: 123456
          driver-class-name: com.mysql.jdbc.Driver
          url: jdbc:mysql://47.xxx.xx.xxx:3308/dynamic?characterEncoding=utf8&useSSL=false
       #...omitted
       #The above configures a default library master with two sublibraries slave_1 and slave_2 under a group slave

Multi-master and multi-slave scheme

spring:
  datasource:
    dynamic:
      datasource:
        master_1:
        master_2:
        slave_1:
        slave_2:
        slave_3:

Multi-database schema

spring:
  datasource:
    dynamic:
      primary: mysql #Remember to set a default data source
      datasource:
        mysql:
        oracle:
        sqlserver: 
        h2:

3 Use notes

3.1 Note Introduction

@DS can annotate on methods and classes, and there are method annotations that take precedence over class annotations, which are strongly recommended on service implementations or mapper interface methods.

Note that @DS null annotations are no longer supported from 2.0.0. You must specify the database group name you want or a specific database name.

annotation Result
No @DS Default data source
@DS("dsName") dsName can be either a group name or a specific library name

Annotations can be used at the sevice and mapper layers and on method classes

4 Inherit druid

springBoot 2.x uses HikariCP by default, so I don't think it's necessary to change Druid for HikariCP, because it's more for domestic use or just write down

**Note:**Master slaves can use different database connection pools, such as master monitoring using Druid and HikariCP using HikariCP from library.If the connection pool type is not configured, Druid takes precedence over HikariCP by default.

4.1 Exclude native Druid configuration

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)
public class Application {

  public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
  }

}

4.2 Configure yml after importing druid packages

spring:
  datasource:
    druid:
      stat-view-servlet:
        loginUsername: root
        loginPassword: root
    dynamic:
      datasource:
        master:
          username: root
          password: root
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://120.xxx.xxx.xxx:3316/aaaa?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
          druid: #The following are default values
            initial-size: 3
            max-active: 8
            min-idle: 2
            max-wait: -1
            min-evictable-idle-time-millis: 30000
            max-evictable-idle-time-millis: 30000
            time-between-eviction-runs-millis: 0
            validation-query: select 1
            validation-query-timeout: -1
            test-on-borrow: false
            test-on-return: false
            test-while-idle: true
            pool-prepared-statements: true
            max-open-prepared-statements: 100
            filters: stat,wall
            share-prepared-statements: true
        slave_1:
          username: root
          password: root
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://120.xx.xx.xxx:3317/aaaa?useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai
          druid: #The following are default values
            initial-size: 3
            max-active: 8
            min-idle: 2
            max-wait: -1
            min-evictable-idle-time-millis: 30000
            max-evictable-idle-time-millis: 30000
            time-between-eviction-runs-millis: 0
            validation-query: select 1
            validation-query-timeout: -1
            test-on-borrow: false
            test-on-return: false
            test-while-idle: true
            pool-prepared-statements: true
            max-open-prepared-statements: 100
            filters: stat,wall
            share-prepared-statements: true

Summary

  • You can use this when projects are not typing, but once there are many databases, using dynamic-datasource can lead to very difficult post-maintenance, so it is highly recommended that the business use sharding-jdbc
  • Always remember to isolate the native Druid configuration
  • Database master-slave separation needs to be done

Posted by dinno2 on Fri, 20 Sep 2019 18:08:41 -0700