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優(yōu)先級:方法 > 類
dynamic-datasource-spring-boot-starter多數據源 + pagehelper出現分頁異常
解決方法
pagehelper: autoRuntimeDialect: true
在yml配置文件添加如上配置即可,注意必須是駝峰型,這個坑是真的坑

后續(xù)
經過查看源碼發(fā)現,PageHelper的配置類不是使用普通的Bean,而是繼承Properties類。
斷點發(fā)現,代碼都沒有進入
public void setAutoRuntimeDialect(Boolean autoRuntimeDialect) {
this.setProperty("autoRuntimeDialect", autoRuntimeDialect.toString());
}
導致這個參數配置的沒有生效。
搞得我還一直納悶是不是dynamic-datasource-spring-boot-starter的問題
總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
PowerJob的TransportServiceAware工作流程源碼解讀
這篇文章主要介紹了PowerJob的TransportServiceAware工作流程源碼解讀,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2024-01-01
Springboot項目的搭建教程(分離出common父依賴)
這篇文章主要介紹了Springboot項目的搭建教程(分離出common父依賴),具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-01-01
Spring Profile與PropertyPlaceholderConfigurer項目多環(huán)境配置切換
這篇文章主要介紹了Spring Profile與PropertyPlaceholderConfigurer項目多環(huán)境配置切換方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-09-09
spring cloud gateway網關路由分配代碼實例解析
這篇文章主要介紹了spring cloud gateway網關路由分配代碼實例解析,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下2020-01-01
java.lang.ArrayIndexOutOfBoundsException數組越界異常問題解決
這篇文章主要給大家介紹了關于java.lang.ArrayIndexOutOfBoundsException數組越界異常問題解決的相關資料,數組越界訪問是一個非常嚴重的問題,文中通過圖文將解決的辦法介紹的非常詳細,需要的朋友可以參考下2024-01-01

