环境:
spring boot:2.6.2
opentracing-spring-jaeger-cloud-starter:3.2.2
jaeger-client:1.6.0
<!--jaeager-->
<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>
注意:
opentracing-spring-jaeger-cloud-starter不支持最新版本的spring-cloud-starter-openfeign。
spring-cloud-starter-openfeign中的LoadBalancerFeignClient被移除了。
所以我们需要自己创建下,直接复制
package org.springframework.cloud.openfeign.ribbon;
public class CachingSpringLoadBalancerFactory {
}
package org.springframework.cloud.openfeign.ribbon;
import feign.Client;
public class LoadBalancerFeignClient {
public LoadBalancerFeignClient(Client delegate, CachingSpringLoadBalancerFactory lbClientFactory, SpringClientFactory clientFactory) {
throw new UnsupportedOperationException();
}
public Client getDelegate() {
throw new UnsupportedOperationException();
}
}
package org.springframework.cloud.openfeign.ribbon;
public class SpringClientFactory {
}
docker直接部署jaeger
docker run -d --name jaeger \
-e COLLECTOR_ZIPKIN_HOST_PORT=:9411 \
-p 5775:5775/udp \
-p 6831:6831/udp \
-p 6832:6832/udp \
-p 5778:5778 \
-p 16686:16686 \
-p 14250:14250 \
-p 14268:14268 \
-p 14269:14269 \
-p 9411:9411 \
jaegertracing/all-in-one:1.33
配置jeager的相关参数
opentracing:
jaeger:
enabled: true # 启用对接Jaeger(可以不配置,默认true)
enable128-bit-traces: true # 使用短TraceID(可以不配置)
udp-sender:
udp-sender:
host: 192.168.5.110 # 配置Jaeger服务器地址
jeager-ui地址: http://localhost:16686
其他链路追踪对比
如果只需要traceId 可以只需要引入
<!--链路追踪 start-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth</artifactId>
<version>2.0.0.RELEASE</version>
<type>pom</type>
<scope>import</scope>
</dependency>
<!--链路追踪 end-->
即可获取到
import brave.Tracer;
@Service
public class Breadcrumb {
@Autowired
private Tracer tracer;
public String breadcrumbId() {
return tracer.currentSpan().context().traceIdString();
}
}