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自己看吧