仅当Mono响应具有主体

时间:2018-08-09 14:01:37

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

我已经登录了一个执行器HttpTraceRepository bean,在编写Mono响应后将调用该bean。如果响应没有正文(只有状态),那么我会在日志消息中获得跟踪/跨度ID。但是,如果响应包含正文,则日志消息没有跟踪/跨度ID,除非它们由先前处理过“无正文”请求的同一NIO线程处理。

这是一个大大简化了示例,暴露了这个问题。

@SpringBootApplication
public class DemoApplication {

    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }

    @Bean
    public WebClient webClient() {
        return WebClient.create();
    }
}

@Component
class AccessLoggingHttpTraceRepository implements HttpTraceRepository {

    @Override
    public List<HttpTrace> findAll() {
        return null;
    }

    @Override
    public void add(HttpTrace trace) {
        LogFactory.getLog("netty.Access").info("ACCESS");
    }
}

@RestController
@RequestMapping("/test")
class PublicationFamilyEndpoint {

    @Autowired
    private WebClient webClient;

    @GetMapping("/body")
    public Mono<String> testWithBody() {
        return callWebClient();
    }

    @GetMapping("/nobody")
    public Mono<ResponseEntity<Object>> testWithoutBody() {
        return callWebClient().map(s -> ResponseEntity.ok().build());
    }

    private Mono<String> callWebClient() { // doesn't matter what happens here - this is just an example of a reactive call
        return webClient.get().uri("http://example.com").retrieve().bodyToMono(String.class);
    }
}

当我打电话

curl -sS 'http://localhost:8080/test/nobody'

我得到日志

[-,ff016a7a5a9aadbb,ff016a7a5a9aadbb,false] 5741 --- [ctor-http-nio-4] netty.Access: ACCESS

当我打电话

curl -sS 'http://localhost:8080/test/body'

我得到日志

[-,,,] 5741 --- [ctor-http-nio-1] netty.Access: ACCESS

请参阅本系列的请求日志。前两个到/nobody端点,其余两个到/body端点。如果/body请求与以前的/nobody请求由同一线程处理,则将显示跟踪/跨度ID。

[-,78bbe5342dbcd517,78bbe5342dbcd517,false] 7091 --- [ctor-http-nio-3] netty.Access: ACCESS
[-,941cc2e3afc9bbc4,941cc2e3afc9bbc4,false] 7091 --- [ctor-http-nio-3] netty.Access: ACCESS
[-,,,] 7091 --- [ctor-http-nio-1] netty.Access: ACCESS
[-,,,] 7091 --- [ctor-http-nio-2] netty.Access: ACCESS
[-,5728dcc1559f64ae,5728dcc1559f64ae,false] 7091 --- [ctor-http-nio-3] netty.Access: ACCESS
[-,,,] 7091 --- [ctor-http-nio-4] netty.Access: ACCESS
[-,,,] 7091 --- [ctor-http-nio-1] netty.Access: ACCESS
[-,,,] 7091 --- [ctor-http-nio-2] netty.Access: ACCESS
[-,00241261eb9931bd,00241261eb9931bd,false] 7091 --- [ctor-http-nio-3] netty.Access: ACCESS
[-,,,] 7091 --- [ctor-http-nio-4] netty.Access: ACCESS

这是一个错误吗?我可以以某种方式解决它吗?

版本:Spring Boot 2.0.4.RELEASE / 2.1.0.BUILD-SNAPSHOT,Spring Cloud Finchley.SR1

依赖项:spring-boot-starter-actuator,spring-boot-starter-webflux,spring-cloud-starter-sleuth

0 个答案:

没有答案