Spring加Mybatis實現MySQL數據庫主從讀寫分離 ,實現的原理是配置了多套數據源,相應的sqlsessionfactory,transactionmanager和事務代理各配置了一套,如果從庫或數據庫有多個的時候,需要配置的信息會越來越多,遠遠不夠優雅,在我們編程界有一個規 范:約定優於配置。所以就用Sping的aop實現了一個簡單的數據庫分離方案,具體實現代碼放在了Github上,地址如下:
https://github.com/bridgeli/practical-util/tree/master/src/main/java/cn/bridgeli
讀者如果想使用再簡單的方法就是把這個代碼download下來,放到自己的項目裡面,當然更優雅的方式是:打成jar包,放到項目裡面了,具體打jar的方法,老夫就不在這裡多說了,相信看這篇文章的讀者肯定都會了。當然僅僅有這份代碼,他們是不會自動生效的,既然是使用Spring的Aop實現數據庫讀寫分離,
所以肯定會有牽涉到Aop的配置了,所以在spring-mybatis.xml中有如下配置:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd">
<!-- 配置寫數據源 -->
<bean id="masterDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${bridgeli.jdbc.driver}" />
<property name="url" value="${bridgeli.jdbc.url}" />
<property name="username" value="${bridgeli.jdbc.username}" />
<property name="password" value="${bridgeli.jdbc.password}" />
<property name="validationQuery" value="${bridgeli.jdbc.validationQuery}" />
<property name="initialSize" value="1" />
<property name="maxActive" value="20" />
<property name="minIdle" value="0" />
<property name="maxWait" value="60000" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="25200000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="filters" value="stat" />
</bean>
<!-- 配置讀數據源 -->
<bean id="parentSlaveDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close">
<property name="driverClassName" value="${bridgeli.jdbc.driver}" />
<property name="validationQuery" value="${bridgeli.jdbc.validationQuery}" />
<property name="initialSize" value="1" />
<property name="maxActive" value="20" />
<property name="minIdle" value="0" />
<property name="maxWait" value="60000" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<property name="minEvictableIdleTimeMillis" value="25200000" />
<property name="removeAbandoned" value="true" />
<property name="removeAbandonedTimeout" value="1800" />
<property name="logAbandoned" value="true" />
<property name="filters" value="stat" />
</bean>
<bean id="slaveDataSource1" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close" parent="parentSlaveDataSource">
<property name="url" value="${bridgeli_slave1.jdbc.url}" />
<property name="username" value="${bridgeli_slave1.jdbc.username}" />
<property name="password" value="${bridgeli_slave1.jdbc.password}" />
</bean>
<bean id="dataSource" class="cn.bridgeli.datasource.MasterSlaveDataSource">
<property name="targetDataSources">
<map>
<entry key-ref="masterDataSource" value-ref="masterDataSource"/>
<entry key-ref="slaveDataSource1" value-ref="slaveDataSource1"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="masterDataSource"/>
<property name="masterSlaveSelector" ref="dataSelector"/>
</bean>
<bean id="dataSelector" class="cn.bridgeli.datasource.MasterSlaveSelectorByPoll">
<property name="masters">
<list>
<ref bean="masterDataSource"/>
</list>
</property>
<property name="slaves">
<list>
<ref bean="slaveDataSource1"/>
</list>
</property>
<property name="defaultDataSource" ref="masterDataSource"/>
</bean>
<aop:aspectj-autoproxy/>
<!-- mybaits 數據工廠 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="dataSource" ref="dataSource" />
</bean>
<!-- 自動掃描所有注解的路徑 -->
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.bridgeli.mapper" />
<!-- <property name="sqlSessionFactory" ref="sqlSessionFactory" /> -->
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"></property>
</bean>
<!-- 數據庫切面 -->
<bean id="masterSlaveAspect" class="cn.bridgeli.datasource.MasterSlaveAspect">
<property name="prefixMasters">
<list>
<value>save</value>
<value>update</value>
<value>delete</value>
</list>
</property>
</bean>
<aop:config>
<aop:aspect id="c" ref="masterSlaveAspect">
<aop:pointcut id="tx" expression="execution(* cn.bridgeli.service..*.*(..))"/>
<aop:before pointcut-ref="tx" method="before"/>
</aop:aspect>
</aop:config>
<context:annotation-config />
<context:component-scan base-package="cn.bridgeli" />
</beans>
這樣我們就很優雅的利用Spring的Aop實現了對數據庫的讀寫分離,讀的時候走slaveDataSource1這個數據源,寫的時候走masterDataSource這個數據源。
哎,等等,這裡哪裡體現了約定優於配置這一規范,他們怎麼知道哪些方法走讀庫哪些走寫庫?同學你別急,仔細讀讀這個配置文件,你就會發現在第98行,配置了一個MasterSlaveAspect,
也就是說代碼裡面service層(為什麼是service層?)的方法以這裡面配置的這些關鍵字打頭,都將會走寫庫,所以當我們想讓一個方法走主庫的時候,必須在這個地方添加該方法的前綴或者用這裡面已有的前綴,
這就要求我們必須約定好走主庫的方法的打頭,即約定優於配置。