Java注解实现动态数据源切换的实例代码

2025-05-29 0 79

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

实现原理

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

看下AbstractRoutingDataSource:

复制代码 代码如下:


public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20
/**

* 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值。

使用方法

定义注解

?

1

2

3

4

5

6

7

8

9

10
/**

* Created by huangyangquan on 2016/11/30.

*/

@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

public @interface DataSource {

DataSourceType value();

}

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

?

1

2

3

4

5

6

7

8

9
/**

* Created by huangyangquan on 2016/11/30.

*/

public enum DataSourceType {

MASTER,

SLAVE

}

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

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

?

1

2

3

4

5

6

7

8

9

10

11
/**

* 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使用了线程本地变量来保证线程安全。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25
/**

* 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值得对应关系:

?

1

2

3

4

5

6

7

8

9

10
<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-DB TEST-SLAVE-DB 表示主库的从库,是我们的两个数据源

接下来配置AOP切面:

?

1

2

3

4

5

6

7

8

9
<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值就是我们希望的那个数据源了。

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36
/**

* 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);

}

}

}

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

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持快网idc。

原文链接:http://www.cnblogs.com/aheizi/p/7071181.html

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Java注解实现动态数据源切换的实例代码 https://www.kuaiidc.com/115740.html

相关文章

发表评论
暂无评论