java-durid、mybatis、spring 整合基于 AbstractRoutingDataSource 的多数据源读写分离配置

java-durid、mybatis、spring 整合基于 AbstractRoutingDataSource 的多数据源读写分离配置


一、定义用于标记使用那个数据源的注解
package me.utils.database;


import java.lang.annotation.ElementType;  
import java.lang.annotation.Target;  
import java.lang.annotation.Retention;  
import java.lang.annotation.RetentionPolicy;  
/** 
 * 定义 @DataSource 注解,标记当前使用的数据源
 * @version 创建时间:2017年4月17日 上午11:02:07
 */
@Retention(RetentionPolicy.RUNTIME)  
@Target(ElementType.METHOD)  
public @interface DataSource {
	String value();
}




二、定义一个切入点,用于在执行数据连接前通过读取 @DataSource 注解,动态选择数据源
package me.utils.database;


import java.lang.reflect.Method;
import org.aspectj.lang.JoinPoint;


import org.aspectj.lang.reflect.MethodSignature;
import org.aspectj.weaver.ast.Var;


import com.sun.tools.javac.resources.javac;


/**
 * 多数据源切换切入点。解析  @DataSource 注解
 * @version 创建时间:2017年4月17日 上午11:08:08
 */
public class DataSourceAspect {


	/**
	 * 在dao层方法之前调用,获取  @DataSource 注解标记的数据源名称
	 */
	public void before(JoinPoint point) {
		Object target = point.getTarget();
		String methodName = point.getSignature().getName();
        MethodSignature methodSignature = (MethodSignature) point.getSignature();  
        Method method = methodSignature.getMethod();  
		//获取方法上的 @DataSource 注解
		DataSource annotation = (DataSource) method.getAnnotation(DataSource.class);
		String dataSourceName = null;
        if (annotation != null) {
        	//获得注解标记的数据源名称
        	dataSourceName = annotation.value();
	        HandleDataSource.putDataSource(dataSourceName);
        }
		printMsg("方法={0};使用数据源={1}",methodName,annotation);
        
	}
	
	void printMsg(String pattern, Object... arguments){
		System.out.println(java.text.MessageFormat.format(pattern,arguments));
	}
}




三、定义一个记录当前线程使用的数据源的对象,管理多线程下的变量存储(线程安全)
package me.utils.database;
/** 
*保存当前线程数据源使用的数据源名
* @version 创建时间:2017年4月17日 上午11:07:25
*/
public class HandleDataSource {
	/**
	 * 保存当前线程使用的数据源名称
	 */
	public static final ThreadLocal<String> holder = new ThreadLocal<String>();  
    /**  
     * 绑定当前线程数据源路由的key     
     * @param key  
     */  
    public static void putDataSource(String datasource) {  
        holder.set(datasource);


		System.out.println(java.text.MessageFormat.format("线程[{0}],设置使用的数据源={1}"
				,Thread.currentThread().getName()
				,datasource));
    }  
      
    /**  
     * 获取当前线程的数据源路由的key  
     * @return  
     */  
    public static String getDataSource() {  
		System.out.println(java.text.MessageFormat.format("线程[{0}],获取使用的数据源={1}"
				,Thread.currentThread().getName()
				,holder.get()));
        return holder.get();  
    }     
}




四、基于 spring 的多数据源选择对象

package me.utils.database;


import java.util.Map;
import java.util.function.BiConsumer;


import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;


/**
 * 继承 AbstractRoutingDataSource 实现自己的路由数据源
 * @version 创建时间:2017年4月17日 上午11:05:20
 */
public class ChooseDataSource extends AbstractRoutingDataSource  {
	
	@Override
	protected Object determineCurrentLookupKey() {
		//返回当前线程使用的数据源名称
		return  HandleDataSource.getDataSource();  
	}
	
	@Override
	public void setTargetDataSources(Map<Object, Object> targetDataSources) {
		super.setTargetDataSources(targetDataSources);
		
		System.out.println("配置读写分离数据源:");
		//设置目标数据源
		if(targetDataSources!=null){
			targetDataSources.forEach((key,val)->{
				System.out.println(java.text.MessageFormat.format("数据源:{0}={1}", key,val));
			});
		}
	}
	
}




五、 mybatis 的配置(其中的顺序很关键)
<?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"
	xmlns:beans="http://www.springframework.org/schema/beans"
	xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans-4.2.xsd        
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context-4.2.xsd
        http://www.springframework.org/schema/tx 
     	http://www.springframework.org/schema/tx/spring-tx-4.2.xsd
     	http://www.springframework.org/schema/aop 
		http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">


	<!-- 1.数据源配置 (druid 数据源 (参考:https://github.com/alibaba/druid/wiki/DruidDataSource%E9%85%8D%E7%BD%AE%E5%B1%9E%E6%80%A7%E5%88%97%E8%A1%A8) -->
	<!-- 1.1 写库 -->
	<bean id="dataSource_druid_write" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
      <!-- 基本属性 url、user、password -->
      <property name="url" value="${jdbc.url}" />
      <property name="username" value="${jdbc.username}" />
      <property name="password" value="${jdbc.password}" />
        
      <!-- 配置初始化大小、最小、最大 -->
      <property name="initialSize" value="1" />
      <property name="minIdle" value="1" /> 
      <property name="maxActive" value="20" />
   
      <!-- 配置获取连接等待超时的时间 -->
      <property name="maxWait" value="60000" />
   
      <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
      <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" />
   
      <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
      <property name="poolPreparedStatements" value="true" />
      <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
   
      <!-- 配置监控统计拦截的filters -->
      <property name="filters" value="mergeStat" /> 
      <!-- 合并多个DruidDataSource的监控数据 -->
	  <property name="useGlobalDataSourceStat" value="true" />
	  <!-- 慢SQL记录监控 -->
	  <property name="connectionProperties" value="druid.stat.slowSqlMillis=5000" />
  	</bean>
	<!-- 1.2 只读库 -->
	<bean id="dataSource_druid_read" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
      <!-- 基本属性 url、user、password -->
      <property name="url" value="${jdbc.url}" />
      <property name="username" value="${jdbc.username}" />
      <property name="password" value="${jdbc.password}" />
        
      <!-- 配置初始化大小、最小、最大 -->
      <property name="initialSize" value="1" />
      <property name="minIdle" value="1" /> 
      <property name="maxActive" value="20" />
   
      <!-- 配置获取连接等待超时的时间 -->
      <property name="maxWait" value="60000" />
   
      <!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
      <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" />
   
      <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
      <property name="poolPreparedStatements" value="true" />
      <property name="maxPoolPreparedStatementPerConnectionSize" value="20" />
   
      <!-- 配置监控统计拦截的filters -->
      <property name="filters" value="mergeStat" /> 
      <!-- 合并多个DruidDataSource的监控数据 -->
	  <property name="useGlobalDataSourceStat" value="true" />
	  <!-- 慢SQL记录监控 -->
	  <property name="connectionProperties" value="druid.stat.slowSqlMillis=5000" />
  	</bean>
  	<!-- 1.3 动态读写分离数据源 -->
    <bean id="demoDataSource" class="me.utils.database.ChooseDataSource">  
        <property name="targetDataSources">      
          <map key-type="java.lang.String">      
             <!-- 写库 -->    
             <entry key="write" value-ref="dataSource_druid_write"/>      
             <!-- 读库 -->    
             <entry key="read" value-ref="dataSource_druid_read"/>      
          </map>
        </property>   
        <!-- 设置默认数据源 -->
        <property name="defaultTargetDataSource" ref="dataSource_druid_write"/>      
      
    </bean> 	
	<!-- 2  注册 mybatis sqlmapper 映射-->
	<!-- 2.1  spring 自动注册 mybatis 的映射文件配置 (mappers.mapper 节点内容) -->
	<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
		<!-- 映射文件中,mapper.namespace 属性值“接口”的基础包名 -->
		<property name="basePackage" value="me.daos" />
		<property name="sqlSessionFactoryBeanName" value="sqlSessionFactory" />
	</bean>
	
    <!-- 2.2 sping 自动注册 mappers.mapper 文件,并将对应的接口注册到ioc容器(即:使用接口实例访问数据库) -->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
    	<!-- 使用的数据源 -->
        <property name="dataSource" ref="demoDataSource" />
        <!-- 如需添加特殊映射文件,可以写到下面的  xml 文件中 -->
        <property name="configLocation"  value="classpath:mybatis-config/sqlMapConfig.xml"/>
        <!-- 自动扫描mapping.xml文件,**表示迭代查找,也可在sqlMapConfig.xml中单独指定xml文件-->
        <property name="mapperLocations" value="classpath:mappers/**/*Mapper.xml" />
    </bean>
    
    <!-- 2.3 使用 sqlSessionFactory 管理 sqlsession 对象,并注册到ioc容器(即:使用 sqlSession.selectList() 等方式访问数据库)-->
    <bean id="sqlSessionTemplate" class="org.mybatis.spring.SqlSessionTemplate">
        <constructor-arg index="0" ref="sqlSessionFactory"></constructor-arg>
    </bean>


	<!-- 3 事务管理 -->
    <!-- 3.1 注册(事务管理)transaction manager, use JtaTransactionManager for global tx -->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    	<!-- 管理的数据源 -->
        <property name="dataSource" ref="demoDataSource" />
    </bean>
    <!-- 3.2 开启使用annotation(注解)控制事务,基于类的事务将启用,默认为“基于接口的代理将起作用” -->
	<tx:annotation-driven transaction-manager="transactionManager" proxy-target-class="true"/>
	
	<!-- 4 切入点,用于通过 @DataSource 注解获取当前方法使用的数据源名称 -->
	<!-- 4.1 注册ioc组件  -->   
    <bean id="dataSourceAspect" class="me.utils.database.DataSourceAspect" />
    <!-- 4.2 配置切入点,在指定包下生效 -->    
    <aop:config proxy-target-class="true">    
    	<!-- 调整aop 执行排序级别 -->
        <aop:aspect id="dataSourceAspect" ref="dataSourceAspect" order="1">
        	<!-- 标记 aop 起作用的包
        		星号作用:第一个*标记任何返回值类型;第二个*标记任何类型;第三个*标记任何方法;
        		双点儿所用:第一个..标记任何子包;第二个..标记任何参数;
        	 -->
            <aop:pointcut id="tx" expression="execution(* me.dals..*.*(..)) "/>
            <!-- 在调用标有 @DataSource注解  的方法之前,执行 me.utils.database.DataSourceAspect.before() 方法 -->
            <aop:before pointcut-ref="tx" method="before" />                
        </aop:aspect>    
    </aop:config>  
</beans>



六、调用测试
package me.dals;


import java.util.List;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;


import me.daos.UserInfoDao;
import me.exceptions.RollbackException;
import me.models.UserInfoModel;


/** 
*
* @version 创建时间:2017年4月11日 下午4:16:54
*/
@Repository
public class UserInfoDal {
	/**
	 * 注入 UserInfoDao 实例
	 */
	@Autowired
	UserInfoDao dao;
	/**
	 * 注入 SqlSessionTemplate 实例
	 */
	@Autowired
	org.mybatis.spring.SqlSessionTemplate sqlSession;
	
	/**
	 * 使用接口形式查询数据库
	 * @return
	 */
	@me.utils.database.DataSource(value="write")
	public List<UserInfoModel> getAllUseMapper() {
		List<UserInfoModel> list = dao.getAll();
		return list;
	}
	/**
	 * 使用 sqlsession 形式查询数据库
	 * @return
	 */
	@me.utils.database.DataSource(value="read")
	public List<UserInfoModel> getAllUseSqlSession() {
		List<UserInfoModel> list = sqlSession.selectList("me.daos.UserInfoDao.getAll");
		
		return list;
	}
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值