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 组件更好的集成。
迁移前的准备工作
在开始迁移之前,请确保你的项目满足以下条件:
- 使用 Spring Boot 2.6.x 或更高版本。
- 移除项目中对 Spring Cloud Sleuth 的直接依赖。
- 准备好了解 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.1 | Micrometer Tracing |
|---|---|
org.springframework.cloud.sleuth.Tracer | io.micrometer.tracing.Tracer |
org.springframework.cloud.sleuth.Span | io.micrometer.tracing.Span |
org.springframework.cloud.sleuth.annotation.NewSpan | io.micrometer.tracing.annotation.NewSpan |
org.springframework.cloud.sleuth.annotation.ContinueSpan | io.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>迁移后的验证
迁移完成后,你可以通过以下方式验证迁移是否成功:
- 启动应用,发送请求,检查 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),仅供参考
