SpringBoot 2.4.X 接入Jaeger

本文介绍了如何在SpringBoot 2.4.x项目中接入Jaeger进行调用链跟踪,推荐使用springcloud 2020.0.x版本,支持Kafka接入。步骤包括:添加Jaeger依赖,考虑引入openfeign依赖,声明ScopeManager,修改logback日志pattern,以及配置Jaeger的相关参数如sampling-rate、skip-pattern等,以控制数据采集和避免超大数据量。
摘要由CSDN通过智能技术生成

Jaeger 全链路追踪架构图

SpringBoot 2.4.X 接入,建议 springcloud 版本 2020.0.X,该版本支持 kafka 接入调用链
步骤一:POM 文件中引入jaeger依赖

<dependency>
 <groupId>io.opentracing.contrib</groupId>
 <artifactId>opentracing-spring-jaeger-cloud-starter</artifactId>
 <version>3.2.2</version>
</dependency>
<dependency>
 <groupId>io.jaegertracing</groupId>
 <artifactId>jaeger-client</artifactId>
 <version>1.6.0</version>
</dependency>

步骤二:如果使用 openfeign,推荐使用 openfeign 依赖


                
当您在Spring Boot应用程序中集成Jaeger时,您需要进行以下步骤: 1. 添加依赖:在您的Spring Boot项目的`pom.xml`文件中添加Jaeger客户端库的依赖项。例如: ```xml <dependency> <groupId>io.jaegertracing</groupId> <artifactId>jaeger-client</artifactId> <version>1.6.0</version> </dependency> ``` 2. 配置Jaeger:在您的应用程序的配置文件(如`application.properties`或`application.yml`)中添加Jaeger的配置。例如: ```yaml # Jaeger配置 jaeger: service-name: your-service-name udp-sender: host: localhost port: 6831 ``` 这里,您需要指定`service-name`作为您的应用程序的名称,并配置Jaeger的发送器(可以是UDP、HTTP等)的主机和端口。 3. 创建Jaeger Tracer Bean:在您的Spring Boot应用程序的配置类中创建一个Jaeger Tracer Bean。例如: ```java import io.jaegertracing.Configuration; import io.jaegertracing.internal.samplers.ConstSampler; import io.opentracing.Tracer; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; @Configuration public class JaegerConfig { @Bean public Tracer jaegerTracer() { Configuration.SamplerConfiguration samplerConfig = Configuration.SamplerConfiguration.fromEnv() .withType(ConstSampler.TYPE) .withParam(1); Configuration.ReporterConfiguration reporterConfig = Configuration.ReporterConfiguration.fromEnv() .withLogSpans(true); Configuration config = new Configuration("your-service-name") .withSampler(samplerConfig) .withReporter(reporterConfig); return config.getTracer(); } } ``` 在上面的代码中,我们创建了一个`jaegerTracer()`方法,该方法使用了Jaeger的配置和参数,并返回一个Jaeger Tracer实例。 4. 使用Jaeger Tracer:在您的应用程序中使用注入的Jaeger Tracer实例进行跟踪。例如,在您的控制器类中: ```java import io.opentracing.Span; import io.opentracing.Tracer; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class MyController { @Autowired private Tracer tracer; @GetMapping("/hello") public String hello() { Span span = tracer.buildSpan("helloSpan").start(); // 执行您的业务逻辑 span.finish(); return "Hello, World!"; } } ``` 在上面的例子中,我们使用了注入的`tracer`实例创建了一个Span,用于跟踪`hello()`方法的执行。您可以根据需要在其他地方创建更多的Span。 5. 运行Jaeger Agent:在集成Jaeger之前,请确保Jaeger Agent正在运行并监听所配置的主机和端口。Jaeger Agent负责接收来自应用程序的跟踪数据并将其发送到Jaeger Collector。 这样,您就完成了Spring Boot应用程序与Jaeger的集成。 请注意,这只是一个简单的示例,您可以根据您的需求进行更复杂的集成。更多关于Jaeger的配置和使用方法,请参考Jaeger的官方文档。 希望这个示例对您有所帮助!如有任何其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值