Java注解--实现动态数据源切换

2023-06-20,,

当一个项目中有多个数据源(也可以是主从库)的时候,我们可以利用注解在mapper接口上标注数据源,从而来实现多个数据源在运行时的动态切换

实现原理

在Spring 2.0.1中引入了AbstractRoutingDataSource, 该类充当了DataSource的路由中介, 能有在运行时, 根据某种key值来动态切换到真正的DataSource上。

看下AbstractRoutingDataSource:

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean

AbstractRoutingDataSource继承了AbstractDataSource,获取数据源部分:

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

抽象方法determineCurrentLookupKey()返回DataSource的key值,然后根据这个key从resolvedDataSources这个map里取出对应的DataSource,如果找不到,则用默认的resolvedDefaultDataSource。

我们要做的就是实现抽象方法determineCurrentLookupKey()返回数据源的key值。

使用方法

定义注解:

/**
* Created by huangyangquan on 2016/11/30.
*/
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface DataSource { DataSourceType value(); }

注解为数据源的名称,可定义一个枚举类表示:

/**
* Created by huangyangquan on 2016/11/30.
*/
public enum DataSourceType { MASTER,
SLAVE }

注解定义好了,我们利用Spring的AOP根据注解内容对数据源进行选择,这里需要利用上面提到的AbstractRoutingDataSource类,该类是能够实现数据源切换的关键所在。

定义类DynamicDataSource继承AbstractRoutingDataSource,并实现determineCurrentLookupKey(),返回数据源的key值。

/**
* Created by huangyangquan on 2016/11/30.
*/
public class DynamicDataSource extends AbstractRoutingDataSource { @Override
protected Object determineCurrentLookupKey() {
return DynamicDataSourceHolder.getDataSourceType();
} }

DynamicDataSourceHolder是我们管理DataSource的类,将一次数据库操作的数据源名称保存在DynamicDataSourceHolder中,以供后面的操作在此context中取数据源key,其中DataSourceType使用了线程本地变量来保证线程安全。

/**
* Created by huangyangquan on 2016/11/30.
*/
public class DynamicDataSourceHolder { // 线程本地环境
private static final ThreadLocal<DataSourceType> contextHolder = new ThreadLocal<DataSourceType>(); // 设置数据源类型
public static void setDataSourceType(DataSourceType dataSourceType) {
Assert.notNull(dataSourceType, "DataSourceType cannot be null");
contextHolder.set(dataSourceType);
} // 获取数据源类型
public static DataSourceType getDataSourceType() {
return (DataSourceType) contextHolder.get();
} // 清除数据源类型
public static void clearDataSourceType() {
contextHolder.remove();
} }

我们在Spring的配置文件中配置数据源key值得对应关系:

<bean id="spyGhotelDataSource" class="com.aheizi.config.DynamicDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<entry key="MASTER" value-ref="TEST-MASTER-DB"></entry>
<entry key="SLAVE" value-ref="TEST-SLAVE-DB"></entry>
</map>
</property>
<property name="defaultTargetDataSource" ref="TEST-MASTER-DB">
</property>
</bean>

设置targetDataSources和defaultTargetDataSource。TEST-MASTER-DBTEST-SLAVE-DB表示主库的从库,是我们的两个数据源。

接下来配置AOP切面:

<aop:aspectj-autoproxy proxy-target-class="false" />
<bean id="manyDataSourceAspect" class="com.aheizi.config.DataSourceAspect" />
<aop:config>
<aop:aspect id="dataSourceCut" ref="manyDataSourceAspect">
<aop:pointcut expression="execution(* com.aheizi.dao.*.*(..))"
id="dataSourceCutPoint" /><!-- 配置切点 -->
<aop:before pointcut-ref="dataSourceCutPoint" method="before" />
</aop:aspect>
</aop:config>

以下是切面中before执行的DataSourceAspect的实现,主要实现的功能是获取方法上的注解,根据注解名称将值设置到DynamicDataSourceHolder中,这样在执行查询的时候,determineCurrentLookupKey()返回数据源的key值就是我们希望的那个数据源了。

/**
* Created by huangyangquan on 2016/11/30.
*/
public class DataSourceAspect { private static final Logger LOG = LoggerFactory.getLogger(DataSourceAspect.class); public void before(JoinPoint point){
Object target = point.getTarget();
String method = point.getSignature().getName();
Class<?>[] classz = target.getClass().getInterfaces();
Class<?>[] parameterTypes = ((MethodSignature) point.getSignature()).getMethod().getParameterTypes();
try {
Method m = classz[0].getMethod(method, parameterTypes);
if (m != null && m.isAnnotationPresent(DataSource.class)) {
// 访问mapper中的注解
DataSource data = m.getAnnotation(DataSource.class);
switch (data.value()) {
case MASTER:
DynamicDataSourceHolder.setDataSourceType(DataSourceType.MASTER);
LOG.info("using dataSource:{}", DataSourceType.MASTER);
break;
case SLAVE:
DynamicDataSourceHolder.setDataSourceType(DataSourceType.SLAVE);
LOG.info("using dataSource:{}", DataSourceType.SLAVE);
break;
}
}
} catch (Exception e) {
LOG.error("dataSource annotation error:{}", e.getMessage());
// 若出现异常,手动设为主库
DynamicDataSourceHolder.setDataSourceType(DataSourceType.MASTER);
}
} }

这样我们就实现了一个动态数据源切换的功能。

Java注解--实现动态数据源切换的相关教程结束。

《Java注解--实现动态数据源切换.doc》

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