Spring動態資料來源+Mybatis攔截器實現資料庫讀寫分離

晦若晨曦發表於2017-12-14

在專案中遇到了需要做讀寫分離的場景。 對於老專案來說,儘量減少程式碼入侵,在底層實現讀寫分離是墜吼的。

用到的技術主要有兩點:

  • spring動態資料來源
  • mybatis攔截器

###spring動態資料來源 對於多資料來源的情況,spring提供了動態資料來源

org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
複製程式碼

動態資料來源可以通過配置key值,來獲取對應的不同的資料來源。

但是要注意一點:動態資料來源不是真正的資料來源

AbstractRoutingDataSource 正如其名,只是提供了資料來源路由的功能,具體的資料來源還需要進行單獨的配置。所以在我們的實現中,還需要對資料來源的配置和生成進行實現。

資料來源的配置還是十分簡單的,在實現類DynamicDataSource中,宣告瞭三組資料來源集合:

 //直接給定資料來源
    private List<DataSource> roDataSources;
    private List<DataSource> woDataSources;
    private List<DataSource> rwDataSources;
複製程式碼

使用時通過spring注入配置好的資料來源,然後遍歷三個集合,根據配置給指定不同的key。 為了統一進行key的管理,將資料來源key的生成和指派都放在了一個單例的OPCountMapper類中進行管理,此類中根據資料來源所在集合,分別給定只讀,讀寫和只寫三種key以及編號,在進行操作時根據操作的型別,依次呼叫每一種key中的每個資料來源。也就是自帶簡單的負載均衡功能。

import static com.kingsoft.multidb.MultiDbConstants.*;


import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * 資料來源key管理
 * 每個資料來源對應一個單獨的的key例如:
 * ro_0,rw_1,wo_2
 * 之類。
 * 本對映類通過操作型別選定一個可用的資料庫進行操作。
 * 當對應型別沒有可用資料來源時,使用讀寫資料來源。
 * Created by SHIZHIDA on 2017/7/4.
 */
public class OPCountMapper {

    private Map<String,Integer> countMapper = new ConcurrentHashMap<>();
    private Map<String,Integer> lastRouter = new ConcurrentHashMap<>();

    public OPCountMapper(){
        countMapper.put(RO,0);
        countMapper.put(RW,0);
        countMapper.put(WO,0);
        lastRouter.put(RO,0);
        lastRouter.put(RW,0);
        lastRouter.put(WO,0);
    }

    public String getCurrentRouter(String key){
        int total = countMapper.get(key);
        if(total==0){
            if(!key.equals(RW))
                return getCurrentRouter(RW);
            else{
                return null;
            }
        }
        int last = lastRouter.get(key);
        return key+"_"+(last+1)%total;
    }


    public String appendRo() {
        return appendKey(RO);
    }
    public String appendWo() {
        return appendKey(WO);
    }
    public String appendRw() {
        return appendKey(RW);
    }

    private String appendKey(String key){
        int total = countMapper.get(key);
        String sk = key+"_"+total++;
        countMapper.put(key,total);
        return sk;
    }
}

複製程式碼

最後則是在使用中指定當前資料來源,這裡利用到java的ThreadLocal類。此類為每一個執行緒維護一個單獨的成員變數。在使用時,可以根據當前的操作,指定此執行緒中需要使用的資料來源型別:

/**
 * 資料庫選擇
 * Created by SHIZHIDA on 2017/7/4.
 */
public final class DataSourceSelector {

    private static ThreadLocal<String> currentKey = new ThreadLocal<>();

    public static String getCurrentKey(){
        String key = currentKey.get();
        if(StringUtils.isNotEmpty(key))
            return key;
        else return RW;
    }

    public static void setRO(){
        setCurrenKey(RO);
    }
    public static void setRW(){
        setCurrenKey(RW);
    }
    public static void setWO(){
        setCurrenKey(WO);
    }

    public static void setCurrenKey(String key){
        if(Arrays.asList(RO,WO,RW).indexOf(key)>=0){
            currentKey.set(key);
        }else{
            currentKey.set(RW);
            warn("undefined key:"+key);
        }
    }
    
}
複製程式碼

Mybatis攔截器

上面講述了資料來源的配置和選擇,那麼進行選擇的功能就交給Mybatis的攔截器來實現了。

首先,Mybatis所有的SQL讀寫操作,都是通過 org.apache.ibatis.executor.Executor 類來進行操作的。追蹤程式碼可發現,這個類中讀寫只有三個介面,而且功能一目瞭然:

int update(MappedStatement ms, Object parameter) throws SQLException;

  <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler, CacheKey cacheKey, BoundSql boundSql) throws SQLException;

  <E> List<E> query(MappedStatement ms, Object parameter, RowBounds rowBounds, ResultHandler resultHandler) throws SQLException;

複製程式碼

也就是說只要監控了這三個介面,就可以對所有的讀寫操作指派相應的資料來源。

程式碼也十分簡單:


import com.kingsoft.multidb.datasource.DataSourceSelector;
import org.apache.ibatis.cache.CacheKey;
import org.apache.ibatis.executor.Executor;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.plugin.*;
import org.apache.ibatis.session.ResultHandler;
import org.apache.ibatis.session.RowBounds;

import java.util.Properties;

/**
 * 攔截器,對update使用寫庫,對query使用讀庫
 * Created by SHIZHIDA on 2017/7/4.
 */
@Intercepts({
        @Signature(
                type= Executor.class,
                method = "update",
                args = {MappedStatement.class,Object.class}),
        @Signature(
                type= Executor.class,
                method = "query",
                args = {MappedStatement.class,Object.class, RowBounds.class, ResultHandler.class,CacheKey.class,BoundSql.class}),
        @Signature(
                type= Executor.class,
                method = "query",
                args = {MappedStatement.class,Object.class,RowBounds.class, ResultHandler.class}),
})
public class DbSelectorInterceptor implements Interceptor {

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        String name = invocation.getMethod().getName();
        if(name.equals("update"))
            DataSourceSelector.setWO();
        if(name.equals("query"))
            DataSourceSelector.setRO();
        return invocation.proceed();
    }

    @Override
    public Object plugin(Object target) {
        if(target instanceof Executor)
            return Plugin.wrap(target,this);
        else return target;
    }

    @Override
    public void setProperties(Properties properties) {

    }

}

複製程式碼

###總結

至此一套簡單的資料庫讀寫分離功能就已經實現了,只要在spring中配置了資料來源,並且為mybatis的SqlSessionFactory進行如下配置:

<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    <property name="dataSource" ref="dynamicDataSource"/>
    <property name="configLocation" value="classpath:mybatis-config.xml"/>
    <property name="plugins" ref="dbSelectorInterceptor"/>
</bean>
複製程式碼

就可以在對程式碼0侵入的情況下實現讀寫分離,附贈多資料庫負載均衡的功能。

相關文章