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