经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » Java相关 » Spring » 查看文章
一种实现Spring动态数据源切换的方法
来源:cnblogs  作者:京东云开发者  时间:2023/6/19 17:17:05  对本文有异议

1 目标

不在现有查询代码逻辑上做任何改动,实现dao维度的数据源切换(即表维度)

2 使用场景

节约bdp的集群资源。接入新的宽表时,通常uat验证后就会停止集群释放资源,在对应的查询服务器uat环境时需要查询的是生产库的表数据(uat库表因为bdp实时任务停止,没有数据落入),只进行服务器配置文件的改动而无需进行代码的修改变更,即可按需切换查询的数据源。

2.1 实时任务对应的集群资源

2.2 实时任务产生的数据进行存储的两套环境

2.3 数据使用系统的两套环境(查询展示数据)

即需要在zhongyouex-bigdata-uat中查询生产库的数据。

3 实现过程

3.1 实现重点

  1. org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource
    spring提供的这个类是本次实现的核心,能够让我们实现运行时多数据源的动态切换,但是数据源是需要事先配置好的,无法动态的增加数据源。
  2. Spring提供的Aop拦截执行的mapper,进行切换判断并进行切换。

注:另外还有一个就是ThreadLocal类,用于保存每个线程正在使用的数据源。

3.2 AbstractRoutingDataSource解析

  1. public abstract class AbstractRoutingDataSource extends AbstractDataSource
  2. implements InitializingBean{
  3. @Nullable
  4. private Map<Object, Object> targetDataSources;
  5. @Nullable
  6. private Object defaultTargetDataSource;
  7. @Override
  8. public Connection getConnection() throws SQLException {
  9. return determineTargetDataSource().getConnection();
  10. }
  11. protected DataSource determineTargetDataSource() {
  12. Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
  13. Object lookupKey = determineCurrentLookupKey();
  14. DataSource dataSource = this.resolvedDataSources.get(lookupKey);
  15. if (dataSource == null && (this.lenientFallback || lookupKey == null)) {
  16. dataSource = this.resolvedDefaultDataSource;
  17. }
  18. if (dataSource == null) {
  19. throw new IllegalStateException("Cannot determine target DataSource for lookup key [" + lookupKey + "]");
  20. }
  21. return dataSource;
  22. }
  23. @Override
  24. public void afterPropertiesSet() {
  25. if (this.targetDataSources == null) {
  26. throw new IllegalArgumentException("Property 'targetDataSources' is required");
  27. }
  28. this.resolvedDataSources = new HashMap<>(this.targetDataSources.size());
  29. this.targetDataSources.forEach((key, value) -> {
  30. Object lookupKey = resolveSpecifiedLookupKey(key);
  31. DataSource dataSource = resolveSpecifiedDataSource(value);
  32. this.resolvedDataSources.put(lookupKey, dataSource);
  33. });
  34. if (this.defaultTargetDataSource != null) {
  35. this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource);
  36. }
  37. }

从上面源码可以看出它继承了AbstractDataSource,而AbstractDataSource是javax.sql.DataSource的实现类,拥有getConnection()方法。获取连接的getConnection()方法中,重点是determineTargetDataSource()方法,它的返回值就是你所要用的数据源dataSource的key值,有了这个key值,resolvedDataSource(这是个map,由配置文件中设置好后存入targetDataSources的,通过targetDataSources遍历存入该map)就从中取出对应的DataSource,如果找不到,就用配置默认的数据源。

看完源码,我们可以知道,只要扩展AbstractRoutingDataSource类,并重写其中的determineCurrentLookupKey()方法返回自己想要的key值,就可以实现指定数据源的切换!

3.3 运行流程

  1. 我们自己写的Aop拦截Mapper
  2. 判断当前执行的sql所属的命名空间,然后使用命名空间作为key读取系统配置文件获取当前mapper是否需要切换数据源
  3. 线程再从全局静态的HashMap中取出当前要用的数据源
  4. 返回对应数据源的connection去做相应的数据库操作

3.4 不切换数据源时的正常配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
  4. 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">
  5. <!-- clickhouse数据源 -->
  6. <bean id="dataSourceClickhousePinpin" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" lazy-init="true">
  7. <property name="url" value="${clickhouse.jdbc.pinpin.url}" />
  8. </bean>
  9. <bean id="singleSessionFactoryPinpin" class="org.mybatis.spring.SqlSessionFactoryBean">
  10. <!-- ref直接指向 数据源dataSourceClickhousePinpin -->
  11. <property name="dataSource" ref="dataSourceClickhousePinpin" />
  12. </bean>
  13. </beans>

3.5 进行动态数据源切换时的配置

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <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">
  3. <!-- clickhouse数据源 1 -->
  4. <bean id="dataSourceClickhousePinpin" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" lazy-init="true">
  5. <property name="url" value="${clickhouse.jdbc.pinpin.url}" />
  6. </bean>
  7. <!-- clickhouse数据源 2 -->
  8. <bean id="dataSourceClickhouseOtherPinpin" class="org.apache.commons.dbcp2.BasicDataSource" destroy-method="close" lazy-init="true">
  9. <property name="url" value="${clickhouse.jdbc.other.url}" />
  10. </bean>
  11. <!-- 新增配置 封装注册的两个数据源到multiDataSourcePinpin里 -->
  12. <!-- 对应的key分别是 defaultTargetDataSource和targetDataSources-->
  13. <bean id="multiDataSourcePinpin" class="com.zhongyouex.bigdata.common.aop.MultiDataSource">
  14. <!-- 默认使用的数据源-->
  15. <property name="defaultTargetDataSource" ref="dataSourceClickhousePinpin"></property>
  16. <!-- 存储其他数据源,对应源码中的targetDataSources -->
  17. <property name="targetDataSources">
  18. <!-- 该map即为源码中的resolvedDataSources-->
  19. <map>
  20. <!-- dataSourceClickhouseOther 即为要切换的数据源对应的key -->
  21. <entry key="dataSourceClickhouseOther" value-ref="dataSourceClickhouseOtherPinpin"></entry>
  22. </map>
  23. </property>
  24. </bean>
  25. <bean id="singleSessionFactoryPinpin" class="org.mybatis.spring.SqlSessionFactoryBean">
  26. <!-- ref指向封装后的数据源multiDataSourcePinpin -->
  27. <property name="dataSource" ref="multiDataSourcePinpin" />
  28. </bean>
  29. </beans>

核心是AbstractRoutingDataSource,由spring提供,用来动态切换数据源。我们需要继承它,来进行操作。这里我们自定义的com.zhongyouex.bigdata.common.aop.MultiDataSource就是继承了AbstractRoutingDataSource

  1. package com.zhongyouex.bigdata.common.aop;
  2. import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
  3. /**
  4. * @author: cuizihua
  5. * @description: 动态数据源
  6. * @date: 2021/9/7 20:24
  7. * @return
  8. */
  9. public class MultiDataSource extends AbstractRoutingDataSource {
  10. /* 存储数据源的key值,InheritableThreadLocal用来保证父子线程都能拿到值。
  11. */
  12. private static final ThreadLocal<String> dataSourceKey = new InheritableThreadLocal<String>();
  13. /**
  14. * 设置dataSourceKey的值
  15. *
  16. * @param dataSource
  17. */
  18. public static void setDataSourceKey(String dataSource) {
  19. dataSourceKey.set(dataSource);
  20. }
  21. /**
  22. * 清除dataSourceKey的值
  23. */
  24. public static void toDefault() {
  25. dataSourceKey.remove();
  26. }
  27. /**
  28. * 返回当前dataSourceKey的值
  29. */
  30. @Override
  31. protected Object determineCurrentLookupKey() {
  32. return dataSourceKey.get();
  33. }
  34. }

3.6 AOP代码

  1. package com.zhongyouex.bigdata.common.aop;
  2. import com.zhongyouex.bigdata.common.util.LoadUtil;
  3. import lombok.extern.slf4j.Slf4j;
  4. import org.aspectj.lang.JoinPoint;
  5. import org.aspectj.lang.reflect.MethodSignature;
  6. import java.lang.reflect.Method;
  7. /**
  8. * 方法拦截 粒度在mapper上(对应的sql所属xml)
  9. * @author cuizihua
  10. * @desc 切换数据源
  11. * @create 2021-09-03 16:29
  12. **/
  13. @Slf4j
  14. public class MultiDataSourceInterceptor {
  15. //动态数据源对应的key
  16. private final String otherDataSource = "dataSourceClickhouseOther";
  17. public void beforeOpt(JoinPoint mi) {
  18. //默认使用默认数据源
  19. MultiDataSource.toDefault();
  20. //获取执行该方法的信息
  21. MethodSignature signature = (MethodSignature) mi.getSignature();
  22. Method method = signature.getMethod();
  23. String namespace = method.getDeclaringClass().getName();
  24. //本项目命名空间统一的规范为xxx.xxx.xxxMapper
  25. namespace = namespace.substring(namespace.lastIndexOf(".") + 1);
  26. //这里在配置文件配置的属性为xxxMapper.ck.switch=1 or 0 1表示切换
  27. String isOtherDataSource = LoadUtil.loadByKey(namespace, "ck.switch");
  28. if ("1".equalsIgnoreCase(isOtherDataSource)) {
  29. MultiDataSource.setDataSourceKey(otherDataSource);
  30. String methodName = method.getName();
  31. }
  32. }
  33. }

3.7 AOP代码逻辑说明

通过org.aspectj.lang.reflect.MethodSignature可以获取对应执行sql的xml空间名称,拿到sql对应的xml命名空间就可以获取配置文件中配置的属性决定该xml是否开启切换数据源了。

3.8 对应的aop配置

  1. <!--动态数据源-->
  2. <bean id="multiDataSourceInterceptor" class="com.zhongyouex.bigdata.common.aop.MultiDataSourceInterceptor" ></bean>
  3. <!--将自定义拦截器注入到spring中-->
  4. <aop:config proxy-target-class="true" expose-proxy="true">
  5. <aop:aspect ref="multiDataSourceInterceptor">
  6. <!--切入点,也就是你要监控哪些类下的方法,这里写的是DAO层的目录,表达式需要保证只扫描dao层-->
  7. <aop:pointcut id="multiDataSourcePointcut" expression="execution(* com.zhongyouex.bigdata.clickhouse..*.*(..)) "/>
  8. <!--在该切入点使用自定义拦截器-->
  9. <aop:before method="beforeOpt" pointcut-ref="multiDataSourcePointcut" />
  10. </aop:aspect>
  11. </aop:config>

以上就是整个实现过程,希望能帮上有需要的小伙伴

作者:京东物流 崔子华

来源:京东云开发者社区

原文链接:https://www.cnblogs.com/Jcloud/p/17491451.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号