spring 动态数据源

2023-06-20,,

1、动态数据源

   在一个项目中,有时候需要用到多个数据库,比如读写分离,数据库的分布式存储等等,这时我们要在项目中配置多个数据库。

2、原理:

    (1)、spring 单数据源获取数据连接过程:

    DataSource --> SessionFactory --> Session 
    DataSouce   实现javax.sql.DateSource接口的数据源, 
    DataSource  注入SessionFactory,
    从sessionFactory 获取 Session,实现数据库的 CRUD。

  (2)、动态数据源切换:  

    动态数据源原理之一:实现 javax.sql.DataSource接口, 封装DataSource, 在 DataSource 配置多个数据库连接,这种方式只需要一个dataSouce,就能实现多个数据源,最理想的实现,但是需要自己实现DataSource,自己实现连接池,对技术的要求较高,而且自己实现的连接池在性能和稳定性上都有待考验。

    动态数据源原理之二:配置多个DataSource, SessionFactory注入多个DataSource,实现SessionFactory动态调用DataSource,这种方式需要自己实现SessesionFactory,第三方实现一般不支持注入多个DataSource。

    动态数据源原理之三:配置多个DataSource, 在DataSource和SessionFactory之间插入 RoutingDataSource路由,即 DataSource --> RoutingDataSource --> SessionFactory --> Session, 在SessionFactory调用时在 RoutingDataSource 层实现DataSource的动态切换, spring提供了 AbstratRoutingDataSource抽象类, 对动态数据源切换提供了很好的支持, 不需要开发者实现复杂的底层逻辑, 推荐实现方式。

    动态数据源原理之四:配置多个SessionFactory,这种实现对技术要求最低,但是相对切换数据源最不灵活。  

3、实现:

  这里我们使用原理三以读写分离为例,具体实现如下:

  步骤一:配置多个DateSource,使用的基于阿里的 DruidDataSource

<!-- 引入属性文件,方便配置内容修改 -->
<context:property-placeholder location="classpath:jdbc.properties" /> <!-- 数据库链接(主库) -->
<bean id="dataSourceRW" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc_url}" />
<property name="username" value="${jdbc_username}" />
<property name="password" value="${jdbc_password}" /> <!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${druid_initialSize}" />
<property name="minIdle" value="${druid_minIdle}" />
<property name="maxActive" value="${druid_maxActive}" /> <!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${druid_maxWait}" /> <property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="100" /> <!-- 密码加密 -->
<property name="filters" value="config" />
<property name="connectionProperties" value="config.decrypt=true" />
</bean> <!-- 数据库链接(只读库) -->
<bean id="dataSourceR" class="com.alibaba.druid.pool.DruidDataSource"
destroy-method="close">
<!-- 基本属性 url、user、password -->
<property name="url" value="${jdbc_url_read}" />
<property name="username" value="${jdbc_username_read}" />
<property name="password" value="${jdbc_password_read}" /> <!-- 配置初始化大小、最小、最大 -->
<property name="initialSize" value="${druid_initialSize}" />
<property name="minIdle" value="${druid_minIdle}" />
<property name="maxActive" value="${druid_maxActive}" /> <!-- 配置获取连接等待超时的时间 -->
<property name="maxWait" value="${druid_maxWait}" /> <property name="validationQuery" value="SELECT 'x'" />
<property name="testWhileIdle" value="true" /> <!-- 打开PSCache,并且指定每个连接上PSCache的大小 -->
<property name="poolPreparedStatements" value="true" />
<property name="maxPoolPreparedStatementPerConnectionSize"
value="100" /> <!-- 密码加密 -->
<property name="filters" value="config" />
<property name="connectionProperties" value="config.decrypt=true" />
</bean>

步骤二:配置 DynamicDataSource

<!-- 动态数据源 -->
<bean id="dynamicDataSource" class="base.dataSource.DynamicDataSource">
<!-- 通过key-value关联数据源 -->
<property name="targetDataSources">
<map>
<entry value-ref="dataSourceRW" key="dataSourceRW"></entry>
<entry value-ref="dataSourceR" key="dataSourceR"></entry>
</map>
</property>
<!-- 默认的DataSource配置-->
<property name="defaultTargetDataSource" ref="dataSourceR" />
</bean>
package base.dataSource;

import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;

public class DynamicDataSource extends AbstractRoutingDataSource{

    @Override
protected Object determineCurrentLookupKey() {
return DBContextHolder.getDbType();
}
}

DynamicDataSource 继承了spring 的 AbstractRoutingDataSource 抽象类 实现determineCurrentLookupKey()方法

  determineCurrentLookupKey()方法在 SessionFactory 获取 DataSoure时被调用,AbstractRoutingDataSource 代码:

//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by Fernflower decompiler)
// package org.springframework.jdbc.datasource.lookup; import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Map.Entry;
import javax.sql.DataSource;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.jdbc.datasource.AbstractDataSource;
import org.springframework.jdbc.datasource.lookup.DataSourceLookup;
import org.springframework.jdbc.datasource.lookup.JndiDataSourceLookup;
import org.springframework.util.Assert; public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean {
private Map<Object, Object> targetDataSources;
private Object defaultTargetDataSource;
private boolean lenientFallback = true;
private DataSourceLookup dataSourceLookup = new JndiDataSourceLookup();
private Map<Object, DataSource> resolvedDataSources;
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(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());
Iterator var1 = this.targetDataSources.entrySet().iterator(); while(var1.hasNext()) {
Entry entry = (Entry)var1.next();
Object lookupKey = this.resolveSpecifiedLookupKey(entry.getKey());
DataSource dataSource = this.resolveSpecifiedDataSource(entry.getValue());
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;
}
} protected abstract Object determineCurrentLookupKey();
}
AbstractRoutingDataSource 两个主要变量:
  targetDataSources 初始化了 DataSource 的map集合, defaultTargetDataSource 初始化默认的DataSource 并实现了 DataSource的 getConnection() 获取数据库连接的方法,该方法从determineTargetDataSource()获取  DataSource, determineTargetDataSource() 调用了我们 DynamicDataSource 中实现的 determineCurrentLookupKey() 方法获取DataSource(determineCurrentLookupKey()方法返回的只是我们初始化的DataSource Map集合key值, 通过key获取DataSource的方法这里不做赘述,感兴趣自己研究下),
determineTargetDataSource()的主要逻辑是获取我们切换的DataSource, 如果没有的话读取默认的DataSource。 在DynamicDataSource中我们定义了一个线程变量DBContextHolder来存放我们切换的DataSource, 防止其它线程覆盖我们的DataSource。
package base.dataSource;

/**
*
* @author xiao
* @date 下午3:27:52
*/
public final class DBContextHolder { /**
* 线程threadlocal
*/
private static ThreadLocal<String> contextHolder = new ThreadLocal<>(); private static String DEFAUL_DB_TYPE_RW = "dataSourceKeyRW"; /**
* 获取本线程的dbtype
* @return
*/
public static String getDbType() {
String db = contextHolder.get();
if (db == null) {
db = DEFAUL_DB_TYPE_RW;// 默认是读写库
}
return db;
} /**
*
* 设置本线程的dbtype
*
* @param str
*/
public static void setDbType(String str) {
contextHolder.set(str);
} /**
* clearDBType
*
* @Title: clearDBType
* @Description: 清理连接类型
*/
public static void clearDBType() {
contextHolder.remove();
}
}

至此我们获取DataSource的逻辑已完成, 接下来我们要考虑 设置DataSource, 即为DBContextHolder, set值。我们在代码中调用DBContextHolder.set()来设置DataSource,理论上可以在代码的任何位置设置, 不过为了统一规范,我们通过aop来实现,此时我们面临的问题,在哪一层切入, 方案一: 在dao层切入,dao封装了数据库的CRUD,在这一层切入控制最灵活,但是我们一般在service业务层切入事务,如果在dao层切换数据源,会遇到事务无法同步的问题,虽然有分布式事务机制,但是目前成熟的框架很难用,如果使用过 就会知道分布式事务是一件非常恶心的事情,而且分布式事务本就不是一个好的选择。方案二: 在service业务层切入,可以避免事务问题,但也相对影响了数据源切换的灵活性,这里要根据实际情况灵活选择,我们采用的在service业务层切入,具体实现如下:

步骤三:实现aop

package base.dataSource.aop;

import java.util.Map;

import org.aspectj.lang.JoinPoint;
import org.springframework.core.Ordered; import base.dataSource.DBContextHolder; /**
* 动态数据源切换aop
* @author xiao
* @date 2015年7月23日下午4:17:13
*/
public final class DynamicDataSourceAOP implements Ordered{ /**
* 方法, 数据源应映射规则map
*/
Map<String, String> methods; /**
* 默认数据源
*/
String defaultDataSource; public String getDefaultDataSource() {
return defaultDataSource;
} public void setDefaultDataSource(String defaultDataSource) {
if(null == defaultDataSource || "".equals(defaultDataSource)){
throw new NullPointerException("defaultDataSource Must have a default value");
}
this.defaultDataSource = defaultDataSource;
} public Map<String, String> getMethods() {
return methods;
} public void setMethods(Map<String, String> methods) {
this.methods = methods;
} /**
* before 数据源切换
*
* @param pjp
* @throws Throwable
*/
public void dynamicDataSource(JoinPoint pjp) throws Throwable {
DBContextHolder.setDbType(getDBTypeKey(pjp.getSignature().getName()));
} private String getDBTypeKey(String methodName) {
methodName = methodName.toUpperCase();
for (String method : methods.keySet()) {
String m = method.toUpperCase();
/**
* 忽略大小写
* method 如果不包含 '*', 则以方法名匹配 method
* method 包含 '*', 则匹配以 method 开头, 或者 等于method 的方法
*/
if (!method.contains("*")
&& m.equals(methodName)
|| methodName
.startsWith(m.substring(0, m.indexOf("*") - 1))
|| methodName.equals(m.substring(0, m.indexOf("*") - 1))) {
return methods.get(method);
}
}
return defaultDataSource;
} //设置AOP执行顺序, 这里设置优于事务
@Override
public int getOrder() {
return 1;
}
}

这里有一个小知识点,aop实现类实现了orderd接口,这个接口有一个方法getOrder(),返回aop的执行顺序,就是在同一个切点如果切入了多个aop,则按order从小到大执行,这里我们设置优于事务aop,因为事务是 基于dataSource的,即先切换数据源,在开启事务,否则可能会存在切换了已开启了事务的数据源,导致事务不生效。

步骤四:配置aop切面

<!-- 数据源读写分离  aop -->
<bean id="dynamicDataSourceAOP" class="base.dataSource.aop.DynamicDataSourceAOP">
<property name="methods">
<map>
<entry key="select*" value="dataSourceKeyR" />
<entry key="get*" value="dataSourceKeyR" />
<entry key="find*" value="dataSourceKeyR" />
<entry key="page*" value="dataSourceKeyR" />
<entry key="query*" value="dataSourceKeyRW" />
</map>
</property>
<property name="defaultDataSource" value="dataSourceKeyRW"/>
</bean> <aop:config>
<!-- 切点 管理所有Service的方法 -->
<aop:pointcut
expression="execution(* com.b2c.*.service.*Service.*(..))"
id="transactionPointCut" />
<!-- 进行事务控制 Advisor -->
<aop:advisor advice-ref="txAdvice" pointcut-ref="transactionPointCut" /> <!-- 动态数据源aop, aop:advisor配置一定要在 aop:aspect之前,否则报错 -->
<aop:aspect ref="dynamicDataSourceAOP">
<aop:before method="dynamicDataSource" pointcut-ref="transactionPointCut" />
</aop:aspect> </aop:config>

至此全部完成, 另外这只是个人观点,有更好的想法欢迎交流指正。

spring 动态数据源的相关教程结束。

《spring 动态数据源.doc》

下载本文的Word格式文档,以方便收藏与打印。