AOP方式代码控制多数据源切换
- 2019 年 10 月 4 日
- 笔记
版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/qq_37933685/article/details/80527461
个人博客:https://suveng.github.io/blog/
AOP方式代码控制多数据源切换
要想控制多数据源,必须继承spring提供的一个接口类
重写里面AbstractRoutingDataSource
的determineCurrentLookupKey()
这里需要返回Map<Object, Object> targetDataSources
的 key,那么在spring配置一个bean吧,当然也可以写死,或者new 一个对象,但是spring还是推荐用配置.
注意,为什么会知道呢?也是百度和看了一下AbstractRoutingDataSource
的源码的
那么就需要配置spring了如下,我这里配置一个主库和两个从库,
mysql怎么配置主从复制?上面一篇已经说明了
看下面配置spring-cfg.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd" > <!--扫描注解生成bean--> <context:annotation-config/> <!--包扫描--> <context:component-scan base-package="com.*"/> <context:property-placeholder location="classpath:jdbc.properties"/> <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean"> <property name="dataSource" ref="dataSource"/> <property name="mapperLocations" value="classpath:com/*/**/**.xml"/> </bean> <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer"> <property name="basePackage" value="com.*.*.dao"/> <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> </bean> <!--声明事务管理 采用注解方式--> <tx:annotation-driven transaction-manager="transactionManager"/> <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager"> <property name="dataSource" ref="dataSource"/> </bean> <aop:aspectj-autoproxy/> <bean id="switchDataSourceAspect" class="com.*.common.DataSourceAspect"/> <aop:config> <aop:aspect ref="switchDataSourceAspect"> <aop:pointcut id="tx" expression="execution(* com.*.*.service.*.*(..))"/> <aop:before method="before" pointcut-ref="tx"/> </aop:aspect> </aop:config> <!--数据库设置--> <bean id="masterdataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" init-method="init"> <property name="url" value="${jdbc_url_m}"/> <property name="username" value="${jdbc_username}"/> <property name="password" value="${jdbc_password}"/> </bean> <bean id="slavedataSource_1" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" init-method="init"> <property name="url" value="${jdbc_url_s_1}"/> <property name="username" value="${jdbc_username}"/> <property name="password" value="${jdbc_password}"/> </bean> <bean id="slavedataSource_2" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" init-method="init"> <property name="url" value="${jdbc_url_s_2}"/> <property name="username" value="${jdbc_username}"/> <property name="password" value="${jdbc_password}"/> </bean> <bean id="dataSource" class="com.*.common.DynamicDataSource"> <property name="targetDataSources"> <map> <entry key="master" value-ref="masterdataSource"/> <entry key="slave_1" value-ref="slavedataSource_1"/> <entry key="slave_2" value-ref="slavedataSource_2"/> </map> </property> <property name="defaultTargetDataSource" ref="masterdataSource"/> </bean> </beans>
如上面配置所示,首先三个数据源的配置分别为masterdataSource,slavedataSource_1,slavedataSource_2,其次就是配置datasource了,datasource额里面有两个属性targetdatasources 和 defaultTargetDataSource.
配置好了就可以通过AbstractRoutingDataSource
的determineCurrentLookupKey()
传入的key来控制用那个数据源.
源码地址https://github.com/1344115844/mysql-master-slave , clone自己看吧