当一个项目中有多个数据源(也可以是主从库)的时候,我们可以利用注解在 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;
- }
抽象方法
返回 DataSource 的 key 值,然后根据这个 key 从 resolvedDataSources 这个 map 里取出对应的 DataSource,如果找不到,则用默认的 resolvedDefaultDataSource。
- determineCurrentLookupKey()
我们要做的就是实现抽象方法
返回数据源的 key 值。
- determineCurrentLookupKey()
定义注解:
- /**
- * 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,并实现
,返回数据源的 key 值。
- determineCurrentLookupKey()
- /**
- * Created by huangyangquan on 2016/11/30.
- */
- public class DynamicDataSource extends AbstractRoutingDataSource {
- @Override
- protected Object determineCurrentLookupKey() {
- return DynamicDataSourceHolder.getDataSourceType();
- }
- }
是我们管理 DataSource 的类,将一次数据库操作的数据源名称保存在 DynamicDataSourceHolder 中,以供后面的操作在此 context 中取数据源 key,其中 DataSourceType 使用了线程本地变量来保证线程安全。
- DynamicDataSourceHolder
- /**
- * 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-DB
表示主库的从库,是我们的两个数据源。
- TEST-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 中,这样在执行查询的时候,
返回数据源的 key 值就是我们希望的那个数据源了。
- determineCurrentLookupKey()
- /**
- * 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);
- }
- }
- }
这样我们就实现了一个动态数据源切换的功能。
来源: http://www.cnblogs.com/aheizi/p/7071181.html