Spring Cloud Sleuth - traceId 不会为 RestTemplate 传播

时间:2021-07-19 09:02:56

标签: spring logging httpclient resttemplate spring-cloud-sleuth

我正在尝试使用 Spring cloud Sleuth,但是我面临着获取正确日志信息的问题。我的应用程序不会为 RestTemplate 传播 traceId 和 spanId。

我使用的是最新版本的 Sleuth。

<dependency>
  <groupId>org.springframework.cloud</groupId>
  <artifactId>spring-cloud-starter-sleuth</artifactId>
  <version>3.0.3</version>
</dependency>

这是日志:

2021-07-19 10:46:22.834 DEBUG [My app,340b0c68ce92b6be,340b0c68ce92b6be] 28840 --- [       Thread-3] org.apache.http.headers                  : http-outgoing-0 << Date: Mon, 19 Jul 2021 08:46:15 GMT
2021-07-19 10:46:22.834 DEBUG [My app,340b0c68ce92b6be,340b0c68ce92b6be] 28840 --- [       Thread-3] o.a.http.impl.execchain.MainClientExec   : Connection can be kept alive indefinitely
2021-07-19 10:46:22.834 DEBUG [My app,,] 28840 --- [       Thread-3] o.s.web.client.RestTemplate              : Response 200 OK
2021-07-19 10:46:22.834 DEBUG [My app,,] 28840 --- [       Thread-3] o.s.web.client.RestTemplate              : Reading to [com.app.myapp.DeviceService]
2021-07-19 10:46:22.834 DEBUG [My app,,] 28840 --- [       Thread-3] h.i.c.PoolingHttpClientConnectionManager : Connection [id: 0][route: {s}->https://ttsce.dot.eu:443] can be kept alive indefinitely
2021-07-19 10:46:22.844 DEBUG [My app,,] 28840 --- [       Thread-3] .w.m.TracingClientHttpRequestInterceptor : Wrapping an outbound http call with span [NoopSpan(4bda0829902d27f6/4bda0829902d27f6)]
2021-07-19 10:46:22.844 DEBUG [My app,4bda0829902d27f6,4bda0829902d27f6] 28840 --- [       Thread-3] o.a.h.client.protocol.RequestAddCookies  : CookieSpec selected: default

这就是我创建 RestTemplate bean 的方式(我正在测试这两种 bean 设置)。

@Primary
@Bean("newRestTemplate")
public RestTemplate restTemplate() {
    return new RestTemplate();
}

@Bean("customRestTemplate")
public RestTemplate restTemplate(RestTemplateBuilder builder) {
    return builder.setConnectTimeout(Duration.ofMillis(300000))
            .setReadTimeout(Duration.ofMillis(300000)).build();
}

1 个答案:

答案 0 :(得分:0)

您能否提供您使用的是哪个版本的 Sleuth 和 Cloud? RestTemplate 是目前测试最多的组件,它确实有效。这里有一个独立应用程序的示例,其中包含确实有效 https://github.com/spring-cloud-samples/spring-cloud-sleuth-samples/tree/main/resttemplate 的其余模板。

相关问题