9阅网

您现在的位置是:首页 > 知识 > 正文

知识

java - 无法使用spring-boot应用程序从Jaeger UI追踪服务。

admin2022-11-07知识17

无法在Jaeger UI(localhost:16686search)上追踪springboot应用的服务。在这里,我可以成功运行一个应用程序,但无法在jaeger ui中获得服务(除了defalut一个jaeger-query)。

我使用docker cmd来启动jaeger服务。

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

打开积家的用户界面 http:/localhost:16686search

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.compavolloffayopentracing-java-examples。请帮我解决这个问题。



【回答】:

问题是你的Jaeger收集器不能像你在docker命令中指定的那样从docker网络主机外部访问。只有当你的spring boot应用程序部署在主机网络上时,这才会有效。

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

它应该会触发。