日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

2 数据源配置_Spring, MyBatis 多数据源的配置和管理

發布時間:2025/3/15 javascript 50 豆豆
生活随笔 收集整理的這篇文章主要介紹了 2 数据源配置_Spring, MyBatis 多数据源的配置和管理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

作者:digdeep

出處:https://www.cnblogs.com/digdeep/p/4512368.html

熱門推薦

vue+websocket+Springboot實現的即時通信開源項目

springboot炸翔版CMS開源系統

同一個項目有時會涉及到多個數據庫,也就是多數據源。多數據源又可以分為兩種情況:

1)兩個或多個數據庫沒有相關性,各自獨立,其實這種可以作為兩個項目來開發。比如在游戲開發中一個數據庫是平臺數據庫,其它還有平臺下的游戲對應的數據庫;

2)兩個或多個數據庫是master-slave的關系,比如有mysql搭建一個 master-master,其后又帶有多個slave;或者采用MHA搭建的master-slave復制;

目前我所知道的 Spring 多數據源的搭建大概有兩種方式,可以根據多數據源的情況進行選擇。

1. 采用spring配置文件直接配置多個數據源

比如針對兩個數據庫沒有相關性的情況,可以采用直接在spring的配置文件中配置多個數據源,然后分別進行事務的配置,如下所示:

<context:component-scan base-package="net.aazj.service,net.aazj.aop" /> <context:component-scan base-package="net.aazj.aop" /> <context:property-placeholder location="classpath:config/db.properties" /> <bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_username}" /> <property name="password" value="${jdbc_password}" /> <property name="initialSize" value="0" /> <property name="maxActive" value="20" /> <property name="maxIdle" value="20" /> <property name="minIdle" value="0" /> <property name="maxWait" value="60000" /> bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:config/mybatis-config.xml" /> <property name="mapperLocations" value="classpath*:config/mappers/**/*.xml" /> bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> bean> <tx:annotation-driven transaction-manager="transactionManager" /> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="net.aazj.mapper" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> bean> <aop:aspectj-autoproxy/> <bean name="dataSource_2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc_url_2}" /> <property name="username" value="${jdbc_username_2}" /> <property name="password" value="${jdbc_password_2}" /> <property name="initialSize" value="0" /> <property name="maxActive" value="20" /> <property name="maxIdle" value="20" /> <property name="minIdle" value="0" /> <property name="maxWait" value="60000" /> bean> <bean id="sqlSessionFactory_slave" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource_2" /> <property name="configLocation" value="classpath:config/mybatis-config-2.xml" /> <property name="mapperLocations" value="classpath*:config/mappers2/**/*.xml" /> bean> <bean id="transactionManager_2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource_2" /> bean> <tx:annotation-driven transaction-manager="transactionManager_2" /> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="net.aazj.mapper2" /> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_2"/> bean>

如上所示,我們分別配置了兩個 dataSource,兩個sqlSessionFactory,兩個transactionManager,以及關鍵的地方在于MapperScannerConfigurer 的配置——使用sqlSessionFactoryBeanName屬性,注入不同的sqlSessionFactory的名稱,這樣的話,就為不同的數據庫對應的 mapper 接口注入了對應的 sqlSessionFactory。

需要注意的是,多個數據庫的這種配置是不支持分布式事務的,也就是同一個事務中,不能操作多個數據庫。這種配置方式的優點是很簡單,但是卻不靈活。對于master-slave類型的多數據源配置而言不太適應,master-slave性的多數據源的配置,需要特別靈活,需要根據業務的類型進行細致的配置。比如對于一些耗時特別大的select語句,我們希望放到slave上執行,而對于update,delete等操作肯定是只能在master上執行的,另外對于一些實時性要求很高的select語句,我們也可能需要放到master上執行——比如一個場景是我去商城購買一件兵器,購買操作的很定是master,同時購買完成之后,需要重新查詢出我所擁有的兵器和金幣,那么這個查詢可能也需要防止master上執行,而不能放在slave上去執行,因為slave上可能存在延時,我們可不希望玩家發現購買成功之后,在背包中卻找不到兵器的情況出現。

所以對于master-slave類型的多數據源的配置,需要根據業務來進行靈活的配置,哪些select可以放到slave上,哪些select不能放到slave上。所以上面的那種所數據源的配置就不太適應了。

2. 基于 AbstractRoutingDataSource 和 AOP 的多數據源的配置

基本原理是,我們自己定義一個DataSource類ThreadLocalRountingDataSource,來繼承AbstractRoutingDataSource,然后在配置文件中向ThreadLocalRountingDataSource注入 master 和 slave 的數據源,然后通過 AOP 來靈活配置,在哪些地方選擇? master 數據源,在哪些地方需要選擇 slave數據源。下面看代碼實現:

1)先定義一個enum來表示不同的數據源:

package net.aazj.enums;/** * 數據源的類別:master/slave */public enum DataSources { MASTER, SLAVE}

2)通過 TheadLocal 來保存每個線程選擇哪個數據源的標志(key):

package net.aazj.util;import net.aazj.enums.DataSources;public class DataSourceTypeManager { private static final ThreadLocal dataSourceTypes = new ThreadLocal(){ @Override protected DataSources initialValue(){ return DataSources.MASTER; } }; public static DataSources get(){ return dataSourceTypes.get(); } public static void set(DataSources dataSourceType){ dataSourceTypes.set(dataSourceType); } public static void reset(){ dataSourceTypes.set(DataSources.MASTER0); }}

3)定義 ThreadLocalRountingDataSource,繼承AbstractRoutingDataSource:

package net.aazj.util;import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;public class ThreadLocalRountingDataSource extends AbstractRoutingDataSource { @Override protected Object determineCurrentLookupKey() { return DataSourceTypeManager.get(); }}

4)在配置文件中向 ThreadLocalRountingDataSource 注入 master 和 slave 的數據源:

<context:component-scan base-package="net.aazj.service,net.aazj.aop" /> <context:component-scan base-package="net.aazj.aop" /> <context:property-placeholder location="classpath:config/db.properties" /> <bean name="dataSourceMaster" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc_url}" /> <property name="username" value="${jdbc_username}" /> <property name="password" value="${jdbc_password}" /> <property name="initialSize" value="0" /> <property name="maxActive" value="20" /> <property name="maxIdle" value="20" /> <property name="minIdle" value="0" /> <property name="maxWait" value="60000" /> bean> <bean name="dataSourceSlave" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close"> <property name="url" value="${jdbc_url_slave}" /> <property name="username" value="${jdbc_username_slave}" /> <property name="password" value="${jdbc_password_slave}" /> <property name="initialSize" value="0" /> <property name="maxActive" value="20" /> <property name="maxIdle" value="20" /> <property name="minIdle" value="0" /> <property name="maxWait" value="60000" /> bean> <bean id="dataSource" class="net.aazj.util.ThreadLocalRountingDataSource"> <property name="defaultTargetDataSource" ref="dataSourceMaster" /> <property name="targetDataSources"> <map key-type="net.aazj.enums.DataSources"> <entry key="MASTER" value-ref="dataSourceMaster"/> <entry key="SLAVE" value-ref="dataSourceSlave"/> map> property> bean> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource" /> <property name="configLocation" value="classpath:config/mybatis-config.xml" /> <property name="mapperLocations" value="classpath*:config/mappers/**/*.xml" /> bean> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource" /> bean> <tx:annotation-driven transaction-manager="transactionManager" /> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="net.aazj.mapper" /> bean>

上面spring的配置文件中,我們針對master數據庫和slave數據庫分別定義了dataSourceMaster和dataSourceSlave兩個dataSource,然后注入到>?中,這樣我們的dataSource就可以來根據 key 的不同來選擇dataSourceMaster和 dataSourceSlave了。

5)使用Spring AOP 來指定 dataSource 的 key ,從而dataSource會根據key選擇 dataSourceMaster 和 dataSourceSlave:

package net.aazj.aop;import net.aazj.enums.DataSources;import net.aazj.util.DataSourceTypeManager;import org.aspectj.lang.JoinPoint;import org.aspectj.lang.annotation.Aspect;import org.aspectj.lang.annotation.Before;import org.aspectj.lang.annotation.Pointcut;import org.springframework.stereotype.Component;@Aspect // for aop@Component // for auto scan@Order(0)? // execute before @Transactionalpublic class DataSourceInterceptor { @Pointcut("execution(public * net.aazj.service..*.getUser(..))") public void dataSourceSlave(){}; @Before("dataSourceSlave()") public void before(JoinPoint jp) { DataSourceTypeManager.set(DataSources.SLAVE); } // ... ...}

這里我們定義了一個?Aspect?類,我們使用?@Before?來在符合?@Pointcut("execution(public * net.aazj.service..*.getUser(..))")?中的方法被調用之前,調用?DataSourceTypeManager.set(DataSources.SLAVE)?設置了 key 的類型為?DataSources.SLAVE,所以 dataSource 會根據key=DataSources.SLAVE?選擇 dataSourceSlave 這個dataSource。所以該方法對于的sql語句會在slave數據庫上執行(經網友老劉1987提醒,這里存在多個Aspect之間的一個執行順序的問題,必須保證切換數據源的Aspect必須在@Transactional這個Aspect之前執行,所以這里使用了@Order(0)來保證切換數據源先于@Transactional執行)。

我們可以不斷的擴充?DataSourceInterceptor??這個?Aspect,在中進行各種各樣的定義,來為某個service的某個方法指定合適的數據源對應的dataSource。

這樣我們就可以使用 Spring AOP 的強大功能來,十分靈活進行配置了。

6)AbstractRoutingDataSource原理剖析

ThreadLocalRountingDataSource繼承了AbstractRoutingDataSource,實現其抽象方法protected abstract Object determineCurrentLookupKey(); 從而實現對不同數據源的路由功能。我們從源碼入手分析下其中原理:

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBeanAbstractRoutingDataSource 實現了 InitializingBean 那么spring在初始化該bean時,會調用InitializingBean的接口void afterPropertiesSet() throws Exception; 我們看下AbstractRoutingDataSource是如何實現這個接口的:@Override public void afterPropertiesSet() { if (this.targetDataSources == null) { throw new IllegalArgumentException("Property 'targetDataSources' is required"); } this.resolvedDataSources = new HashMap(this.targetDataSources.size()); for (Map.Entry entry : this.targetDataSources.entrySet()) { Object lookupKey = resolveSpecifiedLookupKey(entry.getKey()); DataSource dataSource = resolveSpecifiedDataSource(entry.getValue()); this.resolvedDataSources.put(lookupKey, dataSource); } if (this.defaultTargetDataSource != null) { this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource); } }targetDataSources 是我們在xml配置文件中注入的 dataSourceMaster 和 dataSourceSlave. afterPropertiesSet方法就是使用注入的dataSourceMaster 和 dataSourceSlave來構造一個HashMap——resolvedDataSources。方便后面根據 key 從該map 中取得對應的dataSource
我們在看下 AbstractDataSource 接口中的 Connection getConnection() throws SQLException; 是如何實現的:@Override public Connection getConnection() throws SQLException { return determineTargetDataSource().getConnection(); }

關鍵在于?determineTargetDataSource(),根據方法名就可以看出,應該此處就決定了使用哪個 dataSource :

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; }Object lookupKey = determineCurrentLookupKey(); 該方法是我們實現的,在其中獲取ThreadLocal中保存的 key 值。獲得了key之后,
在從afterPropertiesSet()中初始化好了的resolvedDataSources這個map中獲得key對應的dataSource。而ThreadLocal中保存的 key 值
是通過AOP的方式在調用service中相關方法之前設置好的。OK,到此搞定!

7)擴展 ThreadLocalRountingDataSource

上面我們只是實現了 master-slave 數據源的選擇。如果有多臺 master 或者有多臺 slave。多臺master組成一個HA,要實現當其中一臺master掛了是,自動切換到另一臺master,這個功能可以使用LVS/Keepalived來實現,也可以通過進一步擴展ThreadLocalRountingDataSource來實現,可以另外加一個線程專門來每個一秒來測試mysql是否正常來實現。同樣對于多臺slave之間要實現負載均衡,同時當一臺slave掛了時,要實現將其從負載均衡中去除掉,這個功能既可以使用LVS/Keepalived來實現,同樣也可以通過近一步擴展ThreadLocalRountingDataSource來實現。

3. 總結

從本文中我們可以體會到AOP的強大和靈活。

本文使用的是mybatis,其實使用Hibernate也應該是相似的配置。

總結

以上是生活随笔為你收集整理的2 数据源配置_Spring, MyBatis 多数据源的配置和管理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。