使用Spring AOP实现MySQL读写分离
spring aop , mysql 主从配置 实现读写分离,下来把自己的配置过程,以及遇到的问题记录下来,方便下次操作,也希望给一些朋友带来帮助。
mysql主从配置参看:http://blog.csdn.net/huoyunshen88/article/details/26597483
1.使用spring aop 拦截机制现数据源的动态选取。
- @Retention(RetentionPolicy.RUNTIME)
- @Target(ElementType.METHOD)
- public @interface DataSource {
- String value();
- }
3.利用Spring的AbstractRoutingDataSource解决多数据源的问题 参考本站《使用Spring的AbstractRoutingDataSource解决多数据源的问题》
- import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
- public class ChooseDataSource extends AbstractRoutingDataSource {
- @Override
- protected Object determineCurrentLookupKey() {
- return HandleDataSource.getDataSource();
- }
- }
4.利用ThreadLocal解决线程安全问题
- public class HandleDataSource {
- public static final ThreadLocal<String> holder = new ThreadLocal<String>();
- public static void putDataSource(String datasource) {
- holder.set(datasource);
- }
- public static String getDataSource() {
- return holder.get();
- }
- }
5.定义一个数据源切面类,通过aop访问,在spring配置文件中配置了,所以没有使用aop注解
- import java.lang.reflect.Method;
- import org.aspectj.lang.JoinPoint;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.aspectj.lang.annotation.Pointcut;
- import org.aspectj.lang.reflect.MethodSignature;
- import org.springframework.stereotype.Component;
- //@Aspect
- //@Component
- public class DataSourceAspect {
- //@Pointcut("execution(* com.apc.cms.service.*.*(..))")
- public void pointCut(){};
- // @Before(value = "pointCut()")
- public void before(JoinPoint point)
- {
- Object target = point.getTarget();
- System.out.println(target.toString());
- String method = point.getSignature().getName();
- System.out.println(method);
- Class<?>[] classz = target.getClass().getInterfaces();
- Class<?>[] parameterTypes = ((MethodSignature) point.getSignature())
- .getMethod().getParameterTypes();
- try {
- Method m = classz[0].getMethod(method, parameterTypes);
- System.out.println(m.getName());
- if (m != null && m.isAnnotationPresent(DataSource.class)) {
- DataSource data = m.getAnnotation(DataSource.class);
- HandleDataSource.putDataSource(data.value());
- }
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- }
6.配置applicationContext.xml
- <!-- 主库数据源 -->
- <bean id="writeDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
- <property name="driverClass" value="com.mysql.jdbc.Driver"/>
- <property name="jdbcUrl" value="jdbc:mysql://172.22.14.6:3306/cpp?autoReconnect=true"/>
- <property name="username" value="root"/>
- <property name="password" value="root"/>
- <property name="partitionCount" value="4"/>
- <property name="releaseHelperThreads" value="3"/>
- <property name="acquireIncrement" value="2"/>
- <property name="maxConnectionsPerPartition" value="40"/>
- <property name="minConnectionsPerPartition" value="20"/>
- <property name="idleMaxAgeInSeconds" value="60"/>
- <property name="idleConnectionTestPeriodInSeconds" value="60"/>
- <property name="poolAvailabilityThreshold" value="5"/>
- </bean>
- <!-- 从库数据源 -->
- <bean id="readDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
- <property name="driverClass" value="com.mysql.jdbc.Driver"/>
- <property name="jdbcUrl" value="jdbc:mysql://172.22.14.7:3306/cpp?autoReconnect=true"/>
- <property name="username" value="root"/>
- <property name="password" value="root"/>
- <property name="partitionCount" value="4"/>
- <property name="releaseHelperThreads" value="3"/>
- <property name="acquireIncrement" value="2"/>
- <property name="maxConnectionsPerPartition" value="40"/>
- <property name="minConnectionsPerPartition" value="20"/>
- <property name="idleMaxAgeInSeconds" value="60"/>
- <property name="idleConnectionTestPeriodInSeconds" value="60"/>
- <property name="poolAvailabilityThreshold" value="5"/>
- </bean>
- <!-- transaction manager, 事务管理 -->
- <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
- <property name="dataSource" ref="dataSource" />
- </bean>
- <!-- 注解自动载入 -->
- <context:annotation-config />
- <!--enale component scanning (beware that this does not enable mapper scanning!)-->
- <context:component-scan base-package="com.apc.cms.persistence.rdbms" />
- <context:component-scan base-package="com.apc.cms.service">
- <context:include-filter type="annotation"
- expression="org.springframework.stereotype.Component" />
- </context:component-scan>
- <context:component-scan base-package="com.apc.cms.auth" />
- <!-- enable transaction demarcation with annotations -->
- <tx:annotation-driven />
- <!-- define the SqlSessionFactory -->
- <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
- <property name="dataSource" ref="dataSource" />
- <property name="typeAliasesPackage" value="com.apc.cms.model.domain" />
- </bean>
- <!-- scan for mappers and let them be autowired -->
- <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
- <property name="basePackage" value="com.apc.cms.persistence" />
- <property name="sqlSessionFactory" ref="sqlSessionFactory" />
- </bean>
- <bean id="dataSource" class="com.apc.cms.utils.ChooseDataSource">
- <property name="targetDataSources">
- <map key-type="java.lang.String">
- <!-- write -->
- <entry key="write" value-ref="writeDataSource"/>
- <!-- read -->
- <entry key="read" value-ref="readDataSource"/>
- </map>
- </property>
- <property name="defaultTargetDataSource" ref="writeDataSource"/>
- </bean>
- <!-- 激活自动代理功能 -->
- <aop:aspectj-autoproxy proxy-target-class="true"/>
- <!-- 配置数据库注解aop -->
- <bean id="dataSourceAspect" class="com.apc.cms.utils.DataSourceAspect" />
- <aop:config>
- <aop:aspect id="c" ref="dataSourceAspect">
- <aop:pointcut id="tx" expression="execution(* com.apc.cms.service..*.*(..))"/>
- <aop:before pointcut-ref="tx" method="before"/>
- </aop:aspect>
- </aop:config>
- <!-- 配置数据库注解aop -->
7.使用注解,动态选择数据源,分别走读库和写库
- @DataSource("write")
- public void update(User user) {
- userMapper.update(user);
- }
- @DataSource("read")
- public Document getDocById(long id) {
- return documentMapper.getById(id);
- }
测试读操作:
后台修改从库数据,查看主库的数据没有被修改,在应用页面中刷新,发现读的是从库的数据,说明读写分离ok。
http://www.sunyaozong.com/mysql-read-write-sepreate-solution-by-spring-aop/
使用Spring AOP实现MySQL读写分离的更多相关文章
- Spring AOP 实现数据库读写分离
背景 我们一般应用对数据库而言都是"读多写少",也就说对数据库读取数据的压力比较大,有一个思路就是说采用数据库集群的方案, 其中一个是主库,负责写入数据,我们称之为:写库: 其它都 ...
- 从零开始学 Java - Spring AOP 实现主从读写分离
深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...
- Spring Boot+MyBatis+MySQL读写分离
读写分离要做的事情就是对于一条sql语句该选择去哪个数据库执行,至于谁来做选择数据库的事情,无非两个,1:中间件(比如MyCat):二:程序自己去做分离操作. 但是从程序成眠去做读写分离最大的弱点就是 ...
- 170301、使用Spring AOP实现MySQL数据库读写分离案例分析
使用Spring AOP实现MySQL数据库读写分离案例分析 原创 2016-12-29 徐刘根 Java后端技术 一.前言 分布式环境下数据库的读写分离策略是解决数据库读写性能瓶颈的一个关键解决方案 ...
- spring集成mybatis实现mysql读写分离
前言 在网站的用户达到一定规模后,数据库因为负载压力过高而成为网站的瓶颈.幸运的是目前大部分的主流数据库都提供主从热备功能,通过配置两台数据库主从关系,可以将一台数据库的数据更新同步到另一台服务器上. ...
- 使用Spring实现MySQL读写分离(转)
使用Spring实现MySQL读写分离 为什么要进行读写分离 大量的JavaWeb应用做的是IO密集型任务, 数据库的压力较大, 需要分流 大量的应用场景, 是读多写少, 数据库读取的压力更大 一个很 ...
- Spring配置动态数据源-读写分离和多数据源
在现在互联网系统中,随着用户量的增长,单数据源通常无法满足系统的负载要求.因此为了解决用户量增长带来的压力,在数据库层面会采用读写分离技术和数据库拆分等技术.读写分离就是就是一个Master数据库,多 ...
- Spring AOP实现Mysql数据库主从切换(一主多从)
设置数据库主从切换的原因:数据库中经常发生的是“读多写少”,这样读操作对数据库压力比较大,通过采用数据库集群方案, 一个数据库是主库,负责写:其他为从库,负责读,从而实现读写分离增大数据库的容错率. ...
- 提高性能,MySQL 读写分离环境搭建
这是松哥之前一个零散的笔记,整理出来分享给大伙! MySQL 读写分离在互联网项目中应该算是一个非常常见的需求了.受困于 Linux 和 MySQL 版本问题,很多人经常会搭建失败,今天松哥就给大伙举 ...
随机推荐
- 【CF662C】Binary Table(FWT)
[CF662C]Binary Table(FWT) 题面 洛谷 CF 翻译: 有一个\(n*m\)的表格(\(n<=20,m<=10^5\)), 每个表格里面有一个\(0/1\), 每次可 ...
- Ubuntu下使用Evernote
Ubuntu下安装使用Evernote http://blog.csdn.net/yangzhuoluo/article/details/8118996 http://blog.csdn.net/ya ...
- 支付宝 app支付 沙盘使用
文档说明 沙箱测试: App支付沙箱接入注意点 1.app支付支持沙箱接入:在沙箱调通接口后,必须在线上进行测试与验收,所有返回码及业务逻辑以线上为准:2.app支付只支持余额支付,不支持银行卡.余额 ...
- AtCoder [Dwango Programming Contest V] E 动态规划 多项式
原文链接 https://www.cnblogs.com/zhouzhendong/p/AtCoder-Dwango-Programming-Contest-V-E.html 题意 有 $n$ 个数, ...
- BZOJ3262/洛谷P3810 陌上花开 分治 三维偏序 树状数组
原文链接http://www.cnblogs.com/zhouzhendong/p/8672131.html 题目传送门 - BZOJ3262 题目传送门 - 洛谷P3810 题意 有$n$个元素,第 ...
- BZOJ1503 [NOI2004]郁闷的出纳员 splay
原文链接http://www.cnblogs.com/zhouzhendong/p/8086240.html 题目传送门 - BZOJ1503 题意概括 如果某一个员工的工资低于了min,那么,他会立 ...
- {}动态规划}记忆化dp
先搞个模板 #include<stdio.h> #include<string.h> using namespace std; typedef long long ll; ]; ...
- Excel表列名称(给定一个正整数,返回它在 Excel 表中相对应的列名称。)
例如, 1 -> A 2 -> B 3 -> C ... 26 -> Z 27 -> AA 28 -> AB ... 示例 1: 输入: 1 输出: "A ...
- HDU 3416 Marriage Match IV 【最短路】(记录路径)+【最大流】
<题目链接> 题目大意: 给你一张图,问你其中没有边重合的最短路径有多少条. 解题分析: 建图的时候记得存一下链式后向边,方便寻找最短路径,然后用Dijkstra或者SPFA跑一遍最短路, ...
- Oracle no TOP, how to get top from order
On ROWNUM and Limiting Results Our technologist explains how ROWNUM works and how to make it work fo ...