Browse Source

优化系统性能:实现读写分离、修复公海池逻辑、调整登录动画时间、优化简道云按钮逻辑

KH
Trae AI 1 month ago
parent
commit
473b64d05d
  1. 66
      web/src/main/java/com/example/web/aspect/ReadWriteSeparationAspect.java
  2. 18
      web/src/main/java/com/example/web/config/DataSourceConfig.java
  3. 22
      web/src/main/resources/application.yaml
  4. 12
      web/src/main/resources/mapper/UsersMapper.xml
  5. 899
      web/src/main/resources/static/index.html
  6. 4
      web/src/main/resources/static/login.html

66
web/src/main/java/com/example/web/aspect/ReadWriteSeparationAspect.java

@ -0,0 +1,66 @@
package com.example.web.aspect;
import com.example.web.config.DataSourceContextHolder;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class ReadWriteSeparationAspect {
private static final Logger logger = LoggerFactory.getLogger(ReadWriteSeparationAspect.class);
// 定义切入点,拦截所有Mapper方法
@Pointcut("execution(* com.example.web.mapper.*Mapper.*(..))")
public void mapperPointcut() {
}
@Before("mapperPointcut()")
public void beforeMapperMethod(JoinPoint joinPoint) {
String methodName = joinPoint.getSignature().getName();
String className = joinPoint.getTarget().getClass().getSimpleName();
// 获取当前使用的数据源
String currentDataSource = DataSourceContextHolder.getDataSource();
// 根据方法名判断是读操作还是写操作
if (isReadOperation(methodName)) {
// 读操作使用从库
String slaveDataSource = currentDataSource.contains("wechat") ? "wechat-slave" : "primary-slave";
DataSourceContextHolder.setDataSource(slaveDataSource);
logger.debug("切换到从库数据源: {},执行方法: {}.{}", slaveDataSource, className, methodName);
} else {
// 写操作使用主库
String masterDataSource = currentDataSource.contains("wechat") ? "wechat" : "primary";
DataSourceContextHolder.setDataSource(masterDataSource);
logger.debug("切换到主库数据源: {},执行方法: {}.{}", masterDataSource, className, methodName);
}
}
@After("mapperPointcut()")
public void afterMapperMethod() {
// 方法执行完成后清除数据源上下文
DataSourceContextHolder.clearDataSource();
}
/**
* 判断是否为读操作
* @param methodName 方法名
* @return 是否为读操作
*/
private boolean isReadOperation(String methodName) {
// 通常查询方法以find、get、select、query、list、count等开头的方法为读操作
return methodName.startsWith("find") ||
methodName.startsWith("get") ||
methodName.startsWith("select") ||
methodName.startsWith("query") ||
methodName.startsWith("list") ||
methodName.startsWith("count");
}
}

18
web/src/main/java/com/example/web/config/DataSourceConfig.java

@ -19,13 +19,27 @@ public class DataSourceConfig {
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
// userlogin从库数据源
@Bean(name = "primarySlaveDataSource")
@ConfigurationProperties(prefix = "spring.datasource.primary-slave")
public DataSource primarySlaveDataSource() {
return DataSourceBuilder.create().build();
}
// 第二个数据源(wechat_app)
@Bean(name = "wechatDataSource")
@ConfigurationProperties(prefix = "spring.datasource.wechat")
public DataSource wechatDataSource() {
return DataSourceBuilder.create().build();
}
// wechat_app从库数据源
@Bean(name = "wechatSlaveDataSource")
@ConfigurationProperties(prefix = "spring.datasource.wechat-slave")
public DataSource wechatSlaveDataSource() {
return DataSourceBuilder.create().build();
}
// 动态数据源配置
@Primary
@ -35,7 +49,9 @@ public class DataSourceConfig {
dynamicDataSource.setDefaultTargetDataSource(primaryDataSource());
Map<Object, Object> dataSources = new HashMap<>();
dataSources.put("primary", primaryDataSource());
dataSources.put("primary-slave", primarySlaveDataSource());
dataSources.put("wechat", wechatDataSource());
dataSources.put("wechat-slave", wechatSlaveDataSource());
dynamicDataSource.setTargetDataSources(dataSources);
return dynamicDataSource;
}

22
web/src/main/resources/application.yaml

@ -11,6 +11,17 @@ spring:
idle-timeout: 600000
minimum-idle: 5
maximum-pool-size: 20
# userlogin从库数据库
primary-slave:
jdbc-url: jdbc:mysql://1.95.162.61:3306/userlogin?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: schl@2025
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
max-lifetime: 1200000
idle-timeout: 600000
minimum-idle: 5
maximum-pool-size: 30
# wechat_app数据库
wechat:
jdbc-url: jdbc:mysql://1.95.162.61:3306/wechat_app?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
@ -22,6 +33,17 @@ spring:
idle-timeout: 600000
minimum-idle: 5
maximum-pool-size: 20
# wechat_app从库数据库
wechat-slave:
jdbc-url: jdbc:mysql://1.95.162.61:3306/wechat_app?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&useSSL=false&allowPublicKeyRetrieval=true
username: root
password: schl@2025
driver-class-name: com.mysql.cj.jdbc.Driver
hikari:
max-lifetime: 1200000
idle-timeout: 600000
minimum-idle: 5
maximum-pool-size: 30
server:
port: 8083

12
web/src/main/resources/mapper/UsersMapper.xml

@ -181,7 +181,13 @@
<select id="findPublicAllWithPagination" resultType="com.example.web.entity.Users">
SELECT u.* FROM users u
JOIN usermanagements um ON u.userId = um.userId
WHERE u.type != 'Colleague'
AND (um.managercompany IS NULL OR um.managercompany = '')
AND (um.managerdepartment IS NULL OR um.managerdepartment = '')
AND (um.organization IS NULL OR um.organization = '')
AND (um.role IS NULL OR um.role = '')
AND (um.userName IS NULL OR um.userName = '')
<if test="phoneNumber != null and phoneNumber != ''">
AND u.phoneNumber = #{phoneNumber}
</if>
@ -191,7 +197,13 @@
<select id="countPublicAll" resultType="java.lang.Integer">
SELECT COUNT(*) FROM users u
JOIN usermanagements um ON u.userId = um.userId
WHERE u.type != 'Colleague'
AND (um.managercompany IS NULL OR um.managercompany = '')
AND (um.managerdepartment IS NULL OR um.managerdepartment = '')
AND (um.organization IS NULL OR um.organization = '')
AND (um.role IS NULL OR um.role = '')
AND (um.userName IS NULL OR um.userName = '')
<if test="phoneNumber != null and phoneNumber != ''">
AND u.phoneNumber = #{phoneNumber}
</if>

899
web/src/main/resources/static/index.html

File diff suppressed because it is too large

4
web/src/main/resources/static/login.html

@ -292,12 +292,12 @@
loadingOverlay.appendChild(loadingText);
document.body.appendChild(loadingOverlay);
// 延迟3秒后跳转到主页面
// 延迟2秒后跳转到主页面
console.log('准备跳转到index.html...');
setTimeout(() => {
// 优化:使用绝对路径跳转
window.location.href = '/KH/index.html';
}, 3000);
}, 2000);
} else {
console.log('登录失败,错误信息:', data.message);
errorMessage.textContent = data.message;

Loading…
Cancel
Save