小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

Spring, MyBatis 多數(shù)據(jù)源的配置和管理

 KyunraWang 2018-01-19

同一個(gè)項(xiàng)目有時(shí)會(huì)涉及到多個(gè)數(shù)據(jù)庫(kù),也就是多數(shù)據(jù)源。多數(shù)據(jù)源又可以分為兩種情況:

1)兩個(gè)或多個(gè)數(shù)據(jù)庫(kù)沒(méi)有相關(guān)性,各自獨(dú)立,其實(shí)這種可以作為兩個(gè)項(xiàng)目來(lái)開發(fā)。比如在游戲開發(fā)中一個(gè)數(shù)據(jù)庫(kù)是平臺(tái)數(shù)據(jù)庫(kù),其它還有平臺(tái)下的游戲?qū)?yīng)的數(shù)據(jù)庫(kù);

2)兩個(gè)或多個(gè)數(shù)據(jù)庫(kù)是master-slave的關(guān)系,比如有mysql搭建一個(gè) master-master,其后又帶有多個(gè)slave;或者采用MHA搭建的master-slave復(fù)制;

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

1. 采用spring配置文件直接配置多個(gè)數(shù)據(jù)源

比如針對(duì)兩個(gè)數(shù)據(jù)庫(kù)沒(méi)有相關(guān)性的情況,可以采用直接在spring的配置文件中配置多個(gè)數(shù)據(jù)源,然后分別進(jìn)行事務(wù)的配置,如下所示:

    <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" />

    <!-- 配置數(shù)據(jù)源 -->
    <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" />
        <!-- 連接池最大使用連接數(shù)量 -->
        <property name="maxActive" value="20" />
        <!-- 連接池最大空閑 -->
        <property name="maxIdle" value="20" />
        <!-- 連接池最小空閑 -->
        <property name="minIdle" value="0" />
        <!-- 獲取連接最大等待時(shí)間 -->
        <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>
    
    <!-- Transaction manager for a single JDBC DataSource -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>
    
    <!-- 使用annotation定義事務(wù) -->
    <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>
<!-- Enables the use of the @AspectJ style of Spring AOP --> <aop:aspectj-autoproxy/> <!-- ===============第二個(gè)數(shù)據(jù)源的配置=============== --> <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" /> <!-- 連接池最大使用連接數(shù)量 --> <property name="maxActive" value="20" /> <!-- 連接池最大空閑 --> <property name="maxIdle" value="20" /> <!-- 連接池最小空閑 --> <property name="minIdle" value="0" /> <!-- 獲取連接最大等待時(shí)間 --> <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> <!-- Transaction manager for a single JDBC DataSource --> <bean id="transactionManager_2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource_2" /> </bean> <!-- 使用annotation定義事務(wù) --> <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>

如上所示,我們分別配置了兩個(gè) dataSource,兩個(gè)sqlSessionFactory,兩個(gè)transactionManager,以及關(guān)鍵的地方在于MapperScannerConfigurer 的配置——使用sqlSessionFactoryBeanName屬性,注入不同的sqlSessionFactory的名稱,這樣的話,就為不同的數(shù)據(jù)庫(kù)對(duì)應(yīng)的 mapper 接口注入了對(duì)應(yīng)的 sqlSessionFactory。

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

所以對(duì)于master-slave類型的多數(shù)據(jù)源的配置,需要根據(jù)業(yè)務(wù)來(lái)進(jìn)行靈活的配置,哪些select可以放到slave上,哪些select不能放到slave上。所以上面的那種所數(shù)據(jù)源的配置就不太適應(yīng)了。

2. 基于 AbstractRoutingDataSource 和 AOP 的多數(shù)據(jù)源的配置

基本原理是,我們自己定義一個(gè)DataSource類ThreadLocalRountingDataSource,來(lái)繼承AbstractRoutingDataSource,然后在配置文件中向ThreadLocalRountingDataSource注入 master 和 slave 的數(shù)據(jù)源,然后通過(guò) AOP 來(lái)靈活配置,在哪些地方選擇  master 數(shù)據(jù)源,在哪些地方需要選擇 slave數(shù)據(jù)源。下面看代碼實(shí)現(xiàn):

1)先定義一個(gè)enum來(lái)表示不同的數(shù)據(jù)源:

package net.aazj.enums;

/**
 * 數(shù)據(jù)源的類別:master/slave
 */
public enum DataSources {
    MASTER, SLAVE
}

2)通過(guò) TheadLocal 來(lái)保存每個(gè)線程選擇哪個(gè)數(shù)據(jù)源的標(biāo)志(key):

package net.aazj.util;

import net.aazj.enums.DataSources;

public class DataSourceTypeManager {
    private static final ThreadLocal<DataSources> dataSourceTypes = new ThreadLocal<DataSources>(){
        @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 的數(shù)據(jù)源:

    <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" />    
    <!-- 配置數(shù)據(jù)源Master -->
    <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" />
        <!-- 連接池最大使用連接數(shù)量 -->
        <property name="maxActive" value="20" />
        <!-- 連接池最大空閑 -->
        <property name="maxIdle" value="20" />
        <!-- 連接池最小空閑 -->
        <property name="minIdle" value="0" />
        <!-- 獲取連接最大等待時(shí)間 -->
        <property name="maxWait" value="60000" />
    </bean>    
    <!-- 配置數(shù)據(jù)源Slave -->
    <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" />
        <!-- 連接池最大使用連接數(shù)量 -->
        <property name="maxActive" value="20" />
        <!-- 連接池最大空閑 -->
        <property name="maxIdle" value="20" />
        <!-- 連接池最小空閑 -->
        <property name="minIdle" value="0" />
        <!-- 獲取連接最大等待時(shí)間 -->
        <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"/>
                <!-- 這里還可以加多個(gè)dataSource -->
            </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>    
    <!-- Transaction manager for a single JDBC DataSource -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>    
    <!-- 使用annotation定義事務(wù) -->
    <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>        

上面spring的配置文件中,我們針對(duì)master數(shù)據(jù)庫(kù)和slave數(shù)據(jù)庫(kù)分別定義了dataSourceMaster和dataSourceSlave兩個(gè)dataSource,然后注入到<bean id="dataSource" class="net.aazj.util.ThreadLocalRountingDataSource"> 中,這樣我們的dataSource就可以來(lái)根據(jù) key 的不同來(lái)選擇dataSourceMaster和 dataSourceSlave了。

5)使用Spring AOP 來(lái)指定 dataSource 的 key ,從而dataSource會(huì)根據(jù)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 @Transactional
public class DataSourceInterceptor { @Pointcut("execution(public * net.aazj.service..*.getUser(..))") public void dataSourceSlave(){}; @Before("dataSourceSlave()") public void before(JoinPoint jp) { DataSourceTypeManager.set(DataSources.SLAVE); }
// ... ... }

這里我們定義了一個(gè) Aspect 類,我們使用 @Before 來(lái)在符合 @Pointcut("execution(public * net.aazj.service..*.getUser(..))") 中的方法被調(diào)用之前,調(diào)用 DataSourceTypeManager.set(DataSources.SLAVE) 設(shè)置了 key 的類型為 DataSources.SLAVE,所以 dataSource 會(huì)根據(jù)key=DataSources.SLAVE 選擇 dataSourceSlave 這個(gè)dataSource。所以該方法對(duì)于的sql語(yǔ)句會(huì)在slave數(shù)據(jù)庫(kù)上執(zhí)行(經(jīng)網(wǎng)友老劉1987提醒,這里存在多個(gè)Aspect之間的一個(gè)執(zhí)行順序的問(wèn)題,必須保證切換數(shù)據(jù)源的Aspect必須在@Transactional這個(gè)Aspect之前執(zhí)行,所以這里使用了@Order(0)來(lái)保證切換數(shù)據(jù)源先于@Transactional執(zhí)行)。

我們可以不斷的擴(kuò)充 DataSourceInterceptor  這個(gè) Aspect,在中進(jìn)行各種各樣的定義,來(lái)為某個(gè)service的某個(gè)方法指定合適的數(shù)據(jù)源對(duì)應(yīng)的dataSource。

這樣我們就可以使用 Spring AOP 的強(qiáng)大功能來(lái),十分靈活進(jìn)行配置了。

6)AbstractRoutingDataSource原理剖析

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

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean
AbstractRoutingDataSource 實(shí)現(xiàn)了 InitializingBean 那么spring在初始化該bean時(shí),會(huì)調(diào)用InitializingBean的接口
void afterPropertiesSet() throws Exception; 我們看下AbstractRoutingDataSource是如何實(shí)現(xiàn)這個(gè)接口的:
    @Override
    public void afterPropertiesSet() {
        if (this.targetDataSources == null) {
            throw new IllegalArgumentException("Property 'targetDataSources' is required");
        }
        this.resolvedDataSources = new HashMap<Object, DataSource>(this.targetDataSources.size());
        for (Map.Entry<Object, Object> 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 是我們?cè)趚ml配置文件中注入的 dataSourceMaster 和 dataSourceSlave. afterPropertiesSet方法就是使用注入的
dataSourceMaster 和 dataSourceSlave來(lái)構(gòu)造一個(gè)HashMap——resolvedDataSources。方便后面根據(jù) key 從該map 中取得對(duì)應(yīng)的dataSource。
我們?cè)诳聪?AbstractDataSource 接口中的 Connection getConnection() throws SQLException; 是如何實(shí)現(xiàn)的:
    @Override
    public Connection getConnection() throws SQLException {
        return determineTargetDataSource().getConnection();
    }

關(guān)鍵在于 determineTargetDataSource(),根據(jù)方法名就可以看出,應(yīng)該此處就決定了使用哪個(gè) 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(); 該方法是我們實(shí)現(xiàn)的,在其中獲取ThreadLocal中保存的 key 值。獲得了key之后,
在從afterPropertiesSet()中初始化好了的resolvedDataSources這個(gè)map中獲得key對(duì)應(yīng)的dataSource。而ThreadLocal中保存的 key 值
是通過(guò)AOP的方式在調(diào)用service中相關(guān)方法之前設(shè)置好的。OK,到此搞定!

7)擴(kuò)展 ThreadLocalRountingDataSource

上面我們只是實(shí)現(xiàn)了 master-slave 數(shù)據(jù)源的選擇。如果有多臺(tái) master 或者有多臺(tái) slave。多臺(tái)master組成一個(gè)HA,要實(shí)現(xiàn)當(dāng)其中一臺(tái)master掛了是,自動(dòng)切換到另一臺(tái)master,這個(gè)功能可以使用LVS/Keepalived來(lái)實(shí)現(xiàn),也可以通過(guò)進(jìn)一步擴(kuò)展ThreadLocalRountingDataSource來(lái)實(shí)現(xiàn),可以另外加一個(gè)線程專門來(lái)每個(gè)一秒來(lái)測(cè)試mysql是否正常來(lái)實(shí)現(xiàn)。同樣對(duì)于多臺(tái)slave之間要實(shí)現(xiàn)負(fù)載均衡,同時(shí)當(dāng)一臺(tái)slave掛了時(shí),要實(shí)現(xiàn)將其從負(fù)載均衡中去除掉,這個(gè)功能既可以使用LVS/Keepalived來(lái)實(shí)現(xiàn),同樣也可以通過(guò)近一步擴(kuò)展ThreadLocalRountingDataSource來(lái)實(shí)現(xiàn)。

3. 總結(jié)

從本文中我們可以體會(huì)到AOP的強(qiáng)大和靈活。

本文使用的是mybatis,其實(shí)使用Hibernate也應(yīng)該是相似的配置。

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多