1 方针
不在现有查询代码逻辑上做任何改动,完成dao维度的数据源切换(即表维度)
2 运用场景
节约bdp的集群资源。接入新的宽表时,一般uat验证后就会中止集群释放资源,在对应的查询服务器uat环境时需求查询的是出产库的表数据(uat库表因为bdp实时使命中止,没有数据落入),只进行服务器装备文件的改动而无需进行代码的修改变更,即可按需切换查询的数据源。
2.1 实时使命对应的集群资源
2.2 实时使命发生的数据进行存储的两套环境
2.3 数据运用系统的两套环境(查询展现数据)
即需求在zhongyouex-bigdata-uat中查询出产库的数据。
3 完成过程
3.1 完成要点
- org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
spring供给的这个类是本次完成的中心,能够让咱们完成运转时多数据源的动态切换,但是数据源是需求事先装备好的,无法动态的增加数据源。 - Spring供给的Aop阻拦执行的mapper,进行切换判别并进行切换。
注:另外还有一个便是ThreadLocal类,用于保存每个线程正在运用的数据源。
3.2 AbstractRoutingDataSource解析
public abstract class AbstractRoutingDataSource extends AbstractDataSource
implements InitializingBean{
@Nullable
private Map<Object, Object> targetDataSources;
@Nullable
private Object defaultTargetDataSource;
@Override
public Connection getConnection() throws SQLException {
return determineTargetDataSource().getConnection();
}
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;
}
@Override
public void afterPropertiesSet() {
if (this.targetDataSources == null) {
throw new IllegalArgumentException("Property 'targetDataSources' is required");
}
this.resolvedDataSources = new HashMap<>(this.targetDataSources.size());
this.targetDataSources.forEach((key, value) -> {
Object lookupKey = resolveSpecifiedLookupKey(key);
DataSource dataSource = resolveSpecifiedDataSource(value);
this.resolvedDataSources.put(lookupKey, dataSource);
});
if (this.defaultTargetDataSource != null) {
this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource);
}
}
从上面源码能够看出它承继了AbstractDataSource,而AbstractDataSource是javax.sql.DataSource的完成类,具有getConnection()办法。获取连接的getConnection()办法中,要点是determineTargetDataSource()办法,它的回来值便是你所要用的数据源dataSource的key值,有了这个key值,resolvedDataSource(这是个map,由装备文件中设置好后存入targetDataSources的,通过targetDataSources遍历存入该map)就从中取出对应的DataSource,假如找不到,就用装备默许的数据源。
看完源码,咱们能够知道,只要扩展AbstractRoutingDataSource类,并重写其间的determineCurrentLookupKey()办法回来自己想要的key值,就能够完成指定数据源的切换!
3.3 运转流程
- 咱们自己写的Aop阻拦Mapper
- 判别当时执行的sql所属的命名空间,然后运用命名空间作为key读取系统装备文件获取当时mapper是否需求切换数据源
- 线程再从全局静态的HashMap中取出当时要用的数据源
- 回来对应数据源的connection去做相应的数据库操作
3.4 不切换数据源时的正常装备
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- clickhouse数据源 -->
<bean id="dataSourceClickhousePinpin" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" lazy-init="true">
<property name="url" value="${clickhouse.jdbc.pinpin.url}" />
</bean>
<bean id="singleSessionFactoryPinpin" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- ref直接指向 数据源dataSourceClickhousePinpin -->
<property name="dataSource" ref="dataSourceClickhousePinpin" />
</bean>
</beans>
3.5 进行动态数据源切换时的装备
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- clickhouse数据源 1 -->
<bean id="dataSourceClickhousePinpin" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" lazy-init="true">
<property name="url" value="${clickhouse.jdbc.pinpin.url}" />
</bean>
<!-- clickhouse数据源 2 -->
<bean id="dataSourceClickhouseOtherPinpin" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" lazy-init="true">
<property name="url" value="${clickhouse.jdbc.other.url}" />
</bean>
<!-- 新增装备 封装注册的两个数据源到multiDataSourcePinpin里 -->
<!-- 对应的key分别是 defaultTargetDataSource和targetDataSources-->
<bean id="multiDataSourcePinpin" class="com.zhongyouex.bigdata.common.aop.MultiDataSource">
<!-- 默许运用的数据源-->
<property name="defaultTargetDataSource" ref="dataSourceClickhousePinpin"></property>
<!-- 存储其他数据源,对应源码中的targetDataSources -->
<property name="targetDataSources">
<!-- 该map即为源码中的resolvedDataSources-->
<map>
<!-- dataSourceClickhouseOther 即为要切换的数据源对应的key -->
<entry key="dataSourceClickhouseOther" value-ref="dataSourceClickhouseOtherPinpin"></entry>
</map>
</property>
</bean>
<bean id="singleSessionFactoryPinpin" class="org.mybatis.spring.SqlSessionFactoryBean">
<!-- ref指向封装后的数据源multiDataSourcePinpin -->
<property name="dataSource" ref="multiDataSourcePinpin" />
</bean>
</beans>
中心是AbstractRoutingDataSource,由spring供给,用来动态切换数据源。咱们需求承继它,来进行操作。这儿咱们自定义的com.zhongyouex.bigdata.common.aop.MultiDataSource便是承继了AbstractRoutingDataSource
package com.zhongyouex.bigdata.common.aop;
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
/**
* @author: cuizihua
* @description: 动态数据源
* @date: 2021/9/7 20:24
* @return
*/
public class MultiDataSource extends AbstractRoutingDataSource {
/* 存储数据源的key值,InheritableThreadLocal用来确保父子线程都能拿到值。
*/
private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();
/**
* 设置dataSourceKey的值
*
* @param dataSource
*/
public static void setDataSourceKey(String dataSource) {
dataSourceKey.set(dataSource);
}
/**
* 清除dataSourceKey的值
*/
public static void toDefault() {
dataSourceKey.remove();
}
/**
* 回来当时dataSourceKey的值
*/
@Override
protected Object determineCurrentLookupKey() {
return dataSourceKey.get();
}
}
3.6 AOP代码
package com.zhongyouex.bigdata.common.aop;
import com.zhongyouex.bigdata.common.util.LoadUtil;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.reflect.MethodSignature;
import java.lang.reflect.Method;
/**
* 办法阻拦 粒度在mapper上(对应的sql所属xml)
* @author cuizihua
* @desc 切换数据源
* @create 2021-09-03 16:29
**/
@Slf4j
public class MultiDataSourceInterceptor {
//动态数据源对应的key
private final String otherDataSource = "dataSourceClickhouseOther";
public void beforeOpt(JoinPoint mi) {
//默许运用默许数据源
MultiDataSource.toDefault();
//获取执行该办法的信息
MethodSignature signature = (MethodSignature) mi.getSignature();
Method method = signature.getMethod();
String namespace = method.getDeclaringClass().getName();
//本项目命名空间统一的规范为xxx.xxx.xxxMapper
namespace = namespace.substring(namespace.lastIndexOf(".") + 1);
//这儿在装备文件装备的特点为xxxMapper.ck.switch=1 or 0 1表示切换
String isOtherDataSource = LoadUtil.loadByKey(namespace, "ck.switch");
if ("1".equalsIgnoreCase(isOtherDataSource)) {
MultiDataSource.setDataSourceKey(otherDataSource);
String methodName = method.getName();
}
}
}
3.7 AOP代码逻辑阐明
通过org.aspectj.lang.reflect.MethodSignature能够获取对应执行sql的xml空间名称,拿到sql对应的xml命名空间就能够获取装备文件中装备的特点决议该xml是否开启切换数据源了。
3.8 对应的aop装备
<!--动态数据源-->
<bean id="multiDataSourceInterceptor" class="com.zhongyouex.bigdata.common.aop.MultiDataSourceInterceptor" ></bean>
<!--将自定义阻拦器注入到spring中-->
<aop:config proxy-target-class="true" expose-proxy="true">
<aop:aspect ref="multiDataSourceInterceptor">
<!--切入点,也便是你要监控哪些类下的办法,这儿写的是DAO层的目录,表达式需求确保只扫描dao层-->
<aop:pointcut id="multiDataSourcePointcut" expression="execution(* com.zhongyouex.bigdata.clickhouse..*.*(..)) "/>
<!--在该切入点运用自定义阻拦器-->
<aop:before method="beforeOpt" pointcut-ref="multiDataSourcePointcut" />
</aop:aspect>
</aop:config>
以上便是整个完成过程,希望能帮上有需求的小伙伴
作者:京东物流 崔子华
来历:京东云开发者社区



