有需求就要想办法解决,最近参与的项目其涉及的三个数据表分别在三台不同的服务器上,这就有点突兀了,第一次遇到这种情况,可这难不倒笔者,资料一查,代码一打,回头看看源码,万事大吉

1. 预备知识

这里默认大家都会SSM框架了,使用时我们要往sqlSessionFactory里注入数据源。那么猜测:1、可以往sqlSessionFactory里注入多数据源来实现切换;2、将多个数据源封装成一个总源,再把这个总源注入到sqlSessionFactory里实现切换。答案是使用后者,即封装成总源的形式。Spring提供了动态切换数据源的功能,那么我们来看看其实现原理

2. 实现原理

笔者是根据源码讲解的,这些步骤讲完会贴出源码内容

一、

Spring提供了AbstractRoutingDataSource抽象类,其继承了AbstractDataSource。而AbstractDataSource又实现了DataSource。因此我们可以将AbstractRoutingDataSource的实现类注入到sqlSessionFactory中来实现切换数据源

二、

刚才我们将多个数据源封装成总源的想法在AbstractRoutingDataSource中有体现,其内部用一个Map集合封装多个数据源,即 private Map<Object, DataSource> resolvedDataSources; ,那么要使用时从该Map集合中获取即可

三、

AbstractRoutingDataSource中有个determineTargetDataSource()方法,其作用是决定使用哪个数据源。我们通过determineTargetDataSource()方法从Map集合中获取数据源,那么必须有个key值指定才行。所以determineTargetDataSource()方法内部通过调用determineCurrentLookupKey()方法来获取key值,Spring将determineCurrentLookupKey()方法抽象出来给用户实现,从而让用户决定使用哪个数据源

四、

既然知道我们需要重写determineCurrentLookupKey()方法,那么就开始把。实现时发现该方法没有参数,我们无法传参来决定返回的key值,又不能改动方法(因为是重写),所以方法内部调用我们自定义类的静态方法即可解决问题

public class DynamicDataSource extends AbstractRoutingDataSource {
@Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceHolder.getDataSourceKey();
}
}

五、

自定义类,作用是让我们传入key值来决定使用哪个key

public class DynamicDataSourceHolder {

    // ThreadLocal没什么好说的,绑定当前线程
private static final ThreadLocal<String> dataSourceKey = new ThreadLocal<String>(); public static String getDataSourceKey(){
return dataSourceKey.get();
} public static void setDataSourceKey(String key){
dataSourceKey.set(key);
} public static void clearDataSourceKey(){
dataSourceKey.remove();
}
}

六、

AbstractRoutingDataSource抽象类源码(不喜可跳

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {
@Nullable
private Map<Object, Object> targetDataSources;
@Nullable
private Object defaultTargetDataSource;
private boolean lenientFallback = true;
private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
@Nullable
private Map<Object, DataSource> resolvedDataSources;
@Nullable
private DataSource resolvedDefaultDataSource; public AbstractRoutingDataSource() {
} public void setTargetDataSources(Map<Object, Object> targetDataSources) {
this.targetDataSources = targetDataSources;
} public void setDefaultTargetDataSource(Object defaultTargetDataSource) {
this.defaultTargetDataSource = defaultTargetDataSource;
} public void setLenientFallback(boolean lenientFallback) {
this.lenientFallback = lenientFallback;
} public void setDataSourceLookup(@Nullable DataSourceLookup dataSourceLookup) {
this.dataSourceLookup = (DataSourceLookup)(dataSourceLookup != null ? dataSourceLookup : new JndiDataSourceLookup());
} public void afterPropertiesSet() {
if (this.targetDataSources == null) {
throw new IllegalArgumentException("Property 'targetDataSources' is required");
} else {
this.resolvedDataSources = new HashMap(this.targetDataSources.size());
this.targetDataSources.forEach((key, value) -> {
Object lookupKey = this.resolveSpecifiedLookupKey(key);
DataSource dataSource = this.resolveSpecifiedDataSource(value);
this.resolvedDataSources.put(lookupKey, dataSource);
});
if (this.defaultTargetDataSource != null) {
this.resolvedDefaultDataSource = this.resolveSpecifiedDataSource(this.defaultTargetDataSource);
} }
} protected Object resolveSpecifiedLookupKey(Object lookupKey) {
return lookupKey;
} protected DataSource resolveSpecifiedDataSource(Object dataSource) throws IllegalArgumentException {
if (dataSource instanceof DataSource) {
return (DataSource)dataSource;
} else if (dataSource instanceof String) {
return this.dataSourceLookup.getDataSource((String)dataSource);
} else {
throw new IllegalArgumentException("Illegal data source value - only [javax.sql.DataSource] and String supported: " + dataSource);
}
} public Connection getConnection() throws SQLException {
return this.determineTargetDataSource().getConnection();
} public Connection getConnection(String username, String password) throws SQLException {
return this.determineTargetDataSource().getConnection(username, password);
} public <T> T unwrap(Class<T> iface) throws SQLException {
return iface.isInstance(this) ? this : this.determineTargetDataSource().unwrap(iface);
} public boolean isWrapperFor(Class<?> iface) throws SQLException {
return iface.isInstance(this) || this.determineTargetDataSource().isWrapperFor(iface);
} protected DataSource determineTargetDataSource() {
Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
Object lookupKey = this.determineCurrentLookupKey();
DataSource 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 + "]");
} else {
return dataSource;
}
} @Nullable
protected abstract Object determineCurrentLookupKey();
}

3. 配置

3.1 配置db.properties

这里配置两个数据库,一个评论库,一个用户库

# 问题库
howl.comments.driverClassName = com.mysql.jdbc.Driver
howl.comments.url = jdbc:mysql://127.0.0.1:3306/comment
howl.comments.username = root
howl.comments.password = # 用户库
howl.users.driverClassName = com.mysql.jdbc.Driver
howl.users.url = jdbc:mysql://127.0.0.1:3306/user
howl.users.username = root
howl.users.password =

3.2 配置applicationContext.xml

<!--  加载properties文件  -->
<context:property-placeholder location="classpath:db.properties"></context:property-placeholder> <!-- 问题的数据源 -->
<bean id="commentsDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${howl.comments.driverClassName}"></property>
<property name="url" value="${howl.comments.url}"></property>
<property name="username" value="${howl.comments.username}"></property>
<property name="password" value="${howl.comments.password}"></property>
</bean> <!-- 用户的数据源 -->
<bean id="usersDataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${howl.users.driverClassName}"></property>
<property name="url" value="${howl.users.url}"></property>
<property name="username" value="${howl.users.username}"></property>
<property name="password" value="${howl.users.password}"></property>
</bean> <!-- 通过setter方法,往DynamicDataSource的Map集合中注入数据 -->
<!-- 具体参数,看名字可以明白 -->
<bean id="dynamicDataSource" class="com.howl.util.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="cds" value-ref="commentsDataSource"/>
<entry key="uds" value-ref="usersDataSource"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="commentsDataSource"></property>
</bean> <!-- 将`总源`注入SqlSessionFactory工厂 -->
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="dynamicDataSource"></property>
</bean>

因为dynamicDataSource是继承AbstractRoutingDataSource,所以setter注入方法得去父类里面去找,开始笔者也是懵了一下

3.3 切换数据源

数据源是在Service层切换的

UserService

@Service
public class UserService { @Autowired
private UserDao userDao; public User selectUserById(int id) { // 表明使用usersDataSource库
DynamicDataSourceHolder.setDataSourceKey("uds");
return userDao.selectUserById(id);
}
}

CommentService

@Service
public class CommentService { @Autowired
CommentDao commentDao; public List<Comment> selectCommentById(int blogId) { // 表明使用评论库
DynamicDataSourceHolder.setDataSourceKey("cds");
return commentDao.selectCommentById(blogId, -1);
}
}

3.4 自动切换

手动切换容易忘记,我们学了AOP可以使用AOP来切换,这里使用注解实现

<!-- 开启AOP注解支持 -->
<aop:aspectj-autoproxy></aop:aspectj-autoproxy>

切面类

@Component
@Aspect
public class DataSourceAspect { @Pointcut("execution(* com.howl.service.impl.*(..))")
private void pt1() {
} @Around("pt1()")
public Object around(ProceedingJoinPoint pjp) { Object rtValue = null;
try {
String name = pjp.getTarget().getClass().getName();
if (name.equals("com.howl.service.UserService")) {
DynamicDataSourceHolder.setDataSourceKey("uds");
}
if (name.equals("com.howl.service.CommentService")){
DynamicDataSourceHolder.setDataSourceKey("cds");
}
// 调用业务层方法
rtValue = pjp.proceed(); System.out.println("后置通知");
} catch (Throwable t) {
System.out.println("异常通知");
t.printStackTrace();
} finally {
System.out.println("最终通知");
}
return rtValue;
}
}

使用环绕通知实现切入com.howl.service.impl里的所有方法,在遇到UserService、CommentService时,前置通知动态切换对应的数据源

4. 总结

  1. 以前笔者认为Service层多了impl包和接口是多余的,现在要用到AOP的时候后悔莫及,所以默认结构如此肯定有道理的
  2. 出bug的时候,才知道分步测试哪里出问题了,如果TDD推动那么能快速定位报错地方,日志也很重要

参考

https://www.jianshu.com/p/d97cd60e404f

SSM动态切换数据源的更多相关文章

  1. ssm 动态切换数据源

    1,添加数据库配置 jdbc.driver=com.microsoft.sqlserver.jdbc.SQLServerDriver #jdbc.url=jdbc:sqlserver://192.16 ...

  2. Spring动态切换数据源及事务

    前段时间花了几天来解决公司框架ssm上事务问题.如果不动态切换数据源话,直接使用spring的事务配置,是完全没有问题的.由于框架用于各个项目的快速搭建,少去配置各个数据源配置xml文件等.采用了动态 ...

  3. Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源 方法

    一.开篇 这里整合分别采用了Hibernate和MyBatis两大持久层框架,Hibernate主要完成增删改功能和一些单一的对象查询功能,MyBatis主要负责查询功能.所以在出来数据库方言的时候基 ...

  4. hibernate动态切换数据源

    起因: 公司的当前产品,主要是两个项目集成的,一个是java项目,还有一个是php项目,两个项目用的是不同的数据源,但都是mysql数据库,因为java这边的开发工作已经基本完成了,而php那边任务还 ...

  5. Spring + Mybatis 项目实现动态切换数据源

    项目背景:项目开发中数据库使用了读写分离,所有查询语句走从库,除此之外走主库. 最简单的办法其实就是建两个包,把之前数据源那一套配置copy一份,指向另外的包,但是这样扩展很有限,所有采用下面的办法. ...

  6. 在使用 Spring Boot 和 MyBatis 动态切换数据源时遇到的问题以及解决方法

    相关项目地址:https://github.com/helloworlde/SpringBoot-DynamicDataSource 1. org.apache.ibatis.binding.Bind ...

  7. Spring3.3 整合 Hibernate3、MyBatis3.2 配置多数据源/动态切换数据源方法

    一.开篇 这里整合分别采用了Hibernate和MyBatis两大持久层框架,Hibernate主要完成增删改功能和一些单一的对象查询功能,MyBatis主要负责查询功能.所以在出来数据库方言的时候基 ...

  8. Spring学习总结(16)——Spring AOP实现执行数据库操作前根据业务来动态切换数据源

    深刻讨论为什么要读写分离? 为了服务器承载更多的用户?提升了网站的响应速度?分摊数据库服务器的压力?就是为了双机热备又不想浪费备份服务器?上面这些回答,我认为都不是错误的,但也都不是完全正确的.「读写 ...

  9. Spring+Mybatis动态切换数据源

    功能需求是公司要做一个大的运营平台: 1.运营平台有自身的数据库,维护用户.角色.菜单.部分以及权限等基本功能. 2.运营平台还需要提供其他不同服务(服务A,服务B)的后台运营,服务A.服务B的数据库 ...

随机推荐

  1. MOOC(13) - 连接数据库

    散装代码 # -*- coding: utf-8 -*- # @Time : 2020/2/18 9:00 # @File : connect_db_21.py # @Author: Hero Liu ...

  2. 吴裕雄--天生自然 JAVASCRIPT开发学习:对象 实例(2)

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title> ...

  3. 吴裕雄--天生自然 HADOOP大数据分布式处理:使用WinSCP连接本机与虚拟机

  4. 安装与使用django-restframework

    django-restframework 一.安装与使用 1.安装 >: pip3 install djangorestframework 2.使用 在settings.py中注册: INSTA ...

  5. MS15-020 文件共享漏洞利用

    Metasploit MS15-020漏洞利用 环境: 共享机/受害者:windows xp IP:192.168.222.1 攻击机:kali linux IP:192.168.10.1 msfco ...

  6. flink 基本原理

    state状态操作 https://ci.apache.org/projects/flink/flink-docs-release-1.8/concepts/programming-model.htm ...

  7. 64位WIN7 配置IIS遇到问题

    App_global.asax.sr8_llzl.dll' -- '拒绝访问. 设置c:windows\temp 目录访问权限 temp--> 属性-->安全-- > 添加IIS_I ...

  8. 【Linux_Shell 脚本编程学习笔记一、条件表达式】

    条件表达式返回的结果都为布尔型 真为1,假为0 条件测试的表达式 [expression] 比较符 整数比较 -eq:比较两个整数是否相等,$A -eq $B -ne:测试两个整数是否不等,不等则为真 ...

  9. 2015-09-14-初级vector

    标准库vector类型 #include<vector> using std::vector; vector为一个类模板. vector的初始化 vector<T> v1; v ...

  10. C语言标准库 常用函数说明

    void *memset(void *str, int c, size_t n) Syntax void *memset(void *str, int c, size_t n) Description ...