Java中使用Jaeger进行分布式系统跟踪

2025-04发布4次浏览

Java中使用Jaeger进行分布式系统跟踪

在现代微服务架构中,分布式系统追踪是一个关键需求。Jaeger 是一个开源的分布式追踪系统,能够帮助开发者监控和分析分布式系统的性能与行为。本文将介绍如何在Java项目中集成Jaeger,并通过代码示例展示其实际应用。

1. Jaeger简介

Jaeger 是由 Uber 开源的一个分布式追踪工具,支持 OpenTracing 标准。它可以帮助开发人员监控和优化分布式系统的性能,提供以下功能:

  • 分布式上下文传播:跨服务传递请求的上下文信息。
  • 分布式事务监控:跟踪请求从客户端到后端服务的完整路径。
  • 延迟分析:识别性能瓶颈和服务依赖关系。

2. 环境准备

在开始之前,请确保已安装以下内容:

  • Java JDK(建议版本8或更高)
  • MavenGradle
  • Jaeger All-in-One:可以通过 Docker 启动,命令如下:
    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:latest
    

访问 http://localhost:16686 可以查看 Jaeger UI。

3. Maven依赖配置

在项目的 pom.xml 文件中添加以下依赖项:

<dependency>
    <groupId>io.opentracing.contrib</groupId>
    <artifactId>opentracing-spring-jaeger-cloud-starter</artifactId>
    <version>3.4.0</version>
</dependency>

4. 配置Jaeger

application.properties 中添加以下配置:

# Jaeger configuration
spring.sleuth.sampler.probability=1.0
spring.jaeger.udp-sender.host=localhost
spring.jaeger.udp-sender.port=6831
spring.jaeger.service-name=my-service
  • spring.sleuth.sampler.probability=1.0 表示采样率为100%。
  • spring.jaeger.service-name 定义了服务名称。

5. 实践步骤

5.1 创建Spring Boot项目

创建一个简单的 Spring Boot 项目,包含两个服务:ServiceAServiceBServiceA 调用 ServiceB,并记录整个调用链路。

5.2 ServiceA代码示例
package com.example.servicea;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

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

@RestController
class ServiceARestController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/call-b")
    public String callServiceB() {
        return restTemplate.getForObject("http://localhost:8081/service-b", String.class);
    }
}
5.3 ServiceB代码示例
package com.example.serviceb;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

@RestController
class ServiceBRestController {

    @GetMapping("/service-b")
    public String serviceB() {
        return "Response from Service B";
    }
}
5.4 启动服务

分别启动 ServiceAServiceB,并在浏览器中访问 http://localhost:8080/call-b。此时,Jaeger 将会记录整个调用链路。

6. 查看Jaeger UI

打开 http://localhost:16686,选择服务名称 my-service,即可看到完整的调用链路。

7. 扩展知识

  • OpenTracing:Jaeger 支持 OpenTracing 标准,允许开发者在不同框架之间切换追踪实现。
  • Zipkin兼容性:Jaeger 提供对 Zipkin 的兼容性支持,可以轻松迁移到其他追踪系统。
  • 性能优化:通过调整采样率 (spring.sleuth.sampler.probability),可以减少追踪数据对生产环境的影响。