当前位置: 首页 > news >正文

Spring Cloud Sleuth 迁移指南:从 3.1 到 Micrometer Tracing 的终极路径

Spring Cloud Sleuth 迁移指南:从 3.1 到 Micrometer Tracing 的终极路径

【免费下载链接】spring-cloud-sleuthDistributed tracing for spring cloud项目地址: https://gitcode.com/gh_mirrors/sp/spring-cloud-sleuth

Spring Cloud Sleuth 是 Spring Cloud 生态中的分布式追踪解决方案,它能够帮助开发者追踪请求和消息在分布式系统中的流转,从而快速定位问题和优化性能。然而,随着技术的发展,Spring Cloud Sleuth 的核心功能已迁移至 Micrometer Tracing 项目, instrumentations 也将逐步迁移至 Micrometer 及其他相关项目。本文将为你提供一份详细的迁移指南,助你顺利从 Spring Cloud Sleuth 3.1 过渡到 Micrometer Tracing。

为什么要迁移到 Micrometer Tracing?

Spring Cloud Sleuth 的核心已被迁移到 Micrometer Tracing 项目,这意味着未来的开发和维护将主要集中在 Micrometer Tracing 上。迁移到 Micrometer Tracing 可以让你享受到更活跃的社区支持、更丰富的功能以及与其他 Micrometer 组件更好的集成。

迁移前的准备工作

在开始迁移之前,请确保你的项目满足以下条件:

  1. 使用 Spring Boot 2.6.x 或更高版本。
  2. 移除项目中对 Spring Cloud Sleuth 的直接依赖。
  3. 准备好了解 Micrometer Tracing 的基本概念和术语。

核心依赖替换

移除 Spring Cloud Sleuth 依赖

首先,需要从你的pom.xml文件中移除 Spring Cloud Sleuth 的相关依赖。例如:

<!-- 移除 Spring Cloud Sleuth 依赖 --> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-sleuth</artifactId> </dependency>

添加 Micrometer Tracing 依赖

然后,添加 Micrometer Tracing 的核心依赖。Micrometer Tracing 提供了对多种追踪系统的支持,你可以根据自己的需求选择合适的依赖。例如,如果你使用 Zipkin 作为追踪系统,可以添加以下依赖:

<!-- 添加 Micrometer Tracing 核心依赖 --> <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.zipkin.reporter2</groupId> <artifactId>zipkin-reporter-brave</artifactId> </dependency>

API 变更与替换

核心类和接口的变更

Spring Cloud Sleuth 中的一些核心类和接口在 Micrometer Tracing 中发生了变化。以下是一些常见的变更:

Spring Cloud Sleuth 3.1Micrometer Tracing
org.springframework.cloud.sleuth.Tracerio.micrometer.tracing.Tracer
org.springframework.cloud.sleuth.Spanio.micrometer.tracing.Span
org.springframework.cloud.sleuth.annotation.NewSpanio.micrometer.tracing.annotation.NewSpan
org.springframework.cloud.sleuth.annotation.ContinueSpanio.micrometer.tracing.annotation.ContinueSpan

代码示例:创建新的 Span

在 Spring Cloud Sleuth 3.1 中,创建新的 Span 可能如下所示:

import org.springframework.cloud.sleuth.Tracer; import org.springframework.cloud.sleuth.annotation.NewSpan; @Service public class MyService { private final Tracer tracer; public MyService(Tracer tracer) { this.tracer = tracer; } @NewSpan("my-operation") public void doSomething() { // 业务逻辑 Span currentSpan = tracer.currentSpan(); currentSpan.tag("key", "value"); } }

在 Micrometer Tracing 中,相应的代码将变为:

import io.micrometer.tracing.Tracer; import io.micrometer.tracing.annotation.NewSpan; @Service public class MyService { private final Tracer tracer; public MyService(Tracer tracer) { this.tracer = tracer; } @NewSpan("my-operation") public void doSomething() { // 业务逻辑 io.micrometer.tracing.Span currentSpan = tracer.currentSpan(); currentSpan.tag("key", "value"); } }

配置变更

应用名称配置

在 Spring Cloud Sleuth 中,应用名称通常通过spring.application.name配置。在 Micrometer Tracing 中,这个配置仍然有效,但你也可以通过 Micrometer 的配置来进一步自定义服务名称:

spring: application: name: my-service micrometer: tracing: service: name: my-service # 可选,默认为 spring.application.name

采样率配置

采样率配置也有所变化。在 Spring Cloud Sleuth 中,你可能会这样配置采样率:

spring: sleuth: sampler: probability: 1.0 # 100% 采样

在 Micrometer Tracing 中,相应的配置为:

micrometer: tracing: sampler: probability: 1.0 # 100% 采样

追踪数据导出

导出到 Zipkin

如果你之前使用 Zipkin 来收集追踪数据,迁移到 Micrometer Tracing 后,配置方式如下:

zipkin: base-url: http://localhost:9411 # Zipkin 服务器地址 micrometer: tracing: exporter: zipkin: enabled: true

图:Zipkin 中显示的追踪数据示例,展示了一个分布式请求的调用链。

常见问题与解决方案

问题:@NewSpan 注解不生效

解决方案:确保你已经添加了micrometer-tracing-annotation依赖,并且在配置类上添加了@EnableTracing注解。

<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-tracing-annotation</artifactId> </dependency>
import io.micrometer.tracing.annotation.EnableTracing; @Configuration @EnableTracing public class TracingConfig { }

问题:日志中没有追踪信息

解决方案:检查你的日志配置,确保日志格式中包含了追踪相关的变量。例如,在 Logback 中,你可以这样配置:

<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level [%X{traceId:-},%X{spanId:-}] %logger{36} - %msg%n</pattern> </encoder> </appender>

迁移后的验证

迁移完成后,你可以通过以下方式验证迁移是否成功:

  1. 启动应用,发送请求,检查 Zipkin 服务器是否收到追踪数据。
  2. 查看应用日志,确认日志中包含 traceId 和 spanId。
  3. 测试带有@NewSpan@ContinueSpan注解的方法,确保 Span 能够正确创建和传播。

总结

从 Spring Cloud Sleuth 3.1 迁移到 Micrometer Tracing 是一个相对平滑的过程,主要涉及依赖替换、API 调整和配置更新。通过本文提供的指南,你可以快速完成迁移,并享受到 Micrometer Tracing 带来的优势。如果你在迁移过程中遇到问题,可以参考官方文档或社区资源获取帮助。

希望本文对你的迁移工作有所帮助,祝你在分布式追踪的道路上越走越远!🚀

【免费下载链接】spring-cloud-sleuthDistributed tracing for spring cloud项目地址: https://gitcode.com/gh_mirrors/sp/spring-cloud-sleuth

创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考

http://www.jsqmd.com/news/849372/

相关文章:

  • DPM-Solver代码架构解析:从模型包装器到求解器核心
  • AI Agent Harness Engineering 后端架构选型:微服务 vs 单体架构的取舍
  • 倍仕得电气科技(杭州)股份有限公司2026通用工业连接器厂商优选:圆形/M12/储能/流体/传感器连接器厂家甄选倍仕得电 - 栗子测评
  • Keil C51字符数组初始化错误解析与解决方案
  • 紧急预警:Perplexity即将下线课程语义模糊匹配模式!最后48小时掌握高精度查询黄金语法
  • CANN/cannbot-skills模型推理融合算子优化
  • Knot项目实战:从零构建一个完整的iOS抓包工具
  • TinyExpr快速入门:5分钟学会在C语言中解析和计算数学表达式
  • Knot部署指南:真机调试与App Store上架完整流程
  • CANN稀疏FlashAttention反向算子
  • Keil开发环境下的CANopen与DeviceNet协议实现指南
  • CANN/ops-blas Ssyr算子实现
  • NCE外汇:服务体验与平台稳定性的协同提升
  • svelte-preprocess 性能优化最佳实践:提升构建速度的10个技巧
  • CANN社区Sign算子优化设计
  • Spire性能优化技巧:如何高效使用Rational和SafeLong提升Scala数值计算效率
  • Element React终极指南:快速构建企业级React应用UI界面
  • HC-05蓝牙模块连接Arduino/STM32的3.3V/5V电平匹配全攻略,附电路图与代码
  • 如何在Windows11中安装Android应用?WSA工具使用教程
  • CANN AsNumpy排序函数API
  • ops-collections架构深度解析:如何实现NPU上的高性能哈希表
  • 别再被数学劝退!用PyTorch从零实现DDPM扩散模型(附完整代码)
  • 通过环境变量为hermesagent配置taotoken作为自定义模型服务提供方
  • CANN/asc-devkit 设置梯度输出类型
  • CANNBot torch-compile 快速入门
  • 2026河北钢制防火门多少钱一平米?甲乙丙级最新报价
  • CANN混元视频配置说明
  • 数据中心工频UPS哪家好?2026工频不间断电源/核磁用UPS电源生产厂家权威推荐 - 栗子测评
  • CTF中的音频隐写术实战:从‘兔耳’和‘调频收音机’两道Misc题,学会用Python脚本提取隐藏信息
  • HermesAgent工具连接Taotoken自定义模型提供方的完整流程