当前位置:网站首页>jeecg master-slave database read-write separation configuration "recommended collection"
jeecg master-slave database read-write separation configuration "recommended collection"
2022-07-31 15:49:00 【Full stack programmer webmaster】
大家好,又见面了,我是你们的朋友全栈君.
1、修改Dbconfig.properties数据库配置文件: 注意:The names of the attributes of the slave library should be distinguished from the attribute names of the master library,The property name will be used later in the configuration file.
#数据库配置 主库-写入库
#MySQL
hibernate.dialect=org.hibernate.dialect.MySQLDialect
validationQuery.sqlserver=SELECT 1
jdbc.url=jdbc\:mysql\://127.0.0.1\:3306/database001?useUnicode\=true&characterEncoding\=UTF-8
jdbc.username=root
jdbc.password=root
jdbc.dbType=mysql
#数据库配置 从库-读库
#MySQL
jdbc.url.slave=jdbc\:mysql\://127.0.0.1\:3306/database002?useUnicode\=true&characterEncoding\=UTF-8
jdbc.username.slave=root
jdbc.password.slave=root
jdbc.dbType.slave=mysql
#更新|创建|验证数据库表结构|不作改变 默认update(create,validate,none)
hibernate.hbm2ddl.auto=none2、修改spring-mvc-hibernate.xml配置文件 2.1、配置数据源2:Copy the original data source configuration,做以下修改: 1) 数据源的名称name需要重新命名; 2) Set the link property of the database to Dbconfig.properties中数据源2的属性值.
<!-- 配置数据源2 -->
<bean name="dataSource_slave" class="com.alibaba.druid.pool.DruidDataSource"
init-method="init" destroy-method="close">
<property name="url" value="${jdbc.url.jeewx.slave}" />
<property name="username" value="${jdbc.username.jeewx.slave}" />
<property name="password" value="${jdbc.password.jeewx.slave}" />
<!-- 初始化连接大小 -->
<property name="initialSize" value="0" />
<!-- 连接池最大使用连接数量 -->
<property name="maxActive" value="250" />
<!-- 连接池最大空闲 -->
<property name="maxIdle" value="20" />
<!-- 连接池最小空闲 -->
<property name="minIdle" value="5" />
<!-- 获取连接最大等待时间 -->
<property name="maxWait" value="60000" />
<!-- <property name="poolPreparedStatements" value="true" /> <property
name="maxPoolPreparedStatementPerConnectionSize" value="33" /> -->
<property name="validationQuery" value="${validationQuery.sqlserver}" />
<property name="testOnBorrow" value="false" />
<property name="testOnReturn" value="false" />
<property name="testWhileIdle" value="true" />
<!-- 配置间隔多久才进行一次检测,检测需要关闭的空闲连接,单位是毫秒 -->
<property name="timeBetweenEvictionRunsMillis" value="60000" />
<!-- 配置一个连接在池中最小生存的时间,单位是毫秒 -->
<property name="minEvictableIdleTimeMillis" value="25200000" />
<!-- 打开removeAbandoned功能 -->
<property name="removeAbandoned" value="true" />
<!-- 1800秒,也就是30分钟 -->
<property name="removeAbandonedTimeout" value="1800" />
<!-- 关闭abanded连接时输出错误日志 -->
<property name="logAbandoned" value="true" />
<!-- 开启Druid的监控统计功能 -->
<property name="filters" value="stat" />
<!--<property name="filters" value="mergeStat" /> -->
<!-- OracleThe connection is to get field annotations -->
<property name="connectProperties">
<props>
<prop key="remarksReporting">true</prop>
</props>
</property>
</bean>2.2、Configure the data source set:
<!-- 数据源集合 -->
<bean id="dataSource"
class="org.jeecgframework.core.extend.datasource.DynamicDataSource">
<property name="targetDataSources">
<map key-type="org.jeecgframework.core.extend.datasource.DataSourceType">
<entry key="dataSource_jeecg" value-ref="dataSource_jeecg" />
<entry key="dataSource_slave" value-ref="dataSource_slave" />
<!-- <entry key="mapdataSource" value-ref="mapdataSource" /> -->
</map>
</property>
<property name="defaultTargetDataSource" ref="dataSource_jeecg" />
</bean>2.3、定义AOPslicer,关于AOPThe grammar of Baidu can be consulted by yourself,The focus is on the following: (1) org.jeecgframework.core.interceptors.DataSourceAspect类需要实现,This class is mainly used to dynamically switch the database; (2) The expressions here are mainly set for this packageCommonServiceAll methods are faceted; (3) DataSourceAspect类中必须实现before方法,The specific implementation of dynamic switching of the database is controlled here.
<!-- 定义AOP切面处理器 -->
<bean class="org.jeecgframework.core.interceptors.DataSourceAspect" id="dataSourceAspect" />
<aop:config>
<!-- 定义切面,CommonService的所有方法 -->
<aop:pointcut id="txPointcut" expression="execution(* org.jeecgframework.core.common.service.*.*(..))" />
<!-- 将切面应用到自定义slicer上,-9999保证该切面优先级最高执行 -->
<aop:aspect ref="dataSourceAspect" order="-9999">
<aop:before method="before" pointcut-ref="txPointcut" />
</aop:aspect>
</aop:config>3、实现类org.jeecgframework.core.interceptors.DataSourceAspect:
package org.jeecgframework.core.interceptors;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.jeecgframework.core.extend.datasource.DataSourceContextHolder;
/**
* 定义数据源的AOP切面,通过该Service的方法名判断是应该走读库还是写库
*
* @author yaoxy
*
*/
public class DataSourceAspect {
/**
* 在进入Service方法之前执行
*
* @param point 切面对象
*/
public void before(JoinPoint point) {
// 获取到当前执行的方法名
String methodName = point.getSignature().getName();
if (isSlave(methodName)) {
// 标记为读库
DataSourceContextHolder.markSlave();
} else {
// 标记为写库
DataSourceContextHolder.markMaster();
}
}
/**
* 判断是否为读库
*
* @param methodName
* @return
*/
private Boolean isSlave(String methodName) {
// 方法名以query、find、get开头的方法名走从库
return StringUtils.startsWithAny(methodName, "query","find","get");
}
}实现原理: 1) 判断AOP切面处理的CommonServiceWhether the name of the method called in the class contains the query keyword at the beginning; 2) 如果CommonServiceThe called method contains the query keyword,调用DataSourceContextHolderSwitch the library to read from the library,Otherwise switch to the main library for write operations. 4、DataSourceContextHolderAdded the method of master-slave switching
ackage org.jeecgframework.core.extend.datasource;
/**
*类名:DataSourceContextHolder.java
*功能:Class to get and set the context,主要负责改变上下文数据源的名称
*/
public class DataSourceContextHolder {
//读库对应的数据源key
private static final ThreadLocal contextHolder=new ThreadLocal();
public static void setDataSourceType(DataSourceType dataSourceType){
contextHolder.set(dataSourceType);
}
public static DataSourceType getDataSourceType(){
return (DataSourceType) contextHolder.get();
}
public static void clearDataSourceType(){
contextHolder.remove();
}
/**
* 标记写库
*/
public static void markMaster(){
setDataSourceType(DataSourceType.dataSource_jeecg);
}
/**
* 标记读库
*/
public static void markSlave(){
setDataSourceType(DataSourceType.dataSource_slave);
}
}5、Enumeration class for data sourcesDataSourcetypeAppend the newly added slave database data source name to
package org.jeecgframework.core.extend.datasource;
public enum DataSourceType {
dataSource_jeecg,dataSource_enter,dataSource4,mapdataSource,dataSource_slave
//dataSource_jeecg:主库名,dataSource_slave:从库名.
}总结: The read and write analysis of the application layer involves the following5个文件,see folder【Application layer read-write separation setting file】:
实现的原理:利用Spring AOPThe principle of slice processing,Before the method that operates on the database is executed, it is judged whether to read or write according to the name of the method,进行主从/Switch between read and write databases.
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/127994.html原文链接:https://javaforall.cn
边栏推荐
- 腾讯云部署----DevOps
- ansible学习笔记02
- 单细胞测序流程(单细胞rna测序)
- Implementing click on the 3D model in RenderTexture in Unity
- 2020微信小程序反编译教程(小程序反编译源码能用吗)
- Deployment应用生命周期与Pod健康检查
- Replication Latency Case (3) - Monotonic Read
- 网银被盗?这篇文章告诉你如何安全使用网银
- The new BMW 3 Series is on the market, with safety and comfort
- OPPO在FaaS领域的探索与思考
猜你喜欢

Premiere Pro 2022 for (pr 2022)v22.5.0

全新宝马3系上市,安全、舒适一个不落

The 2nd China PWA Developer Day

mongo进入报错

How useful is four-quadrant time management?

i.MX6ULL驱动开发 | 33 - NXP原厂网络设备驱动浅读(LAN8720 PHY)

「秋招系列」MySQL面试核心25问(附答案)

Public Key Retrieval is not allowed error solution when DBeaver connects to MySQL 8.x

.NET 20周年专访 - 张善友:.NET 技术是如何赋能并改变世界的

长得很怪的箱图
随机推荐
更新数据表update
org.apache.jasperException(could not initialize class org)
Codeforces Round #796 (Div. 2)(A-D)
C程序是如何跑起来的01 —— 普通可执行文件的构成
Public Key Retrieval is not allowed error solution when DBeaver connects to MySQL 8.x
Deployment application life cycle and Pod health check
数据库的范式(第一范式,第二范式,第三范式,BCNF范式)「建议收藏」
ansible学习笔记02
Doing things software development - the importance of law and understanding of reasonable conclusions
多主复制下处理写冲突(4)-多主复制拓扑
Implementing click on the 3D model in RenderTexture in Unity
多主复制的适用场景(2)-需离线操作的客户端和协作编辑
Browser's built-in color picker
vb中如何连接mysql_vb怎么连接数据库「建议收藏」
做事软件开发-法的重要性所在以及合理结论的认识
Handling write conflicts under multi-master replication (4) - multi-master replication topology
SQL、HQL、JPQL 到底有什么区别
[CUDA study notes] First acquaintance with CUDA
01 Encounter typescript, build environment
ML.NET相关资源整理