spring动态数据源,多数据源_spring动态多数据源

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6

Spring是如何支持多数据源的

Spring提供了一个AbstractRoutingDataSource类用来实现对多个DataSource的按需路由本文介绍的就是基于此方式实现的多数据源实践。

一、什么是AbstractRoutingDataSource

先看类上的注释

Abstract {@link javax.sql.DataSource} implementation that routes {@link #getConnection()}
calls to one of various target DataSources based on a lookup key. The latter is usually
(but not necessarily) determined through some thread-bound transaction context.

课代表翻译这是一个抽象类可以通过一个lookup key把对getConnection()方法的调用路由到目标DataSource。后者指lookup key通常是由和线程绑定的上下文决定的。

这段注释可谓字字珠玑没有一句废话。下文结合主要代码解释其含义。

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {

    //目标 DataSource Map可以装很多个 DataSource
    @Nullable
    private Map<Object, Object> targetDataSources;
    
    @Nullable
    private Map<Object, DataSource> resolvedDataSources;

    //Bean初始化时将 targetDataSources 遍历并解析后放入 resolvedDataSources
    @Override
    public void afterPropertiesSet() {
        if (this.targetDataSources == null) {
            throw new IllegalArgumentException("Property 'targetDataSources' is required");
        }
        this.resolvedDataSources = CollectionUtils.newHashMap(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);
        }
    }
    
    @Override
    public Connection getConnection() throws SQLException {
        return determineTargetDataSource().getConnection();
    }

    /**
     * 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()
     */
     //根据 #determineCurrentLookupKey()返回的lookup key 去解析好的数据源 Map 里取相应的数据源
    protected DataSource determineTargetDataSource() {
        Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
        // 当前 lookupKey 的值由用户自己实现↓
        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;
    }
    
    /**
     * Determine the current lookup key. This will typically be
     * implemented to check a thread-bound transaction context.
     * <p>Allows for arbitrary keys. The returned key needs
     * to match the stored lookup key type, as resolved by the
     * {@link #resolveSpecifiedLookupKey} method.
     */
    // 该方法用来决定lookup key通常用线程绑定的上下文来实现
    @Nullable
    protected abstract Object determineCurrentLookupKey();
    
    // 省略其余代码...

}

首先看类图

是个DataSource并且实现了InitializingBean说明有Bean的初始化操作。

其次看实例变量

private Map<Object, Object> targetDataSources;private Map<Object, DataSource> resolvedDataSources;其实是一回事后者是经过对前者的解析得来的本质就是用来存储多个 DataSource实例的 Map

最后看核心方法

使用DataSource本质就是调用其getConnection()方法获得连接从而进行数据库操作。

AbstractRoutingDataSource#getConnection()方法首先调用determineTargetDataSource()决定使用哪个目标数据源并使用该数据源的getConnection()连接数据库

@Override
public Connection getConnection() throws SQLException {
   return determineTargetDataSource().getConnection();
}
protected DataSource determineTargetDataSource() {
   Assert.notNull(this.resolvedDataSources, "DataSource router not initialized");
   // 这里使用的 lookupKey 就能决定返回的数据源是哪个
   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()方法该方法是抽象方法由用户自己实现通过改变其返回值控制返回不同的数据源。用表格表示如下

lookupKeyDataSource
firstfirstDataSource
secondsecondDataSource

如何实现这个方法呢结合Spring在注释里给的提示

后者指 lookup key通常是由和线程绑定的上下文决定的。

应该能联想到ThreadLocal了吧ThreadLocal可以维护一个与当前线程绑定的变量充当这个线程的上下文。

二、实现

设计yaml文件外部化配置多个数据源

spring:
  datasource:
    first:
      driver-class-name: org.h2.Driver
      jdbc-url: jdbc:h2:mem:db1
      username: sa
      password:
    second:
      driver-class-name: org.h2.Driver
      jdbc-url: jdbc:h2:mem:db2
      username: sa
      password:

创建lookupKey的上下文持有类

/**
 * 数据源 key 上下文
 * 通过控制 ThreadLocal变量 LOOKUP_KEY_HOLDER 的值用于控制数据源切换
 * @see RoutingDataSource
 * @author :Java课代表
 */
public class RoutingDataSourceContext {

    private static final ThreadLocal<String> LOOKUP_KEY_HOLDER = new ThreadLocal<>();

    public static void setRoutingKey(String routingKey) {
        LOOKUP_KEY_HOLDER.set(routingKey);
    }

    public static String getRoutingKey() {
        String key = LOOKUP_KEY_HOLDER.get();
        // 默认返回 key 为 first 的数据源
        return key == null ? "first" : key;
    }

    public static void reset() {
        LOOKUP_KEY_HOLDER.remove();
    }
}

实现AbstractRoutingDataSource

/**
 * 支持动态切换的数据源
 * 通过重写 determineCurrentLookupKey 实现数据源切换
 * @author :Java课代表
 */
public class RoutingDataSource extends AbstractRoutingDataSource {

    @Override
    protected Object determineCurrentLookupKey() {
        return RoutingDataSourceContext.getRoutingKey();
    }

}

给我们的RoutingDataSource初始化上多个数据源

/**
 * 数据源配置
 * 把多个数据源装配到一个 RoutingDataSource 里
 * @author :Java课代表
 */
@Configuration
public class RoutingDataSourcesConfig {

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.first")
    public DataSource firstDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Bean
    @ConfigurationProperties(prefix = "spring.datasource.second")
    public DataSource secondDataSource() {
        return DataSourceBuilder.create().build();
    }

    @Primary
    @Bean
    public RoutingDataSource routingDataSource() {
        RoutingDataSource routingDataSource = new RoutingDataSource();
        routingDataSource.setDefaultTargetDataSource(firstDataSource());
        Map<Object, Object> dataSourceMap = new HashMap<>();
        dataSourceMap.put("first", firstDataSource());
        dataSourceMap.put("second", secondDataSource());
        routingDataSource.setTargetDataSources(dataSourceMap);
        return routingDataSource;
    }

}

演示一下手工切换的代码

public void init() {
    // 手工切换为数据源 first初始化表
    RoutingDataSourceContext.setRoutingKey("first");
    createTableUser();
    RoutingDataSourceContext.reset();

    // 手工切换为数据源 second初始化表
    RoutingDataSourceContext.setRoutingKey("second");
    createTableUser();
    RoutingDataSourceContext.reset();

}

这样就实现了最基本的多数据源切换了。

不难发现切换工作很明显可以抽成一个切面我们可以优化一下利用注解标明切点哪里需要切哪里。

三、引入AOP

自定义注解

/**
 * @author :Java课代表
 */
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WithDataSource {
    String value() default "";
}

创建切面

@Aspect
@Component
// 指定优先级高于@Transactional的默认优先级
// 从而保证先切换数据源再进行事务操作
@Order(Ordered.LOWEST_PRECEDENCE - 1)
public class DataSourceAspect {

    @Around("@annotation(withDataSource)")
    public Object switchDataSource(ProceedingJoinPoint pjp, WithDataSource withDataSource) throws Throwable {

        // 1.获取 @WithDataSource 注解中指定的数据源
        String routingKey = withDataSource.value();
        // 2.设置数据源上下文
        RoutingDataSourceContext.setRoutingKey(routingKey);
        // 3.使用设定好的数据源处理业务
        try {
            return pjp.proceed();
        } finally {
            // 4.清空数据源上下文
            RoutingDataSourceContext.reset();
        }
    }
}

有了注解和切面使用起来就方便多了

// 注解标明使用"second"数据源
@WithDataSource("second")
public List<User> getAllUsersFromSecond() {
    List<User> users = userService.selectAll();
    return users;
}

关于切面有两个细节需要注意

  1. 需要指定优先级高于声明式事务

    原因声明式事务事务的本质也是 AOP其只对开启时使用的数据源生效所以一定要在切换到指定数据源之后再开启声明式事务默认的优先级是最低级这里只需要设定自定义的数据源切面的优先级比它高即可。

  2. 业务执行完之后一定要清空上下文

    原因假设方法 A 使用@WithDataSource("second")指定走"second"数据源紧跟着方法 B 不写注解期望走默认的first数据源。但由于方法A放入上下文的lookupKey此时还是"second"并未删除所以导致方法 B 执行的数据源与期望不符。

四、回顾

至此基于AbstractRoutingDataSource+AOP的多数据源就实现好了。

在配置DataSource 这个Bean的时候用的是自定义的RoutingDataSource并且标记为 @Primary。这样就可以让mybatis-spring-boot-starter使用RoutingDataSource帮我们自动配置好mybatis比搞两套DataSource+两套Mybatis配置的方案简单多了。

文中相关代码已上传课代表的github

特别说明

样例中为了减少代码层级让展示更直观在 controller 层写了事务注解实际开发中可别这么干controller 层的任务是绑定、校验参数封装返回结果尽量不要在里面写业务

五、优化

对于一般的多数据源使用场景本文方案已足够覆盖可以实现灵活切换。

但还是存在如下不足

  • 每个应用使用时都要新增相关类大量重复代码
  • 修改或新增功能时所有相关应用都得改
  • 功能不够强悍没有高级功能比如读写分离场景下的读多个从库负载均衡

其实把这些代码封装到一个starter里面高级功能慢慢扩展就可以。

好在开源世界早就有现成工具可用了开发mybatis-plus的"baomidou"团队在其生态中开源了一个多数据源框架 Dynamic-Datasource底层原理就是AbstractRoutingDataSource增加了更多强悍的扩展功能下篇介绍其使用。

阿里云国内75折 回扣 微信号:monov8
阿里云国际,腾讯云国际,低至75折。AWS 93折 免费开户实名账号 代冲值 优惠多多 微信号:monov8 飞机:@monov6
标签: Spring