【问题标题】:Spring cloud sleuth 2.x custom trace idSpring cloud sleuth 2.x 自定义跟踪 id
【发布时间】:2021-12-30 15:19:58
【问题描述】:

我目前正在开发一个使用 Spring cloud sleuth 依赖项的 spring webflux 项目。

    <dependency>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-sleuth</artifactId>
        <version>2.2.8.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-webflux</artifactId>
        <version>2.5.1</version>
    </dependency>

我正在研究一组 API,它们获取包含 3 个字符串的请求负载。我想连接这 3 个字符串以用作我的自定义跟踪 ID。我也想达到以下要求。

  1. 这个 custom_trace_id 应该可以添加到每个日志中。 (我们通过在 log4j.xml 文件中添加 %X{traceId} 以在所有日志中打印 traceId 来获取日志中的侦探跟踪 ID)
  2. 此自定义跟踪 ID 必须发送到 WebClient 进行的调用(Sleuth 将 X-B3-TraceId 标头设置为 WebClient 进行的所有调用,我想要相同但使用 custom_trace_id )
  3. 如果可能的话,我想将 X-B3-TraceId 标头的名称更改为不同的值,例如 correlation-id,这样如果有人向我们的 spring webflux 应用程序发出请求,侦探应该首先检查 correlation-id 并将其用作跟踪 id。 (注:custom_trace_id = 相关 ID)

请注意,spring sleuth 已经完成了上述 3 件事。我想为跟踪 id 设置自定义值。有人可以为我的用例提供帮助或建议吗

【问题讨论】:

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


    【解决方案1】:

    Spring Boot 2.5.x 与 Spring Cloud Hoxton (Sleuth 2.2.x) 不兼容。 您需要使用 Spring Cloud 2020.0.x aka Ilford (Sleuth 3.0.x)。

    您可以将spring.sleuth.propagation.type 设置为CUSTOM 并实现自己的PropagationPropagation.Factory,请参阅文档:How to Change The Context Propagation Mechanism?

    这应该可以帮助您实现上述目标,但是也有一些缺点。这主要是我的意见,请随意忽略它并做任何你想做的事情:

    • B3、W3C 和 AWS 传播类型是支持其他项目/工具的标准
    • 您正在尝试创建不受任何支持(但可能由内部系统支持)的第四个“标准”
    • 您需要为每个新组件添加对这种自定义传播类型的支持,以便它们也能理解这个“标准”
    • 有时值得考虑转向通用标准而不是使用自定义标准,即使它背后有旧系统

    【讨论】:

    • 嗨乔纳坦。侦探 2.2.8 工作正常。不使用 Sleuth 3.x 的原因是我在启动 spring boot 应用程序时遇到了以下问题。 Description: An attempt was made to call a method that does not exist. The attempt was made from the following location: org.springframework.cloud.sleuth.instrument.web.client.HttpClientBeanPostProcessor.postProcessAfterInitialization(HttpClientBeanPostProcessor.java:76) The following method did not exist: reactor.netty.http.client.HttpClient.mapConnect(Ljava/util/function/Function;)Lreactor/netty/http/client/HttpClient;
    • 相信我,它们不兼容,但根据您收到的错误,我感觉您的依赖关系不正确。请不要定义版本,而是使用 spring boot 和 spring cloud BOM。
    • 对于 Sleuth 我如何获得依赖管理(托管版本)?由于它不是由spring-boot-starter-parent 管理的,对
    • 您可以在文档中找到:search.maven.org/artifact/org.springframework.cloud/… 如果您想将 Spring Boot Parent 用作父 pom,如果您不使用 maven 或不想使用父 pom,则它适用于 maven pom:search.maven.org/artifact/org.springframework.boot/…
    • 我正在使用 Maven 并按照您的建议,我让 spring-boot-starter-parent 为我选择依赖版本。我正在使用 spring-boot-starter-parent 的 2.3.3.RELEASE ,它也将我的 spring webflux 版本更改为 2.3.3.RELEASE。似乎不支持 Sleuth 3,因为我的 reactor-netty 是 0.9.11.RELEASE 版本。你能告诉我我能做什么吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-09-03
    • 1970-01-01
    • 2017-06-08
    • 2018-08-03
    • 2017-09-28
    • 1970-01-01
    • 2020-11-25
    相关资源
    最近更新 更多