增加分布式日志链路追踪
This commit is contained in:
+6
@@ -2,6 +2,7 @@ package com.central.common.config;
|
||||
|
||||
import com.central.common.feign.UserService;
|
||||
import com.central.common.interceptor.TenantInterceptor;
|
||||
import com.central.common.interceptor.TraceInterceptor;
|
||||
import com.central.common.resolver.ClientArgumentResolver;
|
||||
import com.central.common.resolver.TokenArgumentResolver;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -26,7 +27,12 @@ public class DefaultWebMvcConfig extends WebMvcConfigurationSupport {
|
||||
*/
|
||||
@Override
|
||||
protected void addInterceptors(InterceptorRegistry registry) {
|
||||
//租户拦截器
|
||||
registry.addInterceptor(new TenantInterceptor()).addPathPatterns("/**");
|
||||
|
||||
//日志链路追踪拦截器
|
||||
registry.addInterceptor(new TraceInterceptor()).addPathPatterns("/**");
|
||||
|
||||
super.addInterceptors(registry);
|
||||
}
|
||||
|
||||
|
||||
+10
@@ -102,4 +102,14 @@ public interface CommonConstant {
|
||||
* 租户id参数
|
||||
*/
|
||||
String TENANT_ID_PARAM = "tenantId";
|
||||
|
||||
|
||||
/**
|
||||
* 日志链路追踪id信息头
|
||||
*/
|
||||
String TRACE_ID_HEADER = "x-traceId-header";
|
||||
/**
|
||||
* 日志链路追踪id日志标志
|
||||
*/
|
||||
String LOG_TRACE_ID = "traceId";
|
||||
}
|
||||
|
||||
+26
@@ -0,0 +1,26 @@
|
||||
package com.central.common.interceptor;
|
||||
|
||||
import cn.hutool.core.util.StrUtil;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.web.servlet.HandlerInterceptor;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
/**
|
||||
* 日志链路追踪拦截器
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/8/13
|
||||
*/
|
||||
public class TraceInterceptor implements HandlerInterceptor {
|
||||
@Override
|
||||
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {
|
||||
String traceId = request.getHeader(CommonConstant.TRACE_ID_HEADER);
|
||||
if (StrUtil.isNotEmpty(traceId)) {
|
||||
MDC.put(CommonConstant.LOG_TRACE_ID, traceId);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
@@ -17,5 +17,14 @@
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>slf4j-api</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-context</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</project>
|
||||
|
||||
+15
@@ -0,0 +1,15 @@
|
||||
package com.central.log.config;
|
||||
|
||||
import com.central.log.properties.TraceProperties;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
|
||||
/**
|
||||
* 日志自动配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/8/13
|
||||
*/
|
||||
@EnableConfigurationProperties(TraceProperties.class)
|
||||
public class LogAutoConfigure {
|
||||
|
||||
}
|
||||
+23
@@ -0,0 +1,23 @@
|
||||
package com.central.log.properties;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.Setter;
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
import org.springframework.cloud.context.config.annotation.RefreshScope;
|
||||
|
||||
/**
|
||||
* 日志链路追踪配置
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/8/13
|
||||
*/
|
||||
@Setter
|
||||
@Getter
|
||||
@ConfigurationProperties(prefix = "zlt.trace")
|
||||
@RefreshScope
|
||||
public class TraceProperties {
|
||||
/**
|
||||
* 是否开启日志链路追踪
|
||||
*/
|
||||
private Boolean enable = false;
|
||||
}
|
||||
@@ -0,0 +1,2 @@
|
||||
org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
|
||||
com.central.log.config.LogAutoConfigure
|
||||
@@ -17,8 +17,8 @@
|
||||
|
||||
<!-- 彩色日志格式 -->
|
||||
<property name="CONSOLE_LOG_PATTERN"
|
||||
value="[${APP_NAME}:${ServerIP}:${ServerPort}] %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%level){blue} %clr(${PID}){magenta} %clr([%thread]){orange} %clr(%logger){cyan} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />
|
||||
<property name="CONSOLE_LOG_PATTERN_NO_COLOR" value="[${APP_NAME}:${ServerIP}:${ServerPort}] %d{yyyy-MM-dd HH:mm:ss.SSS} %level ${PID} [%thread] %logger %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />
|
||||
value="[${APP_NAME}:${ServerIP}:${ServerPort}] %clr(%d{yyyy-MM-dd HH:mm:ss.SSS}){faint} %clr(%level){blue} %clr(${PID}){magenta} %clr([%X{traceId}]){yellow} %clr([%thread]){orange} %clr(%logger){cyan} %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />
|
||||
<property name="CONSOLE_LOG_PATTERN_NO_COLOR" value="[${APP_NAME}:${ServerIP}:${ServerPort}] %d{yyyy-MM-dd HH:mm:ss.SSS} %level ${PID} [%X{traceId}] [%thread] %logger %m%n${LOG_EXCEPTION_CONVERSION_WORD:-%wEx}" />
|
||||
|
||||
<!-- 控制台日志 -->
|
||||
<appender name="StdoutAppender" class="ch.qos.logback.core.ConsoleAppender">
|
||||
|
||||
+7
@@ -5,6 +5,7 @@ import com.central.common.constant.CommonConstant;
|
||||
import com.central.common.constant.SecurityConstants;
|
||||
import com.central.common.utils.TenantContextHolder;
|
||||
import feign.RequestInterceptor;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
@@ -63,6 +64,12 @@ public class FeignInterceptorConfig {
|
||||
if (StrUtil.isNotEmpty(tenant)) {
|
||||
template.header(SecurityConstants.TENANT_HEADER, tenant);
|
||||
}
|
||||
|
||||
//传递日志traceId
|
||||
String traceId = MDC.get(CommonConstant.LOG_TRACE_ID);
|
||||
if (StrUtil.isNotEmpty(traceId)) {
|
||||
template.header(CommonConstant.TRACE_ID_HEADER, traceId);
|
||||
}
|
||||
};
|
||||
return requestInterceptor;
|
||||
}
|
||||
|
||||
@@ -18,4 +18,7 @@ zlt.sentinel.dashboard=192.168.28.130:6999
|
||||
|
||||
##### fastDFS配置
|
||||
zlt.fdfs.web-url=192.168.28.130
|
||||
zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
|
||||
zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
|
||||
|
||||
##### 日志链路追踪
|
||||
zlt.trace.enable=true
|
||||
@@ -18,4 +18,7 @@ zlt.sentinel.dashboard=192.168.28.130:6999
|
||||
|
||||
##### fastDFS配置
|
||||
zlt.fdfs.web-url=192.168.28.130
|
||||
zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
|
||||
zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
|
||||
|
||||
##### 日志链路追踪
|
||||
zlt.trace.enable=true
|
||||
@@ -18,4 +18,7 @@ zlt.sentinel.dashboard=192.168.28.130:6999
|
||||
|
||||
##### fastDFS配置
|
||||
zlt.fdfs.web-url=192.168.28.130
|
||||
zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
|
||||
zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
|
||||
|
||||
##### 日志链路追踪
|
||||
zlt.trace.enable=true
|
||||
+51
@@ -0,0 +1,51 @@
|
||||
package com.central.gateway.filter.pre;
|
||||
|
||||
import cn.hutool.core.util.IdUtil;
|
||||
import com.central.common.constant.CommonConstant;
|
||||
import com.central.log.properties.TraceProperties;
|
||||
import com.netflix.zuul.ZuulFilter;
|
||||
import com.netflix.zuul.context.RequestContext;
|
||||
import org.slf4j.MDC;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.cloud.netflix.zuul.filters.support.FilterConstants;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
||||
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.FORM_BODY_WRAPPER_FILTER_ORDER;
|
||||
|
||||
/**
|
||||
* 生成日志链路追踪id,并传入header中
|
||||
*
|
||||
* @author zlt
|
||||
* @date 2019/8/13
|
||||
*/
|
||||
@Component
|
||||
public class TraceFilter extends ZuulFilter {
|
||||
@Autowired
|
||||
private TraceProperties traceProperties;
|
||||
|
||||
@Override
|
||||
public String filterType() {
|
||||
return FilterConstants.PRE_TYPE;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int filterOrder() {
|
||||
return FORM_BODY_WRAPPER_FILTER_ORDER - 1;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean shouldFilter() {
|
||||
//根据配置控制是否开启过滤器
|
||||
return traceProperties.getEnable();
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object run() {
|
||||
//链路追踪id
|
||||
String traceId = IdUtil.fastSimpleUUID();
|
||||
MDC.put(CommonConstant.LOG_TRACE_ID, traceId);
|
||||
RequestContext ctx = RequestContext.getCurrentContext();
|
||||
ctx.addZuulRequestHeader(CommonConstant.TRACE_ID_HEADER, traceId);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -15,6 +15,7 @@
|
||||
<option value="logLevel">日志级别</option>
|
||||
<option value="appName">应用名</option>
|
||||
<option value="classname">类名</option>
|
||||
<option value="traceId">链路追踪id</option>
|
||||
</select> 
|
||||
<input id="sysLog-search-value" class="layui-input search-input" style="width: 300px" type="text" placeholder="输入关键字"/> 
|
||||
<button id="sysLog-btn-search" class="layui-btn icon-btn"><i class="layui-icon"></i>搜索</button>
|
||||
@@ -50,7 +51,8 @@
|
||||
{field: 'serverIp', sort: true, title: '服务ip', width: 130},
|
||||
{field: 'serverPort', sort: true, title: '服务端口', width: 100},
|
||||
{field: 'threadName', sort: true, title: '线程名', width: 150},
|
||||
{field: 'classname', sort: true, title: '类名', width: 250}
|
||||
{field: 'classname', sort: true, title: '类名', width: 250},
|
||||
{field: 'traceId', sort: true, title: '链路追踪id', width: 200}
|
||||
]]
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user