spring +mybatis读写分离

标签: spring mybatis 分离 | 发表时间:2018-03-04 23:49 | 作者:guotufu
出处:http://www.iteye.com
一、配置定义数据库连接属性

二、定义bean
<!-- 主数据库连接池 -->
    <bean id="masterDataSource" parent="abstractDataSource">
        <property name="url" value="${master.jdbc.url}"/>
        <property name="username" value="${master.jdbc.username}"/>
        <property name="password" value="${master.jdbc.password}"/>
    </bean>

    <!-- 从数据库连接池1 -->
    <bean id="slave1DataSource" parent="abstractDataSource">
        <property name="url" value="${slave1.jdbc.url}"/>
        <property name="username" value="${slave1.jdbc.username}"/>
        <property name="password" value="${slave1.jdbc.password}"/>
    </bean>

    <!-- 从数据库连接池2 -->
    <bean id="slave2DataSource" parent="abstractDataSource">
        <property name="url" value="${slave2.jdbc.url}"/>
        <property name="username" value="${slave2.jdbc.username}"/>
        <property name="password" value="${slave2.jdbc.password}"/>
    </bean>

    <!-- 从数据库连接池3 -->
    <bean id="slave3DataSource" parent="abstractDataSource">
        <property name="url" value="${slave3.jdbc.url}"/>
        <property name="username" value="${slave3.jdbc.username}"/>
        <property name="password" value="${slave3.jdbc.password}"/>
    </bean>

    <!-- 动态切换数据源 -->
    <bean id="dynamicDataSource"  class="com.common.datasource.DynamicDataSource">
        <property name="writeDataSource"  ref="masterDataSource"></property>
        <property name="readDataSources">
            <list>
                <ref bean="slave1DataSource" />
                <ref bean="slave2DataSource" />
                <ref bean="slave3DataSource" />
            </list>
        </property>
        <!--轮询方式-->
        <property name="readDataSourcePollPattern" value="1" />
        <property name="onlyWrite" value="true" />
    </bean>

三、事务定义
<!-- 事务管理器 -->
    <bean id="dynamicTransactionManager" class="com.common.datasource.DynamicDataSourceTransactionManager">
        <property name="dataSource" ref="dynamicDataSource"/>
    </bean>

    <tx:annotation-driven transaction-manager="dynamicTransactionManager"/>

四、配置插件
<!-- 配置分页插件 -->
    <plugins>
        <plugin interceptor="com.common.datasource.DynamicPlugin" />
        <plugin interceptor="com.github.pagehelper.PageHelper">
            <!-- 设置数据库类型 Oracle,Mysql,MariaDB,SQLite,Hsqldb,PostgreSQL六种数据库 -->
            <property name="dialect" value="mysql"/>
        </plugin>
    </plugins>

五、编写动态数据源类
public class DynamicDataSource extends AbstractRoutingDataSource {

    private static final Long MAX_POOL = Long.MAX_VALUE;
    private final Lock lock = new ReentrantLock();
    private Object writeDataSource; //写数据源
    private List<Object> readDataSources; //多个读数据源
    private int readDataSourceSize; //读数据源个数
    private int readDataSourcePollPattern = 0; //获取读数据源方式,0:随机,1:轮询
    private AtomicLong counter = new AtomicLong(0);
    private boolean onlyWrite=false;

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

    @Override
    protected Object determineCurrentLookupKey() {
        if (onlyWrite){
            return DynamicDataSourceGlobal.WRITE.name();
        }
        DynamicDataSourceGlobal dynamicDataSourceGlobal = DynamicDataSourceHolder.getDataSource();
        if (dynamicDataSourceGlobal == null || dynamicDataSourceGlobal == DynamicDataSourceGlobal.WRITE || readDataSourceSize <= 0) {
            return DynamicDataSourceGlobal.WRITE.name();
        }
        //index表示选择第几个从数据库
        int index;
        if(readDataSourcePollPattern == 1) {
            //轮询方式
            long currValue = counter.incrementAndGet();
            if((currValue + 1) >= MAX_POOL) {
                try {
                    lock.lock();
                    if((currValue + 1) >= MAX_POOL) {
                        counter.set(0);
                    }
                } finally {
                    lock.unlock();
                }
            }
            index = (int) (currValue % readDataSourceSize);
        } else {
            //随机方式
            index = ThreadLocalRandom.current().nextInt(0, readDataSourceSize);
        }
        return dynamicDataSourceGlobal.name() + index;
    }
    public void setWriteDataSource(Object writeDataSource) {
        this.writeDataSource = writeDataSource;
    }

    public List<Object> getReadDataSources() {
        return readDataSources;
    }

    public void setReadDataSources(List<Object> readDataSources) {
        this.readDataSources = readDataSources;
    }

    public Object getWriteDataSource() {
        return writeDataSource;
    }

    public int getReadDataSourceSize() {
        return readDataSourceSize;
    }

    public void setReadDataSourceSize(int readDataSourceSize) {
        this.readDataSourceSize = readDataSourceSize;
    }

    public int getReadDataSourcePollPattern() {
        return readDataSourcePollPattern;
    }

    public void setReadDataSourcePollPattern(int readDataSourcePollPattern) {
        this.readDataSourcePollPattern = readDataSourcePollPattern;
    }

    public boolean isOnlyWrite() {
        return onlyWrite;
    }

    public void setOnlyWrite(boolean onlyWrite) {
        this.onlyWrite = onlyWrite;
    }
}


public class DynamicDataSourceHolder {
    private static final ThreadLocal<DynamicDataSourceGlobal> holder = new ThreadLocal<DynamicDataSourceGlobal>();

    private DynamicDataSourceHolder() {
        //
    }

    public static void putDataSource(DynamicDataSourceGlobal dataSource) {
        holder.set(dataSource);
    }

    public static DynamicDataSourceGlobal getDataSource() {
        return holder.get();
    }

    public static void clearDataSource() {
        holder.remove();
    }
}

public class DynamicDataSourceTransactionManager extends DataSourceTransactionManager {

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

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

    /**
     * 清理本地线程的数据源
     *
     * @param transaction
     */
    @Override
    protected void doCleanupAfterCompletion(Object transaction) {
        super.doCleanupAfterCompletion(transaction);
        DynamicDataSourceHolder.clearDataSource();
    }
}

/**
* Desc: Mybatis动态数据库切换插件
*/
@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, DynamicDataSourceGlobal> cacheMap = new ConcurrentHashMap<>();

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

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

            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 = DynamicDataSourceGlobal.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 = DynamicDataSourceGlobal.WRITE;
                        } else {
                            dynamicDataSourceGlobal = DynamicDataSourceGlobal.READ;
                        }
                    }
                } else {
                    dynamicDataSourceGlobal = DynamicDataSourceGlobal.WRITE;
                }
                logger.warn("设置方法[{}] use [{}] Strategy, SqlCommandType [{}]..", ms.getId(), dynamicDataSourceGlobal.name(), ms.getSqlCommandType().name());
                cacheMap.put(ms.getId(), dynamicDataSourceGlobal);
            }
            DynamicDataSourceHolder.putDataSource(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) {
        //
    }

已有 0 人发表留言,猛击->> 这里<<-参与讨论


ITeye推荐



相关 [spring mybatis 分离] 推荐:

spring +mybatis读写分离

- - 编程语言 - ITeye博客
一、配置定义数据库连接属性. .     .     .     .     .         . .

Spring+MyBatis实践——MyBatis访问数据库

- - 开源软件 - ITeye博客
    在http://dufengx201406163237.iteye.com/blog/2102054中描述了工程的配置,在此记录一下如何使用MyBatis访问数据库;. . .

maven工程下整合spring+mybatis+freemarker

- - CSDN博客架构设计推荐文章
博客地址:http://zhengyinhui.com/?p=142. 由于工作主要是前端开发,做后端的项目比较少,最近自己做个项目,发觉好多的都忘了,这里写篇博客整理下maven工程下整合spring+mybatis+freemarker相关内容. 新建个Archetype为maven-archetype-webapp的maven项目(安装maven插件:http://download.eclipse.org/technology/m2e/releases),在pom文件添加相关依赖:.

spring与mybatis四种整合方法

- - 企业架构 - ITeye博客
  1、采用数据映射器(MapperFactoryBean)的方式,不用写mybatis映射文件,采用注解方式提供相应的sql语句和输入参数.   (1)Spring配置文件:.      .       .       .

mybatis-generator配置

- - 开源软件 - ITeye博客
新项目要用mybatis,为了开发效率和方便开发,研究了mybatis-generate,在maven环境下,通过插件的形式配置,废话不多说. .    由于M2e不支持这个goal,会报错,忽略这个goal就好了,具体原因请看:. 解决办法把下面这段配置添加到与plugins平级目录中即可解决:.

搭建 springboot 2.0 mybatis 读写分离 配置区分不同环境 - 黄青石 - 博客园

- -
最近公司打算使用springboot2.0, springboot支持HTTP/2,所以提前先搭建一下环境. 网上很多都在springboot1.5实现的,所以还是有些差异的.   1) 我用的开发工具是Idea. 工程结构的每个部分的说明: .   config:  用于配置动态数据源的配置,同时使用切面实现数据库读写分离.

spring实现数据库读写分离

- - 行业应用 - ITeye博客
现在大型的电子商务系统,在数据库层面大都采用读写分离技术,就是一个Master数据库,多个Slave数据库. Master库负责数据更新和 实时数据查询,Slave库当然负责非实时数据查询. 因为在实际的应用中,数据库都是读多写少(读取数据的频率高,更新数据的频率相对较少),而读取数据 通常耗时比较长,占用数据库服务器的CPU较多,从而影响用户体验.

使用Spring实现读写分离

- - 编程语言 - ITeye博客
 1. 使用 背景. 我们一般应用对数据库而言都是“读多写少”,也就说对数据库读取数据的压力比较大,有一个思路就是说采用数据库集群的方案,. 其中一个是主库,负责写入数据,我们称之为:写库;. 其它都是从库,负责读取数据,我们称之为:读库;. 解决读写分离的方案有两种:应用层解决和中间件解决. 2.1.  应用层解决:.

Spring中@Transactional与读写分离

- - 编程语言 - ITeye博客
    本文主要介绍如何使用Spring @Transactional基于JDBC Replication协议便捷的实现数据库的读写分离.     1)Spring 4.x + 环境.     3)tomcat-jdbc-pool连接池.     4)spring @Transaction使用与JDBC Replcation协议.

ibatis和mybatis区别

- - SQL - 编程语言 - ITeye博客
简介: 本文主要讲述了 iBatis 2.x 和 MyBatis 3.0.x 的区别,以及从 iBatis 向 MyBatis 移植时需要注意的地方. 通过对本文的学习,读者基本能够了解 MyBatis 有哪些方面的改进,并能够顺利使用 MyBatis 进行开发. 本文更适合有 iBatis 基础的开发人员阅读.