添加全局请求id 方便线上快速查询日志
This commit is contained in:
parent
15905b7022
commit
da0319e053
@ -93,6 +93,12 @@
|
||||
<artifactId>ruoyi-workflow</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- mdc模块 -->
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-mdc</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>de.codecentric</groupId>
|
||||
<artifactId>spring-boot-admin-starter-client</artifactId>
|
||||
|
@ -2,7 +2,7 @@
|
||||
<configuration>
|
||||
<property name="log.path" value="./logs"/>
|
||||
<property name="console.log.pattern"
|
||||
value="%cyan(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %highlight(%-5level) %boldMagenta(%logger{36}%n) - %msg%n"/>
|
||||
value="%cyan(%d{yyyy-MM-dd HH:mm:ss}) %green([%thread]) %red([%X{requestId}]) %highlight(%-5level) %boldMagenta(%logger{36}%n) - %msg%n"/>
|
||||
<property name="log.pattern" value="%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n"/>
|
||||
|
||||
<!-- 控制台输出 -->
|
||||
|
@ -34,6 +34,7 @@
|
||||
<module>ruoyi-common-tenant</module>
|
||||
<module>ruoyi-common-websocket</module>
|
||||
<module>ruoyi-common-sse</module>
|
||||
<module>ruoyi-common-mdc</module>
|
||||
</modules>
|
||||
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
|
@ -179,6 +179,13 @@
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
<!-- mdc模块 -->
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-mdc</artifactId>
|
||||
<version>${revision}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
|
||||
|
33
ruoyi-common/ruoyi-common-mdc/pom.xml
Normal file
33
ruoyi-common/ruoyi-common-mdc/pom.xml
Normal file
@ -0,0 +1,33 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<parent>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common</artifactId>
|
||||
<version>${revision}</version>
|
||||
</parent>
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<artifactId>ruoyi-common-mdc</artifactId>
|
||||
|
||||
<description>
|
||||
ruoyi-common-mdc 全局请求id模块
|
||||
</description>
|
||||
|
||||
<dependencies>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.dromara.sms4j</groupId>
|
||||
<artifactId>sms4j-spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
|
||||
<!-- RuoYi Common web-->
|
||||
<dependency>
|
||||
<groupId>org.dromara</groupId>
|
||||
<artifactId>ruoyi-common-web</artifactId>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
</project>
|
@ -0,0 +1,25 @@
|
||||
package org.dromara.common.mdc.config;
|
||||
|
||||
import org.dromara.common.mdc.filter.RequestIdFilter;
|
||||
import org.springframework.boot.autoconfigure.AutoConfiguration;
|
||||
import org.springframework.boot.web.servlet.FilterRegistrationBean;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
|
||||
/**
|
||||
* 短信配置类
|
||||
*
|
||||
* @author Feng
|
||||
*/
|
||||
@AutoConfiguration
|
||||
public class MdcAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
public FilterRegistrationBean<RequestIdFilter> requestIdFilterRegistration(RequestIdFilter requestIdFilter) {
|
||||
FilterRegistrationBean<RequestIdFilter> registration = new FilterRegistrationBean<>();
|
||||
registration.setFilter(requestIdFilter);
|
||||
// 确保是最先执行的过滤器之一
|
||||
registration.setOrder(1);
|
||||
registration.addUrlPatterns("/*");
|
||||
return registration;
|
||||
}
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package org.dromara.common.mdc.filter;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import jakarta.servlet.FilterChain;
|
||||
import jakarta.servlet.ServletException;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import jakarta.servlet.http.HttpServletResponse;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.filter.OncePerRequestFilter;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.UUID;
|
||||
|
||||
/**
|
||||
* 请求ID过滤器
|
||||
* 为每个HTTP请求生成唯一的请求ID并放入MDC中
|
||||
*/
|
||||
@Component
|
||||
@Slf4j
|
||||
public class RequestIdFilter extends OncePerRequestFilter {
|
||||
|
||||
public static final String REQUEST_ID_KEY = "requestId";
|
||||
public static final String REQUEST_ID_HEADER = "X-Request-ID";
|
||||
|
||||
@Override
|
||||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
|
||||
throws ServletException, IOException {
|
||||
try {
|
||||
// 尝试从请求头中获取请求ID(支持从上游服务传递过来)
|
||||
String requestId = request.getHeader(REQUEST_ID_HEADER);
|
||||
|
||||
// 如果请求头中没有请求ID,则生成一个新的
|
||||
if (StrUtil.isBlank(requestId)) {
|
||||
requestId = generateRequestId();
|
||||
log.debug("生成新的请求ID: {}", requestId);
|
||||
// 将请求ID添加到响应头中
|
||||
response.addHeader(REQUEST_ID_HEADER, requestId);
|
||||
} else {
|
||||
log.debug("从请求头获取到请求ID: {}", requestId);
|
||||
}
|
||||
|
||||
// 将请求ID放入MDC
|
||||
MDC.put(REQUEST_ID_KEY, requestId);
|
||||
// 继续过滤器链
|
||||
filterChain.doFilter(request, response);
|
||||
} finally {
|
||||
// 清理MDC,防止内存泄漏
|
||||
MDC.remove(REQUEST_ID_KEY);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 生成请求ID
|
||||
*/
|
||||
private String generateRequestId() {
|
||||
return UUID.randomUUID().toString().replace("-", "");
|
||||
}
|
||||
}
|
@ -0,0 +1 @@
|
||||
org.dromara.common.mdc.config.MdcAutoConfiguration
|
Loading…
x
Reference in New Issue
Block a user