异步日志优化

This commit is contained in:
liuweijw
2018-04-15 15:52:18 +08:00
parent 45157da638
commit 11d1824f30
3 changed files with 8 additions and 23 deletions

View File

@@ -13,8 +13,6 @@ import com.github.liuweijw.core.beans.system.AuthLog;
import com.github.liuweijw.core.beans.system.Log;
import com.github.liuweijw.core.commons.constants.CommonConstant;
import com.github.liuweijw.core.commons.constants.MqQueueConstant;
import com.github.liuweijw.core.commons.jwt.JwtUtil;
import com.github.liuweijw.core.configuration.JwtConfiguration;
/**
* 日志队列消息监听:消息对象必须是经过序列化操作的对象
@@ -28,15 +26,10 @@ public class LogRabbitListener {
@Autowired
private LogInfoService logInfoService;
@Autowired
private JwtConfiguration jwtConfiguration;
@RabbitHandler
public void receive(AuthLog authLog) {
String username = JwtUtil.getUserName(authLog.getToken(), jwtConfiguration.getJwtkey());
MDC.put(CommonConstant.KEY_USER, username);
Log sysLog = authLog.getLog();
authLog.getLog().setCreateBy(username);
MDC.put(CommonConstant.KEY_USER, authLog.getLog().getCreateBy());
LogInfo logInfo = new LogInfo();
BeanUtils.copyProperties(sysLog, logInfo);
logInfoService.saveOrUpdate(logInfo);

View File

@@ -8,8 +8,6 @@ public class AuthLog implements Serializable {
private Log log;
private String token;
public Log getLog() {
return log;
}
@@ -18,12 +16,4 @@ public class AuthLog implements Serializable {
this.log = log;
}
public String getToken() {
return token;
}
public void setToken(String token) {
this.token = token;
}
}

View File

@@ -12,6 +12,8 @@ import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpStatus;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import com.github.liuweijw.core.beans.system.AuthLog;
@@ -83,12 +85,12 @@ public class LogServiceImpl implements LogService {
syslog.setException(throwable.getMessage());
}
// 保存发往MQ只保存授权请求
AuthLog authLog = new AuthLog();
authLog.setLog(syslog);
String headAuthorization = request.getHeader(CommonConstant.REQ_HEADER);
if (StringHelper.isNotEmpty(headAuthorization)) {
authLog.setToken(headAuthorization);
// 保存发往MQ只保存授权
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
if (authentication != null && StringHelper.isNotBlank(authentication.getName())) {
syslog.setCreateBy(authentication.getName());
authLog.setLog(syslog);
rabbitTemplate.convertAndSend(MqQueueConstant.LOG_QUEUE, authLog);
}
}