一、除了MySQL驅(qū)動(dòng),我們還需要用到postgresql的驅(qū)動(dòng),所以我們先把驅(qū)動(dòng)的依賴給導(dǎo)入進(jìn)來(lái)
<!-- PostgreSql驅(qū)動(dòng)包 -->
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
</dependency>
二,修改application-druid.yml:
# 數(shù)據(jù)源配置
spring:
datasource:
type: com.alibaba.druid.pool.DruidDataSource
druid:
# 主庫(kù)數(shù)據(jù)源
master:
driverClassName: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/i_ren_shi?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8
username: root
password: root
# 從庫(kù)數(shù)據(jù)源
slave:
# 從數(shù)據(jù)源開(kāi)關(guān)/默認(rèn)關(guān)閉
enabled: true
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/easytrack
username: 123456
password: 123456
easytrack:
# 從數(shù)據(jù)源開(kāi)關(guān)/默認(rèn)關(guān)閉
enabled: true
driverClassName: org.postgresql.Driver
url: jdbc:postgresql://localhost:5432/easytrack
username: 123456
password: 123456
三、新數(shù)據(jù)源的配置
(1)修改DatasourceType
package com.ruoyi.common.enums;
/**
* 數(shù)據(jù)源
*
* @author ruoyi
*/
public enum DataSourceType
{
/**
* 主庫(kù)
*/
MASTER,
/**
* 從庫(kù)
*/
SLAVE,
/**
* 新配置數(shù)據(jù)源名稱
*/
EASYTRACK
}
(2)修改DruidConfig,這里有很多細(xì)節(jié)要注意,就是大小寫(xiě)的問(wèn)題
@Bean
@ConfigurationProperties("spring.datasource.druid.easytrack")
@ConditionalOnProperty(prefix = "spring.datasource.druid.easytrack", name = "enabled", havingValue = "true")
public DataSource easyTrackDataSource(DruidProperties druidProperties)
{
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource);
}
@Bean(name = "dynamicDataSource")
@Primary
public DynamicDataSource dataSource(DataSource masterDataSource)
{
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
setDataSource(targetDataSources, DataSourceType.SLAVE.name(), "slaveDataSource");
setDataSource(targetDataSources, DataSourceType.EASYTRACK.name(), "easyTrackDataSource");
return new DynamicDataSource(masterDataSource, targetDataSources);
}
文章來(lái)源:http://www.zghlxwxcb.cn/news/detail-595222.html
(3)使用選擇數(shù)據(jù)源,會(huì)自動(dòng)切換數(shù)據(jù)源
@DataSource(value = DataSourceType.EASYTRACK)
文章來(lái)源地址http://www.zghlxwxcb.cn/news/detail-595222.html
到了這里,關(guān)于ruoyi(若依)配置多數(shù)據(jù)源(mysql+postgresql),rouyi(Springboot)多數(shù)據(jù)源設(shè)置的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!