当前位置: 首页 > news >正文

Spring, MyBatis 多数据源的配置和管理

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

同一个项目有时会涉及到多个数据库,也就是多数据源。多数据源又可以分为两种情况:

 1)两个或多个数据库没有相关性,各自独立,其实这种可以作为两个项目来开发。比如在游戏开发中一个数据库是平台数据库,其它还有平台下的游戏对应的数据库;

 2)两个或多个数据库是master-slave的关系,比如有mysql搭建一个 master-master,其后又带有多个slave;或者采用MHA搭建的master-slave复制; 目前我所知道的 Spring 多数据源的搭建大概有两种方式,可以根据多数据源的情况进行选择。

 1. 采用spring配置文件直接配置多个数据源 

比如针对两个数据库没有相关性的情况,可以采用直接在spring的配置文件中配置多个数据源,然后分别进行事务的配置,如下所示:

<context:component-scan base-package="net.aazj.service,net.aazj.aop" />
<context:component-scan base-package="net.aazj.aop" />
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:config/db.properties" />
 
<!-- 配置数据源 -->
<bean name="dataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="url" value="${jdbc_url}" />
    <property name="username" value="${jdbc_username}" />
    <property name="password" value="${jdbc_password}" />
    <!-- 初始化连接大小 -->
    <property name="initialSize" value="0" />
    <!-- 连接池最大使用连接数量 -->
    <property name="maxActive" value="20" />
    <!-- 连接池最大空闲 -->
    <property name="maxIdle" value="20" />
    <!-- 连接池最小空闲 -->
    <property name="minIdle" value="0" />
    <!-- 获取连接最大等待时间 -->
    <property name="maxWait" value="60000" />
</bean>
 
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="configLocation" value="classpath:config/mybatis-config.xml" />
  <property name="mapperLocations" value="classpath*:config/mappers/**/*.xml" />
</bean>
 
<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>
 
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" /> 
 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="net.aazj.mapper" />
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/>
</bean>
 
<!-- Enables the use of the @AspectJ style of Spring AOP -->
<aop:aspectj-autoproxy/>
 
<!-- ===============第二个数据源的配置=============== -->
<bean name="dataSource_2" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="url" value="${jdbc_url_2}" />
    <property name="username" value="${jdbc_username_2}" />
    <property name="password" value="${jdbc_password_2}" />
    <!-- 初始化连接大小 -->
    <property name="initialSize" value="0" />
    <!-- 连接池最大使用连接数量 -->
    <property name="maxActive" value="20" />
    <!-- 连接池最大空闲 -->
    <property name="maxIdle" value="20" />
    <!-- 连接池最小空闲 -->
    <property name="minIdle" value="0" />
    <!-- 获取连接最大等待时间 -->
    <property name="maxWait" value="60000" />
</bean>
 
<bean id="sqlSessionFactory_slave" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource_2" />
  <property name="configLocation" value="classpath:config/mybatis-config-2.xml" />
  <property name="mapperLocations" value="classpath*:config/mappers2/**/*.xml" />
</bean>
 
<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager_2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource_2" />
</bean>
 
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager_2" /> 
 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="net.aazj.mapper2" />
  <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory_2"/>
</bean>

如上所示,我们分别配置了两个 dataSource,两个sqlSessionFactory,两个transactionManager,以及关键的地方在于MapperScannerConfigurer 的配置——使用sqlSessionFactoryBeanName属性,注入不同的sqlSessionFactory的名称,这样的话,就为不同的数据库对应的 mapper 接口注入了对应的 sqlSessionFactory。

 需要注意的是,多个数据库的这种配置是不支持分布式事务的,也就是同一个事务中,不能操作多个数据库。这种配置方式的优点是很简单,但是却不灵活。对于master-slave类型的多数据源配置而言不太适应,master-slave性的多数据源的配置,需要特别灵活,需要根据业务的类型进行细致的配置。比如对于一些耗时特别大的select语句,我们希望放到slave上执行,而对于update,delete等操作肯定是只能在master上执行的,另外对于一些实时性要求很高的select语句,我们也可能需要放到master上执行——比如一个场景是我去商城购买一件兵器,购买操作的很定是master,同时购买完成之后,需要重新查询出我所拥有的兵器和金币,那么这个查询可能也需要防止master上执行,而不能放在slave上去执行,因为slave上可能存在延时,我们可不希望玩家发现购买成功之后,在背包中却找不到兵器的情况出现。 所以对于master-slave类型的多数据源的配置,需要根据业务来进行灵活的配置,哪些select可以放到slave上,哪些select不能放到slave上。所以上面的那种所数据源的配置就不太适应了。 2. 基于 AbstractRoutingDataSource 和 AOP 的多数据源的配置 基本原理是,我们自己定义一个DataSource类ThreadLocalRountingDataSource,来继承AbstractRoutingDataSource,然后在配置文件中向ThreadLocalRountingDataSource注入 master 和 slave 的数据源,然后通过 AOP 来灵活配置,在哪些地方选择  master 数据源,在哪些地方需要选择 slave数据源。下面看代码实现: 1)先定义一个enum来表示不同的数据源:  package net.aazj.enums; /** * 数据源的类别:master/slave */public enum DataSources {    MASTER, SLAVE} 

2)通过 TheadLocal 来保存每个线程选择哪个数据源的标志(key):

package net.aazj.util;
 
import net.aazj.enums.DataSources;
 
public class DataSourceTypeManager {
    private static final ThreadLocal<DataSources> dataSourceTypes = new ThreadLocal<DataSources>(){
        @Override
        protected DataSources initialValue(){
            return DataSources.MASTER;
        }
    };
     
    public static DataSources get(){
        return dataSourceTypes.get();
    }
     
    public static void set(DataSources dataSourceType){
        dataSourceTypes.set(dataSourceType);
    }
     
    public static void reset(){
        dataSourceTypes.set(DataSources.MASTER0);
    }
}

3)定义 ThreadLocalRountingDataSource,继承AbstractRoutingDataSource:

  

package net.aazj.util; 
import org.springframework.jdbc.datasource.lookup.AbstractRoutingDataSource;
public class ThreadLocalRountingDataSource extends AbstractRoutingDataSource {    
     @Override    
     protected Object determineCurrentLookupKey() {        
               return DataSourceTypeManager.get();    
     }
}

 

4)在配置文件中向 ThreadLocalRountingDataSource 注入 master 和 slave 的数据源:

<context:component-scan base-package="net.aazj.service,net.aazj.aop" />
<context:component-scan base-package="net.aazj.aop" />
<!-- 引入属性文件 -->
<context:property-placeholder location="classpath:config/db.properties" />    
<!-- 配置数据源Master -->
<bean name="dataSourceMaster" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="url" value="${jdbc_url}" />
    <property name="username" value="${jdbc_username}" />
    <property name="password" value="${jdbc_password}" />
    <!-- 初始化连接大小 -->
    <property name="initialSize" value="0" />
    <!-- 连接池最大使用连接数量 -->
    <property name="maxActive" value="20" />
    <!-- 连接池最大空闲 -->
    <property name="maxIdle" value="20" />
    <!-- 连接池最小空闲 -->
    <property name="minIdle" value="0" />
    <!-- 获取连接最大等待时间 -->
    <property name="maxWait" value="60000" />
</bean>    
<!-- 配置数据源Slave -->
<bean name="dataSourceSlave" class="com.alibaba.druid.pool.DruidDataSource" init-method="init" destroy-method="close">
    <property name="url" value="${jdbc_url_slave}" />
    <property name="username" value="${jdbc_username_slave}" />
    <property name="password" value="${jdbc_password_slave}" />
    <!-- 初始化连接大小 -->
    <property name="initialSize" value="0" />
    <!-- 连接池最大使用连接数量 -->
    <property name="maxActive" value="20" />
    <!-- 连接池最大空闲 -->
    <property name="maxIdle" value="20" />
    <!-- 连接池最小空闲 -->
    <property name="minIdle" value="0" />
    <!-- 获取连接最大等待时间 -->
    <property name="maxWait" value="60000" />
</bean>    
<bean id="dataSource" class="net.aazj.util.ThreadLocalRountingDataSource">
    <property name="defaultTargetDataSource" ref="dataSourceMaster" />
    <property name="targetDataSources">
        <map key-type="net.aazj.enums.DataSources">
            <entry key="MASTER" value-ref="dataSourceMaster"/>
            <entry key="SLAVE" value-ref="dataSourceSlave"/>
            <!-- 这里还可以加多个dataSource -->
        </map>
    </property>
</bean>    
<bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
  <property name="dataSource" ref="dataSource" />
  <property name="configLocation" value="classpath:config/mybatis-config.xml" />
  <property name="mapperLocations" value="classpath*:config/mappers/**/*.xml" />
</bean>    
<!-- Transaction manager for a single JDBC DataSource -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
    <property name="dataSource" ref="dataSource" />
</bean>    
<!-- 使用annotation定义事务 -->
<tx:annotation-driven transaction-manager="transactionManager" /> 
<bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">
  <property name="basePackage" value="net.aazj.mapper" />
  <!-- <property name="sqlSessionFactoryBeanName" value="sqlSessionFactory"/> -->
</bean>

上面spring的配置文件中,我们针对master数据库和slave数据库分别定义了dataSourceMaster和dataSourceSlave两个dataSource,然后注入到<bean id="dataSource" class="net.aazj.util.ThreadLocalRountingDataSource"> 中,这样我们的dataSource就可以来根据 key 的不同来选择dataSourceMaster和 dataSourceSlave了。

5)使用Spring AOP 来指定 dataSource 的 key ,从而dataSource会根据key选择 dataSourceMaster 和 dataSourceSlave:

package net.aazj.aop;
 
import net.aazj.enums.DataSources;
import net.aazj.util.DataSourceTypeManager;
 
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;
 
@Aspect    // for aop
@Component // for auto scan
public class DataSourceInterceptor {    
    @Pointcut("execution(public * net.aazj.service..*.getUser(..))")
    public void dataSourceSlave(){};
     
    @Before("dataSourceSlave()")
    public void before(JoinPoint jp) {
        DataSourceTypeManager.set(DataSources.SLAVE);
    }
    // ... ...
}

这里我们定义了一个 Aspect 类,我们使用 @Before 来在符合 @Pointcut("execution(public * net.aazj.service..*.getUser(..))") 中的方法被调用之前,调用 DataSourceTypeManager.set(DataSources.SLAVE) 设置了 key 的类型为 DataSources.SLAVE,所以 dataSource 会根据key=DataSources.SLAVE 选择 dataSourceSlave 这个dataSource。所以该方法对于的sql语句会在slave数据库上执行。

 我们可以不断的扩充 DataSourceInterceptor  这个 Aspect,在中进行各种各样的定义,来为某个service的某个方法指定合适的数据源对应的dataSource。 这样我们就可以使用 Spring AOP 的强大功能来,十分灵活进行配置了。 6)AbstractRoutingDataSource原理剖析 

ThreadLocalRountingDataSource继承了AbstractRoutingDataSource,实现其抽象方法protected abstract Object determineCurrentLookupKey(); 从而实现对不同数据源的路由功能。我们从源码入手分析下其中原理:

public abstract class AbstractRoutingDataSource extends AbstractDataSource implements InitializingBean
AbstractRoutingDataSource 实现了 InitializingBean 那么spring在初始化该bean时,会调用InitializingBean的接口
void afterPropertiesSet() throws Exception; 我们看下AbstractRoutingDataSource是如何实现这个接口的:
 
    @Override
    public void afterPropertiesSet() {
        if (this.targetDataSources == null) {
            throw new IllegalArgumentException("Property 'targetDataSources' is required");
        }
        this.resolvedDataSources = new HashMap<Object, DataSource>(this.targetDataSources.size());
        for (Map.Entry<Object, Object> entry : this.targetDataSources.entrySet()) {
            Object lookupKey = resolveSpecifiedLookupKey(entry.getKey());
            DataSource dataSource = resolveSpecifiedDataSource(entry.getValue());
            this.resolvedDataSources.put(lookupKey, dataSource);
        }
        if (this.defaultTargetDataSource != null) {
            this.resolvedDefaultDataSource = resolveSpecifiedDataSource(this.defaultTargetDataSource);
        }
    }

targetDataSources 是我们在xml配置文件中注入的 dataSourceMaster 和 dataSourceSlave. afterPropertiesSet方法就是使用注入的

dataSourceMaster 和 dataSourceSlave来构造一个HashMap——resolvedDataSources。方便后面根据 key 从该map 中取得对应的dataSource。我们在看下 AbstractDataSource 接口中的 Connection getConnection() throws SQLException; 是如何实现的:   

@Override    

public Connection getConnection() throws SQLException {        

       return determineTargetDataSource().getConnection();    

}

关键在于 determineTargetDataSource(),根据方法名就可以看出,应该此处就决定了使用哪个 dataSource :

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

Object lookupKey = determineCurrentLookupKey(); 该方法是我们实现的,在其中获取ThreadLocal中保存的 key 值。获得了key之后,

在从afterPropertiesSet()中初始化好了的resolvedDataSources这个map中获得key对应的dataSource。而ThreadLocal中保存的 key 值是通过AOP的方式在调用service中相关方法之前设置好的。OK,到此搞定!3. 总结 从本文中我们可以体会到AOP的强大和灵活。 

本文使用的是mybatis,其实使用Hibernate也应该是相似的配置。

转载于:https://my.oschina.net/Sheamus/blog/796651

相关文章:

  • Vue 新手学习实战宝典
  • 初探插头dp
  • OSChina 周六乱弹 ——你们怎么懂我的少年心
  • Oracle内建函数(greatest, least, to_single_byte)
  • Selenium2+python自动化11-定位一组元素find_elements
  • Chapter 2 Open Book——24
  • 如何造一个移动端的联动选择器(二)
  • JFinal输出流的Render
  • 剑指offer五:
  • Android开发环境
  • 移动端解决方案学习记录
  • 条件编译使用实例
  • sql server中分布式查询随笔(链接服务器(sp_addlinkedserver)和远程登录映射(sp_addlinkedsrvlogin)使用小总结)...
  • 架构设计从这5点考虑,能帮后期运维很大忙!
  • 安装最小化Linux,配置桌面
  • 【跃迁之路】【641天】程序员高效学习方法论探索系列(实验阶段398-2018.11.14)...
  • ECMAScript6(0):ES6简明参考手册
  • es6--symbol
  • gcc介绍及安装
  • learning koa2.x
  • LintCode 31. partitionArray 数组划分
  • Lucene解析 - 基本概念
  • Meteor的表单提交:Form
  • MySQL Access denied for user 'root'@'localhost' 解决方法
  • Promise面试题,控制异步流程
  • Redux系列x:源码分析
  • Sass Day-01
  • 大快搜索数据爬虫技术实例安装教学篇
  • 前端相关框架总和
  • 什么软件可以剪辑音乐?
  • 手机端车牌号码键盘的vue组件
  • 原生JS动态加载JS、CSS文件及代码脚本
  • 测评:对于写作的人来说,Markdown是你最好的朋友 ...
  • 京东物流联手山西图灵打造智能供应链,让阅读更有趣 ...
  • #13 yum、编译安装与sed命令的使用
  • #Linux(make工具和makefile文件以及makefile语法)
  • (1)常见O(n^2)排序算法解析
  • (16)Reactor的测试——响应式Spring的道法术器
  • (C++20) consteval立即函数
  • (javascript)再说document.body.scrollTop的使用问题
  • (附源码)基于SpringBoot和Vue的厨到家服务平台的设计与实现 毕业设计 063133
  • (附源码)基于ssm的模具配件账单管理系统 毕业设计 081848
  • (切换多语言)vantUI+vue-i18n进行国际化配置及新增没有的语言包
  • (十) 初识 Docker file
  • (五)大数据实战——使用模板虚拟机实现hadoop集群虚拟机克隆及网络相关配置
  • (译) 函数式 JS #1:简介
  • (原+转)Ubuntu16.04软件中心闪退及wifi消失
  • **Java有哪些悲观锁的实现_乐观锁、悲观锁、Redis分布式锁和Zookeeper分布式锁的实现以及流程原理...
  • .a文件和.so文件
  • .NET 中让 Task 支持带超时的异步等待
  • .NET使用HttpClient以multipart/form-data形式post上传文件及其相关参数
  • .NET文档生成工具ADB使用图文教程
  • .NET中使用Redis (二)
  • /dev下添加设备节点的方法步骤(通过device_create)
  • @拔赤:Web前端开发十日谈