Spring實現(xiàn)動態(tài)數(shù)據(jù)源切換的方法總結(jié)
1 目標
不在現(xiàn)有查詢代碼邏輯上做任何改動,實現(xiàn)dao維度的數(shù)據(jù)源切換(即表維度)
2 使用場景
節(jié)約bdp的集群資源。接入新的寬表時,通常uat驗證后就會停止集群釋放資源,在對應(yīng)的查詢服務(wù)器uat環(huán)境時需要查詢的是生產(chǎn)庫的表數(shù)據(jù)(uat庫表因為bdp實時任務(wù)停止,沒有數(shù)據(jù)落入),只進行服務(wù)器配置文件的改動而無需進行代碼的修改變更,即可按需切換查詢的數(shù)據(jù)源。
2.1 實時任務(wù)對應(yīng)的集群資源
2.2 實時任務(wù)產(chǎn)生的數(shù)據(jù)進行存儲的兩套環(huán)境
2.3 數(shù)據(jù)使用系統(tǒng)的兩套環(huán)境(查詢展示數(shù)據(jù))
即需要在zhongyouex-bigdata-uat中查詢生產(chǎn)庫的數(shù)據(jù)。
3 實現(xiàn)過程
3.1 實現(xiàn)重點
- org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
spring提供的這個類是本次實現(xiàn)的核心,能夠讓我們實現(xiàn)運行時多數(shù)據(jù)源的動態(tài)切換,但是數(shù)據(jù)源是需要事先配置好的,無法動態(tài)的增加數(shù)據(jù)源。 - Spring提供的Aop攔截執(zhí)行的mapper,進行切換判斷并進行切換。
注:另外還有一個就是ThreadLocal類,用于保存每個線程正在使用的數(shù)據(jù)源。
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的實現(xiàn)類,擁有g(shù)etConnection()方法。獲取連接的getConnection()方法中,重點是determineTargetDataSource()方法,它的返回值就是你所要用的數(shù)據(jù)源dataSource的key值,有了這個key值,resolvedDataSource(這是個map,由配置文件中設(shè)置好后存入targetDataSources的,通過targetDataSources遍歷存入該map)就從中取出對應(yīng)的DataSource,如果找不到,就用配置默認的數(shù)據(jù)源。
看完源碼,我們可以知道,只要擴展AbstractRoutingDataSource類,并重寫其中的determineCurrentLookupKey()方法返回自己想要的key值,就可以實現(xiàn)指定數(shù)據(jù)源的切換!
3.3 運行流程
- 我們自己寫的Aop攔截Mapper
- 判斷當前執(zhí)行的sql所屬的命名空間,然后使用命名空間作為key讀取系統(tǒng)配置文件獲取當前mapper是否需要切換數(shù)據(jù)源
- 線程再從全局靜態(tài)的HashMap中取出當前要用的數(shù)據(jù)源
- 返回對應(yīng)數(shù)據(jù)源的connection去做相應(yīng)的數(shù)據(jù)庫操作
3.4 不切換數(shù)據(jù)源時的正常配置
<?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數(shù)據(jù)源 --> <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直接指向 數(shù)據(jù)源dataSourceClickhousePinpin --> <property name="dataSource" ref="dataSourceClickhousePinpin" /> </bean> </beans>
3.5 進行動態(tài)數(shù)據(jù)源切換時的配置
<?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數(shù)據(jù)源 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數(shù)據(jù)源 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> <!-- 新增配置 封裝注冊的兩個數(shù)據(jù)源到multiDataSourcePinpin里 --> <!-- 對應(yīng)的key分別是 defaultTargetDataSource和targetDataSources--> <bean id="multiDataSourcePinpin" class="com.zhongyouex.bigdata.common.aop.MultiDataSource"> <!-- 默認使用的數(shù)據(jù)源--> <property name="defaultTargetDataSource" ref="dataSourceClickhousePinpin"></property> <!-- 存儲其他數(shù)據(jù)源,對應(yīng)源碼中的targetDataSources --> <property name="targetDataSources"> <!-- 該map即為源碼中的resolvedDataSources--> <map> <!-- dataSourceClickhouseOther 即為要切換的數(shù)據(jù)源對應(yīng)的key --> <entry key="dataSourceClickhouseOther" value-ref="dataSourceClickhouseOtherPinpin"></entry> </map> </property> </bean> <bean id="singleSessionFactoryPinpin" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- ref指向封裝后的數(shù)據(jù)源multiDataSourcePinpin --> <property name="dataSource" ref="multiDataSourcePinpin" /> </bean> </beans>
核心是AbstractRoutingDataSource,由spring提供,用來動態(tài)切換數(shù)據(jù)源。我們需要繼承它,來進行操作。這里我們自定義的com.zhongyouex.bigdata.common.aop.MultiDataSource就是繼承了AbstractRoutingDataSource
package com.zhongyouex.bigdata.common.aop; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /** * @author: cuizihua * @description: 動態(tài)數(shù)據(jù)源 * @date: 2021/9/7 20:24 * @return */ public class MultiDataSource extends AbstractRoutingDataSource { /* 存儲數(shù)據(jù)源的key值,InheritableThreadLocal用來保證父子線程都能拿到值。 */ private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>(); /** * 設(shè)置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上(對應(yīng)的sql所屬xml) * @author cuizihua * @desc 切換數(shù)據(jù)源 * @create 2021-09-03 16:29 **/ @Slf4j public class MultiDataSourceInterceptor { //動態(tài)數(shù)據(jù)源對應(yīng)的key private final String otherDataSource = "dataSourceClickhouseOther"; public void beforeOpt(JoinPoint mi) { //默認使用默認數(shù)據(jù)源 MultiDataSource.toDefault(); //獲取執(zhí)行該方法的信息 MethodSignature signature = (MethodSignature) mi.getSignature(); Method method = signature.getMethod(); String namespace = method.getDeclaringClass().getName(); //本項目命名空間統(tǒng)一的規(guī)范為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可以獲取對應(yīng)執(zhí)行sql的xml空間名稱,拿到sql對應(yīng)的xml命名空間就可以獲取配置文件中配置的屬性決定該xml是否開啟切換數(shù)據(jù)源了。
3.8 對應(yīng)的aop配置
<!--動態(tài)數(shù)據(jù)源--> <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"> <!--切入點,也就是你要監(jiān)控哪些類下的方法,這里寫的是DAO層的目錄,表達式需要保證只掃描dao層--> <aop:pointcut id="multiDataSourcePointcut" expression="execution(* com.zhongyouex.bigdata.clickhouse..*.*(..)) "/> <!--在該切入點使用自定義攔截器--> <aop:before method="beforeOpt" pointcut-ref="multiDataSourcePointcut" /> </aop:aspect> </aop:config>
到此這篇關(guān)于Spring實現(xiàn)動態(tài)數(shù)據(jù)源切換的方法總結(jié)的文章就介紹到這了,更多相關(guān)Spring動態(tài)數(shù)據(jù)源切換內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java反射實現(xiàn)javabean轉(zhuǎn)json實例代碼
基于java反射機制實現(xiàn)javabean轉(zhuǎn)json字符串實例,大家參考使用吧2013-12-12JAVA 16位ID生成工具類含16位不重復的隨機數(shù)數(shù)字+大小寫
這篇文章主要介紹了JAVA 16位ID生成工具類含16位不重復的隨機數(shù)數(shù)字+大小寫,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧2020-02-02Java Iterator迭代器與foreach循環(huán)代碼解析
這篇文章主要介紹了Java-Iterator迭代器與foreach循環(huán),主要包括Iterator迭代器接口的操作方法和foreach 循環(huán)語法解析,需要的朋友可以參考下2022-04-04