添加多数据源
parent
d3807cbb89
commit
1603507d32
|
|
@ -0,0 +1,69 @@
|
|||
package basepro.manager.config;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Qualifier;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.boot.jdbc.DataSourceBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||||
import org.springframework.transaction.PlatformTransactionManager;
|
||||
import javax.sql.DataSource;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@Configuration
|
||||
public class DataSourceConfig {
|
||||
/**
|
||||
* 数据源1
|
||||
* spring.datasource.db1:application.properteis中对应属性的前缀
|
||||
* @return
|
||||
*/
|
||||
@Bean(name = "db1")
|
||||
@ConfigurationProperties(prefix = "spring.datasource.druid.db1")
|
||||
public DataSource dataSourceOne() {
|
||||
return DataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
/**
|
||||
* 数据源2
|
||||
* spring.datasource.db2:application.properteis中对应属性的前缀
|
||||
* @return
|
||||
*/
|
||||
@Bean(name = "db2")
|
||||
@ConfigurationProperties(prefix = "spring.datasource.druid.db2")
|
||||
public DataSource dataSourceTwo() {
|
||||
return DataSourceBuilder.create().build();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 动态数据源: 通过AOP在不同数据源之间动态切换
|
||||
* @return
|
||||
*/
|
||||
@Primary
|
||||
@Bean(name = "dynamicDataSource")
|
||||
public DataSource dynamicDataSource() {
|
||||
DynamicDataSource dynamicDataSource = new DynamicDataSource();
|
||||
// 默认数据源
|
||||
dynamicDataSource.setDefaultTargetDataSource(dataSourceOne());
|
||||
// 配置多数据源
|
||||
Map<Object, Object> dsMap = new HashMap<>();
|
||||
dsMap.put("db1", dataSourceOne());
|
||||
dsMap.put("db2", dataSourceTwo());
|
||||
|
||||
dynamicDataSource.setTargetDataSources(dsMap);
|
||||
return dynamicDataSource;
|
||||
}
|
||||
|
||||
/**
|
||||
* 配置多数据源后IOC中存在多个数据源了,事务管理器需要重新配置,不然器不知道选择哪个数据源
|
||||
* 事务管理器此时管理的数据源将是动态数据源dynamicDataSource
|
||||
* 配置@Transactional注解
|
||||
* @return
|
||||
*/
|
||||
@Bean
|
||||
public PlatformTransactionManager transactionManager() {
|
||||
return new DataSourceTransactionManager(dynamicDataSource());
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,40 @@
|
|||
package basepro.manager.config;
|
||||
|
||||
/**
|
||||
* 数据源切换工具
|
||||
* @date 2022/5/19
|
||||
*/
|
||||
public class DataSourceUtil {
|
||||
/**
|
||||
* 默认数据源
|
||||
*/
|
||||
public static final String DEFAULT_DS = "db1";
|
||||
/**
|
||||
* 数据源属于一个公共的资源
|
||||
* 采用ThreadLocal可以保证在多线程情况下线程隔离
|
||||
*/
|
||||
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
|
||||
|
||||
/**
|
||||
* 设置数据源名
|
||||
* @param dbType
|
||||
*/
|
||||
public static void setDB(String dbType) {
|
||||
contextHolder.set(dbType);
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取数据源名
|
||||
* @return
|
||||
*/
|
||||
public static String getDB() {
|
||||
return (contextHolder.get());
|
||||
}
|
||||
|
||||
/**
|
||||
* 清除数据源名
|
||||
*/
|
||||
public static void clearDB() {
|
||||
contextHolder.remove();
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,13 @@
|
|||
package basepro.manager.config;
|
||||
|
||||
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
|
||||
/**
|
||||
* 动态数据源类
|
||||
* @date 2022/2/11
|
||||
*/
|
||||
public class DynamicDataSource extends AbstractRoutingDataSource {
|
||||
@Override
|
||||
protected Object determineCurrentLookupKey() {
|
||||
return DataSourceUtil.getDB();
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue