多数据源场景下的动态切换

206 阅读3分钟

一、AbstractRoutingDataSource简介

AbstractRoutingDataSource是Spring(package:org.springframework.jdbc.datasource.lookup)提供的一个多数据源解决方案。 1.多租户支持:根据租户选取对应的数据源,从而达到租户隔离的目的 2.分库分表:根据分片规则选取不同的数据源 3.读写分离:根据读写类型选取合适的数据源 4.数据源负载均衡:根据负载均衡的策略选取合适的数据源 5.多数据库支持:根据业务需求选择不同类型的数据源,如同时使用MySQL和Oracle

二、AbstractRoutingDataSource原理

1.过程描述

AbstractRoutingDataSource继承自AbstractDataSource,同时AbstractDataSource实现了DataSource接口。 我们可以把AbstractRoutingDataSource看成是一堆DataSource实例的集合,当需要使用的时候,从中取出对应的Connection,然后再执行相应的sql。

2.源码解析

AbstractRoutingDataSource

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {
    ……
}

可以看到AbstractRoutingDataSource继承自AbstractDataSource,进一步还能看到AbstractDataSource实现了DataSource接口,整体的继承实现关系如下:

AbstractRoutingDataSource.png

getConnection()

getConnection()是通过一个determineTargetDataSource()的方法获取的Connection

@Override
public Connection getConnection() throws SQLException {
    return this.determineTargetDataSource().getConnection();
}

@Override
public Connection getConnection(String username, String password) throws SQLException {
    return this.determineTargetDataSource().getConnection(username, password);
}

determineTargetDataSource

	/**
	 * 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;
	}

其中有一个名为resolvedDataSources的Map,我们看下这个Map的定义:

@Nullable
private Map<Object, DataSource> resolvedDataSources;

可以知道这个Map是实际上存放DataSource的容器。 那么Map的Key是如何获取的呢,从源码可以知道key=lookupKey,通过determineCurrentLookupKey()方法获取的。

	/**
	 * 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.
	 */
	@Nullable
	protected abstract Object determineCurrentLookupKey();

这个方法是一个抽象方法,所以这就是我们需要实现的业务相关的核心方法。

afterPropertiesSet()

方法对targetDataSources和defaultTargetDataSource稍作加工和校验之后,分别赋值给resolvedDataSources和resolvedDefaultDataSource

	@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);
		}
	}
	/**
	 * Specify the map of target DataSources, with the lookup key as key.
	 * The mapped value can either be a corresponding {@link javax.sql.DataSource}
	 * instance or a data source name String (to be resolved via a
	 * {@link #setDataSourceLookup DataSourceLookup}).
	 * <p>The key can be of arbitrary type; this class implements the
	 * generic lookup process only. The concrete key representation will
	 * be handled by {@link #resolveSpecifiedLookupKey(Object)} and
	 * {@link #determineCurrentLookupKey()}.
	 */
	public void setTargetDataSources(Map<Object, Object> targetDataSources) {
		this.targetDataSources = targetDataSources;
	}
        
        
	/**
	 * Specify the default target DataSource, if any.
	 * <p>The mapped value can either be a corresponding {@link javax.sql.DataSource}
	 * instance or a data source name String (to be resolved via a
	 * {@link #setDataSourceLookup DataSourceLookup}).
	 * <p>This DataSource will be used as target if none of the keyed
	 * {@link #setTargetDataSources targetDataSources} match the
	 * {@link #determineCurrentLookupKey()} current lookup key.
	 */
	public void setDefaultTargetDataSource(Object defaultTargetDataSource) {
		this.defaultTargetDataSource = defaultTargetDataSource;
	}

三、其他的AbstractRoutingDataSource

@DS简介

@DS是baomidou封装的mybatis-plus的注解,内部有其自身的AbstractRoutingDataSource,并用DynamicRoutingDataSource做了相关的实现

初始化

在实例化的时候,通过DynamicRoutingDataSource的afterPropertiesSet()实现dataSource的初始化:

    @Override
    public void afterPropertiesSet() {
        // 检查开启了配置但没有相关依赖
        checkEnv();
        // 添加并分组数据源
        Map<String, DataSource> dataSources = new HashMap<>(16);
        for (DynamicDataSourceProvider provider : providers) {
            Map<String, DataSource> dsMap = provider.loadDataSources();
            if (dsMap != null) {
                dataSources.putAll(dsMap);
            }
        }
        for (Map.Entry<String, DataSource> dsItem : dataSources.entrySet()) {
            addDataSource(dsItem.getKey(), dsItem.getValue());
        }
        // 检测默认数据源是否设置
        if (groupDataSources.containsKey(primary)) {
            log.info("dynamic-datasource initial loaded [{}] datasource,primary group datasource named [{}]", dataSources.size(), primary);
        } else if (dataSourceMap.containsKey(primary)) {
            log.info("dynamic-datasource initial loaded [{}] datasource,primary datasource named [{}]", dataSources.size(), primary);
        } else {
            log.warn("dynamic-datasource initial loaded [{}] datasource,Please add your primary datasource or check your configuration", dataSources.size());
        }
    }

其中provider会默认从yml文件加载所有的数据源,我们也可以通过实现DynamicDataSourceProvider接口loadDataSources()方法的方式,实现从其他地方加载数据源的功能并通过addDataSource()方法放入一个叫dataSourceMap的Map中

数据源导入

与spring提供的AbstractRoutingDataSource不同的是,@DS直接提供了路由到具体数据源的方法,即通过AOP拦截的方式。 数据操作会被DynamicDataSourceAnnotationInterceptor拦截,并把数据源名放入DynamicDataSourceContextHolder的ThreadLocal

获取指定数据源

进行数据操作的时候,会调用AbstractRoutingDataSource的getConnection()方法。 DynamicRoutingDataSource会从DynamicDataSourceContextHolder获取之前拦截器放入ThreadLocal的数据源名称再通过数据源名称到dataSourceMap中获取指定数据源