首页 技术 正文
技术 2022年11月6日
0 收藏 330 点赞 519 浏览 4287 个字

设置数据库主从切换的原因:数据库中经常发生的是“读多写少”,这样读操作对数据库压力比较大,通过采用数据库集群方案, 
一个数据库是主库,负责写;其他为从库,负责读,从而实现读写分离增大数据库的容错率。 
那么,对数据库的要求是: 
1. 读库和写库的数据一致; 
2. 写数据必须写到写库; 
3. 读数据必须到读库;

Spring AOP实现Mysql数据库主从切换的过程:在进入Service之前,使用AOP来做出判断,是使用写库还是读库,判断依据可以根据方法名判断,比如说以”update”, “insert”, “delete”, “save”开头的就走写库,其他的走读库。

Spring AOP实现Mysql数据库主从切换(一主多从)

Spring AOP实现Mysql数据库主从切换(一主多从)

实现主从(一主多从)分离:

Spring AOP实现Mysql数据库主从切换(一主多从)

首先配置主从数据库

Spring AOP实现Mysql数据库主从切换(一主多从)

主程序启动过程中,通过配置文件将主从数据库的URL以及用户名密码加载到内存中:

Spring AOP实现Mysql数据库主从切换(一主多从)

AOP切入点的实现:

Spring AOP实现Mysql数据库主从切换(一主多从)

Spring AOP实现Mysql数据库主从切换(一主多从)

保证每个线程用到的是自己的数据源,使用ThreadLocal来防止并发带来的问题:

Spring AOP实现Mysql数据库主从切换(一主多从)

Spring AOP实现Mysql数据库主从切换(一主多从)

对数据库的主从切换进行调试,最好是分别使用SELECT,UPDATE来调试:

Spring AOP实现Mysql数据库主从切换(一主多从)

Spring AOP实现Mysql数据库主从切换(一主多从)

Spring AOP实现Mysql数据库主从切换(一主多从)

下面的代码是一主多从配置文件的详细解释,而与一主一从的区别在于,一主一从的配置都在数据源配置中完成:

参考链接:https://blog.csdn.net/zbw18297786698/article/details/54343188

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd"> <!--声明:各DataSource的name 及 MapperScannerConfigurer的name,不要随意更改,否则会影响AOP的读写分离正常使用--> <bean id="parentDataSource" class="com.alibaba.druid.pool.DruidDataSource" init-method="init"
destroy-method="close">
<property name="driverClassName" value="${jdbc-driver}"/>
<property name="url" value="${jdbc-url-restaurant}"/>
<property name="username" value="${jdbc-user-restaurant}"/>
<property name="password" value="${jdbc-password-restaurant}"/>
<property name="filters" value="stat"/>
<!-- 连接池最大数量 -->
<property name="maxActive" value="20"/>
<!-- 初始化连接大小 -->
<property name="initialSize" value="1"/>
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="5000"/>
<!-- 连接池最小空闲 -->
<property name="minIdle" value="1"/>
<property name="timeBetweenEvictionRunsMillis" value="3000"/>
<property name="minEvictableIdleTimeMillis" value="180000"/>
<property name="validationQuery" value="SELECT 'x' FROM DUAL"/>
<property name="testWhileIdle" value="true"/>
<property name="testOnBorrow" value="false"/>
<property name="testOnReturn" value="false"/>
<property name="poolPreparedStatements" value="false"/>
<property name="maxPoolPreparedStatementPerConnectionSize" value="20"/>
<!-- 超过时间限制是否回收 -->
<property name="removeAbandoned" value="true"/>
<!-- 超时时间;单位为秒。300秒=5分钟 -->
<property name="removeAbandonedTimeout" value="300"/>
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true"/>
<!--<property name="connectionInitSqls" value="set names utf8mb4;"/>-->
</bean> <!--动态获取数据库-->
<bean id="dsRestaurant_master" parent="parentDataSource">
<property name="url" value="${jdbc-url-restaurant}"/>
<property name="username" value="${jdbc-user-restaurant}"/>
<property name="password" value="${jdbc-password-restaurant}"/>
</bean> <!--restaurant数据源-->
<bean id="dsRestaurant" class="cn.mwee.service.shop.lookup.CustomRoutingDataSource">
<property name="targetDataSources">
<map key-type="java.lang.String">
<!--写库-->
<entry key="master" value-ref="dsRestaurant_master"/>
</map>
</property>
<property name="defaultTargetDataSource" ref="dsRestaurant_master"/>
</bean> <!--restaurant库--><!-- spring和MyBatis完美整合,不需要mybatis的配置映射文件 -->
<bean id="sqlSessionFactoryRestaurant" class="org.mybatis.spring.SqlSessionFactoryBean">
<property name="configLocation" value="classpath:mybatis-config.xml"></property>
<property name="dataSource" ref="dsRestaurant"/>
<!-- 自动扫描mapping.xml文件 -->
<property name="mapperLocations">
<array>
<value>classpath:mybatis/restaurant/*.xml</value>
</array>
</property>
</bean> <!-- Mapper接口所在包名,Spring会自动查找其下的类 -->
<bean id="restaurantScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
<property name="basePackage" value="cn.mwee.service.shop.mapper.restaurant"/>
<property name="sqlSessionFactoryBeanName" value="sqlSessionFactoryRestaurant"/>
</bean> <!--事务管理-->
<bean id="restaurantTxManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dsRestaurant"/>
</bean>
<tx:annotation-driven transaction-manager="restaurantTxManager"/> <!--AOP切面设置 -->
<bean id="masterSlaveAspect" class="cn.mwee.service.shop.util.MasterSlaveAspect"/>
<aop:config>
<aop:aspect ref="masterSlaveAspect" order="1">
<aop:pointcut id="masterSlave"
expression="this(tk.mybatis.mapper.common.Mapper)"/>
<aop:before pointcut-ref="masterSlave" method="doBefore"/>
</aop:aspect>
</aop:config></beans>
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,487
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,486
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,126
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289