使用 Sleuth 与 Zipkin 进行分布式追踪
使用 Spring Cloud Sleuth 和 Zipkin 实现分布式追踪,以了解微服务之间的交互情况。
使用 Sleuth 与 Zipkin 进行分布式追踪 是 CoddyKit 上的免费 Spring Boot 4 Complete Guide 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Boot 4 Complete Guide 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Boot 4 Complete Guide 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Tracing Microservices Flow
In microservices architectures, a single user request might travel through many different services. This makes debugging and understanding performance bottlenecks very challenging.
Distributed tracing helps by tracking the full path of a request as it moves across these services, providing end-to-end visibility.
Spring Cloud Sleuth's Role
Spring Cloud Sleuth is a powerful tool that integrates distributed tracing capabilities into your Spring Boot applications. It automatically instruments your services to generate and propagate tracing information.
Sleuth works by adding unique identifiers to every request, allowing you to follow its journey across different service boundaries.
Core Concepts: Trace & Span
Sleuth relies on two fundamental concepts:
- Trace: Represents a single, complete request or workflow across all participating services. Think of it as the entire story of a request.
- Span: A single operation within a trace. Each service call, database query, or method execution can be a span. Spans have parent-child relationships, forming a tree structure for the trace.
Every span has a unique ID, and all spans belonging to the same trace share a common trace ID.
Adding Sleuth Dependency
To enable Sleuth in your Spring Boot application, you simply need to add the spring-cloud-starter-sleuth dependency to your project's pom.xml (for Maven) or build.gradle (for Gradle).
This starter pulls in all necessary components for Sleuth to auto-configure itself.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>Basic Sleuth in Action
Once spring-cloud-starter-sleuth is on your classpath, Spring Boot automatically instruments your application. Trace and Span IDs will appear in your logs!
Try running this simple Spring Boot app and observe the console output (though without a full server setup, you won't see actual requests, but the app structure is runnable):
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class MyTracedApplication {
public static void main(String[] args) {
SpringApplication.run(MyTracedApplication.class, args);
}
@GetMapping("/hello")
public String hello() {
// Sleuth would automatically add trace/span IDs to logs here
System.out.println("Processing /hello request...");
return "Hello from Traced Service!";
}
}Visualize Traces with Zipkin
While Sleuth adds tracing information to your application's logs, visualizing these traces across multiple services can be complex.
This is where Zipkin comes in! Zipkin is a distributed tracing system that collects and visualizes the trace data generated by Sleuth. It provides a user interface to search and analyze traces.
Launching Zipkin Server
To use Zipkin, you first need to run a Zipkin server. The easiest way is often with Docker:
docker run -p 9411:9411 openzipkin/zipkin
Once running, you can access the Zipkin UI in your browser at http://localhost:9411. This UI will display all the traces sent by your Sleuth-enabled applications.
Connecting Sleuth to Zipkin
To send trace data from your Sleuth-enabled application to the Zipkin server, you need to add the spring-cloud-starter-zipkin dependency and configure the Zipkin server's URL.
Add this dependency:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-zipkin</artifactId>
</dependency>Configuring Zipkin URL
Then, specify the Zipkin server's base URL in your application.properties:
spring.zipkin.base-url=http://localhost:9411
Now, your Spring Boot app will send its trace information to the running Zipkin server for collection and visualization.
Sleuth & Zipkin Check
Let's check your understanding of Spring Cloud Sleuth and Zipkin.
Lesson Summary
In this lesson, you learned about distributed tracing and how Spring Cloud Sleuth and Zipkin provide powerful tools for observability in microservices.
- Sleuth instruments your applications, generating unique trace and span IDs.
- Zipkin collects and visualizes this data, offering a clear view of request flows.
Understanding these tools is crucial for debugging, monitoring, and optimizing complex distributed systems.
常见问题解答
「使用 Sleuth 与 Zipkin 进行分布式追踪」课时是免费的吗?
是的 — 「使用 Sleuth 与 Zipkin 进行分布式追踪」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Boot 4 Complete Guide 课程的其余内容,请升级到 CoddyKit PRO。 Spring Boot 4 Complete Guide 课程共包含 4 节课。
「使用 Sleuth 与 Zipkin 进行分布式追踪」这节课中我会学到什么?
使用 Spring Cloud Sleuth 和 Zipkin 实现分布式追踪,以了解微服务之间的交互情况。 你通过在浏览器中直接运行的动手代码来练习 Spring Boot 4 Complete Guide,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Spring Boot 4 Complete Guide 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Spring Boot 4 Complete Guide 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用 Sleuth 与 Zipkin 进行分布式追踪」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Spring Boot 4 Complete Guide 课中编写并运行代码吗?
能。每节 Spring Boot 4 Complete Guide 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- Kubernetes 编排基础
- 使用 Sleuth 与 Zipkin 进行分布式追踪
- 健康检查与弹性模式
- 使用 Micrometer 和 Prometheus 监控指标与仪表板