在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

详解使用spring aop实现业务层mysql 读写分离

2017-3-7 12:50| 发布者: zhangjf| 查看: 1270| 评论: 0

摘要: spring aop , mysql 主从配置 实现读写分离,接下来把自己的配置过程,以及遇到的问题记录下来,方便下次操作,也希望给一些朋友带来帮助。 1.使用spring aop 拦截机制现数据源的动态选取。 import java.lang.ann ...

spring aop , mysql 主从配置 实现读写分离,接下来把自己的配置过程,以及遇到的问题记录下来,方便下次操作,也希望给一些朋友带来帮助。

1.使用spring aop 拦截机制现数据源的动态选取。

  1. import java.lang.annotation.ElementType;
  2. import java.lang.annotation.Target;
  3. import java.lang.annotation.Retention;
  4. import java.lang.annotation.RetentionPolicy;
  5. /**
  6. * RUNTIME
  7. * 编译器将把注释记录在类文件中,在运行时 VM 将保留注释,因此可以反射性地读取。
  8. * @author yangGuang
  9. *
  10. */
  11. @Retention(RetentionPolicy.RUNTIME)
  12. @Target(ElementType.METHOD)
  13. public @interface DataSource {
  14. String value();
  15. }
复制代码

3.利用Spring的AbstractRoutingDataSource解决多数据源的问题

  1. import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
  2. public class ChooseDataSource extends AbstractRoutingDataSource {
  3. @Override
  4. protected Object determineCurrentLookupKey() {
  5. return HandleDataSource.getDataSource();
  6. }
  7. }
复制代码

4.利用ThreadLocal解决线程安全问题

  1. public class HandleDataSource {
  2. public static final ThreadLocal<String> holder = new ThreadLocal<String>();
  3. public static void putDataSource(String datasource) {
  4. holder.set(datasource);
  5. }
  6. public static String getDataSource() {
  7. return holder.get();
  8. }
  9. }
复制代码

5.定义一个数据源切面类,通过aop访问,在spring配置文件中配置了,所以没有使用aop注解。

  1. import java.lang.reflect.Method;
  2. import org.aspectj.lang.JoinPoint;
  3. import org.aspectj.lang.annotation.Aspect;
  4. import org.aspectj.lang.annotation.Before;
  5. import org.aspectj.lang.annotation.Pointcut;
  6. import org.aspectj.lang.reflect.MethodSignature;
  7. import org.springframework.stereotype.Component;
  8. //@Aspect
  9. //@Component
  10. public class DataSourceAspect {
  11. //@Pointcut("execution(* com.apc.cms.service.*.*(..))")
  12. public void pointCut(){};
  13. // @Before(value = "pointCut()")
  14. public void before(JoinPoint point)
  15. {
  16. Object target = point.getTarget();
  17. System.out.println(target.toString());
  18. String method = point.getSignature().getName();
  19. System.out.println(method);
  20. Class<?>[] classz = target.getClass().getInterfaces();
  21. Class<?>[] parameterTypes = ((MethodSignature) point.getSignature())
  22. .getMethod().getParameterTypes();
  23. try {
  24. Method m = classz[0].getMethod(method, parameterTypes);
  25. System.out.println(m.getName());
  26. if (m != null && m.isAnnotationPresent(DataSource.class)) {
  27. DataSource data = m.getAnnotation(DataSource.class);
  28. HandleDataSource.putDataSource(data.value());
  29. }
  30. } catch (Exception e) {
  31. e.printStackTrace();
  32. }
  33. }
  34. }
复制代码

6.配置applicationContext.xml

  1. <!-- 主库数据源 -->
  2. <bean id="writeDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
  3. <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  4. <property name="jdbcUrl" value="jdbc:mysql://172.22.14.6:3306/cpp?autoReconnect=true"/>
  5. <property name="username" value="root"/>
  6. <property name="password" value="root"/>
  7. <property name="partitionCount" value="4"/>
  8. <property name="releaseHelperThreads" value="3"/>
  9. <property name="acquireIncrement" value="2"/>
  10. <property name="maxConnectionsPerPartition" value="40"/>
  11. <property name="minConnectionsPerPartition" value="20"/>
  12. <property name="idleMaxAgeInSeconds" value="60"/>
  13. <property name="idleConnectionTestPeriodInSeconds" value="60"/>
  14. <property name="poolAvailabilityThreshold" value="5"/>
  15. </bean>
  16. <!-- 从库数据源 -->
  17. <bean id="readDataSource" class="com.jolbox.bonecp.BoneCPDataSource" destroy-method="close">
  18. <property name="driverClass" value="com.mysql.jdbc.Driver"/>
  19. <property name="jdbcUrl" value="jdbc:mysql://172.22.14.7:3306/cpp?autoReconnect=true"/>
  20. <property name="username" value="root"/>
  21. <property name="password" value="root"/>
  22. <property name="partitionCount" value="4"/>
  23. <property name="releaseHelperThreads" value="3"/>
  24. <property name="acquireIncrement" value="2"/>
  25. <property name="maxConnectionsPerPartition" value="40"/>
  26. <property name="minConnectionsPerPartition" value="20"/>
  27. <property name="idleMaxAgeInSeconds" value="60"/>
  28. <property name="idleConnectionTestPeriodInSeconds" value="60"/>
  29. <property name="poolAvailabilityThreshold" value="5"/>
  30. </bean>
  31. <!-- transaction manager, 事务管理 -->
  32. <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
  33. <property name="dataSource" ref="dataSource" />
  34. </bean>
  35. <!-- 注解自动载入 -->
  36. <context:annotation-config />
  37. <!--enale component scanning (beware that this does not enable mapper scanning!)-->
  38. <context:component-scan base-package="com.apc.cms.persistence.rdbms" />
  39. <context:component-scan base-package="com.apc.cms.service">
  40. <context:include-filter type="annotation"
  41. expression="org.springframework.stereotype.Component" />
  42. </context:component-scan>
  43. <context:component-scan base-package="com.apc.cms.auth" />
  44. <!-- enable transaction demarcation with annotations -->
  45. <tx:annotation-driven />
  46. <!-- define the SqlSessionFactory -->
  47. <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  48. <property name="dataSource" ref="dataSource" />
  49. <property name="typeAliasesPackage" value="com.apc.cms.model.domain" />
  50. </bean>
  51. <!-- scan for mappers and let them be autowired -->
  52. <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  53. <property name="basePackage" value="com.apc.cms.persistence" />
  54. <property name="sqlSessionFactory" ref="sqlSessionFactory" />
  55. </bean>
  56. <bean id="dataSource" class="com.apc.cms.utils.ChooseDataSource">
  57. <property name="targetDataSources">
  58. <map key-type="java.lang.String">
  59. <!-- write -->
  60. <entry key="write" value-ref="writeDataSource"/>
  61. <!-- read -->
  62. <entry key="read" value-ref="readDataSource"/>
  63. </map>
  64. </property>
  65. <property name="defaultTargetDataSource" ref="writeDataSource"/>
  66. </bean>
  67. <!-- 激活自动代理功能 -->
  68. <aop:aspectj-autoproxy proxy-target-class="true"/>
  69. <!-- 配置数据库注解aop -->
  70. <bean id="dataSourceAspect" class="com.apc.cms.utils.DataSourceAspect" />
  71. <aop:config>
  72. <aop:aspect id="c" ref="dataSourceAspect">
  73. <aop:pointcut id="tx" expression="execution(* com.apc.cms.service..*.*(..))"/>
  74. <aop:before pointcut-ref="tx" method="before"/>
  75. </aop:aspect>
  76. </aop:config>
  77. <!-- 配置数据库注解aop -->
复制代码

7.使用注解,动态选择数据源,分别走读库和写库。

  1. @DataSource("write")
  2. public void update(User user) {
  3. userMapper.update(user);
  4. }
  5. @DataSource("read")
  6. public Document getDocById(long id) {
  7. return documentMapper.getById(id);
  8. }
复制代码

测试写操作:可以通过应用修改数据,修改主库数据,发现从库的数据被同步更新了,所以定义的write操作都是走的写库

测试读操作: 后台修改从库数据,查看主库的数据没有被修改,在应用页面中刷新,发现读的是从库的数据,说明读写分离ok。

遇到的问题总结:

问题1:项目是maven工程,用到了Spring aop机制,除了spring的核心jar包以为,还需要用到的jar包有aspectj.jar,aspectjweaver.jar,aopalliance.jar查看项目中的pom,发现缺少依赖包,由于本地仓库没有这些jar,查找可以提供下载jar包的maven中央库库,配置到maven中,自动更新:

  1. <repository>
  2. <id>nexus</id>
  3. <name>nexus</name>
  4. <url>http://repository.sonatype.org/content/groups/public/</url>
  5. <layout>default</layout>
  6. </repository>
复制代码

配置项目依赖的jar,主要是缺少这两个。

  1. <dependency>
  2. <groupId>aspectj</groupId>
  3. <artifactId>aspectjrt</artifactId>
  4. <version>1.5.4</version>
  5. </dependency>
  6. <dependency>
  7. <groupId>aspectj</groupId>
  8. <artifactId>aspectjweaver</artifactId>
  9. <version>1.5.4</version>
  10. lt;/dependency>
复制代码

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持程序员之家。

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-5-4 02:36

Copyright 2015-2025 djqfx

返回顶部