温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

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

发布时间:2023-03-21 11:00:05 来源:亿速云 阅读:250 作者:iii 栏目:开发技术

本篇内容介绍了“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+7QFaBsPMVgdPR1udDSqZe75Yh6Obp8YHyRw==
              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配置文件添加如上配置即可,注意必须是驼峰型,这个坑是真的坑

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

    后续

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

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

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

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

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

    “Springboot多数据源配置之怎么整合dynamic-datasource”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注亿速云网站,小编将为大家输出更多高质量的实用文章!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI