作者:suroot

spring动态配置多数据源,即在大型应用中对数据进行切分,并且采用多个数据库实例进行管理,这样可以有效提高系统的水平伸缩性。而这样的方案就会不同于常见的单一数据实例的方案,这就要程序在运行时根据当时的请求及系统状态来动态的决定将数据存储在哪个数据库实例中,以及从哪个数据库提取数据。
Spring2.x以后的版本中采用Proxy模式,就是我们在方案中实现一个虚拟的数据源,并且用它来封装数据源选择逻辑,这样就可以有效地将数据源选择逻辑从Client中分离出来。Client提供选择所需的上下文(因为这是Client所知道的),由虚拟的DataSource根据Client提供的上下文来实现数据源的选择。
实现
具体的实现就是,虚拟的DataSource仅需继承AbstractRoutingDataSource实现determineCurrentLookupKey()在其中封装数据源的选择逻辑。

1.动态配置多数据源
数据源的名称常量和一个获得和设置上下文环境的类,主要负责改变上下文数据源的名称

public class DataSourceContextHolder {

    public static final String DATA_SOURCE_A = "dataSourceA";
public static final String DATA_SOURCE_B = "dataSourceB";
public static final String DATA_SOURCE_C = "dataSourceC";
public static final String DATA_SOURCE_D = "dataSourceD";
// 线程本地环境
private static final ThreadLocal<String> contextHolder = new ThreadLocal<String>(); // 设置数据源类型
public static void setDbType(String dbType) {
// System.out.println("此时切换的数据源为:"+dbType);
contextHolder.set(dbType);
}
// 获取数据源类型
public static String getDbType() {
return (contextHolder.get());
}
// 清除数据源类型
public static void clearDbType() {
contextHolder.remove();
}
}

2.建立动态数据源类,注意,这个类必须继承AbstractRoutingDataSource,且实现方法determineCurrentLookupKey,该方法返回一个Object,一般是返回字符串:

public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
System.out.println("此时获取到的数据源为:"+DataSourceContextHolder.getDbType());
return DataSourceContextHolder.getDbType();
}
}

为了更好的理解为什么会切换数据源,我们来看一下AbstractRoutingDataSource.java源码,源码中确定数据源部分主要内容如下

    /**
* Retrieve the current target DataSource. Determines the
* {@link #determineCurrentLookupKey() current lookup key}, performs
* a lookup in the {@link #setTargetDataSources targetDataSources} map,
* falls back to the specified
* {@link #setDefaultTargetDataSource default target DataSource} if necessary.
* @see #determineCurrentLookupKey()
*/
protected DataSource determineTargetDataSource() {
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
Object lookupKey = determineCurrentLookupKey();
DataSource dataSource = this.resolvedDataSources.get(lookupKey);
if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
dataSource = this.resolvedDefaultDataSource;
}
if (dataSource == null) {
throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
}
return dataSource;
}

上面这段源码的重点在于determineCurrentLookupKey()方法,这是AbstractRoutingDataSource类中的一个抽象方法,而它的返回值是你所要用的数据源dataSource的key值,有了这个key值,resolvedDataSource(这是个map,由配置文件中设置好后存入的)就从中取出对应的DataSource,如果找不到,就用配置默认的数据源。
看完源码,应该有点启发了吧,没错!你要扩展AbstractRoutingDataSource类,并重写其中的determineCurrentLookupKey()方法,来实现数据源的切换:

3.编写spring的配置文件配置多个数据源

    <!-- 配置数据源  dataSourceA-->
<bean name="dataSourceA" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" /> <!-- 初始化连接大小 -->
<property name="initialSize" value="20" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="500" />
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="0" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" /> <property name="validationQuery" value="${validationQuery}" />
<property name="testOnBorrow" value="true" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize" value="1000" /> <!-- 监控数据库 -->
<!--<property name="filters" value="stat,log4j"/>-->
<property name="filters" value="stat" />
</bean> <!-- 配置数据源 dataSourceB -->
<bean name="dataSourceB" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_new_url}" />
<property name="username" value="${jdbc_new_username}" />
<property name="password" value="${jdbc_new_password}" />
<property name="initialSize" value="10" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="10" />
<property name="minIdle" value="0" />
<property name="maxWait" value="10000" />
<property name="validationQuery" value="${validationQuery3}" />
<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> <!-- 配置数据源 dataSourceC -->
<bean name="dataSourceC" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
<property name="url" value="${jdbc_mysql_url}" />
<property name="username" value="${jdbc_mysql_username}" />
<property name="password" value="${jdbc_mysql_password}" />
<property name="initialSize" value="10" />
<property name="maxActive" value="100" />
<property name="maxIdle" value="10" />
<property name="minIdle" value="0" />
<property name="maxWait" value="60000" />
<property name="validationQuery" value="${validationQuery2}" />
<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="dataSource" class="top.suroot.base.datasource.DynamicDataSource">
<!-- 默认使用dataSourceA的数据源 -->
<property name="defaultTargetDataSource" ref="dataSourceA"></property>
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry value-ref="dataSourceA" key="dataSourceA"></entry>
<entry value-ref="dataSourceB" key="dataSourceB"></entry>
<entry value-ref="dataSourceC" key="dataSourceC"></entry>
<!--<entry value-ref="dataSourceD" key="dataSourceD"></entry>-->
</map>
</property>
</bean>

在这个配置中第一个property属性配置目标数据源,<map key-type="java.lang.String">中的key-type必须要和静态键值对照类DataSourceConst中的值的类型相 同;<entry key="User" value-ref="userDataSource"/>中key的值必须要和静态键值对照类中的值相同,如果有多个值,可以配置多个< entry>标签。第二个property属性配置默认的数据源。

4.动态切换是数据源

DataSourceContextHolder.setDbType(DataSourceContextHolder.DATA_SOURCE_B);

以上讲的是怎么动态切换数据源,下面要说下自定义注解、aop方式自动切换数据源 ,感兴趣的可以继续往下看看
5.配置自定义注解

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE,ElementType.METHOD})
public @interface DynamicDataSourceAnnotation {
//dataSource 自定义注解的参数
String dataSource() default DataSourceContextHolder.DATA_SOURCE_A;
}

6.配置切面类

@Aspect
@Component
@Order(1)
public class DynamicDataSourceAspect { @Before("@annotation(top.suroot.base.aop.DynamicDataSourceAnnotation)") //前置通知
public void testBefore(JoinPoint point){
//获得当前访问的class
Class<?> className = point.getTarget().getClass();
DynamicDataSourceAnnotation dataSourceAnnotation = className.getAnnotation(DynamicDataSourceAnnotation.class);
if (dataSourceAnnotation != null ) {
//获得访问的方法名
String methodName = point.getSignature().getName();
//得到方法的参数的类型
Class[] argClass = ((MethodSignature)point.getSignature()).getParameterTypes();
String dataSource = DataSourceContextHolder.DATA_SOURCE_A;
try {
Method method = className.getMethod(methodName, argClass);
if (method.isAnnotationPresent(DynamicDataSourceAnnotation.class)) {
DynamicDataSourceAnnotation annotation = method.getAnnotation(DynamicDataSourceAnnotation.class);
dataSource = annotation.dataSource();
System.out.println("DataSource Aop ====> "+dataSource);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
DataSourceContextHolder.setDbType(dataSource);
} } @After("@annotation(top.suroot.base.aop.DynamicDataSourceAnnotation)") //后置通知
public void testAfter(JoinPoint point){
//获得当前访问的class
Class<?> className = point.getTarget().getClass();
DynamicDataSourceAnnotation dataSourceAnnotation = className.getAnnotation(DynamicDataSourceAnnotation.class);
if (dataSourceAnnotation != null ) {
//获得访问的方法名
String methodName = point.getSignature().getName();
//得到方法的参数的类型
Class[] argClass = ((MethodSignature)point.getSignature()).getParameterTypes();
String dataSource = DataSourceContextHolder.DATA_SOURCE_A;
try {
Method method = className.getMethod(methodName, argClass);
if (method.isAnnotationPresent(DynamicDataSourceAnnotation.class)) {
DynamicDataSourceAnnotation annotation = method.getAnnotation(DynamicDataSourceAnnotation.class);
dataSource = annotation.dataSource();
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(dataSource != null && !DataSourceContextHolder.DATA_SOURCE_A.equals(dataSource)) DataSourceContextHolder.clearDbType();
}
}
}

7.在切入点添加自定义的注解

@Service("baseService")
@DynamicDataSourceAnnotation
public class BaseServiceImpl implements BaseService { @DynamicDataSourceAnnotation(dataSource = DataSourceContextHolder.DATA_SOURCE_B)
public void changeDataSource() {
System.out.println("切换数据源serviceImple");
}
}

8.当然注解扫描、和aop代理一定要在配置文件中配好

    <!-- 自动扫描(bean注入) -->
<context:component-scan base-package="top.suroot.*" />
<!-- AOP自动代理功能 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>

大功告成,当调用changeDataSource方法的时候会进入切面类中切换数据源,方法调用完毕会把数据源切换回来。

作者:suroot
链接:http://www.jianshu.com/p/ddebf4ae57c1
來源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出

最新文章

  1. WPF 显示文件列表中使用 ListBox 变到ListView 最后使用DataGrid
  2. 使用Wireshark 抓取数据包
  3. Evolutionary Computing: 2. Genetic Algorithm(1)
  4. 基于DDD的.NET开发框架 - ABP分层设计
  5. [Hibernate] - Annotations - One To One
  6. 扯一扯前端css的整体架构设计:(2)base基础类的那些事儿
  7. EXCLE中快速插入图片
  8. android 通过工具抓包
  9. (function(){})()这个是什么?有不明觉厉的感觉么?
  10. (转)myrepo
  11. JQuery简单实现图片轮播效果
  12. 初学 Play Framework 以及可能遇到的问题
  13. EMCA创建em资料库时报错
  14. C++内存分配和拷贝构造函数写研究
  15. sass 安装和使用
  16. 老司机教你下载tumblr上视频和图片的正确姿势
  17. 关于 IdentityServer4 中的 Jwt Token 与 Reference Token
  18. nginx set变量后lua无法改值
  19. CentOS 6下gcc升级的操作记录(由默认的4.4.7升级到6.4.0版本)
  20. vue中的iviewUI导出1W条列表数据每次只导出2000条的逻辑

热门文章

  1. Win10 系统直接在目录下打开cmd
  2. 第十一节:Asp.Net Core 之内容缓存(IMemoryCache)
  3. Python 监控脚本
  4. 50道Java线程面试题分析及答案
  5. vim:spell语法
  6. Windows定时清理文件处理脚本
  7. 使用JavaConfig配置SpringMVC
  8. MySQL基础-2
  9. python json库
  10. Springboot vue.js html 跨域 前后分离 Activiti6 shiro 权限