關於AbstractRoutingDataSource動態切換數據源是我在研究某開源項目時候才注意到的,大概就看懂了Spring AOP切面這里,根據作者的意思是通過繼承這個抽象類可以實現數據源的動態切換,也就是Controller調用Service的時候會切換數據源。最終研究了一天也沒發現什么結果,第二天便嘗試查看源碼和查看相關資料,試圖揭開這個疑惑
- 首先貼上源代碼如下:
package com.zdd.data.aspect; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.apache.commons.lang3.StringUtils; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /** * 獲取數據源 * */ public class ChooseDataSource extends AbstractRoutingDataSource { public static Map<String, List<String>> METHODTYPE = new HashMap<String, List<String>>(); // 獲取數據源名稱 protected Object determineCurrentLookupKey() { System.out.println(HandleDataSource.getDataSource()); return HandleDataSource.getDataSource(); } // 設置方法名前綴對應的數據源 public void setMethodType(Map<String, String> map) { for (String key : map.keySet()) { List<String> v = new ArrayList<String>(); String[] types = map.get(key).split(","); for (String type : types) { if (StringUtils.isNotBlank(type)) { v.add(type); } } METHODTYPE.put(key, v); } } }
package com.zdd.data.aspect; import org.apache.commons.lang3.StringUtils; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.annotation.After; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Before; import org.aspectj.lang.annotation.Pointcut; import org.slf4j.LoggerFactory; import org.springframework.context.annotation.EnableAspectJAutoProxy; import org.springframework.core.annotation.Order; import org.springframework.stereotype.Component; @Aspect @EnableAspectJAutoProxy(proxyTargetClass = true) @Component public class DataSourceAspect { private final org.slf4j.Logger logger = LoggerFactory.getLogger(DataSourceAspect.class); @Pointcut("execution(* com.zdd.data.service.impl.*.*(..))") public void aspect() {} /** * 配置前置通知,使用在方法aspect()上注冊的切入點 */ @Before("aspect()") public void before(JoinPoint point) { String className = point.getTarget().getClass().getName(); String method = point.getSignature().getName(); logger.info(className + "." + method + "(" + StringUtils.join(point.getArgs(), ",") + ")"); try { L: for (String key : ChooseDataSource.METHODTYPE.keySet()) { for (String type : ChooseDataSource.METHODTYPE.get(key)) { if (method.startsWith(type)) { System.out.println(key); HandleDataSource.putDataSource(key); break L; } } } } catch (Exception e) { logger.error(e.toString()); HandleDataSource.putDataSource("write"); } } @After("aspect()") public void after(JoinPoint point) { HandleDataSource.clear(); } }
package com.zdd.data.aspect; /** */ public class HandleDataSource { // 數據源名稱線程池 private static final ThreadLocal<String> holder = new ThreadLocal<String>(); public static void putDataSource(String datasource) { holder.set(datasource); } public static String getDataSource() { return holder.get(); } public static void clear() { holder.remove(); } }
#以上是核心代碼,我疑惑的地方在於AbstractRoutingDataSource這里,剛開始去看的時候不明白為什么繼承這個就能實現數據源切換,最后進入AbstractRoutingDataSource.class去查看究竟,發現繼承了一個AbstractDataSource的抽象類,這里我突然有點明白AbstractRoutingDataSource的意思,一個路由數據源,一般用到Routing的地方都可以進行動態配置。
-
大致翻譯一下源碼作者對這個類的說明:調用一個基於各種目標數據源。
繼續往下看又看到幾個重要信息
上圖發現2個方法分別是setTargetDataSources(注入目標數據源) 和setDefaultTargetDataSource(注入默認的數據源);然后想到是否可以通過Spring 配置來注入這2個方法,帶着這個思緒繼續接着繼續往下看;然后又看到比較重要的信息如下:
上圖發現有getConnection方法,jdbc獲取連接的方法,而當我點進determineTargetDataSource方法時得到了一個確切答案,代碼如下:
上圖大致的意思是會調用determineCurrentLookupKey這個抽象方法獲取數據源,如果是null則獲取默認的數據源,反之則是獲取我們注入的數據源, 這樣就實現了數據源的動態切換, 這時候我通過斷點啟動也驗證了這一個流程確實如此。
分析了一個動態切換流程后,那么問題來了,如何讓我們的代碼實現這個功能呢?其實就是重寫AbstractRoutingDataSource方法后再通過aop動態切換數據源,那么如何知道切換條件是什么?這時候可以根據Service層的方法來確定,一般我們Service都是會以find,add,delete,update 開頭; 例如發現是find,get開頭則走讀庫,delete,update則走寫庫。
#分析完后把spring配置文件貼上來
<bean id="dataSource" class="com.zdd.data.aspect.ChooseDataSource" lazy-init="true"> <description>數據源</description> <property name="targetDataSources"> <map key-type="java.lang.String" value-type="javax.sql.DataSource"> <!-- write --> <entry key="write" value-ref="writedataSource" /> <!-- read --> <entry key="read" value-ref="readdataSource" /> </map> </property> <!-- 設置默認的目標數據源為讀 --> <property name="defaultTargetDataSource" ref="readdataSource" /> <property name="methodType"> <map key-type="java.lang.String"> <!-- read --> <entry key="read" value=",get,,find,select,count,list,query," /> <!-- write --> <entry key="write" value=",add,insert,create,update,delete,remove," /> </map> </property> </bean>
#上面的配置大概流程是先指定2個數據源注入targetDataSources,然后配置一組數組用來存放判斷走讀庫還是寫庫的條件,如果是read那么肯定是把read注入的數據源拿出來,如果是write則把write的數據源取出來。
更新時間-----------------------------------------------2018年5月16日 13:54:27------------------------------------------------------------
#但是這時候會有一個奇怪的現象就是每次調用的時候會走chooseDataSource再走AOP,經查詢發現是事務問題導致,因為它先走了事務切面,然后事務還沒結束的時候如果再去切換數據源的話是不成立的。方法解決很簡單,設置一下切換數據源的AOP的優先級,確保在事務執行之前就已經切換數據源,如下圖: