问题描述
在一个春季启动应用程序中(目前只有一个),我通过添加依赖项opentracing-spring-jaeger-web-starter
和以下bean来包含jaeger
@Bean
public static JaegerTracer getTracer() {
io.jaegertracing.Configuration.SamplerConfiguration samplerConfig =
io.jaegertracing.Configuration.SamplerConfiguration.fromEnv().withType("const").withParam(1);
io.jaegertracing.Configuration.ReporterConfiguration reporterConfig =
io.jaegertracing.Configuration.ReporterConfiguration.fromEnv().withLogSpans(true);
io.jaegertracing.Configuration config = new io.jaegertracing.Configuration("fooService").withSampler(samplerConfig).withReporter(reporterConfig);
return config.getTracer();
}
@postconstruct
public void setproperty() {
System.setProperty("JAEGER_REPORTER_LOG_SPANS","true");
}
在docker docker run -d --name jaeger -p 16686:16686 -p 6831:6831/udp jaegertracing/all-in-one:1.9
中启动Jaeger之后,我得到了跟踪。
我现在发现了另一个依赖关系,并阅读了不同的教程,这使我不确定以哪种方式将Jaeger与spring boot一起使用。
我将使用哪个依赖项?
https://github.com/opentracing-contrib/java-spring-cloud
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-spring-cloud-starter</artifactId>
</dependency>
https://github.com/signalfx/tracing-examples/tree/master/jaeger-java-spring-boot-web
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-spring-jaeger-web-starter</artifactId>
</dependency>
可能跟在Jaeger documentation之后
<dependency>
<groupId>io.jaegertracing</groupId>
<artifactId>jaeger-client</artifactId>
<version>$jaegerVersion</version>
</dependency>
就够了!?
在尝试使用Jaeger之前,我使用了Zipkin,该产品非常容易在Spring中集成,因为有用于侦探的启动器。日志在单独的字段中包含跟踪和跨度ID,因此可以搜索它们,例如在基巴纳积家(Jaeger)做not。
可以自定义吗?如何?
是否可以将Jaeger与Brave一起使用进行检测。该项目例如包括spring-cloud-starter-sleuth
作为依赖项。与现有的bean存在一些冲突。 Jaeger可以勇敢地使用吗?
解决方法
此链接(https://objectpartners.com/2019/04/25/distributed-tracing-with-apache-kafka-and-jaeger/)提供有关如何启用jaeger跟踪的详细信息。
使jaeger能够启动应用程序的最简单方法是添加依赖项和必需的属性。
依赖性:
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-spring-jaeger-web-starter</artifactId>
<version>3.2.0</version>
</dependency>
示例属性
opentracing.jaeger.udp-sender.host=localhost
opentracing.jaeger.udp-sender.port=6831
opentracing.jaeger.const-sampler.decision=true
opentracing.jaeger.enabled=true
opentracing.jaeger.log-spans=true
opentracing.jaeger.service-name=ms-name
,
回答有关依赖性的问题,这在“依赖性”部分(https://github.com/opentracing-contrib/java-spring-jaeger)中进行了解释:
opentracing-spring-jaeger-web-starter启动器是便捷启动器,其中包括opentracing-spring-jaeger-starter和opentracing-spring-web-starter。这意味着通过包含它,简单的Web Spring Boot微服务包括所有必要的依赖关系,以检测Web请求/响应并向Jaeger发送跟踪。
opentracing-spring-jaeger-cloud-starter启动器是便捷启动器,其中包括opentracing-spring-jaeger-starter和opentracing-spring-cloud-starter这意味着通过包含它,Spring Cloud堆栈的所有部分都受支持通过Opentracing将被检测
顺便说一句:
- opentracing.jaeger.log-spans默认为true
与:
- opentracing.jaeger.udp-sender.host = localhost
- opentracing.jaeger.udp-sender.port = 6831
- opentracing.jaeger.const-sampler.decision = true
- opentracing.jaeger.enabled = true
要配置 Jaeger,我们需要在每个服务中(在 pom.xml 中)添加 Jaeger Client 的依赖项。
Jaeger 客户端依赖:
<dependency>
<groupId>io.opentracing.contrib</groupId>
<artifactId>opentracing-spring-jaeger-web-starter</artifactId>
<version>3.2.0</version>
</dependency>
添加依赖后,我们需要在每个服务上添加 Jaeger Client 配置。
@Configuration
public class JaegerConfig {
@Bean
public JaegerTracer jaegerTracer() {
return new io.jaegertracing.Configuration("jaeger-client")
.withSampler(new io.jaegertracing.Configuration.SamplerConfiguration().withType(ConstSampler.TYPE)
.withParam(1))
.withReporter(new io.jaegertracing.Configuration.ReporterConfiguration().withLogSpans(true))
.getTracer();
}
}
我们还可以根据我们在 Jaeger 中使用的采样策略来更改配置。 您可以参考官方文档了解采样策略。 [1]:https://www.jaegertracing.io/docs/1.22/sampling/
设置 Jaeger Run 后
docker run -d --name jaeger -p 16686:16686 -p 6831:6831/udp jaegertracing/多合一:1.9
在端口 16686 上运行 Jaeger UI。