如何配置mysql数据库的主从?

单机配置mysql主从:http://my.oschina.net/god/blog/496

常见的解决数据库读写分离有两种方案

1、应用层

http://neoremind.net/2011/06/spring实现数据库读写分离

目前的一些解决方案需要在程序中手动指定数据源,比较麻烦,后边我会通过AOP思想来解决这个问题。

2、中间件

mysql-proxy:http://hi.baidu.com/geshuai2008/item/0ded5389c685645f850fab07

Amoeba for MySQL:http://www.iteye.com/topic/188598http://www.iteye.com/topic/1113437

此处我们介绍一种在应用层的解决方案,通过spring动态数据源和AOP来解决数据库的读写分离。

该方案目前已经在一个互联网项目中使用了,而且可以很好的工作。

该方案目前支持

一读多写;当写时默认读操作到写库、当写时强制读操作到读库。

考虑未来支持

读库负载均衡、读库故障转移等。

使用场景

不想引入中间件,想在应用层解决读写分离,可以考虑这个方案;

建议数据访问层使用jdbc、ibatis,不建议hibernate。

优势

应用层解决,不引入额外中间件;

在应用层支持『当写时默认读操作到写库』,这样如果我们采用这种方案,在写操作后读数据直接从写库拿,不会产生数据复制的延迟问题;

应用层解决读写分离,理论支持任意数据库。

缺点

1、不支持@Transactional注解事务,此方案要求所有读方法必须是read-only=true,因此如果是@Transactional,这样就要求在每一个读方法头上加@Transactional 且readOnly属性=true,相当麻烦。 :oops:

2、必须按照配置约定进行配置,不够灵活。

两种方案

方案1:当只有读操作的时候,直接操作读库(从库);

当在写事务(即写主库)中读时,也是读主库(即参与到主库操作),这样的优势是可以防止写完后可能读不到刚才写的数据;

此方案其实是使用事务传播行为为:SUPPORTS解决的。

方案2:当只有读操作的时候,直接操作读库(从库);

当在写事务(即写主库)中读时,强制走从库,即先暂停写事务,开启读(读从库),然后恢复写事务。

此方案其实是使用事务传播行为为:NOT_SUPPORTS解决的。

核心组件

cn.javass.common.datasource.ReadWriteDataSource:读写分离的动态数据源,类似于AbstractRoutingDataSource,具体参考javadoc;

cn.javass.common.datasource.ReadWriteDataSourceDecision:读写库选择的决策者,具体参考javadoc;

cn.javass.common.datasource.ReadWriteDataSourceProcessor:此类实现了两个职责(为了减少类的数量将两个功能合并到一起了):读/写动态数据库选择处理器、通过AOP切面实现读/写选择,具体参考javadoc。

具体配置

1、数据源配置

1.1、写库配置

  1. <bean id="writeDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
  2. <property name="alias" value="writeDataSource"/>
  3. <property name="driver" value="${write.connection.driver_class}" />
  4. <property name="driverUrl" value="${write.connection.url}" />
  5. <property name="user" value="${write.connection.username}" />
  6. <property name="password" value="${write.connection.password}" />
  7. <property name="maximumConnectionCount" value="${write.proxool.maximum.connection.count}"/>
  8. <property name="minimumConnectionCount" value="${write.proxool.minimum.connection.count}" />
  9. <property name="statistics" value="${write.proxool.statistics}" />
  10. <property name="simultaneousBuildThrottle" value="${write.proxool.simultaneous.build.throttle}"/>
  11. </bean>
  	<bean id="writeDataSource" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="alias" value="writeDataSource"/>
<property name="driver" value="${write.connection.driver_class}" />
<property name="driverUrl" value="${write.connection.url}" />
<property name="user" value="${write.connection.username}" />
<property name="password" value="${write.connection.password}" />
<property name="maximumConnectionCount" value="${write.proxool.maximum.connection.count}"/>
<property name="minimumConnectionCount" value="${write.proxool.minimum.connection.count}" />
<property name="statistics" value="${write.proxool.statistics}" />
<property name="simultaneousBuildThrottle" value="${write.proxool.simultaneous.build.throttle}"/>
</bean>

1.2、读库配置

  1. <bean id="readDataSource1" class="org.logicalcobwebs.proxool.ProxoolDataSource">
  2. <property name="alias" value="readDataSource"/>
  3. <property name="driver" value="${read.connection.driver_class}" />
  4. <property name="driverUrl" value="${read.connection.url}" />
  5. <property name="user" value="${read.connection.username}" />
  6. <property name="password" value="${read.connection.password}" />
  7. <property name="maximumConnectionCount" value="${read.proxool.maximum.connection.count}"/>
  8. <property name="minimumConnectionCount" value="${read.proxool.minimum.connection.count}" />
  9. <property name="statistics" value="${read.proxool.statistics}" />
  10. <property name="simultaneousBuildThrottle" value="${read.proxool.simultaneous.build.throttle}"/>
  11. </bean>
    <bean id="readDataSource1" class="org.logicalcobwebs.proxool.ProxoolDataSource">
<property name="alias" value="readDataSource"/>
<property name="driver" value="${read.connection.driver_class}" />
<property name="driverUrl" value="${read.connection.url}" />
<property name="user" value="${read.connection.username}" />
<property name="password" value="${read.connection.password}" />
<property name="maximumConnectionCount" value="${read.proxool.maximum.connection.count}"/>
<property name="minimumConnectionCount" value="${read.proxool.minimum.connection.count}" />
<property name="statistics" value="${read.proxool.statistics}" />
<property name="simultaneousBuildThrottle" value="${read.proxool.simultaneous.build.throttle}"/>
</bean> 

1.3、读写动态库配置

通过writeDataSource指定写库,通过readDataSourceMap指定从库列表,从库列表默认通过顺序轮询来使用读库,具体参考javadoc;

  1. <bean id="readWriteDataSource" class="cn.javass.common.datasource.ReadWriteDataSource">
  2. <property name="writeDataSource" ref="writeDataSource"/>
  3. <property name="readDataSourceMap">
  4. <map>
  5. <entry key="readDataSource1" value-ref="readDataSource1"/>
  6. <entry key="readDataSource2" value-ref="readDataSource1"/>
  7. <entry key="readDataSource3" value-ref="readDataSource1"/>
  8. <entry key="readDataSource4" value-ref="readDataSource1"/>
  9. </map>
  10. </property>
  11. </bean>
    <bean id="readWriteDataSource" class="cn.javass.common.datasource.ReadWriteDataSource">
<property name="writeDataSource" ref="writeDataSource"/>
<property name="readDataSourceMap">
<map>
<entry key="readDataSource1" value-ref="readDataSource1"/>
<entry key="readDataSource2" value-ref="readDataSource1"/>
<entry key="readDataSource3" value-ref="readDataSource1"/>
<entry key="readDataSource4" value-ref="readDataSource1"/>
</map>
</property>
</bean> 

2XML事务属性配置

所以读方法必须是read-only(必须,以此来判断是否是读方法)。

  1. <tx:advice id="txAdvice" transaction-manager="txManager">
  2. <tx:attributes>
  3. <tx:method name="save*" propagation="REQUIRED" />
  4. <tx:method name="add*" propagation="REQUIRED" />
  5. <tx:method name="create*" propagation="REQUIRED" />
  6. <tx:method name="insert*" propagation="REQUIRED" />
  7. <tx:method name="update*" propagation="REQUIRED" />
  8. <tx:method name="merge*" propagation="REQUIRED" />
  9. <tx:method name="del*" propagation="REQUIRED" />
  10. <tx:method name="remove*" propagation="REQUIRED" />
  11. <tx:method name="put*" read-only="true"/>
  12. <tx:method name="query*" read-only="true"/>
  13. <tx:method name="use*" read-only="true"/>
  14. <tx:method name="get*" read-only="true" />
  15. <tx:method name="count*" read-only="true" />
  16. <tx:method name="find*" read-only="true" />
  17. <tx:method name="list*" read-only="true" />
  18. <tx:method name="*" propagation="REQUIRED"/>
  19. </tx:attributes>
  20. </tx:advice>
    <tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="save*" propagation="REQUIRED" />
<tx:method name="add*" propagation="REQUIRED" />
<tx:method name="create*" propagation="REQUIRED" />
<tx:method name="insert*" propagation="REQUIRED" />
<tx:method name="update*" propagation="REQUIRED" />
<tx:method name="merge*" propagation="REQUIRED" />
<tx:method name="del*" propagation="REQUIRED" />
<tx:method name="remove*" propagation="REQUIRED" /> <tx:method name="put*" read-only="true"/>
<tx:method name="query*" read-only="true"/>
<tx:method name="use*" read-only="true"/>
<tx:method name="get*" read-only="true" />
<tx:method name="count*" read-only="true" />
<tx:method name="find*" read-only="true" />
<tx:method name="list*" read-only="true" /> <tx:method name="*" propagation="REQUIRED"/>
</tx:attributes>
</tx:advice> 

3、事务管理器

事务管理器管理的是readWriteDataSource

  1. <bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  2. <property name="dataSource" ref="readWriteDataSource"/>
  3. </bean>
<bean id="txManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="readWriteDataSource"/>
</bean> 

4、读/写动态数据库选择处理器

根据之前的txAdvice配置的事务属性决定是读/写,具体参考javadoc;

forceChoiceReadWhenWrite:用于确定在如果目前是写(即开启了事务),下一步如果是读,是直接参与到写库进行读,还是强制从读库读,具体参考javadoc;

  1. <bean id="readWriteDataSourceTransactionProcessor" class="cn.javass.common.datasource.ReadWriteDataSourceProcessor">
  2. <property name="forceChoiceReadWhenWrite" value="false"/>
  3. </bean>
    <bean id="readWriteDataSourceTransactionProcessor" class="cn.javass.common.datasource.ReadWriteDataSourceProcessor">
<property name="forceChoiceReadWhenWrite" value="false"/>
</bean> 

5、事务切面和读/写库选择切面

  1. <aop:config expose-proxy="true">
  2. <!-- 只对业务逻辑层实施事务 -->
  3. <aop:pointcut id="txPointcut" expression="execution(* cn.javass..service..*.*(..))" />
  4. <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
  5. <!-- 通过AOP切面实现读/写库选择 -->
  6. <aop:aspect order="-2147483648" ref="readWriteDataSourceTransactionProcessor">
  7. <aop:around pointcut-ref="txPointcut" method="determineReadOrWriteDB"/>
  8. </aop:aspect>
  9. </aop:config>
    <aop:config expose-proxy="true">
<!-- 只对业务逻辑层实施事务 -->
<aop:pointcut id="txPointcut" expression="execution(* cn.javass..service..*.*(..))" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/> <!-- 通过AOP切面实现读/写库选择 -->
<aop:aspect order="-2147483648" ref="readWriteDataSourceTransactionProcessor">
<aop:around pointcut-ref="txPointcut" method="determineReadOrWriteDB"/>
</aop:aspect>
</aop:config> 

1、事务切面一般横切业务逻辑层;

2、此处我们使用readWriteDataSourceTransactionProcessor的通过AOP切面实现读/写库选择功能,order=Integer.MIN_VALUE(即最高的优先级),从而保证在操作事务之前已经决定了使用读/写库。

6、测试用例

只要配置好事务属性(通过read-only=true指定读方法)即可,其他选择读/写库的操作都交给readWriteDataSourceTransactionProcessor完成。

可以参考附件的:

cn.javass.readwrite.ReadWriteDBTestWithForceChoiceReadOnWriteFalse

cn.javass.readwrite.ReadWriteDBTestWithNoForceChoiceReadOnWriteTrue

可以下载附件的代码进行测试,具体选择主/从可以参考日志输出。

暂不想支持@Transactional注解式事务。

PS:欢迎拍砖指正。

  • 大小: 19.7 KB
  • 大小: 15.3 KB

在应用层通过spring特性解决数据库读写分离的更多相关文章

  1. Spring+MyBatis实现数据库读写分离方案

    推荐第四种:https://github.com/shawntime/shawn-rwdb 方案1 通过MyBatis配置文件创建读写分离两个DataSource,每个SqlSessionFactor ...

  2. 使用Spring AOP切面解决数据库读写分离

    http://blog.jobbole.com/103496/ 为了减轻数据库的压力,一般会使用数据库主从(master/slave)的方式,但是这种方式会给应用程序带来一定的麻烦,比如说,应用程序如 ...

  3. Spring AOP 实现数据库读写分离

    背景 我们一般应用对数据库而言都是"读多写少",也就说对数据库读取数据的压力比较大,有一个思路就是说采用数据库集群的方案, 其中一个是主库,负责写入数据,我们称之为:写库: 其它都 ...

  4. Spring boot实现数据库读写分离

    背景 数据库配置主从之后,如何在代码层面实现读写分离? 用户自定义设置数据库路由 Spring boot提供了AbstractRoutingDataSource根据用户定义的规则选择当前的数据库,这样 ...

  5. mysql+spring+mybatis实现数据库读写分离[代码配置] .

    场景:一个读数据源一个读写数据源. 原理:借助spring的[org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource] ...

  6. 161220、使用Spring AOP实现MySQL数据库读写分离案例分析

    一.前言 分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解决方案,更是最大限度了提高了应用中读取 (Read)数据的速度和并发量. 在进行数据库读写分离的时候,我们首先要进行数据库 ...

  7. spring+mybatis利用interceptor(plugin)兑现数据库读写分离

    使用spring的动态路由实现数据库负载均衡 系统中存在的多台服务器是"地位相当"的,不过,同一时间他们都处于活动(Active)状态,处于负载均衡等因素考虑,数据访问请求需要在这 ...

  8. 170301、使用Spring AOP实现MySQL数据库读写分离案例分析

    使用Spring AOP实现MySQL数据库读写分离案例分析 原创 2016-12-29 徐刘根 Java后端技术 一.前言 分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解决方案 ...

  9. 使用Spring AOP实现MySQL数据库读写分离案例分析

    一.前言 分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解决方案,更是最大限度了提高了应用中读取 (Read)数据的速度和并发量. 在进行数据库读写分离的时候,我们首先要进行数据库 ...

随机推荐

  1. C# 1.将整个文件夹复制到目标文件夹中 2.将指定文件复制到指定目标文件夹中

    ].Items.Clear(); string filePath = Application.StartupPath; string sourcePath = Path.Combine(filePat ...

  2. 超实用的JavaScript代码段

    1. 判断日期是否有效 JavaScript中自带的日期函数还是太过简单,很难满足真实项目中对不同日期格式进行解析和判断的需要.JQuery也有一些第三方库来使日期相关的处理变得简单,但有时你可能只需 ...

  3. 团体程序设计天梯赛-练习集-L1-025. 正整数A+B

    L1-025. 正整数A+B 本题的目标很简单,就是求两个正整数A和B的和,其中A和B都在区间[1,1000].稍微有点麻烦的是,输入并不保证是两个正整数. 输入格式: 输入在一行给出A和B,其间以空 ...

  4. Flask框架函数

    title: flask学习笔记 subtitle: 1. flask框架函数 date: 2018-12-14 10:17:28 --- Flask学习 学习Miguel Grinberg的2017 ...

  5. JTextArea+JScrollPane滚动条自动在最下边(转帖)

    这是我制作五子棋的过程中遇到的问题,在网上搜了好几种答案,分别列在下面了.不过感觉第一种相当方便.用得简洁,爽! 1. 利用JTextArea的selectAll();方法在添加信息之后强制将光标移动 ...

  6. [noip2011 luogu1312] Mayan游戏(模拟)

    原题:传送门 大模拟- 两个剪枝: 1.如果左边不为空就不往左边走(因为一定不如左边的移到右边优) 2.如果相邻两颜色相同不需移动 当然也有别的小剪枝(我没写)比如如果当前某一颜色剩余块数满足1< ...

  7. php输出网页源代码莫名奇妙的多了一堆方框,导致ajax验证失败.

    今天在用一个ajax验证用户名的功能,返回值报错,抓包看了下,多出来一堆点,源代码里显示方框和6个空行 这堆东西导致ajax判断返回值会错误,度娘了一下午(皇天不负游戏人啊),原来是一个坑爹的BOM头 ...

  8. Updates were rejected because the remote contains work that you do(gitee报错解决方案)

    今天向Gitee远程仓库提交本地项目文件时,遇到了下列错误,很是郁闷 正在推送 1203笔记本Error: failed to push some refs to 'https://gitee.com ...

  9. spring中的单例和多例

    单例 对象在整个系统中只有一份,所有的请求都用一个对象来处理,如service和dao层的对象一般是单例的. 为什么使用单例:因为没有必要每个请求都新建一个对象的时候,浪费CPU和内存. 多例 对象在 ...

  10. mysql 在Linux 配置 主从同步

    一.主服务器相关配置 1.创建同步账户并指定服务器地址 [root@localhost ~]mysql -uroot -pmysql>use mysqlmysql>grant replic ...