Closed dodomao closed 4 years ago
Maybe you could provide your codes? @dodomao
@zhaoyuguang Please take a look, look like Gateway plugin is still no stable.
Spring Cloud Gateway: Spring Boot:'org.springframework.boot' version '2.1.4.RELEASE' Spring Cloud:"Greenwich.SR2" build.gradle: compile 'org.apache.skywalking:apm-toolkit-logback-1.x:6.5.0' compile 'org.apache.skywalking:apm-toolkit-trace:6.5.0' GlobalFilter: `@Component public class LoggerFilter implements GlobalFilter, Ordered {
private static final Logger LOGGER = LoggerFactory.getLogger(LoggerFilter.class);
@Override
public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
ServerHttpRequest request = exchange.getRequest();
URI uri = request.getURI();
HttpHeaders httpHeaders = request.getHeaders();
String zhiGongXm = "";
if (LOGGER.isDebugEnabled()) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append("traceID:'").append(TraceContext.traceId()).append("'");
LOGGER.debug(stringBuilder.toString());
}
String finalZhiGongXm = zhiGongXm;
ServerHttpRequest serverHttpRequest = exchange.getRequest().mutate().headers(headers -> {
try {
headers.add("DangQianYhMc", URLEncoder.encode(finalZhiGongXm, "UTF-8"));
} catch (UnsupportedEncodingException e) {
LOGGER.error("UnsupportedEncodingException:", e);
}
}).build();
return chain.filter(exchange.mutate().request(serverHttpRequest).build());
}
@Override
public int getOrder() {
return Integer.MAX_VALUE;
}
}`
Micro Service:
Logback.xml:
`
<File>${logs.dir}/micro-service.log</File>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<charset>UTF-8</charset>
<layout class="org.apache.skywalking.apm.toolkit.log.logback.v1.x.TraceIdPatternLogbackLayout">
<pattern>{[%tid] - %d - [%t] %-5p %c:%L %X - %m}%n</pattern>
</layout>
</encoder>
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>DEBUG</level>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${logs.dir}/micro-service.log.%d{yyyy-MM-dd}</fileNamePattern>
<maxHistory>7</maxHistory>
</rollingPolicy>
</appender>`
@zhaoyuguang thanks
received, I will try it
Test of time, you need a concurrent execution, this problem will be very obvious. A single execution, may not be able to recurrence。the TraceID Obtained from gateway logback with MicroService TraceID No relevance
@dodomao There is a fix in the master branch for Gateway and Webflux. Could you recheck whether does it fix this?
Gateway plugin improved through #4878 again.
Please answer these questions before submitting your issue.
Question
What do you want to know? Spring Cloud Gateway TraceContext.traceId() Is not equal to micro service logback tid I think that is the cause of the TraceCrossThread Before 6.5.0, the traceid Repeated Generation in spring cloud gateway。Now,the bug is solve,but the traceid is not a GlobalTraceId
Bug
Which version of SkyWalking, OS and JRE? SkyWalking 6.5 jdk1.8
What happen? spring cloud gateway GlobalFilter: LOGGER.info(TraceContext.traceId()); [ctor-http-nio-3] c.d.cbhis.his_gateway.filter.GrayFilter : 3729.128.15741514902390093 Micro service logback: [TID:3729.128.15741514391310083]
Requirement or improvement
How to get the GlobalTraceId in spring cloud gateway GlobalFilter or GatewayFilter.