You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为Docker容器内的Spring Boot应用配置Jaeger OpenTracing埋点?

在Docker容器中运行Spring Boot应用并通过Jaeger实现OpenTracing埋点

我来帮你梳理下完整的配置流程,结合OpenTracing Spring Web和Jaeger客户端,让你的Docker化Spring Boot应用轻松实现链路追踪:

1. 添加必要的依赖

首先在你的pom.xml(Maven)或build.gradle(Gradle)中加入核心依赖,记得替换成最新的稳定版本:

Maven 依赖

<!-- OpenTracing Spring Web 自动埋点依赖 -->
<dependency>
    <groupId>io.opentracing.contrib</groupId>
    <artifactId>opentracing-spring-web-starter</artifactId>
    <version>0.40.0</version>
</dependency>
<!-- Jaeger OpenTracing 客户端 -->
<dependency>
    <groupId>io.jaegertracing</groupId>
    <artifactId>jaeger-client</artifactId>
    <version>1.8.0</version>
</dependency>

2. 完善Tracer Bean配置

你提供的代码片段不完整,这里补全可直接使用的配置类,关键细节都做了注释:

import io.opentracing.Tracer;
import io.opentracing.contrib.spring.web.starter.ServerTracingAutoConfiguration;
import io.jaegertracing.Configuration;
import io.jaegertracing.samplers.ProbabilisticSampler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(ServerTracingAutoConfiguration.class) // 启用Web请求自动埋点,无需手动写拦截器
public class TracingConfig {

    @Bean
    public Tracer jaegerTracer() {
        return new Configuration(
                "hello_service", // 服务标识,会在Jaeger UI中显示
                new Configuration.SamplerConfiguration(
                        ProbabilisticSampler.TYPE,
                        1.0 // 采样率:1.0表示全采样,生产环境建议调整为0.1~0.5避免存储压力
                ),
                new Configuration.ReporterConfiguration()
                        .withLogSpans(true) // 记录Span的详细日志
                        .withSender(new Configuration.SenderConfiguration()
                                .withAgentHost("jaeger") // Docker网络内Jaeger服务的主机名,需和容器名一致
                                .withAgentPort(6831) // Jaeger Agent默认UDP接收端口
                        )
        ).getTracer();
    }
}

3. Docker环境部署注意事项

先启动Jaeger服务

用官方镜像启动All-In-One版Jaeger(包含Agent、Collector、Query和UI):

# 先创建自定义Docker网络,让应用和Jaeger互通
docker network create tracing-network

# 启动Jaeger容器
docker run -d --name jaeger --network tracing-network \
  -p 6831:6831/udp \
  -p 16686:16686 \
  jaegertracing/all-in-one:latest
  • 16686端口是Jaeger UI的访问端口,浏览器打开http://localhost:16686即可查看链路数据

启动你的Spring Boot应用容器

确保把应用加入同一个Docker网络,这样才能通过jaeger主机名访问到Jaeger Agent:

docker run -d --name hello-service --network tracing-network \
  -p 8080:8080 \
  your-spring-boot-image:latest

4. 验证埋点效果

调用你的Spring Boot应用接口(比如GET http://localhost:8080/hello),然后打开Jaeger UI:

  1. 在"Service"下拉框选择hello_service
  2. 点击"Find Traces"按钮
  3. 就能看到请求对应的完整链路追踪数据了

内容的提问来源于stack exchange,提问作者TheManawaha

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 10:04:07