我有一个使用webclient调用另一个微服务(子服务)的父服务。
public Mono<Product> productFromIntegrator() {
String url = "http://localhost:8282/product";
return Mono.deferContextual(contextView -> {
try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView,
ObservationThreadLocalAccessor.KEY)) {
LOG.info("<ACCEPTANCE_TEST> <TRACE:{}> Hello from producer", tracer.currentSpan().context().traceId());
LOG.info("Service Called");
return webClient.get().uri(url)
.retrieve().bodyToMono(Product.class).log(LOG.getName(), Level.FINE)
.onErrorMap(WebClientException.class, ex -> {
LOG.warn("Error retrieving Product");
return ex;
});
}
});
}
来自子级的RestController:
@Autowired
private final Service service;
private final Tracer tracer;
public Controller(Service service, Tracer tracer) {
this.service = service;
this.tracer = tracer;
}
@GetMapping(value = "/product")
public Mono<Product> getProduct() {
return Mono.deferContextual(contextView -> {
try (ContextSnapshot.Scope scope = ContextSnapshot.setThreadLocalsFrom(contextView,
ObservationThreadLocalAccessor.KEY)) {
LOG.info("<ACCEPTANCE_TEST> <TRACE:{}> Hello from producer", tracer.currentSpan().context().traceId());
LOG.info("Service Called");
return this.service.getProduct();
}
});
}
子级未从父级获取TraceId,它正在创建新的观察对象。关于如何将TraceId从父级传播到子级,有何建议/示例?
1条答案
按热度按时间z5btuh9x1#
找到了:How to get trace-id in spring-cloud-gateway using micrometer-brave
在psvm中添加挂钩。