springboot3因sleuth移除使用zipkin解决方案

前言

最近在使用springboot3搭建cloud微服务在链式追踪做了下选型 ,因服务者有多种语言,skywalking的php探针很难使用,所以打算使用zipkin埋点来做链路追踪!不过尴尬的是boot3剔除了sleuth的支持!所以无法使用spring-cloud-starter-sleuth依赖了,直接无效。百度、csdn并没有一个哥们提供一下迁移示例,没办法只能自己看官方文档来做迁移文章了,希望能帮助各位老铁了,点个关注不迷路!顶起来!

迁移官方文档:Spring Cloud Sleuth Features

废话不多说直接上代码!

问题版本,3.0以下可以正常使用sleuth+zipkin,不用看本文,本文只解决3.0及以上版本。

所需依赖 

        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-tracing-bridge-brave</artifactId>
        </dependency>
        <dependency>
            <groupId>io.zipkin.reporter2</groupId>
            <artifactId>zipkin-reporter-brave</artifactId>
        </dependency>
        <dependency>
            <groupId>io.micrometer</groupId>
            <artifactId>micrometer-observation</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-zipkin</artifactId>
            <version>2.2.8.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-micrometer</artifactId>
            <version>12.5</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-starter-openfeign</artifactId>
        </dependency>

注意!spring-boot-starter-actuator这个很重要!官方文档有阐述说明!

package com.example.consumer;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;

@SpringBootApplication
@EnableDiscoveryClient
//声明feign客户端
@EnableFeignClients
public class ConsumerApplication {

    public static void main(String[] args) {
        SpringApplication.run(ConsumerApplication.class, args);
    }


}

 

package com.example.consumer.service;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.web.bind.annotation.GetMapping;
@Service
@FeignClient("java-provider")
public interface IndexService {
    @GetMapping("/index/index")
    String index();
}

 

package com.example.consumer.controller;


import com.example.consumer.service.IndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/index")

public class IndexController {

    @Autowired
    private IndexService indexService;

    @RequestMapping("/test")
    public String test() {
        return "test";
    }
    @RequestMapping("/java")
    public String java() {
        return indexService.index();
    }

}

 

 

server:
  port: 9457
spring:
  application:
    name: java-consumer
management:
  zipkin:
    tracing:
      endpoint: http://127.0.0.1:9411/api/v2/spans
  tracing:
    sampling:
      probability: 1.0 # 记录速率100%

 

 诶,研究了一天,还是自己能力不行啊!

【2023年10月7日更新】

视频讲解:【IT老齐336】Micrometer Tracing与Zipkin实现链路追踪_哔哩哔哩_bilibili

【注意事项】

springboot3后期会抛弃resttemplate,所以想在zipkin中多服务调用实现链路请使用openfeign来进行远程调用!

  • 6
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论
我理解你的问题是关于在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查询跟踪信息。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一码超人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值