【问题标题】:Spring Cloud Sleuth different trace-ID integrate with KafkaSpring Cloud Sleuth 不同的 trace-ID 与 Kafka 集成
【发布时间】:2017-04-16 20:13:00
【问题描述】:

我使用 Kafka 进行微服务之间的 Asyng 调用,并且使用 Spring Sleuth 进行日志记录。日志是可以的,但是当有消息从微服务 1 到微服务 2 时,日志的消息有不同的 Trace-ID。他们不是必须具有相同的 trace-Id 但具有不同的 SpanId 吗?有什么特别的配置吗?

【问题讨论】:

  • 默认情况下,消息头不会被 Kafka binder 传输,您必须按照 here 的描述通过 spring.cloud.stream.kafka.binder.headers 手动设置它。然后检查这些跟踪相关的标头是否已正确发送。
  • 非常感谢您的 snwer @tan9 ! :D。我从 yaml 文件中找到了这段代码: spring: cloud: stream: kafka: binder: headers: - X-B3-TraceId - X-B3-SpanId - X-B3-Sampled - X-B3-ParentSpanId - X-Span- Name - X-Process-Id 你知道怎么放到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=X-B3-SpanId,X-B3-SpanId,...
  • 谢谢,非常感谢@tan9! :D
  • 我将上面的评论安排为下面的正式答案,以便其他人更容易参考。

标签: spring spring-boot spring-cloud spring-cloud-sleuth


【解决方案1】:

Spring Cloud Kafka binder 默认不会传输消息头,您必须按照Spring Cloud Stream Reference Guide 中的说明通过spring.cloud.stream.kafka.binder.headers 手动设置它。然后检查这些跟踪相关的标头是否已正确发送。

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

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 中:

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

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-11-23
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 2018-11-08
    • 1970-01-01
    • 2019-04-28
    • 2023-03-31
    相关资源
    最近更新 更多