一种实现Spring动态数据源切换的方法 | 京东云技术团队
1 目标
不在现有查询代码逻辑上做任何改动,实现dao维度的数据源切换(即表维度)
2 使用场景
节约bdp的集群资源。接入新的宽表时,通常uat验证后就会停止集群释放资源,在对应的查询服务器uat环境时需要查询的是生产库的表数据(uat库表因为bdp实时任务停止,没有数据落入),只进行服务器配置文件的改动而无需进行代码的修改变更,即可按需切换查询的数据源。
2.1 实时任务对应的集群资源
2.2 实时任务产生的数据进行存储的两套环境
2.3 数据使用系统的两套环境(查询展示数据)
即需要在zhongyouex-bigdata-uat中查询生产库的数据。
3 实现过程
3.1 实现重点
- org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
spring提供的这个类是本次实现的核心,能够让我们实现运行时多数据源的动态切换,但是数据源是需要事先配置好的,无法动态的增加数据源。 - Spring提供的Aop拦截执行的mapper,进行切换判断并进行切换。
注:另外还有一个就是ThreadLocal类,用于保存每个线程正在使用的数据源。
3.2 AbstractRoutingDataSource解析
public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean{ @Nullable private Map<Object, Object> targetDataSources; @Nullable private Object defaultTargetDataSource; @Override public Connection getConnection() throws SQLException { return determineTargetDataSource().getConnection(); } 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; } @Override public void afterPropertiesSet() { if (this.targetDataSources == null) { throw new IllegalArgumentException("Property 'targetDataSources' is required"); } this.resolvedDataSources = new HashMap<>(this.targetDataSources.size()); this.targetDataSources.forEach((key, value) -> { Object lookupKey = resolveSpecifiedLookupKey(key); DataSource dataSource = resolveSpecifiedDataSource(value); this.resolvedDataSources.put(lookupKey, dataSource); }); if (this.defaultTargetDataSource != null) { this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource); } }
从上面源码可以看出它继承了AbstractDataSource,而AbstractDataSource是javax.sql.DataSource的实现类,拥有getConnection()方法。获取连接的getConnection()方法中,重点是determineTargetDataSource()方法,它的返回值就是你所要用的数据源dataSource的key值,有了这个key值,resolvedDataSource(这是个map,由配置文件中设置好后存入targetDataSources的,通过targetDataSources遍历存入该map)就从中取出对应的DataSource,如果找不到,就用配置默认的数据源。
看完源码,我们可以知道,只要扩展AbstractRoutingDataSource类,并重写其中的determineCurrentLookupKey()方法返回自己想要的key值,就可以实现指定数据源的切换!
3.3 运行流程
- 我们自己写的Aop拦截Mapper
- 判断当前执行的sql所属的命名空间,然后使用命名空间作为key读取系统配置文件获取当前mapper是否需要切换数据源
- 线程再从全局静态的HashMap中取出当前要用的数据源
- 返回对应数据源的connection去做相应的数据库操作
3.4 不切换数据源时的正常配置
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- clickhouse数据源 --> <bean id="dataSourceClickhousePinpin" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" lazy-init="true"> <property name="url" value="${clickhouse.jdbc.pinpin.url}" /> </bean> <bean id="singleSessionFactoryPinpin" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- ref直接指向 数据源dataSourceClickhousePinpin --> <property name="dataSource" ref="dataSourceClickhousePinpin" /> </bean> </beans>
3.5 进行动态数据源切换时的配置
<?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:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- clickhouse数据源 1 --> <bean id="dataSourceClickhousePinpin" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" lazy-init="true"> <property name="url" value="${clickhouse.jdbc.pinpin.url}" /> </bean> <!-- clickhouse数据源 2 --> <bean id="dataSourceClickhouseOtherPinpin" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" lazy-init="true"> <property name="url" value="${clickhouse.jdbc.other.url}" /> </bean> <!-- 新增配置 封装注册的两个数据源到multiDataSourcePinpin里 --> <!-- 对应的key分别是 defaultTargetDataSource和targetDataSources--> <bean id="multiDataSourcePinpin" class="com.zhongyouex.bigdata.common.aop.MultiDataSource"> <!-- 默认使用的数据源--> <property name="defaultTargetDataSource" ref="dataSourceClickhousePinpin"></property> <!-- 存储其他数据源,对应源码中的targetDataSources --> <property name="targetDataSources"> <!-- 该map即为源码中的resolvedDataSources--> <map> <!-- dataSourceClickhouseOther 即为要切换的数据源对应的key --> <entry key="dataSourceClickhouseOther" value-ref="dataSourceClickhouseOtherPinpin"></entry> </map> </property> </bean> <bean id="singleSessionFactoryPinpin" class="org.mybatis.spring.SqlSessionFactoryBean"> <!-- ref指向封装后的数据源multiDataSourcePinpin --> <property name="dataSource" ref="multiDataSourcePinpin" /> </bean> </beans>
核心是AbstractRoutingDataSource,由spring提供,用来动态切换数据源。我们需要继承它,来进行操作。这里我们自定义的com.zhongyouex.bigdata.common.aop.MultiDataSource就是继承了AbstractRoutingDataSource
package com.zhongyouex.bigdata.common.aop; import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource; /** * @author: cuizihua * @description: 动态数据源 * @date: 2021/9/7 20:24 * @return */ public class MultiDataSource extends AbstractRoutingDataSource { /* 存储数据源的key值,InheritableThreadLocal用来保证父子线程都能拿到值。 */ private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>(); /** * 设置dataSourceKey的值 * * @param dataSource */ public static void setDataSourceKey(String dataSource) { dataSourceKey.set(dataSource); } /** * 清除dataSourceKey的值 */ public static void toDefault() { dataSourceKey.remove(); } /** * 返回当前dataSourceKey的值 */ @Override protected Object determineCurrentLookupKey() { return dataSourceKey.get(); } }
3.6 AOP代码
package com.zhongyouex.bigdata.common.aop; import com.zhongyouex.bigdata.common.util.LoadUtil; import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.JoinPoint; import org.aspectj.lang.reflect.MethodSignature; import java.lang.reflect.Method; /** * 方法拦截 粒度在mapper上(对应的sql所属xml) * @author cuizihua * @desc 切换数据源 * @create 2021-09-03 16:29 **/ @Slf4j public class MultiDataSourceInterceptor { //动态数据源对应的key private final String otherDataSource = "dataSourceClickhouseOther"; public void beforeOpt(JoinPoint mi) { //默认使用默认数据源 MultiDataSource.toDefault(); //获取执行该方法的信息 MethodSignature signature = (MethodSignature) mi.getSignature(); Method method = signature.getMethod(); String namespace = method.getDeclaringClass().getName(); //本项目命名空间统一的规范为xxx.xxx.xxxMapper namespace = namespace.substring(namespace.lastIndexOf(".") + 1); //这里在配置文件配置的属性为xxxMapper.ck.switch=1 or 0 1表示切换 String isOtherDataSource = LoadUtil.loadByKey(namespace, "ck.switch"); if ("1".equalsIgnoreCase(isOtherDataSource)) { MultiDataSource.setDataSourceKey(otherDataSource); String methodName = method.getName(); } } }
3.7 AOP代码逻辑说明
通过org.aspectj.lang.reflect.MethodSignature可以获取对应执行sql的xml空间名称,拿到sql对应的xml命名空间就可以获取配置文件中配置的属性决定该xml是否开启切换数据源了。
3.8 对应的aop配置
<!--动态数据源--> <bean id="multiDataSourceInterceptor" class="com.zhongyouex.bigdata.common.aop.MultiDataSourceInterceptor" ></bean> <!--将自定义拦截器注入到spring中--> <aop:config proxy-target-class="true" expose-proxy="true"> <aop:aspect ref="multiDataSourceInterceptor"> <!--切入点,也就是你要监控哪些类下的方法,这里写的是DAO层的目录,表达式需要保证只扫描dao层--> <aop:pointcut id="multiDataSourcePointcut" expression="execution(* com.zhongyouex.bigdata.clickhouse..*.*(..)) "/> <!--在该切入点使用自定义拦截器--> <aop:before method="beforeOpt" pointcut-ref="multiDataSourcePointcut" /> </aop:aspect> </aop:config>
以上就是整个实现过程,希望能帮上有需要的小伙伴
作者:京东物流 崔子华
来源:京东云开发者社区

低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
细说敏捷测试-敏捷实战中的探索 | 京东云技术团队
1 什么是敏捷? 敏捷开发是一种思想或方法论,就是通过不断迭代开发和增量发布,最终交付符合用户价值的产品 敏捷思想源于最初的《敏捷宣言》: 【敏捷软件开发宣言】 个体和互动高于流程和工具; 工作的软件高于详尽的文档; 客户合作高于合同谈判; 响应变化高于遵循计划; 《敏捷宣言》代表敏捷的价值观,敏捷开发原则则帮助我们通过更灵活的方式思考开发方法和组织;具体十二条敏捷开发原则: 我们最重要的目标是通过持续不断地快速交付有价值的软件使客户满意; 欣然面对需求变化,即使在 开发后期也一样。为了客户的竞争优势,敏捷过程掌控变化。 经常地交付可工作的软件,相隔几星期或一两个月,倾向于采取较短的周期。 业务人员和开发人员必须相互合作,项目中的每一天都不例外。 激发个体的斗志,以他们为核心搭建项目。提供所需的环境和支援,辅以信任,从而达成目标。 不论团队内外,传递信息效果最好、效率最高的方式是面对面交谈。 可工作的软件是进度的首要度量标准。 敏捷过程倡导可持续开发。责任人、开发人员和用户要能够共同维持其不掉稳定、延续。 坚持不懈地追求技术卓越和良好设计,敏捷能力由此增强。 以简洁为本,它是激励减少不...
- 下一篇
开发多按键输入驱动,不妨采用这种方式
多按键输入驱动 荔枝派的全志V3s开发板上面,有4个按键本身不是通过gpio连接到soc上面的。它是通过ad的方法,连接到主芯片的。这个时候,不同的按键被按下的时候,就会生成不同的电压或者电流,那么完全可以根据对应的电信号,推算出当前是哪一个按键被按下去了。 1、查找电路图 简单看一下电路之后,下面就是去找设备树,对应的信号是什么、在哪里。 2、查找设备树 在sun8i-v3s-licheepi-zero-dock.dts文件当中,我们发现了这样的内容, &lradc { vref-supply = <®_vcc3v0>; status = "okay"; button@200 { label = "Volume Up"; linux,code = <KEY_VOLUMEUP>; channel = <0>; voltage = <200000>; }; button@400 { label = "Volume Down"; linux,code = <KEY_VOLUMEDOWN>; channel =...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8编译安装MySQL8.0.19
- SpringBoot2初体验,简单认识spring boot2并且搭建基础工程
- Hadoop3单机部署,实现最简伪集群
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- CentOS7,8上快速安装Gitea,搭建Git服务器
- CentOS7设置SWAP分区,小内存服务器的救世主
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS8安装Docker,最新的服务器搭配容器使用
- Docker安装Oracle12C,快速搭建Oracle学习环境
- Docker使用Oracle官方镜像安装(12C,18C,19C)