1. 修改 日志控制, 通过redis 做队列获取
This commit is contained in:
parent
b5c8486364
commit
a7158c994f
|
|
@ -18,10 +18,10 @@ import com.chushang.common.core.jackson.JacksonUtils;
|
|||
import com.chushang.common.core.util.IPUtils;
|
||||
import com.chushang.common.core.util.ServletUtils;
|
||||
import com.chushang.common.log.annotation.SysLog;
|
||||
import com.chushang.common.log.constants.LogConstant;
|
||||
import com.chushang.common.log.entity.SysLogEntity;
|
||||
import com.chushang.common.log.enums.BusinessType;
|
||||
import com.chushang.common.log.enums.LogTypeEnum;
|
||||
import com.chushang.common.log.service.SysLogService;
|
||||
import com.chushang.security.utils.SecurityUtils;
|
||||
import com.chushang.security.entity.vo.LoginUser;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
|
|
@ -32,11 +32,11 @@ import org.aspectj.lang.annotation.AfterThrowing;
|
|||
import org.aspectj.lang.annotation.Aspect;
|
||||
import org.aspectj.lang.annotation.Pointcut;
|
||||
import org.aspectj.lang.reflect.MethodSignature;
|
||||
import org.redisson.api.RList;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
|
@ -44,7 +44,6 @@ import javax.servlet.http.HttpServletResponse;
|
|||
import java.lang.reflect.Method;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ThreadPoolExecutor;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
|
||||
|
|
@ -59,7 +58,7 @@ import java.util.stream.Collectors;
|
|||
@RequiredArgsConstructor(onConstructor_ = @Autowired)
|
||||
public class SysLogAspect {
|
||||
|
||||
final SysLogService sysLogService;
|
||||
final RedissonClient redissonClient;
|
||||
|
||||
@Pointcut("@annotation(com.chushang.common.log.annotation.SysLog)")
|
||||
public void logPointCut() {
|
||||
|
|
@ -171,7 +170,10 @@ public class SysLogAspect {
|
|||
sysLogEntity.setException(ex);
|
||||
assert syslog != null;
|
||||
if (syslog.isDatabase()){
|
||||
sysLogService.save(sysLogEntity);
|
||||
// 入队列, 方便在system 统一查询
|
||||
RList<SysLogEntity> list = redissonClient.getList(LogConstant.QUEUE_NAME);
|
||||
list.add(sysLogEntity);
|
||||
// sysLogService.save(sysLogEntity);
|
||||
}else {
|
||||
log.info("log {}", JSONObject.toJSONString(sysLogEntity));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
package com.chushang.common.log.constant;
|
||||
package com.chushang.common.log.constants;
|
||||
|
||||
public interface LogApplicationName {
|
||||
public interface LogConstant {
|
||||
|
||||
/**
|
||||
* 系统
|
||||
|
|
@ -15,4 +15,5 @@ public interface LogApplicationName {
|
|||
*/
|
||||
String TASK = "task-service";
|
||||
|
||||
String QUEUE_NAME = "OPERATION_LOG";
|
||||
}
|
||||
|
|
@ -0,0 +1,83 @@
|
|||
package com.chushang.system.component;
|
||||
|
||||
import com.chushang.common.log.constants.LogConstant;
|
||||
import com.chushang.common.log.entity.SysLogEntity;
|
||||
import com.chushang.system.service.SysLogService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RQueue;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import javax.annotation.PostConstruct;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ScheduledThreadPoolExecutor;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
/**
|
||||
* @auther: zhao
|
||||
* @date: 2024/4/12 17:09
|
||||
*/
|
||||
@Slf4j
|
||||
@Component
|
||||
public class LogRedisComponent {
|
||||
|
||||
private final SysLogService logService;
|
||||
private final RedissonClient redissonClient;
|
||||
public LogRedisComponent(SysLogService logService, RedissonClient redissonClient){
|
||||
log.info("构造器执行");
|
||||
this.logService = logService;
|
||||
this.redissonClient = redissonClient;
|
||||
}
|
||||
private final ScheduledThreadPoolExecutor scheduledThreadPoolExecutor = new ScheduledThreadPoolExecutor(1);
|
||||
@PostConstruct
|
||||
public void redisStart() {
|
||||
log.info("线程执行");
|
||||
Thread startLogQueueThread = startLogQueueThread();
|
||||
scheduledThreadPoolExecutor.scheduleWithFixedDelay(()->{
|
||||
Thread runLog = startLogQueueThread;
|
||||
try {
|
||||
boolean runLogThreadAlive = runLog.isAlive();
|
||||
if (!runLogThreadAlive) {
|
||||
throw new NullPointerException("runLogThread alive false");
|
||||
}
|
||||
} catch (Exception ex) {
|
||||
System.out.println("logQueueThread 重启线程");
|
||||
runLog = startLogQueueThread();
|
||||
}
|
||||
}, 10 ,30, TimeUnit.SECONDS);
|
||||
log.info("RedisLogQueueCollect is starting!");
|
||||
}
|
||||
|
||||
private Thread startLogQueueThread() {
|
||||
Thread logQueueThread = new Thread(this::logQueueThread);
|
||||
logQueueThread.start();
|
||||
return logQueueThread;
|
||||
}
|
||||
|
||||
private void logQueueThread()
|
||||
{
|
||||
while (true) {
|
||||
if (log.isDebugEnabled()){
|
||||
log.debug("读取redis 数据");
|
||||
}
|
||||
try {
|
||||
try {
|
||||
// 每1000 毫秒从 redis 获取一遍 入队数据
|
||||
Thread.sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
log.error("", e);
|
||||
Thread.currentThread().interrupt();
|
||||
}
|
||||
if (!redissonClient.isShutdown()){
|
||||
RQueue<SysLogEntity> list = redissonClient.getQueue(LogConstant.QUEUE_NAME);
|
||||
List<SysLogEntity> reqList = list.poll(5000);
|
||||
logService.saveBatch(reqList);
|
||||
}else {
|
||||
if (log.isDebugEnabled()) log.debug(" Redisson is shutdown");
|
||||
}
|
||||
} catch (RuntimeException e) {
|
||||
log.error("logQueueThread error, 并重启线程!", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -4,7 +4,6 @@ import com.chushang.common.core.exception.utils.AssertUtil;
|
|||
import com.chushang.common.core.web.AjaxResult;
|
||||
import com.chushang.common.excel.utils.ExcelUtils;
|
||||
import com.chushang.common.log.annotation.SysLog;
|
||||
import com.chushang.common.log.constant.LogApplicationName;
|
||||
import com.chushang.common.log.enums.BusinessType;
|
||||
import com.chushang.common.mybatis.page.CommonParam;
|
||||
import com.chushang.common.mybatis.utils.PageResult;
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ import com.chushang.common.core.web.AjaxResult;
|
|||
import com.chushang.common.log.annotation.SysLog;
|
||||
import com.chushang.common.log.entity.dto.ListLogDTO;
|
||||
import com.chushang.common.log.enums.BusinessType;
|
||||
import com.chushang.common.log.service.SysLogService;
|
||||
import com.chushang.system.service.SysLogService;
|
||||
import com.chushang.common.mybatis.utils.PageResult;
|
||||
import com.chushang.security.annotation.RequiresPermissions;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
|
|
|||
|
|
@ -1,4 +1,4 @@
|
|||
package com.chushang.common.log.entity.mapper;
|
||||
package com.chushang.system.mapper;
|
||||
|
||||
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
|
||||
import com.chushang.common.log.entity.SysLogEntity;
|
||||
|
|
@ -9,4 +9,4 @@ public interface SysLogMapper extends BaseMapper<SysLogEntity> {
|
|||
@Delete("TRUNCATE TABLE sys_log")
|
||||
void cleanLog();
|
||||
|
||||
}
|
||||
}
|
||||
|
|
@ -1,4 +1,4 @@
|
|||
package com.chushang.common.log.service;
|
||||
package com.chushang.system.service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
|
|
@ -10,8 +10,10 @@ import com.chushang.common.log.entity.dto.ListLogDTO;
|
|||
import com.chushang.common.log.enums.LogTypeEnum;
|
||||
import com.chushang.common.mybatis.utils.PageResult;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.redisson.api.RQueue;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* by zhaowenyuan create 2022/3/16 15:35
|
||||
|
|
@ -1,11 +1,18 @@
|
|||
package com.chushang.common.log.service.impl;
|
||||
package com.chushang.system.service.impl;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
import com.chushang.common.log.constants.LogConstant;
|
||||
import com.chushang.common.log.entity.SysLogEntity;
|
||||
import com.chushang.common.log.entity.mapper.SysLogMapper;
|
||||
import com.chushang.common.log.service.SysLogService;
|
||||
import com.chushang.system.mapper.SysLogMapper;
|
||||
import com.chushang.system.service.SysLogService;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.redisson.api.RQueue;
|
||||
import org.redisson.api.RedissonClient;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import javax.annotation.Resource;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* by zhaowenyuan create 2022/3/16 15:35
|
||||
|
|
@ -13,8 +20,11 @@ import org.springframework.stereotype.Service;
|
|||
@Slf4j
|
||||
@Service
|
||||
public class SysLogServiceImpl extends ServiceImpl<SysLogMapper, SysLogEntity> implements SysLogService {
|
||||
@Resource
|
||||
RedissonClient redissonClient;
|
||||
@Override
|
||||
public void cleanLog() {
|
||||
baseMapper.cleanLog();
|
||||
}
|
||||
|
||||
}
|
||||
Loading…
Reference in New Issue