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-sleuthSpring Cloud Sleuth 是 Spring Cloud 生态中的分布式追踪解决方案它能够帮助开发者追踪请求和消息在分布式系统中的流转从而快速定位问题和优化性能。然而随着技术的发展Spring Cloud Sleuth 的核心功能已迁移至 Micrometer Tracing 项目 instrumentations 也将逐步迁移至 Micrometer 及其他相关项目。本文将为你提供一份详细的迁移指南助你顺利从 Spring Cloud Sleuth 3.1 过渡到 Micrometer Tracing。为什么要迁移到 Micrometer TracingSpring Cloud Sleuth 的核心已被迁移到 Micrometer Tracing 项目这意味着未来的开发和维护将主要集中在 Micrometer Tracing 上。迁移到 Micrometer Tracing 可以让你享受到更活跃的社区支持、更丰富的功能以及与其他 Micrometer 组件更好的集成。迁移前的准备工作在开始迁移之前请确保你的项目满足以下条件使用 Spring Boot 2.6.x 或更高版本。移除项目中对 Spring Cloud Sleuth 的直接依赖。准备好了解 Micrometer Tracing 的基本概念和术语。核心依赖替换移除 Spring Cloud Sleuth 依赖首先需要从你的pom.xml文件中移除 Spring Cloud Sleuth 的相关依赖。例如!-- 移除 Spring Cloud Sleuth 依赖 -- dependency groupIdorg.springframework.cloud/groupId artifactIdspring-cloud-starter-sleuth/artifactId /dependency添加 Micrometer Tracing 依赖然后添加 Micrometer Tracing 的核心依赖。Micrometer Tracing 提供了对多种追踪系统的支持你可以根据自己的需求选择合适的依赖。例如如果你使用 Zipkin 作为追踪系统可以添加以下依赖!-- 添加 Micrometer Tracing 核心依赖 -- dependency groupIdio.micrometer/groupId artifactIdmicrometer-tracing/artifactId /dependency !-- 添加 Zipkin 导出器依赖 -- dependency groupIdio.micrometer/groupId artifactIdmicrometer-tracing-bridge-brave/artifactId /dependency dependency groupIdio.zipkin.reporter2/groupId artifactIdzipkin-reporter-brave/artifactId /dependencyAPI 变更与替换核心类和接口的变更Spring Cloud Sleuth 中的一些核心类和接口在 Micrometer Tracing 中发生了变化。以下是一些常见的变更Spring Cloud Sleuth 3.1Micrometer Tracingorg.springframework.cloud.sleuth.Tracerio.micrometer.tracing.Tracerorg.springframework.cloud.sleuth.Spanio.micrometer.tracing.Spanorg.springframework.cloud.sleuth.annotation.NewSpanio.micrometer.tracing.annotation.NewSpanorg.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 groupIdio.micrometer/groupId artifactIdmicrometer-tracing-annotation/artifactId /dependencyimport io.micrometer.tracing.annotation.EnableTracing; Configuration EnableTracing public class TracingConfig { }问题日志中没有追踪信息解决方案检查你的日志配置确保日志格式中包含了追踪相关的变量。例如在 Logback 中你可以这样配置appender nameCONSOLE classch.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迁移后的验证迁移完成后你可以通过以下方式验证迁移是否成功启动应用发送请求检查 Zipkin 服务器是否收到追踪数据。查看应用日志确认日志中包含 traceId 和 spanId。测试带有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),仅供参考