spring boot3 和 micrometer-tracing-bridge-brave 进行 rabbit链路无效
时间: 2024-01-04 10:04:48 浏览: 54
我理解你的问题是关于在Spring Boot 3中使用Micrometer Tracing Bridge Brave进行RabbitMQ链路追踪时遇到的问题。
首先,你需要在你的项目中添加以下依赖:
```xml
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-zipkin</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
```
然后,在你的配置文件中,你需要启用Micrometer和Zipkin:
```
management.metrics.export.zipkin.enabled=true
management.metrics.export.zipkin.uri=http://localhost:9411
```
接下来,你需要配置RabbitMQ,以便将跟踪信息添加到消息头中:
```
@Bean
public RabbitTemplate rabbitTemplate(ConnectionFactory connectionFactory, Tracer tracer) {
RabbitTemplate rabbitTemplate = new RabbitTemplate(connectionFactory);
rabbitTemplate.setBeforePublishPostProcessors(message -> {
Span span = tracer.currentSpan();
if (span != null) {
message.getMessageProperties().setHeader("X-B3-TraceId", span.context().traceIdString());
message.getMessageProperties().setHeader("X-B3-SpanId", span.context().spanIdString());
}
return message;
});
return rabbitTemplate;
}
```
最后,你需要在你的应用程序中创建一个跨度:
```java
@Autowired
private Tracer tracer;
public void sendMessage() {
Span span = tracer.nextSpan().name("sendMessage").start();
try (Tracer.SpanInScope ws = tracer.withSpan(span)) {
// 发送消息
} finally {
span.finish();
}
}
```
如果你的链路追踪仍然无效,可能是因为你的RabbitMQ配置不正确或你的Zipkin服务器没有启动。你可以使用Zipkin UI查看跟踪信息,或者使用Zipkin API查询跟踪信息。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)