一種實現Spring動態數據源切換的方法

来源:https://www.cnblogs.com/Jcloud/archive/2023/06/19/17491451.html
-Advertisement-
Play Games

## 1 目標 不在現有查詢代碼邏輯上做任何改動,實現dao維度的數據源切換(即表維度) ## 2 使用場景 節約bdp的集群資源。接入新的寬表時,通常uat驗證後就會停止集群釋放資源,在對應的查詢伺服器uat環境時需要查詢的是生產庫的表數據(uat庫表因為bdp實時任務停止,沒有數據落入),只進行 ...


1 目標

不在現有查詢代碼邏輯上做任何改動,實現dao維度的數據源切換(即表維度)

2 使用場景

節約bdp的集群資源。接入新的寬表時,通常uat驗證後就會停止集群釋放資源,在對應的查詢伺服器uat環境時需要查詢的是生產庫的表數據(uat庫表因為bdp實時任務停止,沒有數據落入),只進行伺服器配置文件的改動而無需進行代碼的修改變更,即可按需切換查詢的數據源。

2.1 實時任務對應的集群資源

2.2 實時任務產生的數據進行存儲的兩套環境

2.3 數據使用系統的兩套環境(查詢展示數據)

即需要在zhongyouex-bigdata-uat中查詢生產庫的數據。

3 實現過程

3.1 實現重點

  1. org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
    spring提供的這個類是本次實現的核心,能夠讓我們實現運行時多數據源的動態切換,但是數據源是需要事先配置好的,無法動態的增加數據源。
  2. 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 運行流程

  1. 我們自己寫的Aop攔截Mapper
  2. 判斷當前執行的sql所屬的命名空間,然後使用命名空間作為key讀取系統配置文件獲取當前mapper是否需要切換數據源
  3. 線程再從全局靜態的HashMap中取出當前要用的數據源
  4. 返回對應數據源的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
 **/
@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" 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>

以上就是整個實現過程,希望能幫上有需要的小伙伴

作者:京東物流 崔子華

來源:京東雲開發者社區


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • typora-copy-images-to: upload # 頁面預覽 ## 訂單詳情 ![image-20230227071834134](https://s2.loli.net/2023/06/19/8rXsPWOn3MdlRNx.png) ![image-20230227071900964] ...
  • 緩衝池是主存儲器中的一個區域,在訪問 table 和索引數據時 InnoDB 會對其進行緩存。緩衝池允許直接從記憶體中訪問頻繁使用的數據,從而加快處理速度。在專用伺服器上,通常將高達 80% 的物理記憶體分配給緩衝池。 ...
  • > 在[上一章](https://www.yuque.com/docs/share/adb5b1e4-f3c6-46fd-ba4b-4dabce9b4f2a?# 《現代C++學習指南-類型系統》)我們探討了C++的類型系統,並提出了從低到高,又從高到低的學習思路,本文就是一篇從高到低的學習指南,希望 ...
  • ### 實踐環境 Python3.6 ### 介紹 `multiprocessing`是一個支持使用類似於線程模塊的API派生進程的包。該包同時提供本地和遠程併發,通過使用子進程而不是線程,有效地避開了全局解釋器鎖。因此,`multiprocessing`模塊允許程式員充分利用給定機器上的多個處理器 ...
  • # Manacher演算法是什麼 ~~Manacher演算法就是馬拉車。~~ Manacher演算法就是用於解決迴文子串的個數的。 # 問題引入 [P3805:【模板】manacher 演算法](https://www.luogu.com.cn/problem/P3805) # 題目大意 給出一個只由小寫英 ...
  • ## 函數和關鍵字 本篇主要介紹:`自定義函數`、`巨集函數`、`字元串處理函數`和`關鍵字`。 ### 自定義函數 #### 基本用法 實現一個 add() 函數。請看示例: ```c #include // 自定義函數,用於計算兩個整數的和 int add(int a, int b) { // a ...
  • # Rust語言 - 介面設計的建議之顯而易見(Obvious) - [Rust API 指南 GitHub](https://github.com/rust-lang/api-guidelines): - [Rust API 指南 中文](https://rust-chinese-translat ...
  • # 前言 此文章是Java後端接入微信登錄功能,由於項目需要,捨棄瞭解密用戶信息的`session_key`,只保留`openid`用於檢索用戶信息 後端框架:spring boot 小程式框架:uniapp # 流程概括 - 官方流程:通過自定義登錄態與openid,session_key關聯,之 ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...