首页 > 解决方案 > 无法使用 spring-boot 应用程序从 Jaeger UI 跟踪服务

问题描述

无法在 Jaeger UI (localhost:16686/search) 上跟踪 springboot 应用程序的服务。在这里,我能够成功运行应用程序,但无法在 jaeger ui 中获得服务(默认一个 jaeger-query 除外)。

我使用 docker cmd 启动了 jaeger 服务,

docker run --rm -it --network=host jaegertracing/all-in-one

在http://localhost:16686/search上打开 Jaeger UI

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.web.client.RestTemplate;

import com.uber.jaeger.Configuration;
import com.uber.jaeger.samplers.ProbabilisticSampler;

@SpringBootApplication
public class DemoOpentracing1Application {


@Bean
public RestTemplate restTemplate(RestTemplateBuilder restTemplateBuilder) {
    return restTemplateBuilder.build();
}

@Bean
public io.opentracing.Tracer jaegerTracer() {
    return new Configuration("spring-boot", new Configuration.SamplerConfiguration(ProbabilisticSampler.TYPE, 1),
            new Configuration.ReporterConfiguration())
            .getTracer();
}

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

}

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
public class HelloController {

@Autowired
private RestTemplate restTemplate;

@RequestMapping("/hello")
public String hello() {
    return "Hello from Spring Boot!";
}

@RequestMapping("/chaining")
public String chaining() {
    ResponseEntity<String> response = restTemplate.getForEntity("http://localhost:8082/hello", String.class);
    return "Chaining + " + response.getBody();
}

}

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.2.6.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>demo_opentracing1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo_opentracing1</name>
<description>Demo project for Spring Boot</description>

<properties>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>

    <dependency>
        <groupId>io.opentracing.contrib</groupId>
        <artifactId>opentracing-spring-web-autoconfigure</artifactId>
        <version>0.0.4</version>
    </dependency>

    <dependency>
        <groupId>com.uber.jaeger</groupId>
        <artifactId>jaeger-core</artifactId>
        <version>0.18.0</version>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

图 1 图 2

带有演示的 Github 存储库:https ://github.com/pavolloffay/opentracing-java-examples 。请帮我解决这个问题。

标签: javaspringspring-bootrestjaeger

解决方案


问题是您的 Jaeger 收集器无法从您在 docker 命令中指定的外部 docker 网络主机访问。这仅在您的 Spring Boot 应用程序也部署在主机网络上时才有效。尝试按如下方式运行 Jaeger:

docker run -d --name jaeger \
  -e COLLECTOR_ZIPKIN_HTTP_PORT=9411 \
  -p 5775:5775/udp \
  -p 6831:6831/udp \
  -p 6832:6832/udp \
  -p 5778:5778 \
  -p 16686:16686 \
  -p 14268:14268 \
  -p 14250:14250 \
  -p 9411:9411 \
  jaegertracing/all-in-one:1.17

它应该触发。


推荐阅读