当前位置:网站首页>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
边栏推荐
- The normal form of the database (first normal form, second normal form, third normal form, BCNF normal form) "recommended collection"
- Dialogue with Zhuang Biaowei: The first lesson of open source
- Linux check redis version (check mongodb version)
- TRACE32 - Common Operations
- R language ggplot2 visualization: use the ggmapplot function of the ggpubr package to visualize the MA plot (MA-plot), the font.legend parameter and the font.main parameter to set the title and legend
- Deployment application life cycle and Pod health check
- 01 邂逅typescript,环境搭建
- [MySQL] Mysql paradigm and the role of foreign keys
- Synchronized and volatile interview brief summary
- C程序是如何跑起来的01 —— 普通可执行文件的构成
猜你喜欢

TRACE32 - Common Operations

type of timer

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

定时器的类型

Kubernetes principle analysis and practical application manual, too complete

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

The 2nd China PWA Developer Day

After Grafana is installed, the web opens and reports an error

tooltips使用教程(鼠标悬停时显示提示)

网银被盗?这篇文章告诉你如何安全使用网银
随机推荐
Handling write conflicts under multi-master replication (4) - multi-master replication topology
Replication Latency Case (3) - Monotonic Read
腾讯云部署----DevOps
R language moves time series data forward or backward (custom lag or lead period): use the lag function in the dplyr package to move the time series data forward by one day (set the parameter n to a p
6-22漏洞利用-postgresql数据库密码破解
[MySQL] Mysql paradigm and the role of foreign keys
The principle of hough transform detection of straight lines (opencv hough straight line detection)
做事软件开发-法的重要性所在以及合理结论的认识
Matlab矩阵基本操作(定义,运算)
t-sne 数据可视化网络中的部分参数+
org.apache.jasperException(could not initialize class org)
C程序是如何跑起来的01 —— 普通可执行文件的构成
WeChat chat record search in a red envelope
ansible study notes 02
对话庄表伟:开源第一课
JVM parameter analysis Xmx, Xms, Xmn, NewRatio, SurvivorRatio, PermSize, PrintGC "recommended collection"
border控件的使用
7. Summary of common interview questions
软件实现AT命令操作过程
org.apache.jasperException(could not initialize class org)