diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/config/DefaultWebMvcConfig.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/config/DefaultWebMvcConfig.java
index de5e5442..1694ba09 100644
--- a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/config/DefaultWebMvcConfig.java
+++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/config/DefaultWebMvcConfig.java
@@ -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);
}
diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/CommonConstant.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/CommonConstant.java
index 39ae330b..1a961195 100644
--- a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/CommonConstant.java
+++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/constant/CommonConstant.java
@@ -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";
}
diff --git a/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/interceptor/TraceInterceptor.java b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/interceptor/TraceInterceptor.java
new file mode 100644
index 00000000..b68e069e
--- /dev/null
+++ b/zlt-commons/zlt-common-spring-boot-starter/src/main/java/com/central/common/interceptor/TraceInterceptor.java
@@ -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;
+ }
+}
diff --git a/zlt-commons/zlt-log-spring-boot-starter/pom.xml b/zlt-commons/zlt-log-spring-boot-starter/pom.xml
index dce142b7..fc623dcc 100644
--- a/zlt-commons/zlt-log-spring-boot-starter/pom.xml
+++ b/zlt-commons/zlt-log-spring-boot-starter/pom.xml
@@ -17,5 +17,14 @@
org.slf4j
slf4j-api
+
+
+ org.springframework.boot
+ spring-boot-starter
+
+
+ org.springframework.cloud
+ spring-cloud-context
+
diff --git a/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/config/LogAutoConfigure.java b/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/config/LogAutoConfigure.java
new file mode 100644
index 00000000..73575311
--- /dev/null
+++ b/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/config/LogAutoConfigure.java
@@ -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 {
+
+}
diff --git a/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/properties/TraceProperties.java b/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/properties/TraceProperties.java
new file mode 100644
index 00000000..a3daa546
--- /dev/null
+++ b/zlt-commons/zlt-log-spring-boot-starter/src/main/java/com/central/log/properties/TraceProperties.java
@@ -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;
+}
diff --git a/zlt-commons/zlt-log-spring-boot-starter/src/main/resources/META-INF/spring.factories b/zlt-commons/zlt-log-spring-boot-starter/src/main/resources/META-INF/spring.factories
new file mode 100644
index 00000000..7a25cdef
--- /dev/null
+++ b/zlt-commons/zlt-log-spring-boot-starter/src/main/resources/META-INF/spring.factories
@@ -0,0 +1,2 @@
+org.springframework.boot.autoconfigure.EnableAutoConfiguration=\
+com.central.log.config.LogAutoConfigure
diff --git a/zlt-commons/zlt-log-spring-boot-starter/src/main/resources/logback-spring.xml b/zlt-commons/zlt-log-spring-boot-starter/src/main/resources/logback-spring.xml
index f71684c0..d20c32ba 100644
--- a/zlt-commons/zlt-log-spring-boot-starter/src/main/resources/logback-spring.xml
+++ b/zlt-commons/zlt-log-spring-boot-starter/src/main/resources/logback-spring.xml
@@ -17,8 +17,8 @@
-
+ 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}" />
+
diff --git a/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java
index cc8f3503..5aae34a0 100644
--- a/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java
+++ b/zlt-commons/zlt-ribbon-spring-boot-starter/src/main/java/com/central/common/ribbon/config/FeignInterceptorConfig.java
@@ -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;
}
diff --git a/zlt-config/src/main/resources/application-dev.properties b/zlt-config/src/main/resources/application-dev.properties
index 07957294..c0e15c21 100644
--- a/zlt-config/src/main/resources/application-dev.properties
+++ b/zlt-config/src/main/resources/application-dev.properties
@@ -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
\ No newline at end of file
+zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
+
+##### 日志链路追踪
+zlt.trace.enable=true
\ No newline at end of file
diff --git a/zlt-config/src/main/resources/application-prod.properties b/zlt-config/src/main/resources/application-prod.properties
index b9f5b8b3..de948da5 100644
--- a/zlt-config/src/main/resources/application-prod.properties
+++ b/zlt-config/src/main/resources/application-prod.properties
@@ -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
\ No newline at end of file
+zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
+
+##### 日志链路追踪
+zlt.trace.enable=true
\ No newline at end of file
diff --git a/zlt-config/src/main/resources/application-test.properties b/zlt-config/src/main/resources/application-test.properties
index 07957294..c0e15c21 100644
--- a/zlt-config/src/main/resources/application-test.properties
+++ b/zlt-config/src/main/resources/application-test.properties
@@ -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
\ No newline at end of file
+zlt.fdfs.trackerList=${zlt.fdfs.web-url}:22122
+
+##### 日志链路追踪
+zlt.trace.enable=true
\ No newline at end of file
diff --git a/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/TraceFilter.java b/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/TraceFilter.java
new file mode 100644
index 00000000..2739492e
--- /dev/null
+++ b/zlt-gateway/zuul-gateway/src/main/java/com/central/gateway/filter/pre/TraceFilter.java
@@ -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;
+ }
+}
diff --git a/zlt-web/back-web/src/main/resources/static/pages/log/sysLog.html b/zlt-web/back-web/src/main/resources/static/pages/log/sysLog.html
index f0986b28..97f908af 100644
--- a/zlt-web/back-web/src/main/resources/static/pages/log/sysLog.html
+++ b/zlt-web/back-web/src/main/resources/static/pages/log/sysLog.html
@@ -15,6 +15,7 @@
+
@@ -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}
]]
});