本文是zeus读写分离单元测试,距离分库分表,只有一步了.上代码:
1.ZeusMasterSlaveTest.java
package com.dengliang.zeus.webdemo.test; import java.util.ArrayList; import java.util.List; import org.junit.Assert; import org.junit.Test; import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; import com.dengliang.zeus.webdemo.services.IDajc01Service; import com.dengliang.zeus.webdemo.services.impl.Dajc01Service; import com.dengliang.zeus.webdemo.util.UIDGenerator; import com.dengliang.zeus.webdemo.vo.Dajc01VO; /** * zeus读写分离单元测试 * @author 100755_邓亮 2015年5月8日 * */ public class ZeusMasterSlaveTest { private static ApplicationContext context; /** * 获取bean * * @param id * @return */ public static Object getBean(String id) { if (context == null) { synchronized (id) { context = new ClassPathXmlApplicationContext( "applicationContext-dao.xml", "applicationContext-service.xml", "applicationContext-Master-Slave.xml"); } } return context.getBean(id); } @Test public void slaveTest() { // 只读测试 Dajc01Service service = (Dajc01Service) ZeusMasterSlaveTest.getBean("dajc01Service"); try { List<Dajc01VO> dalist=service.queryAllSystem(); Assert.assertEquals(null, dalist); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } @Test public void masterTest() { //主库上新增,再查询 IDajc01Service service = (IDajc01Service) ZeusMasterSlaveTest.getBean("dajc01Service"); try { List<Dajc01VO> list=new ArrayList(); for (int i = 0; i < 10; i++) { Dajc01VO da001 = new Dajc01VO(); da001.setUuid(UIDGenerator.getUUID()); da001.setSysId(i+""); list.add(da001); } service.addList(list); List<Dajc01VO> dalist=service.queryAllSystem(); Assert.assertNotNull(dalist); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
2.相关配置.
applicationContext-dao.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- DAO --> <bean id="dajc01DAO" class="com.dengliang.zeus.webdemo.dao.Dajc01DAO" parent="zeusdaoSupport"/> </beans>
applicationContext-service.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd "> <!-- BIZ --> <bean id="dajc01Service" class="com.dengliang.zeus.webdemo.services.impl.Dajc01Service"> <property name="dajc01DAO" ref="dajc01DAO" /> </bean> </beans>
applicationContext-Master-Slave.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:p="http://www.springframework.org/schema/p" xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd "> <bean id="parentDataSource" class="com.alibaba.druid.pool.DruidDataSource" destroy-method="close"> <!-- 配置初始化大小、最小、最大 --> <property name="initialSize" value="20" /> <property name="minIdle" value="20" /> <property name="maxIdle" value="30" /> <property name="maxActive" value="50" /> <!-- 配置获取连接等待超时的时间 --> <property name="maxWait" value="0" /> <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 --> <property name="timeBetweenEvictionRunsMillis" value="60000" /> <!-- 配置一个连接在池中最小生存的时间,单位是毫秒 --> <property name="minEvictableIdleTimeMillis" value="300000" /> <property name="validationQuery" value="SELECT 'x'" /> <property name="testWhileIdle" value="true" /> <property name="testOnBorrow" value="false" /> <property name="testOnReturn" value="false" /> <property name="removeAbandoned" value="false" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 --> <property name="poolPreparedStatements" value="true" /> <property name="maxPoolPreparedStatementPerConnectionSize" value="20" /> <property name="filters" value ="stat,wall"></property> </bean> <bean id="master" parent="parentDataSource" destroy-method="close"> <property name="url" value="jdbc:oracle:thin:@10.68.2.23:1521/erpdb" /> <property name="username" value="jlerp_db" /> <property name="password" value="jlerp_db" /> </bean> <bean id="slave" parent="parentDataSource" destroy-method="close"> <property name="url" value="jdbc:oracle:thin:@10.68.2.23:1521/erpdb" /> <property name="username" value="jlerp_f" /> <property name="password" value="fjlerp" /> </bean> <!-- zeus多数据源 --> <bean id="zeusDynamicDataSource" class="com.dengliang.zeus.framework.jdbc.ds.DynamicDataSource"> <property name="targetDataSources"> <map key-type="java.lang.String"> <entry value-ref="master" key="master"></entry> <entry value-ref="slave" key="slave"></entry> </map> </property> <!-- 默认使用读的数据源 --> <property name="defaultTargetDataSource" ref="slave"></property> </bean> <!--daoSupport--> <bean id="zeusdaoSupport" class="com.dengliang.zeus.framework.dao.ZeusDaoSupport"> <property name="dataSource" ref="zeusDynamicDataSource"/> </bean> <bean id = "transactionManager" class="com.dengliang.zeus.framework.jdbc.ds.DbDataSourceTransactionManager"> <property name="dataSource" ref="zeusDynamicDataSource" /> </bean> <!-- 声明式事务配置 --> <tx:advice id="txAdvice" transaction-manager="transactionManager"> <tx:attributes> <tx:method name="do*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/> <tx:method name="del*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/> <tx:method name="save*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/> <tx:method name="add*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/> <tx:method name="begin*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/> <tx:method name="end*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/> <tx:method name="update*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/> <tx:method name="modify*" propagation="REQUIRED" rollback-for="Throwable, Exception, RuntimeException"/> <tx:method name="query*" propagation="NOT_SUPPORTED"/> <tx:method name="load*" propagation="NOT_SUPPORTED"/> <tx:method name="find*" propagation="NOT_SUPPORTED"/> <tx:method name="*" read-only="true" propagation="SUPPORTS" /> </tx:attributes> </tx:advice> <!-- service切面 --> <aop:config> <aop:pointcut expression="execution(* com.dengliang.zeus..services.*.*(..))" id="pointCut" /> <aop:advisor pointcut-ref="pointCut" advice-ref="txAdvice"/> </aop:config> </beans>
代码跑过,达到读写分离要求,
3.读写分离的原理,
就是DbDataSourceTransactionManager.java类.事务管理类
默认数据源是只读,在需要事务的时候,让多数据源切换到主库上,他是线程安全的.
package com.dengliang.zeus.framework.jdbc.ds; import org.springframework.jdbc.datasource.DataSourceTransactionManager; import org.springframework.transaction.TransactionDefinition; /** * 事务管理 * @author 100755_邓亮 2014年1月1日 * */ public class DbDataSourceTransactionManager extends DataSourceTransactionManager{ /** * */ private static final long serialVersionUID = 1749133882378388130L; private static final String Master="master"; @Override protected void doBegin(Object transaction, TransactionDefinition definition) { // TODO Auto-generated method stub DbContextHolder.setDbNum(Master); super.doBegin(transaction, definition); } }