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 determ.NETargetDataSource().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 **/@Slf4jpublic 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>
以上就是整個實現過程,希望能幫上有需要的小伙伴
作者:京東物流 崔子華
來源:京東云開發者社區