首页 > 解决方案 > Spring cloud gateway和spring sleuth集成在延迟异步调用后不起作用

问题描述

在将 spring-cloud-gateway 和 spring sleuth 升级到 3.0.2 之后,对我在我的产品中使用的一个用例进行了一些测试。在过滤器链中发出异步请求后,后续过滤器不会获得 traceId 和 spanId。

请求 -> TestFilter(异步延迟调用)(存在 Traceid 和 spanid)-> LoggingFilter(没有跟踪 id 和 span id)。

日志输出示例

2021-04-05 15:24:44.204  INFO [testapp,,] 2952 --- [           main] c.c.s.SpringsleuthexampleApplication     : Started SpringsleuthexampleApplication in 17.733 seconds (JVM running for 18.419)
2021-04-05 15:24:50.329  INFO [**testapp,606ade6a3d59693bc6bbdccde0438597,c6bbdccde0438597**] 2952 --- [ttp@67e28be3-21] c.c.s.filter.TestFilter                  : Test Filter Incoming-1
2021-04-05 15:24:55.334  INFO [**testapp,,**] 2952 --- [     parallel-1] c.c.s.filter.TestFilter                  : Test Filter Incoming-2
2021-04-05 15:24:55.335  INFO [**testapp,,**] 2952 --- [     parallel-1] c.c.s.filter.LoggingFilter               : Incoming request
2021-04-05 15:25:11.657  INFO [**testapp,,**] 2952 --- [ctor-http-nio-2] c.c.s.filter.LoggingFilter               : Outgoing response

示例项目 spring-cloud-sleuth-integration.zip

卷曲以测试 API。 curl --location --request GET 'http://localhost:8080/test/get'
--header 'Content-Type: application/json'

如果我们从代码(TestFilter.java)中删除异步调用的延迟,如下所示。

    public Mono<String> testAsync() {
        return Mono.just("test");// .delayElement(Duration.ofSeconds(5));
    }

然后有效地记录 print traceid 和 spanid ,如下所示。

2021-04-05 15:33:24.168  INFO [**testapp,606ae06cadef33fdcd761e928c4ae866,cd761e928c4ae866**] 16396 --- [http@e344ad3-37] c.c.s.filter.TestFilter                  : Test Filter Incoming-1
2021-04-05 15:33:24.169  INFO [**testapp,606ae06cadef33fdcd761e928c4ae866,cd761e928c4ae866**] 16396 --- [http@e344ad3-37] c.c.s.filter.TestFilter                  : Test Filter Incoming-2
2021-04-05 15:33:24.170  INFO [**testapp,606ae06cadef33fdcd761e928c4ae866,cd761e928c4ae866**] 16396 --- [http@e344ad3-37] c.c.s.filter.LoggingFilter               : Incoming request
2021-04-05 15:33:39.755  INFO [testapp,,] 16396 --- [ctor-http-nio-2] c.c.s.filter.LoggingFilter               : Outgoing response

标签: spring-cloud-gatewayspring-cloud-sleuth

解决方案


推荐阅读