动态新增多数据源,dynamic-datasource-spring-boot-starter

动态新增多数据源,dynamic-datasource-spring-boot-starter

dynamic-datasource-spring-boot-starter 使用指南

<dependency>
    <groupId>com.baomidou</groupId>
    <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
    <version>3.4.1</version>
</dependency>
  • 使用druid 连接池
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.8</version>
</dependency>
 <dependency>
            <groupId>com.baomidou</groupId>
            <artifactId>mybatis-plus-boot-starter</artifactId>
            <version>3.4.1</version>
 </dependency>
  <dependency>
            <groupId>com.microsoft.sqlserver</groupId>
            <artifactId>mssql-jdbc</artifactId>
            <scope>runtime</scope>
  </dependency>
  • Springboot 配置文件Demo
spring:
  server:
    port: 8080
  autoconfigure:
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    druid:
      #druid login user config monintor
      stat-view-servlet:
        enabled: true
        url-pattern: /druid/*
        login-username: chenyun
        login-password: chenyun666
        reset-enable: false
      #druid uri config monitor
      web-stat-filter:
        enabled: true
        url-pattern: /*
        exclusions: '*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*'
      #druid spring monitor
      aop-patterns: com.druid.*
      filter:
        #druid sql monitor
        stat:
          enabled: true
          slow-sql-millis: 1000
          log-slow-sql: true
          merge-sql: true
        #druid sql firewall monitor
        wall:
          enabled: true
    dynamic:
      strict: false
      primary: master
      datasource:
        master:
          url: jdbc:sqlserver://127.0.0.1:port;DatabaseName=Main
          username: XXXuser
          password: XXXpassword
          driver-class-name: com.microsoft.sqlserver.jdbc.SQLServerDriver
          druid:
            # 初始化连接数大小,最小,最大
            initial-size: 5
            min-idle: 5
            max-active: 20
            # 配置获取连接等待超时的时间
            max-wait: 30000
            # 配置一个连接在池中最小生存的时间,单位是毫秒
            min-evictable-idle-time-millis: 300000
            max-evictable-idle-time-millis: 300000
            # 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒
            time-between-eviction-runs-millis: 60000
            # 验证数据库连接的有效性
            validation-query: select 1
            validation-query-timeout: -1
            test-while-idle: true
            test-on-borrow: false
            test-on-return: false
            # 打开PSCache,并且指定每个连接上PSCache的大小
            pool-prepared-statements: true
            max-open-prepared-statements: 20
            max-pool-prepared-statement-per-connection-size: 20
            # 配置监控统计拦截的filters,去掉后监控界面sql无法统计,'wall'用于防火墙
            filters: stat,wall,slf4j
            # 通过connectProperties属性来打开mergeSql功能;慢SQL记录
            connectionProperties: druid.stat.mergeSql=true;druid.stat.slowSqlMillis=1000;druid.stat.logSlowSql=true
  • Springboot 使用DEMO
import com.baomidou.dynamic.datasource.creator.DefaultDataSourceCreator;
import javax.sql.DataSource;
@Autowired
private DataSource dataSource;
@Autowired
private DefaultDataSourceCreator dataSourceCreator;
/**
 * 通过DBName、数据集链接信息构建数据库链接对象,并且加入到多数据源中去,调用时传入DBNmae即可;
 *  // 切换数据库
 * DynamicDataSourceContextHolder.push("abk");
 *  // 回到主库
 *  DynamicDataSourceContextHolder.poll();
 * @param dbName
 */
private void addDBinfoToDataSource(String dbName) {
    DataSourceProperty dataSourceProperty = new DataSourceProperty();
    dataSourceProperty.setUrl("jdbc:sqlserver://127.0.0.1:port;DatabaseName="+dbName);
    dataSourceProperty.setUsername("XXXXuser");
    dataSourceProperty.setPassword("XXXXX");
    dataSourceProperty.setDriverClassName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
    DynamicRoutingDataSource ds = (DynamicRoutingDataSource) dataSource;
    DataSource dataSource = dataSourceCreator.createDataSource(dataSourceProperty);
    ds.addDataSource(dbName, dataSource);
}