Java註解--實現動態數據源切換

當一個項目中有多個數據源(也能夠是主從庫)的時候,咱們能夠利用註解在mapper接口上標註數據源,從而來實現多個數據源在運行時的動態切換。java

實現原理

在Spring 2.0.1中引入了AbstractRoutingDataSource, 該類充當了DataSource的路由中介, 能有在運行時, 根據某種key值來動態切換到真正的DataSource上。數據庫

看下AbstractRoutingDataSource:express

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean

AbstractRoutingDataSource繼承了AbstractDataSource,獲取數據源部分:安全

/** 
 * Retrieve the current target DataSource. Determines the 
 * {@link #determineCurrentLookupKey() current lookup key}, performs 
 * a lookup in the {@link #setTargetDataSources targetDataSources} map, 
 * falls back to the specified 
 * {@link #setDefaultTargetDataSource default target DataSource} if necessary. 
 * @see #determineCurrentLookupKey() 
 */  
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;  
}

抽象方法determineCurrentLookupKey()返回DataSource的key值,而後根據這個key從resolvedDataSources這個map裏取出對應的DataSource,若是找不到,則用默認的resolvedDefaultDataSource。app

咱們要作的就是實現抽象方法determineCurrentLookupKey()返回數據源的key值。ide

使用方法

定義註解:this

/**
 * Created by huangyangquan on 2016/11/30.
 */
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DataSource {

    DataSourceType value();

}

註解爲數據源的名稱,可定義一個枚舉類表示:線程

/**
 * Created by huangyangquan on 2016/11/30.
 */
public enum DataSourceType {

    MASTER,
    SLAVE

}

註解定義好了,咱們利用Spring的AOP根據註解內容對數據源進行選擇,這裏須要利用上面提到的AbstractRoutingDataSource類,該類是可以實現數據源切換的關鍵所在。code

定義類DynamicDataSource繼承AbstractRoutingDataSource,並實現determineCurrentLookupKey(),返回數據源的key值。orm

/**
 * Created by huangyangquan on 2016/11/30.
 */
public class DynamicDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return DynamicDataSourceHolder.getDataSourceType();
    }

}

DynamicDataSourceHolder是咱們管理DataSource的類,將一次數據庫操做的數據源名稱保存在DynamicDataSourceHolder中,以供後面的操做在此context中取數據源key,其中DataSourceType使用了線程本地變量來保證線程安全。

/**
 * Created by huangyangquan on 2016/11/30.
 */
public class DynamicDataSourceHolder {

    // 線程本地環境
    private static final ThreadLocal<DataSourceType> contextHolder = new ThreadLocal<DataSourceType>();

    // 設置數據源類型
    public static void setDataSourceType(DataSourceType dataSourceType) {
        Assert.notNull(dataSourceType, "DataSourceType cannot be null");
        contextHolder.set(dataSourceType);
    }

    // 獲取數據源類型
    public static DataSourceType getDataSourceType() {
        return (DataSourceType) contextHolder.get();
    }

    // 清除數據源類型
    public static void clearDataSourceType() {
        contextHolder.remove();
    }

}

咱們在Spring的配置文件中配置數據源key值得對應關係:

<bean id="spyGhotelDataSource" class="com.aheizi.config.DynamicDataSource">
    <property name="targetDataSources">
        <map key-type="java.lang.String">
            <entry key="MASTER" value-ref="TEST-MASTER-DB"></entry>
            <entry key="SLAVE" value-ref="TEST-SLAVE-DB"></entry>
        </map>
    </property>
    <property name="defaultTargetDataSource" ref="TEST-MASTER-DB">
    </property>
</bean>

設置targetDataSources和defaultTargetDataSource。TEST-MASTER-DBTEST-SLAVE-DB表示主庫的從庫,是咱們的兩個數據源。

接下來配置AOP切面:

<aop:aspectj-autoproxy proxy-target-class="false" />
<bean id="manyDataSourceAspect" class="com.aheizi.config.DataSourceAspect" />
<aop:config>
    <aop:aspect id="dataSourceCut" ref="manyDataSourceAspect">
        <aop:pointcut expression="execution(* com.aheizi.dao.*.*(..))"
            id="dataSourceCutPoint" /><!-- 配置切點 -->
        <aop:before pointcut-ref="dataSourceCutPoint" method="before" />
    </aop:aspect>
</aop:config>

如下是切面中before執行的DataSourceAspect的實現,主要實現的功能是獲取方法上的註解,根據註解名稱將值設置到DynamicDataSourceHolder中,這樣在執行查詢的時候,determineCurrentLookupKey()返回數據源的key值就是咱們但願的那個數據源了。

/**
 * Created by huangyangquan on 2016/11/30.
 */
public class DataSourceAspect {

    private static final Logger LOG = LoggerFactory.getLogger(DataSourceAspect.class);

    public void before(JoinPoint point){
        Object target = point.getTarget();
        String method = point.getSignature().getName();
        Class<?>[] classz = target.getClass().getInterfaces();
        Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();
        try {
            Method m = classz[0].getMethod(method, parameterTypes);
            if (m != null && m.isAnnotationPresent(DataSource.class)) {
                // 訪問mapper中的註解
                DataSource data = m.getAnnotation(DataSource.class);
                switch (data.value()) {
                    case MASTER:
                        DynamicDataSourceHolder.setDataSourceType(DataSourceType.MASTER);
                        LOG.info("using dataSource:{}", DataSourceType.MASTER);
                        break;
                    case SLAVE:
                        DynamicDataSourceHolder.setDataSourceType(DataSourceType.SLAVE);
                        LOG.info("using dataSource:{}", DataSourceType.SLAVE);
                        break;
                }
            }
        } catch (Exception e) {
            LOG.error("dataSource annotation error:{}", e.getMessage());
            // 若出現異常,手動設爲主庫
            DynamicDataSourceHolder.setDataSourceType(DataSourceType.MASTER);
        }
    }

}

這樣咱們就實現了一個動態數據源切換的功能。

相關文章
相關標籤/搜索