1、数据源枚举类添加MASTERReadOnly数据源
完整代码
package com.ruoyi.common.enums;
/**
* 数据源
*
* @author ruoyi
*/
public enum DataSourceType
{
/**
* 主库,只写
*/
MASTER,
/**
主库,只读
*/
MASTERReadOnly,
/**
* 中冠数据库
*/
SLAVE,
/**
* 执行数据库
*/
SLAVE2,
/**
* 中交token视图
*/
SLAVE3,
/**
* 用户系统
*/
DUUS,
DPMS,
WALLET
}
2、修改数据源配置类DruidConfig增加masterreadonly bean
@Bean
@ConfigurationProperties("spring.datasource.druid.masterreadonly")
@ConditionalOnProperty(prefix = "spring.datasource.druid.masterreadonly", name = "enabled", havingValue = "true")
public DataSource masterReadOnlyDataSource(DruidProperties druidProperties)
{
DruidDataSource dataSource = DruidDataSourceBuilder.create().build();
return druidProperties.dataSource(dataSource);
}
3、修改DruidConfig类中的DynamicDataSource方法增加MASTERReadOnly
@Bean(name = "dynamicDataSource")
@Primary
public DynamicDataSource dataSource(DataSource masterDataSource, DataSource masterReadOnlyDataSource, DataSource slaveDataSource,DataSource slave2DataSource,
DataSource slave3DataSource, DataSource duusDataSource, DataSource dpmsDataSource)
{
Map<Object, Object> targetDataSources = new HashMap<>();
targetDataSources.put(DataSourceType.MASTER.name(), masterDataSource);
targetDataSources.put(DataSourceType.MASTERReadOnly.name(), masterReadOnlyDataSource);
targetDataSources.put(DataSourceType.SLAVE.name(), slaveDataSource);
targetDataSources.put(DataSourceType.SLAVE2.name(), slave2DataSource);
targetDataSources.put(DataSourceType.SLAVE3.name(), slave3DataSource);
targetDataSources.put(DataSourceType.DUUS.name(), duusDataSource);
targetDataSources.put(DataSourceType.DPMS.name(), dpmsDataSource);
return new DynamicDataSource(masterDataSource, targetDataSources);
}
}
4、在yml中配置数据源