读写分离 Spring+mybatis解决方案

对于数据存储层高并发问题,最先想到的可能就是读写分离,在网站访问量大并且读写不平均的情况下,将存储分为master,slave两台,所有的写都路由到master上,所有的读都路由到slave上,然后master和slave同步。如果一台salve不够,可以加多台,比如一台master,3台slave。在写库的数据库发生变动时,会同步到所有从库,只是同步过程中,会有一定的延迟(除非业务中出现,立即写立即读,否则稍微的延迟是可以接受的)。在此我们讨论一下通过何种方式方式实现读写分离:

方案一:

放在代理层,比如MySQL-Proxy,这样针对整个应用程序都是透明的。 mysql官方不建议实际生产中使用

缺点:降低性能, 不支持事务

方案二:

使用AbstractRoutingDataSource+aop+annotation在dao层决定数据源。
如果采用了mybatis, 可以将读写分离放在ORM层,比如mybatis可以通过mybatis plugin拦截sql语句,所有的insert/update/delete都访问master库,所有的select 都访问salve库,这样对于dao层都是透明。 plugin实现时可以通过注解或者分析语句是读写方法来选定主从库。不过这样依然有一个问题, 也就是不支持事务, 所以我们还需要重写一下DataSourceTransactionManager, 将read-only的事务扔进读库, 其余的有读有写的扔进写库。

方案三:

使用AbstractRoutingDataSource+aop+annotation在service层决定数据源,可以支持事务.

缺点:类内部方法通过this.xx()方式相互调用时,aop不会进行拦截,需进行特殊处理。

那么现在主要来看一下方案二实现方式:

方案二实现:https://github.com/mygudou/smartbatis

  • 1 实现新的mybatis plugin, 在此我们通过实现Interceptor接口来完成功能, 所有Executor接口实现的update和query方法都会被拦截到, 先来看看通过注解的形式是怎么实现选库的。 通过注解的方法选库可以用于一些对事务要求不高的场景当中,
@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 })
})
@Slf4j
public class AnnotationInterceptor implements Interceptor {
    private static final Map<String,DataSourceType> cache = new ConcurrentHashMap<String, DataSourceType>();

    @Override
    public Object intercept(Invocation invocation) throws Throwable {
        Object[] args = invocation.getArgs();
        MappedStatement mappedStatement = (MappedStatement) args[0];
        String id = mappedStatement.getId();
        DataSourceType curDataSourceType = DataSourceType.WRITE;
        if(cache.containsKey(id))
            curDataSourceType = cache.get(id);
        else {
            Method method = getMappedInterfaceMethod(id);
            if (method != null && method.isAnnotationPresent(DataSource.class)) {
                curDataSourceType = method.getAnnotation(DataSource.class).type();
                log.debug("@@ROUTING_DATASOURCE {}", curDataSourceType);
            }
            cache.put(id,curDataSourceType);
        }
        CurrentDataSourceHoler.setCurrentDataSource(curDataSourceType);
        log.debug("@@CURRENT_DATASOURCE {}", CurrentDataSourceHoler.getCurrentDataSource());
        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) {}

    private Method getMappedInterfaceMethod(String id){
        String[] items = id.split("\\.");
        ArrayList<String> nameList = new ArrayList<String>(Arrays.asList(items));
        if (nameList.size() < 2)
            return null;
        String methodName = nameList.get(nameList.size()-1);
        nameList.remove(nameList.size()-1);
        String className = StringUtils.join(nameList,".");
        Method method = ReflectUtil.getMethodByName(ReflectUtil.getClass(className),methodName);
        return method;
    }
}
  • 2 下面这种形式是分析method方法, 要注意的是自增id返回的请求也是用写库
@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 }) })
public class DynamicPlugin implements Interceptor {

    protected static final Logger logger = LoggerFactory.getLogger(DynamicPlugin.class);

    private static final String REGEX = ".*insert\\u0020.*|.*delete\\u0020.*|.*update\\u0020.*";

    private static final Map<String, DataSourceType> cacheMap = new ConcurrentHashMap<String, DataSourceType>();

    @Override
    public Object intercept(Invocation invocation) throws Throwable {

        boolean synchronizationActive = TransactionSynchronizationManager.isSynchronizationActive();
        if(!synchronizationActive) {
            Object[] objects = invocation.getArgs();
            MappedStatement ms = (MappedStatement) objects[0];

            DataSourceType dynamicDataSourceGlobal = null;

            if((dynamicDataSourceGlobal = cacheMap.get(ms.getId())) == null) {
                //读方法
                if(ms.getSqlCommandType().equals(SqlCommandType.SELECT)) {
                    //!selectKey 为自增id查询主键(SELECT LAST_INSERT_ID() )方法,使用主库
                    if(ms.getId().contains(SelectKeyGenerator.SELECT_KEY_SUFFIX)) {
                        dynamicDataSourceGlobal = DataSourceType.WRITE;
                    } else {
                        BoundSql boundSql = ms.getSqlSource().getBoundSql(objects[1]);
                        String sql = boundSql.getSql().toLowerCase(Locale.CHINA).replaceAll("[\\t\\n\\r]", " ");
                        if(sql.matches(REGEX)) {
                            dynamicDataSourceGlobal = DataSourceType.WRITE;
                        } else {
                            dynamicDataSourceGlobal = DataSourceType.READ;
                        }
                    }
                }else{
                    dynamicDataSourceGlobal = DataSourceType.WRITE;
                }
                logger.warn("设置方法[{}] use [{}] Strategy, SqlCommandType [{}]..", ms.getId(), dynamicDataSourceGlobal.name(), ms.getSqlCommandType().name());
                cacheMap.put(ms.getId(), dynamicDataSourceGlobal);
            }
            CurrentDataSourceHoler.setCurrentDataSource(dynamicDataSourceGlobal);
        }

        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) {
        //
    }
}
  • 3 接下来我们需要重写datasource, 将读库和写库都包含在内, 变成一个动态库
public class DynamicDataSource extends AbstractRoutingDataSource {
    @Getter @Setter
    private Object writeDataSource;
    @Getter@Setter
    private List<Object> readDataSourceList;
    private int readDataSourceSize;

    private AtomicInteger counter = new AtomicInteger(0);


    @Override
    public void afterPropertiesSet(){
        if (writeDataSource == null){
            throw new IllegalArgumentException("Property 'writeDataSource' is required");
        }
        setDefaultTargetDataSource(writeDataSource);
        Map<Object,Object> dataSourceMap = new HashMap<Object,Object>();
        dataSourceMap.put(DataSourceType.WRITE.name(),writeDataSource);
        if (readDataSourceList == null){
            readDataSourceSize = 0;
        }else{
            for(int i = 0;i < readDataSourceList.size();i++){
                dataSourceMap.put(DataSourceType.READ.name()+i,readDataSourceList.get(i));
            }
            readDataSourceSize = readDataSourceList.size();
        }
        setTargetDataSources(dataSourceMap);
        super.afterPropertiesSet();
    }

    @Override
    protected Object determineCurrentLookupKey() {

        DataSourceType dataSourceType = CurrentDataSourceHoler.getCurrentDataSource();
        if(dataSourceType == DataSourceType.READ && readDataSourceSize > 0){
            int curentValue = counter.incrementAndGet();
            if(curentValue >= Integer.MAX_VALUE)
                counter.set(0);
            int index = curentValue % readDataSourceSize;
            return DataSourceType.READ.name()+index;
        }
        return DataSourceType.WRITE.name();
    }

    @Override
    public <T> T unwrap(Class<T> aClass) throws SQLException {
        return null;
    }

    @Override
    public boolean isWrapperFor(Class<?> aClass) throws SQLException {
        return false;
    }
}

-4 我们可以看出现在的逻辑是一个数据库请求会在mybatis中的plugin中选定CurrentDataSource, 显然CurrentDataSource应该放在一个threadLocal中, 保证线程同步

public class CurrentDataSourceHoler {
    private static final ThreadLocal<DataSourceType> currentDataSource = new ThreadLocal<DataSourceType>();

    static {
        setCurrentDataSource(DataSourceType.WRITE);
    }
    public static void setCurrentDataSource(DataSourceType dataSourceType){
        currentDataSource.set(dataSourceType);
    }

    public static DataSourceType getCurrentDataSource(){
        return currentDataSource.get();
    }
    
    public static void clearDataSource() {
        currentDataSource.remove();
    }
}
  • 5 接下来就应该在Spring层面去配置事务了, 显然只有只读的事务才可以用读库, 读写都有的事务是要用写库的
public class DynamicDataSourceTransactionManager extends DataSourceTransactionManager {

    /**
     * 只读事务到读库,读写事务到写库
     * @param transaction
     * @param definition
     */
    @Override
    protected void doBegin(Object transaction, TransactionDefinition definition) {

        //设置数据源
        boolean readOnly = definition.isReadOnly();
        if(readOnly) {
            CurrentDataSourceHoler.setCurrentDataSource(DataSourceType.READ);
        } else {
            CurrentDataSourceHoler.setCurrentDataSource(DataSourceType.WRITE);
        }
        super.doBegin(transaction, definition);
    }

    /**
     * 清理本地线程的数据源
     * @param transaction
     */
    @Override
    protected void doCleanupAfterCompletion(Object transaction) {
        super.doCleanupAfterCompletion(transaction);
        CurrentDataSourceHoler.clearDataSource();
    }
}
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,015评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,262评论 1 292
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,727评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 43,986评论 0 205
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,363评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,610评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,871评论 2 312
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,582评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,297评论 1 242
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,551评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,053评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,385评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,035评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,079评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,841评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,648评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,550评论 2 270