Spring Cloud Sleuth 不同的跟踪 ID 与 Kafka 集成

问题描述:

我使用 Kafka 进行微服务之间的异步调用,并且使用 Spring Sleuth 进行日志记录.日志没问题,但是当有消息从微服务1到微服务2时,日志的消息有不同的Trace-ID.他们不是必须具有相同的跟踪 ID 但具有不同的 SpanId 吗?有什么特别的配置吗?

I'm using Kafka for Asyng calls between microservices, and i'm using Spring Sleuth for logging. The logging is ok, but when there is a message from Microservice1 to Microservice2, the logging's messages have different Trace-ID. Don't they have to have the same trace-Id but a different SpanId? is there any special configuration?

默认情况下,消息头不会被 Spring Cloud Kafka binder 传输,您必须通过 spring.cloud.stream.kafka.binder 进行设置.headers 手动,如 Spring Cloud 中所述流参考指南.然后检查那些跟踪相关的标头是否正确发送.

Message headers by default will not be transported by Spring Cloud Kafka binder, you have to set it via spring.cloud.stream.kafka.binder.headers manually as described in the Spring Cloud Stream Reference Guide. And then check if those tracing related headers been sent properly.

您可以在 application.yml 中按如下方式设置 Zipkin 标头:

You can set Zipkin headers as following in your application.yml:

spring:
  cloud:
    stream:
      kafka:
        binder:
          headers:
            - X-B3-TraceId
            - X-B3-SpanId
            - X-B3-Sampled
            - X-B3-ParentSpanId
            - X-Span-Name
            - X-Span-Export

或者在您的 application.properties 中:

Or in your application.properties:

spring.cloud.stream.kafka.binder.headers[0]=X-B3-TraceId
spring.cloud.stream.kafka.binder.headers[1]=X-B3-SpanId
spring.cloud.stream.kafka.binder.headers[2]=B3-Sampled
spring.cloud.stream.kafka.binder.headers[3]=X-B3-ParentSpanId
spring.cloud.stream.kafka.binder.headers[4]=X-Span-Name
spring.cloud.stream.kafka.binder.headers[5]=X-Span-Export

或者在逗号分隔的列表中:

Or in a comma-separated list:

spring.cloud.stream.kafka.binder.headers=X-B3-TraceId,X-B3-SpanId,B3-Sampled,\
    X-B3-ParentSpanId,X-Span-Name,X-Span-Export