如何使用springwebflux实现对服务的跟踪?

s8vozzvw  于 2021-07-23  发布在  Java
关注(0)|答案(1)|浏览(460)

我正在使用springwebflux实现服务到服务的集成。每个微服务都是隔离的,并运行不同的端口。我想看到一个使用jaeger的端到端跟踪。问题是每个服务都在毫无问题地捕获跟踪,但我看不到服务到服务的通信和体系结构设计。
首选服务是接收请求并转发客户服务。
使用sleuth和zipkin收集跟踪

implementation 'org.springframework.boot:spring-boot-starter-webflux'
implementation 'org.springframework.cloud:spring-cloud-sleuth-zipkin'
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'

首选服务代码部分

@RequestMapping("/preference")
@ContinueSpan
public Flux<?> preference(@RequestHeader Map<String, String> headers) {

    return callByPriceReactive( headers);
}

@NewSpan("chainingPriceReactive")
private Flux<?> callByPriceReactive(Map<String, String> headers ){
    logger.debug("starting statement");
    logger.info("active span: " + tracer.activeSpan());

    return webClient.get().uri("/customerByPrice?maxPrice=1")
           // .header("X-B3-TRACEID", headers.get("X-B3-TRACEID"))
            .retrieve()
            .bodyToFlux(String.class)
            .checkpoint();

}

首选项服务属性文件

logging.level.com.trace.customer=debug
server.port=7072
logging.level.org.springframework.web=info
spring.sleuth.traceId128=true
spring.sleuth.sampler.probability=1.0
spring.application.name=preference-application 
customer.api.url= http://localhost:7071
spring.sleuth.opentracing.enabled=true

## spring.zipkin.base-url=http://<your-Jaeger-server>:<port>

客户服务代码部分

@RequestMapping("/customerByPrice")
@ContinueSpan
public ResponseEntity<Flux<?>> getCustomer(@RequestParam int maxPrice,@RequestHeader Map<String, String> headers) {

    logger.debug("tracer: " + tracer);
    logger.info("active span: " + tracer.activeSpan());
    // just a log statement to show the current context
    logger.debug("starting statement");

    return ResponseEntity.ok(customerService.
            generateRandomCustomer(maxPrice)
          //  .delayElements(Duration.ofSeconds(1))
            .doOnNext(customer -> logger.info("Found customer  {} for ${}", customer.getCustomerId(), 
             customer.getAmount()))
            .doOnComplete(() -> logger.debug("done!"))
            .doOnError(e -> logger.error("failure", e)));
}

客户服务道具文件

logging.level.com.trace.customer=debug
server.port=7071
logging.level.org.springframework.web=info 
spring.sleuth.traceId128=true
spring.sleuth.sampler.probability=1.0
spring.application.name=customer-application 
spring.sleuth.opentracing.enabled=true

## spring.zipkin.base-url=http://<your-Jaeger-server>:<port>

jaeger正在docker compose文件中运行

version: "3"

services:
 jaeger:
  image: "jaegertracing/all-in-one:latest"
  environment:
   - COLLECTOR_ZIPKIN_HTTP_PORT=9411
  ports:
  - "5775:5775/udp"
  - "6831:6831/udp"
  - "6832:6832/udp"
  - "5778:5778"
  - "16686:16686"
  - "14268:14268"
  - "9411:9411"

jaeger中的首选服务跟踪jaeger中的客户服务跟踪

bt1cpqcv

bt1cpqcv1#

基于此:
问题是每个服务都在毫无问题地捕获跟踪,但我看不到服务到服务的通信和体系结构设计。
跟踪信息似乎没有跨服务传播。您可以通过查看http头并检查 traceId . 为了使这项工作 traceId 请求之间应该相同。你应该看到同样的情况 traceId 也在日志里。
本文档为您提供了一些解决此问题的方法:
如何使restemplate、webclient等工作?
webclient集成

相关问题