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

作者:digdeep

出处:https://www.cnblogs.com/digdeep/p/4512368.html

43e89e0c31d4bfc5e64750aab543273b.png

热门推荐

vue+websocket+Springboot实现的即时通信开源项目

springboot炸翔版CMS开源系统

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

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>            <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    bean>            <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>        <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>            <bean id="transactionManager_2" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource_2" />    bean>            <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 dataSourceTypes = new ThreadLocal(){        @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" />            <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>            <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"/>                            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>            <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">        <property name="dataSource" ref="dataSource" />    bean>            <tx:annotation-driven transaction-manager="transactionManager" />     <bean class="org.mybatis.spring.mapper.MapperScannerConfigurer">      <property name="basePackage" value="net.aazj.mapper" />          bean>

上面spring的配置文件中,我们针对master数据库和slave数据库分别定义了dataSourceMaster和dataSourceSlave两个dataSource,然后注入到> 中,这样我们的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@Order(0)  // execute before @Transactionalpublic 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数据库上执行(经网友老刘1987提醒,这里存在多个Aspect之间的一个执行顺序的问题,必须保证切换数据源的Aspect必须在@Transactional这个Aspect之前执行,所以这里使用了@Order(0)来保证切换数据源先于@Transactional执行)。

我们可以不断的扩充 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(this.targetDataSources.size());        for (Map.Entry 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,到此搞定!

7)扩展 ThreadLocalRountingDataSource

上面我们只是实现了 master-slave 数据源的选择。如果有多台 master 或者有多台 slave。多台master组成一个HA,要实现当其中一台master挂了是,自动切换到另一台master,这个功能可以使用LVS/Keepalived来实现,也可以通过进一步扩展ThreadLocalRountingDataSource来实现,可以另外加一个线程专门来每个一秒来测试mysql是否正常来实现。同样对于多台slave之间要实现负载均衡,同时当一台slave挂了时,要实现将其从负载均衡中去除掉,这个功能既可以使用LVS/Keepalived来实现,同样也可以通过近一步扩展ThreadLocalRountingDataSource来实现。

3. 总结

从本文中我们可以体会到AOP的强大和灵活。

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

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/574428.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

(九十)使用多个storyboard+代码实现控制器的分开管理

使用单个storyboard会使得项目难与管理&#xff0c;使用纯代码又会过于麻烦&#xff0c;因此如果能将二者结合起来&#xff0c;并且使用多个storyboard&#xff0c;会使得项目简单简单、方便许多。 下面以一个简单的视图关系为例&#xff0c;介绍多个storyboard的用法。 ①有pa…

吗 支持windows_M1能否原生支持Windows 苹果把问题抛给了微软

关于苹果M1处理器近期已经有了非常多的报道&#xff0c;人们对其出色的性能表现以及较低的功耗还是充满了期待。那么最后一个也是最重要的一个问题&#xff0c;基于苹果M1处理器的电脑能原生支持微软Windows吗&#xff1f;毕竟很多人买回苹果MacBook第一件事就是安装Windows 10…

数据库设计五要点 让数据库设计更加规范

通常情况下&#xff0c;可以从两个方面来判断数据库是否设计的比较规范。一是看看是否拥有大量的窄表&#xff0c;二是宽表的数量是否足够的少。若符合这两个条件&#xff0c;则可以说明这个数据库的规范化水平还是比较高的。当然这是两个泛泛而谈的指标。为了达到数据库设计规…

4怎么放大字体_win8.1系统如何放大所有字体?

电脑安装上[color#070c0 !important]win8.1正式版系统&#xff0c;发现桌面的字体和网页的字体比较小&#xff0c;看起来比较吃力&#xff0c;从而影响操作。一般win8.1系统字体都默认设置好的&#xff0c;不过用户可以进行放大&#xff0c;那么win8.1系统怎么放大所有字体&…

进程 zabbix_Zabbix监控在windows的进程(非进程数)

场景介绍&#xff1a;小Z同学最近遇到个项目需求&#xff0c;需求是用zabbix监控运行在windows的进程。然鹅&#xff0c;当小Z同学在网上搜了一大堆&#xff0c;发现基本上都是使用官方的proc.num(***.exe)键值拿到进程数量&#xff0c;很显然&#xff0c;这并不符合客户实际的…

spring配置数据库

一.加载数据库驱动 1.利用hibernate.properties文件加载 <bean class"org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">   <property name"locations">     <list>       <value>classpath…

golang 切片 接口_如何理解Golang中的接口?

个人认为&#xff0c;要理解 Go 的接口&#xff0c;一定先了解下鸭子模型。鸭子模型那什么鸭子模型&#xff1f;鸭子模型的解释&#xff0c;通常会用了一个非常有趣的例子&#xff0c;一个东西究竟是不是鸭子&#xff0c;取决于它的能力。游泳起来像鸭子、叫起来也像鸭子&#…

batchnomal_pytorch的batch normalize使用详解

torch.nn.BatchNorm1d()1、BatchNorm1d(num_features, eps 1e-05, momentum0.1, affineTrue)对于2d或3d输入进行BN。在训练时&#xff0c;该层计算每次输入的均值和方差&#xff0c;并进行平行移动。移动平均默认的动量为0.1。在验证时&#xff0c;训练求得的均值/方差将用于标…

phpcms文件所需权限

转载于:https://www.cnblogs.com/zhongheng/p/4684087.html

mysql 设置大小写_mysql修改大小写参数注意事项

原由&#xff1a;数据库中原本参数lower_case_table_names的值为0&#xff0c;应开发要求需要修改为不区分大小写&#xff0c;即修改为1。但是修改完之后&#xff0c;发现本来建立的大写字母的表查不到。修改过程1&#xff0c;在参数文件中修改lower_case_table_names12&#x…

论文中如何写mysql的介绍_mysql优化写论文,从哪几方面入手啊解决方法

引用第7章&#xff1a;优化目录7.1. 优化概述7.1.1. MySQL设计局限与折衷7.1.2. 为可移植性设计应用程序7.1.3. 我们已将MySQL用在何处&#xff1f;7.1.4. MySQL基准套件7.1.5. 使用自己的基准7.2. 优化SELECT语句和其它查询7.2.1. EXPLAIN语法(获取SELECT相关信息)7.2.2. 估计…

如何理解java回电话

同android中间Button的setOnClickListener这个回调案例&#xff1b; Button button (Button)this.findViewById(R.id.button); button.setOnClickListener(new Button.OnClickListener() {//回调函数overridepublicvoid onClick(View v) {buttonTextView.setText("按钮被…

python mysql 时间比较大小_python时间函数与mysql时间函数转换

时间相关操作&#xff0c;时间有三种表示方式&#xff1a;时间戳 1970年1月1日之后的秒&#xff0c;即&#xff1a;time.time()格式化的字符串 2014-11-11 11:11&#xff0c; 即&#xff1a;time.strftime(%Y-%m-%d)结构化时间 元组包含了&#x…

springMVC 前后台日期格式传值解决方式之二(共二) @InitBinder的使用

关于springmvc日期问题的解决方式 除了本博客的【springMVC 前后台日期格式传值解决方式之 DateTimeFormat的使用和配置】一文&#xff0c; 还有如下这种方式: 在Controller里加上这段代码&#xff1a; 1 InitBinder2 public void initBinder(ServletRequestDataBinder…

mysql npe问题_MySQL为Null会导致5个问题,个个致命!

本文转载自微信公众号「Java中文社群」&#xff0c;作者磊哥。转载本文请联系Java中文社群公众号。正式开始之前&#xff0c;我们先来看下 MySQL 服务器的配置和版本号信息&#xff0c;如下图所示&#xff1a;“兵马未动粮草先行”&#xff0c;看完了相关的配置之后&#xff0c…

line-height 与垂直居中!

在此之前&#xff0c;对于line-height 与垂直居中的问题&#xff0c;经常碰到。 比如&#xff0c;图片与span在同一个box中的时候&#xff0c;竟然会各种偏移。要想达到理想的效果真的是各种难。 有时间&#xff0c;决定认真的啃一啃。 一 line-heigtht: 1. line-height: 顾名…

暑假集训-个人赛第六场

这场比赛&#xff0c;不提也罢。 A转载于:https://www.cnblogs.com/macinchang/p/4693690.html

mysql优化插入速度的方法_优化MySQL插入方法的五个妙招

以下是涉及到插入表格的查询的5种改进方法&#xff1a;1)使用LOAD DATA INFILE从文本下载数据这将比使用插入语句快20倍。2)使用带有多个VALUES列表的INSERT语句一次插入几行这将比使用一个单行插入语句快几倍。调整bulk_insert_buffer_size变量也能提高(向包含行的表格中)插入…

数据结构导论(一)

导读&#xff1a;在看书前&#xff0c;我对这本书的内容&#xff0c;是 这么想的&#xff1a;数据结构&#xff0c;那大概就是关于数据和结构的东西。而讲到数据&#xff0c;估计会说到数据类型&#xff0c;数据定义&#xff0c;数据存储等方面。然后在结构方面&#xff0c;就不…

基于java的汽车维修保养智能预约系统论文

摘 要 信息数据从传统到当代&#xff0c;是一直在变革当中&#xff0c;突如其来的互联网让传统的信息管理看到了革命性的曙光&#xff0c;因为传统信息管理从时效性&#xff0c;还是安全性&#xff0c;还是可操作性等各个方面来讲&#xff0c;遇到了互联网时代才发现能补上自古…