SpringCloud搭建微服务之Micrometer分布式链路追踪

1. 概述

由于Spring Cloud Sleuth最新版本只支持Spring Boot 2.7.x,核心项目已经迁移到Micrometer Traceing项目,Spring Boot 3.x版本要实现分布式链路追踪需要集成Micrometer。更多详情可以参阅Micrometer官网
Micrometer官网
本文将以Spring Boot 3.2.x和Spring Cloud 2023.0.x版本和JDK 17实现分布式链路追踪,有需要了解Spring Boot 2.7.x版本实现分布式链路追踪的小伙伴可以参阅SpringCloud搭建微服务之Sleuth分布式请求链路跟踪

2. 搭建搭建Zipkin Server

SpringCloud搭建微服务之Sleuth分布式请求链路跟踪这篇文章第二部分已经详细介绍了如何下载zipkin jar包和安装的步骤,新的版本下载就不再赘述,需要注意的是,最新版zipkin需要本地安装JDK 17。下载后,使用命令java -jar zipkin-server-3.1.1-exec.jar启动zipkin server即可
启动zipkin server
启动zipkin server后,在浏览器输入http://localhost:9411/zipkin进入zipkin首页
zipkin首页

3. 微服务集成Micrometer

3.1. 引入核心依赖

在服务提供端和服务消费端都引如以下依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!--指标追踪-->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing</artifactId>
</dependency>
<!--适配zipkin的桥接包-->
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-tracing-bridge-brave</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-observation</artifactId>
</dependency>
<dependency>
    <groupId>io.github.openfeign</groupId>
    <artifactId>feign-micrometer</artifactId>
</dependency>
<dependency>
    <groupId>io.zipkin.reporter2</groupId>
    <artifactId>zipkin-reporter-brave</artifactId>
</dependency>

3.2. 编写application.yml

management:
  zipkin:
    tracing:
      endpoint: http://localhost:9411/api/v2/spans
  tracing:
    sampling:
      probability: 1.0 #采样率默认0.1(10次只能有一次被记录),值越大手机越及时

4. 验证

分别启动服务提供端和服务消费端微服务
刷新zipkin管理端,可以看到调用请求
调用请求
点击Dependencies,可以查看调用链路
引用

  • 9
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Cloud是一个用于构建分布式系统的开发工具包,而Prometheus是一个开源的监控和警报系统。它们可以结合使用,以实现对Spring Cloud应用程序的监控和指标收集。 在Spring Cloud中,你可以使用Spring Boot Actuator模块来暴露应用程序的运行时指标。这些指标包括HTTP请求的计数、错误率、内存使用情况等。Prometheus可以通过轮询这些指标的端点来收集数据,并将其存储在自己的时间序列数据库中。 要将Spring Cloud与Prometheus集成,你需要做以下几个步骤: 1. 添加Prometheus依赖:在你的Spring Boot项目中,添加Prometheus相关的依赖,如`micrometer-registry-prometheus`。 2. 配置Prometheus端点:在`application.properties`或`application.yml`文件中,配置Spring Boot Actuator的端点路径和Prometheus的路径。 3. 启用Prometheus收集器:通过添加`@EnablePrometheusEndpoint`注解,启用Prometheus收集器。 4. 配置Prometheus收集器:通过添加`@EnableSpringBootMetricsCollector`注解,配置Prometheus收集器的行为。 5. 启动Prometheus服务器:在你的环境中启动Prometheus服务器,并配置它来定期抓取Spring Cloud应用程序的指标。 一旦配置完成,Prometheus就可以定期从你的Spring Cloud应用程序中收集指标,并将其存储在自己的数据库中。你可以使用Prometheus的查询语言(PromQL)来查询和分析这些指标,并创建自定义的监控仪表盘和警报规则。 希望这些信息能对你有所帮助!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值