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" destroy-method="close" lazy-init="true">
<property name="url" value="https://www.cnblogs.com/Jcloud/archive/2023/06/19/${clickhouse.jdbc.pinpin.url}" />
</bean>
<bean id="singleSessionFactoryPinpin" >
<!-- 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" destroy-method="close" lazy-init="true">
<property name="url" value="https://www.cnblogs.com/Jcloud/archive/2023/06/19/${clickhouse.jdbc.pinpin.url}" />
</bean>
<!-- clickhouse資料源 2 -->
<bean id="dataSourceClickhouseOtherPinpin" destroy-method="close" lazy-init="true">
<property name="url" value="https://www.cnblogs.com/Jcloud/archive/2023/06/19/${clickhouse.jdbc.other.url}" />
</bean>
<!-- 新增配置 封裝注冊的兩個資料源到multiDataSourcePinpin里 -->
<!-- 對應的key分別是 defaultTargetDataSource和targetDataSources-->
<bean id="multiDataSourcePinpin" >
<!-- 默認使用的資料源-->
<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" >
<!-- 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" ></bean>
<!--將自定義攔截器注入到spring中-->
<aop:config proxy-target- 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>
以上就是整個實作程序,希望能幫上有需要的小伙伴
作者:京東物流 崔子華
來源:京東云開發者社區
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/555550.html
標籤:其他
上一篇:java~搞懂Comparable介面的compareTo方法
下一篇:返回列表