看到网上有不少实现,但是大多讲的不仔细,或实现的不优雅这里记录一下我的实现方式。
org.springframework.boot
spring-boot-starter-web
org.mybatis.spring.boot
mybatis-spring-boot-starter
2.1.0
org.springframework.boot
spring-boot-devtools
runtime
true
mysql
mysql-connector-java
org.springframework.boot
spring-boot-configuration-processor
true
org.springframework.boot
spring-boot-starter-test
test
org.apache.commons
commons-pool2
mysql
mysql-connector-java
org.springframework.boot
spring-boot-starter-aop
com.alibaba
fastjson
1.2.55
org.springframework.boot
spring-boot-devtools
true
org.apache.commons
commons-lang3
3.8.1
junit
junit
4.12
com.alibaba
druid-spring-boot-starter
1.1.17
org.springframework.boot
spring-boot-starter
org.springframework.boot
spring-boot-starter-logging
org.springframework.boot
spring-boot-starter-log4j2
2.创建多数据源bean的配置类
/**
* 多数据源bean的配置类
* @author hy
*/
@Configuration
public class MultipleDataSourceConfig {
@Bean("master")
@ConfigurationProperties(prefix = "spring.datasource.master")
public DataSource createMasterDataSource(){
return new DruidDataSource();
}
@Bean("slave1")
@ConfigurationProperties(prefix = "spring.datasource.slave1")
public DataSource createSlave1DataSource(){
return new DruidDataSource();
}
/**
* 设置动态数据源,通过@Primary 来确定主DataSource
* @return
*/
@Bean
@Primary
public DataSource createDynamicDataSource(@Qualifier("master") DataSource master, @Qualifier("slave1") DataSource slave1){
DynamicDataSource dynamicDataSource = new DynamicDataSource();
//设置默认数据源
dynamicDataSource.setDefaultTargetDataSource(master);
//配置多数据源
Map<Object, Object> map = new HashMap<>();
map.put("master",master);
map.put("slave1",slave1);
dynamicDataSource.setTargetDataSources(map);
return dynamicDataSource;
}
}
这里是使用配置类注入bean
使用注解@Configuration和ConfigurationProperties,bean的属性值由配置文件的 spring.datasource.master和spring.datasource.slave1决定
bean的类型是Datasource,名字是由注解@Bean 指定
随后在方法createDynamicDataSource中使用 @Qualifier 使用指定名字的
public class DynamicDataSource extends AbstractRoutingDataSource {
Logger logger = LoggerFactory.getLogger(DynamicDataSource.class);
@Override
protected Object determineCurrentLookupKey() {
logger.info("------------------当前数据源 {}", DynamicDataSourceSwitcher.getDataSource());
return DynamicDataSourceSwitcher.getDataSource();
}
}
AbstractRoutingDataSource 是spring jdbc提供的操作读数据源的抽象类,重写determineCurrentLookupKey() 指定获得当前数据源
查看父类AbstractRoutingDataSource 的源码
protected DataSource determineTargetDataSource() {
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
Object lookupKey = determineCurrentLookupKey();
DataSource dataSource = this.resolvedDataSources.get(lookupKey);
if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
dataSource = this.resolvedDefaultDataSource;
}
if (dataSource == null) {
throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
}
return dataSource;
}
上面是determineTargetDataSource方法,可以看到若determineCurrentLookupKey返回空则使用默认数据源
/**
* 操作数据源
* @author hy
*/
public class DynamicDataSourceSwitcher {
static Logger logger = LoggerFactory.getLogger(DynamicDataSourceSwitcher.class);
public static final String Mater = "master";
public static final String Slave1 = "slave1";
private static final ThreadLocal<String> contextHolder = new ThreadLocal<>();
public static void setDataSource(String name){
logger.info("-------- 设置数据源数据源为 :{} ", name);
contextHolder.set(name);
}
public static String getDataSource(){
return contextHolder.get();
}
public static void cleanDataSource(){
contextHolder.remove();
}
}
利用ThreadLocal 将数据源与当前线程绑定,并提供get set方法
5.配置application.yml文件
spring:
profiles: multi
datasource:
type: com.alibaba.druid.pool.DruidDataSource
master:
url: jdbc:mysql://xxx:3306/xxx?useSSL=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false
username: xxx
password: xxx
type: com.alibaba.druid.pool.DruidDataSource
name: master
initialize: true
# 监控统计拦截的filters 有stat,wall,log4j
filters: stat
slave1:
url: jdbc:mysql://xxx:3306/xxx?useSSL=false&useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&autoReconnect=true&failOverReadOnly=false
username: xxx
password: xxx
type: com.alibaba.druid.pool.DruidDataSource
name: slave1
initialize: true
filters: stat
以上就已经实现了多数据源的配置,要实现自动切换还需要加入aop切面
/**
* 自定义的数据源的注解
* @author hy
*/
@Retention(RetentionPolicy.RUNTIME)
@Target({
ElementType.METHOD
})
public @interface MyDataSource {
String value() default "master";
}
/**
* 创建aop切面
* @author hy
*/
@Aspect
@Component
@Order(1) //需要加入切面排序
public class DynamicDataSourceAspect {
private Logger logger = LoggerFactory.getLogger(DynamicDataSourceAspect.class);
/**
* 切入点只对@Service注解的类上的@DataSource方法生效
* @param myDataSource
*/
@Pointcut(value="@within(org.springframework.stereotype.Service) && @annotation(myDataSource)" )
public void dynamicDataSourcePointCut(MyDataSource myDataSource){}
@Before(value = "dynamicDataSourcePointCut(myDataSource)")
public void switchDataSource(MyDataSource myDataSource) {
DynamicDataSourceSwitcher.setDataSource(myDataSource.value());
}
/**
* 切点执行完后 切换成主数据库
* @param myDataSource
*/
@After(value="dynamicDataSourcePointCut(myDataSource)")
public void after(MyDataSource myDataSource){
DynamicDataSourceSwitcher.cleanDataSource();
}
}
使用:
@Service
public class WxUserService {
@Resource
WxUserMapper wxUserMapper;
public WxUser getUserById(Integer id){
return wxUserMapper.selectByPrimaryKey(id);
}
@MyDataSource(value = DynamicDataSourceSwitcher.Slave1)
public WxUser getUserByIdWithSlave(Integer id) {
return wxUserMapper.selectByPrimaryKey(id);
}
}