优化:启用压缩功能

修复:日志记录无异步处理问题
This commit is contained in:
ruying408
2024-08-04 13:37:40 +08:00
parent 37a75f754b
commit 4ff2c51d6c
5 changed files with 119 additions and 31 deletions

View File

@@ -0,0 +1,13 @@
package com.cool.core.config;
import java.util.concurrent.RejectedExecutionHandler;
import java.util.concurrent.ThreadPoolExecutor;
import lombok.extern.slf4j.Slf4j;
@Slf4j
public class LogDiscardPolicy implements RejectedExecutionHandler {
public void rejectedExecution(Runnable r, ThreadPoolExecutor executor) {
log.warn("logTaskExecutor 当前已超过线程池最大队列容量,拒绝策略为丢弃该线程 {}", r.toString());
}
}

View File

@@ -0,0 +1,28 @@
package com.cool.core.config;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
@Data
@Component
@ConfigurationProperties(prefix = "cool.log")
public class LogProperties {
/**
* 请求参数最大字节,超过请求参数不记录
*/
private int maxByteLength;
/**
* 核心线程数的倍数
*/
private int corePoolSizeMultiplier;
/**
* 最大线程数的倍数
*/
private int maxPoolSizeMultiplier;
/**
* 队列容量的倍数
*/
private int queueCapacityMultiplier;
}

View File

@@ -0,0 +1,33 @@
package com.cool.core.config;
import java.util.concurrent.Executor;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
@Configuration
@RequiredArgsConstructor
public class ThreadPoolConfig {
private final LogProperties logProperties;
@Bean(name = "logTaskExecutor")
public Executor loggingTaskExecutor() {
ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
int corePoolSize = Runtime.getRuntime().availableProcessors() * logProperties.getCorePoolSizeMultiplier();
int maxPoolSize = corePoolSize * logProperties.getMaxPoolSizeMultiplier();
int queueCapacity = maxPoolSize * logProperties.getQueueCapacityMultiplier();
executor.setCorePoolSize(corePoolSize);
executor.setMaxPoolSize(maxPoolSize);
executor.setQueueCapacity(queueCapacity);
executor.setThreadNamePrefix("logTask-");
// 自定义拒绝策略
executor.setRejectedExecutionHandler(new LogDiscardPolicy());
executor.initialize();
return executor;
}
}

View File

@@ -6,6 +6,7 @@ import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.cool.core.base.BaseServiceImpl;
import com.cool.core.config.LogProperties;
import com.cool.core.security.IgnoredUrlsProperties;
import com.cool.core.util.CoolSecurityUtil;
import com.cool.core.util.IPUtils;
@@ -20,9 +21,8 @@ import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import jakarta.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.concurrent.Executor;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;
/**
@@ -39,8 +39,10 @@ public class BaseSysLogServiceImpl extends BaseServiceImpl<BaseSysLogMapper, Bas
private final IPUtils ipUtils;
@Value("${cool.log.maxJsonLength:1024}")
private int maxJsonLength;
private final LogProperties logProperties;
private final Executor logTaskExecutor;
private static final AntPathMatcher antPathMatcher = new AntPathMatcher();
@Override
@@ -76,22 +78,8 @@ public class BaseSysLogServiceImpl extends BaseServiceImpl<BaseSysLogMapper, Bas
return;
}
String ipAddr = ipUtils.getIpAddr(request);
JSONObject userInfo = CoolSecurityUtil.getAdminUserInfo(requestParams);
Long userId = null;
if (userInfo != null) {
userId = userInfo.getLong("userId");
}
JSONObject newJSONObject = JSONUtil.parseObj(JSONUtil.toJsonStr(requestParams));
newJSONObject.remove("tokenInfo");
newJSONObject.remove("refreshToken");
newJSONObject.remove("body");
if (newJSONObject.toString().getBytes().length > maxJsonLength) {
// 超过指定
newJSONObject.clear();
}
recordAsync(requestURI, ipAddr, userId, newJSONObject);
// 异步记录日志
recordAsync(ipAddr, requestURI, requestParams);
}
private boolean isIgnoreUrl(String requestURI) {
@@ -99,16 +87,32 @@ public class BaseSysLogServiceImpl extends BaseServiceImpl<BaseSysLogMapper, Bas
.anyMatch(url -> antPathMatcher.match(url, requestURI));
}
public void recordAsync(String ipAddr, String requestURI, JSONObject requestParams) {
logTaskExecutor.execute(() -> {
JSONObject userInfo = CoolSecurityUtil.getAdminUserInfo(requestParams);
@Async
public void recordAsync(String requestURI, String ip, Long userId, JSONObject params) {
BaseSysLogEntity logEntity = new BaseSysLogEntity();
logEntity.setAction(requestURI);
logEntity.setIp(ip);
if (userId != null) {
logEntity.setUserId(userId);
}
logEntity.setParams(params);
save(logEntity);
Long userId = null;
if (userInfo != null) {
userId = userInfo.getLong("userId");
}
JSONObject newJSONObject = JSONUtil.parseObj(JSONUtil.toJsonStr(requestParams));
newJSONObject.remove("tokenInfo");
newJSONObject.remove("refreshToken");
newJSONObject.remove("body");
if (newJSONObject.toString().getBytes().length > logProperties.getMaxByteLength()) {
// 超过指定
newJSONObject.clear();
}
BaseSysLogEntity logEntity = new BaseSysLogEntity();
logEntity.setAction(requestURI);
logEntity.setIp(ipAddr);
if (userId != null) {
logEntity.setUserId(userId);
}
logEntity.setParams(newJSONObject);
save(logEntity);
});
}
}

View File

@@ -2,6 +2,9 @@ server:
port: 8001
servlet:
context-path: /
compression:
enabled: true
mime-types: application/json,application/xml,text/html,text/plain
spring:
application:
@@ -136,7 +139,14 @@ cool:
base-url: http://127.0.0.1:${server.port}/upload
# 系统日志请求参数超过1024字节 就不记录,避免日志过大
log:
maxJsonLength: 1024
# 请求参数最大字节,超过请求参数不记录
max-byte-length: 1024
# 核心线程数的倍数
core-pool-size-multiplier: 2
# 最大线程数的倍数
max-pool-size-multiplier: 3
# 队列容量的倍数
queue-capacity-multiplier: 3
# AutoTable配置根据实体类自动生成表
auto-table:
show-banner: false