Springboot多数据源配置之整合dynamic-datasource方式

多数据源配置之整合dynamic-datasource

技术:SpringBoot + Mybatis-Plus + Druid

SpringBoot里做多数据源配置,可以直接使用dynamic-datasource提供的服务,简单便捷

POM里加入依赖包

<!--多数据源依赖-->
    <dependency>
      <groupId>com.baomidou</groupId>
      <artifactId>dynamic-datasource-spring-boot-starter</artifactId>
      <version>3.3.1</version>
    </dependency>

yml增加多数据配置:

autoconfigure:
    # 自动化配置 例外处理
    exclude: com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure
  datasource:
    type: com.alibaba.druid.pool.DruidDataSource
    #多数据源配置
    dynamic:
      #设置默认的数据源
      primary: master
      datasource:
        # 数据库1
        master:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost:3306/praticce_a?useUnicode=true&characterEncoding=utf-8&useSSL=false
          username: root
          password: e0BhauLW2yhi2se9L+7QFaBsPMVgdPR1udDSqZe75Yh5Obp8YHyRw==
          druid:
            filters: stat,slf4j,wall,config
            initial-size: 10
            max-active: 100
            max-open-prepared-statements: 20
            max-pool-prepared-statement-per-connection-size: 20
            max-wait: 60000
            min-evictable-idle-time-millis: 300000
            min-idle: 10
            pool-prepared-statements: true
            test-on-borrow: false
            test-on-return: false
            test-while-idle: true
            time-between-eviction-runs-millis: 60000
            validation-query: select 'x'
            publicKey: MFwwDQYJKoZIhvcNAr7HWXiJQjmIZN6IYPMPzfRe20da5d8EAAQ==
            connection-properties: config.decrypt=true;config.decrypt.key=${spring.datasource.dynamic.datasource.master.druid.publicKey}
        # 数据库2
        slave1:
          driver-class-name: com.mysql.cj.jdbc.Driver
          url: jdbc:mysql://localhost:3306/pratice_b?useUnicode=true&characterEncoding=utf-8
          username: root
          password: EVHrRAfXPl0Qpd6j4GsodQOwvsV9Q==
          druid:
            filters: stat,slf4j,wall,config
            initial-size: 10
            max-active: 100
            max-open-prepared-statements: 20
            max-pool-prepared-statement-per-connection-size: 20
            max-wait: 60000
            min-evictable-idle-time-millis: 300000
            min-idle: 10
            pool-prepared-statements: true
            test-on-borrow: false
            test-on-return: false
            test-while-idle: true
            time-between-eviction-runs-millis: 60000
            validation-query: select 'x'
            publicKey: MFwwDQYJKoZIhvcNAQEBBQADSwAwQ==
            connection-properties: config.decrypt=true;config.decrypt.key=${spring.datasource.dynamic.datasource.msg.druid.publicKey}

说明:

数据库配置这里使用了Druid的加密方式,如果数据库连接密码不需要加密,则password可以直接使用明文,druid配置中去掉config配置(filters中去掉config,connection-properties可以都去掉)

spring.datasource.dynamic.primary是用于设置默认的数据源,这个最好设置一个,因为我们不可能每个类或接口都指定数据源

spring.autoconfigure.exclude是去除Druid自动装载数据库配置,也可以直接在项目启动类XXXApplication上加

@SpringBootApplication(exclude = DruidDataSourceAutoConfigure.class)

在需要切换数据源的类或方法上加@DS注解

@Mapper
@DS("msg")
public interface TestMapper {
 
    /**
     * 批量数据
     *
     * @param poList 数据
     */
    void batchAdd(@Param("poList") List<TestDTO> poList);
 
}

@DS优先级:方法 > 类

dynamic-datasource-spring-boot-starter多数据源 + pagehelper出现分页异常

解决方法

pagehelper:
 autoRuntimeDialect: true

在yml配置文件添加如上配置即可,注意必须是驼峰型,这个坑是真的坑

在这里插入图片描述

后续

经过查看源码发现,PageHelper的配置类不是使用普通的Bean,而是继承Properties类。

断点发现,代码都没有进入

public void setAutoRuntimeDialect(Boolean autoRuntimeDialect) {
 this.setProperty("autoRuntimeDialect", autoRuntimeDialect.toString());
 }

导致这个参数配置的没有生效。

搞得我还一直纳闷是不是dynamic-datasource-spring-boot-starter的问题

总结

作者:彭小虾原文地址:https://blog.csdn.net/pjx827480541/article/details/116488837

%s 个评论

要回复文章请先登录注册