【代码优化】SYSTEM:操作日志,增加异步记录

This commit is contained in:
YunaiV 2024-08-31 13:28:30 +08:00
parent 424922b488
commit 56ae4503a6
7 changed files with 45 additions and 28 deletions

View File

@ -7,6 +7,8 @@ import com.baomidou.mybatisplus.autoconfigure.MybatisPlusAutoConfiguration;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler; import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator; import com.baomidou.mybatisplus.core.incrementer.IKeyGenerator;
import com.baomidou.mybatisplus.extension.incrementer.*; import com.baomidou.mybatisplus.extension.incrementer.*;
import com.baomidou.mybatisplus.extension.parser.JsqlParserGlobal;
import com.baomidou.mybatisplus.extension.parser.cache.JdkSerialCaffeineJsqlParseCache;
import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor; import com.baomidou.mybatisplus.extension.plugins.MybatisPlusInterceptor;
import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor; import com.baomidou.mybatisplus.extension.plugins.inner.PaginationInnerInterceptor;
import org.apache.ibatis.annotations.Mapper; import org.apache.ibatis.annotations.Mapper;
@ -16,6 +18,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.ConfigurableEnvironment;
import java.util.concurrent.TimeUnit;
/** /**
* MyBaits 配置类 * MyBaits 配置类
* *
@ -26,6 +30,14 @@ import org.springframework.core.env.ConfigurableEnvironment;
lazyInitialization = "${mybatis.lazy-initialization:false}") // Mapper 懒加载目前仅用于单元测试 lazyInitialization = "${mybatis.lazy-initialization:false}") // Mapper 懒加载目前仅用于单元测试
public class YudaoMybatisAutoConfiguration { public class YudaoMybatisAutoConfiguration {
static {
// 动态 SQL 智能优化支持本地缓存加速解析更完善的租户复杂 XML 动态 SQL 支持静态注入缓存
JsqlParserGlobal.setJsqlParseCache(new JdkSerialCaffeineJsqlParseCache(
(cache) -> cache.maximumSize(1024)
.expireAfterWrite(5, TimeUnit.SECONDS))
);
}
@Bean @Bean
public MybatisPlusInterceptor mybatisPlusInterceptor() { public MybatisPlusInterceptor mybatisPlusInterceptor() {
MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor(); MybatisPlusInterceptor mybatisPlusInterceptor = new MybatisPlusInterceptor();
@ -34,7 +46,7 @@ public class YudaoMybatisAutoConfiguration {
} }
@Bean @Bean
public MetaObjectHandler defaultMetaObjectHandler(){ public MetaObjectHandler defaultMetaObjectHandler() {
return new DefaultDBFieldHandler(); // 自动填充参数类 return new DefaultDBFieldHandler(); // 自动填充参数类
} }

View File

@ -69,7 +69,7 @@ public class ApiSignatureAspect {
// 3. nonce 记入缓存防止重复使用重点二此处需要将 ttl 设定为允许 timestamp 时间差的值 x 2 // 3. nonce 记入缓存防止重复使用重点二此处需要将 ttl 设定为允许 timestamp 时间差的值 x 2
String nonce = request.getHeader(signature.nonce()); String nonce = request.getHeader(signature.nonce());
signatureRedisDAO.setNonce(nonce, signature.timeout() * 2, signature.timeUnit()); signatureRedisDAO.setNonce(appId, nonce, signature.timeout() * 2, signature.timeUnit());
return true; return true;
} }
@ -113,7 +113,7 @@ public class ApiSignatureAspect {
} }
// 3. 检查 nonce 是否存在有且仅能使用一次 // 3. 检查 nonce 是否存在有且仅能使用一次
return signatureRedisDAO.getNonce(nonce) == null; return signatureRedisDAO.getNonce(appId, nonce) == null;
} }
/** /**
@ -165,5 +165,4 @@ public class ApiSignatureAspect {
return sortedMap; return sortedMap;
} }
} }

View File

@ -22,7 +22,7 @@ public class ApiSignatureRedisDAO {
* VALUE 格式String * VALUE 格式String
* 过期时间不固定 * 过期时间不固定
*/ */
private static final String SIGNATURE_NONCE = "api_signature_nonce:%s"; private static final String SIGNATURE_NONCE = "api_signature_nonce:%s:%s";
/** /**
* 签名密钥 * 签名密钥
@ -36,16 +36,16 @@ public class ApiSignatureRedisDAO {
// ========== 验签随机数 ========== // ========== 验签随机数 ==========
public String getNonce(String nonce) { public String getNonce(String appId, String nonce) {
return stringRedisTemplate.opsForValue().get(formatNonceKey(nonce)); return stringRedisTemplate.opsForValue().get(formatNonceKey(appId, nonce));
} }
public void setNonce(String nonce, int time, TimeUnit timeUnit) { public void setNonce(String appId, String nonce, int time, TimeUnit timeUnit) {
stringRedisTemplate.opsForValue().set(formatNonceKey(nonce), "", time, timeUnit); stringRedisTemplate.opsForValue().set(formatNonceKey(appId, nonce), "", time, timeUnit);
} }
private static String formatNonceKey(String key) { private static String formatNonceKey(String appId, String nonce) {
return String.format(SIGNATURE_NONCE, key); return String.format(SIGNATURE_NONCE, appId, nonce);
} }
// ========== 签名密钥 ========== // ========== 签名密钥 ==========

View File

@ -69,7 +69,7 @@ public class ApiSignatureTest {
// 断言结果 // 断言结果
assertTrue(result); assertTrue(result);
// 断言调用 // 断言调用
verify(signatureRedisDAO).setNonce(eq(nonce), eq(120), eq(TimeUnit.SECONDS)); verify(signatureRedisDAO).setNonce(eq(appId), eq(nonce), eq(120), eq(TimeUnit.SECONDS));
} }
} }

View File

@ -11,6 +11,7 @@ import com.mzt.logapi.service.ILogRecordService;
import jakarta.annotation.Resource; import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest; import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import java.util.List; import java.util.List;
@ -28,19 +29,24 @@ public class LogRecordServiceImpl implements ILogRecordService {
private OperateLogApi operateLogApi; private OperateLogApi operateLogApi;
@Override @Override
@Async
public void record(LogRecord logRecord) { public void record(LogRecord logRecord) {
// 1. 补全通用字段
OperateLogCreateReqDTO reqDTO = new OperateLogCreateReqDTO(); OperateLogCreateReqDTO reqDTO = new OperateLogCreateReqDTO();
reqDTO.setTraceId(TracerUtils.getTraceId()); try {
// 补充用户信息 reqDTO.setTraceId(TracerUtils.getTraceId());
fillUserFields(reqDTO); // 补充用户信息
// 补全模块信息 fillUserFields(reqDTO);
fillModuleFields(reqDTO, logRecord); // 补全模块信息
// 补全请求信息 fillModuleFields(reqDTO, logRecord);
fillRequestFields(reqDTO); // 补全请求信息
fillRequestFields(reqDTO);
// 2. 异步记录日志 // 2. 异步记录日志
operateLogApi.createOperateLog(reqDTO); operateLogApi.createOperateLog(reqDTO);
} catch (Throwable ex) {
// 由于 @Async 异步调用这里打印下日志更容易跟进
log.error("[record][url({}) log({}) 发生异常]", reqDTO.getRequestUrl(), reqDTO, ex);
}
} }
private static void fillUserFields(OperateLogCreateReqDTO reqDTO) { private static void fillUserFields(OperateLogCreateReqDTO reqDTO) {

View File

@ -63,6 +63,7 @@ public class BpmModelServiceImpl implements BpmModelService {
@Override @Override
public PageResult<Model> getModelPage(BpmModelPageReqVO pageVO) { public PageResult<Model> getModelPage(BpmModelPageReqVO pageVO) {
ModelQuery modelQuery = repositoryService.createModelQuery(); ModelQuery modelQuery = repositoryService.createModelQuery();
modelQuery.modelTenantId(FlowableUtils.getTenantId());
if (StrUtil.isNotBlank(pageVO.getKey())) { if (StrUtil.isNotBlank(pageVO.getKey())) {
modelQuery.modelKey(pageVO.getKey()); modelQuery.modelKey(pageVO.getKey());
} }
@ -78,7 +79,6 @@ public class BpmModelServiceImpl implements BpmModelService {
return PageResult.empty(count); return PageResult.empty(count);
} }
List<Model> models = modelQuery List<Model> models = modelQuery
.modelTenantId(FlowableUtils.getTenantId())
.orderByCreateTime().desc() .orderByCreateTime().desc()
.listPage(PageUtils.getStart(pageVO), pageVO.getPageSize()); .listPage(PageUtils.getStart(pageVO), pageVO.getPageSize());
return new PageResult<>(models, count); return new PageResult<>(models, count);

View File

@ -97,7 +97,7 @@ public class BpmTaskServiceImpl implements BpmTaskService {
} }
if (ArrayUtil.isNotEmpty(pageVO.getCreateTime())) { if (ArrayUtil.isNotEmpty(pageVO.getCreateTime())) {
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[0])); taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[0]));
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[1])); taskQuery.taskCreatedBefore(DateUtils.of(pageVO.getCreateTime()[1]));
} }
long count = taskQuery.count(); long count = taskQuery.count();
if (count == 0) { if (count == 0) {
@ -119,7 +119,7 @@ public class BpmTaskServiceImpl implements BpmTaskService {
} }
if (ArrayUtil.isNotEmpty(pageVO.getCreateTime())) { if (ArrayUtil.isNotEmpty(pageVO.getCreateTime())) {
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[0])); taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[0]));
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[1])); taskQuery.taskCreatedBefore(DateUtils.of(pageVO.getCreateTime()[1]));
} }
// 执行查询 // 执行查询
long count = taskQuery.count(); long count = taskQuery.count();
@ -141,7 +141,7 @@ public class BpmTaskServiceImpl implements BpmTaskService {
} }
if (ArrayUtil.isNotEmpty(pageVO.getCreateTime())) { if (ArrayUtil.isNotEmpty(pageVO.getCreateTime())) {
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[0])); taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[0]));
taskQuery.taskCreatedAfter(DateUtils.of(pageVO.getCreateTime()[1])); taskQuery.taskCreatedBefore(DateUtils.of(pageVO.getCreateTime()[1]));
} }
// 执行查询 // 执行查询
long count = taskQuery.count(); long count = taskQuery.count();