在項目中遇到了須要作讀寫分離的場景。 對於老項目來講,儘可能減小代碼入侵,在底層實現讀寫分離是墜吼的。java
用到的技術主要有兩點:spring
###spring動態數據源 對於多數據源的狀況,spring提供了動態數據源sql
org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
複製代碼
動態數據源能夠經過配置key值,來獲取對應的不一樣的數據源。數據庫
可是要注意一點:動態數據源不是真正的數據源!apache
AbstractRoutingDataSource 正如其名,只是提供了數據源路由的功能,具體的數據源還須要進行單獨的配置。因此在咱們的實現中,還須要對數據源的配置和生成進行實現。bash
數據源的配置仍是十分簡單的,在實現類DynamicDataSource中,聲明瞭三組數據源集合:session
//直接給定數據源
private List<DataSource> roDataSources;
private List<DataSource> woDataSources;
private List<DataSource> rwDataSources;
複製代碼
使用時經過spring注入配置好的數據源,而後遍歷三個集合,根據配置給指定不一樣的key。 爲了統一進行key的管理,將數據源key的生成和指派都放在了一個單例的OPCountMapper類中進行管理,此類中根據數據源所在集合,分別給定只讀,讀寫和只寫三種key以及編號,在進行操做時根據操做的類型,依次調用每一種key中的每一個數據源。也就是自帶簡單的負載均衡功能。mybatis
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類。此類爲每個線程維護一個單獨的成員變量。在使用時,能夠根據當前的操做,指定此線程中須要使用的數據源類型:app
/**
* 數據庫選擇
* 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全部的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侵入的狀況下實現讀寫分離,附贈多數據庫負載均衡的功能。