Spring Sleuth - 关联 ID 未记录
Spring Sleuth - Correlation Id not logging
当前流量:
SchedulerA -> ServiceA -> FeignOfB (Other ServiceB) -> ControllerB
log.xml
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss,SS} [%thread] %-5level %logger{36} -
%msg
${appName:-}, %X{Correlation-Id:-}, %X{traceId:-}, %X{spanId:-}%n
</Pattern>
</layout>
</appender>
application.yml
spring:
sleuth:
baggage:
correlation-enabled: true
correlation-fields:
- Correlation-Id
remote-fields:
- Correlation-Id
调度程序代码:
public void scheduleFixedDelayTask() {
String cId = UUID.randomUUID().toString().replace("-", "");
BaggageField bField = BaggageField.getByName("Correlation-Id");
if (bField != null) {
bField.updateValue(tracer.currentSpan().context(), cId);
}
//MDC.put("Correlation-Id", cId);
log.info("scheduleFixedDelayTask, {}", new Date());
List<Foo> foos = this.feignService.getData("foo");
log.info("scheduleFixedDelayTask - completed", new Date());
}
正在生成 UUID 并设置 BaggageField 值。 Correlation-Id 值打印在 ServiceB 控制器中,但在请求发起的 serviceA 的调度程序日志中,它不打印 Correlation-编号。它在两个服务(自行生成)中打印 spanId 和 traceId。
如果我使用 MDC(注释行)手动设置值,则会打印。
这是手动设置值的正确方法吗?
您需要将 Brave 配置为在更新时执行刷新 - https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/html/project-features.html#features-baggage
@Bean
ScopeDecorator mdcScopeDecorator() {
return MDCScopeDecorator.newBuilder()
.clear()
.add(SingleCorrelationField.newBuilder(BaggageField.create("Correlation-Id"))
.flushOnUpdate()
.build())
.build();
}
当前流量: SchedulerA -> ServiceA -> FeignOfB (Other ServiceB) -> ControllerB
log.xml
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{yyyy-MM-dd HH:mm:ss,SS} [%thread] %-5level %logger{36} -
%msg
${appName:-}, %X{Correlation-Id:-}, %X{traceId:-}, %X{spanId:-}%n
</Pattern>
</layout>
</appender>
application.yml
spring:
sleuth:
baggage:
correlation-enabled: true
correlation-fields:
- Correlation-Id
remote-fields:
- Correlation-Id
调度程序代码:
public void scheduleFixedDelayTask() {
String cId = UUID.randomUUID().toString().replace("-", "");
BaggageField bField = BaggageField.getByName("Correlation-Id");
if (bField != null) {
bField.updateValue(tracer.currentSpan().context(), cId);
}
//MDC.put("Correlation-Id", cId);
log.info("scheduleFixedDelayTask, {}", new Date());
List<Foo> foos = this.feignService.getData("foo");
log.info("scheduleFixedDelayTask - completed", new Date());
}
正在生成 UUID 并设置 BaggageField 值。 Correlation-Id 值打印在 ServiceB 控制器中,但在请求发起的 serviceA 的调度程序日志中,它不打印 Correlation-编号。它在两个服务(自行生成)中打印 spanId 和 traceId。
如果我使用 MDC(注释行)手动设置值,则会打印。 这是手动设置值的正确方法吗?
您需要将 Brave 配置为在更新时执行刷新 - https://docs.spring.io/spring-cloud-sleuth/docs/current/reference/html/project-features.html#features-baggage
@Bean
ScopeDecorator mdcScopeDecorator() {
return MDCScopeDecorator.newBuilder()
.clear()
.add(SingleCorrelationField.newBuilder(BaggageField.create("Correlation-Id"))
.flushOnUpdate()
.build())
.build();
}