使用 Sleuth/Zipkin 进行分布式追踪
集成 Spring Cloud Sleuth 和 Zipkin 进行分布式追踪,了解请求在各个微服务之间的流转情况。
使用 Sleuth/Zipkin 进行分布式追踪 是 CoddyKit 上的免费 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
What is Distributed Tracing?
In microservices, a single user request often travels through many different services. This makes it hard to follow the request's journey!
Distributed tracing is a technique that helps you track the path of a request as it moves across various services in your system.
Why Trace Microservices?
Without tracing, debugging issues in microservices is like finding a needle in a haystack. You don't know which service failed or where the delay occurred.
- Debugging: Pinpoint errors faster.
- Performance: Identify bottlenecks in the request flow.
- Visibility: Understand how services interact.
Sleuth: The Tracing Enabler
Spring Cloud Sleuth integrates with Spring applications to automatically add tracing information to requests and logs.
It injects unique identifiers into HTTP headers and logging contexts, allowing you to link related operations across services.
Trace & Span IDs Explained
Sleuth uses two main IDs:
- Trace ID: A unique ID for the entire request, from start to finish, across all services it touches.
- Span ID: A unique ID for a single operation or unit of work within a service (e.g., an HTTP request or a method call). Spans form a tree structure within a trace.
Add Sleuth Dependency
To enable Sleuth in your Spring Boot application, add the spring-cloud-starter-sleuth dependency to your pom.xml (Maven) or build.gradle (Gradle).
For Maven:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>For Gradle:
implementation 'org.springframework.cloud:spring-cloud-starter-sleuth'Zipkin: The Tracing UI
While Sleuth adds tracing info, you need a way to collect and visualize it. That's where Zipkin comes in!
Zipkin is a distributed tracing system that collects and stores trace data, then provides a web UI to search and view these traces.
Run Zipkin Server
The easiest way to get Zipkin running locally is using Docker. This command pulls the Zipkin image and starts it on port 9411.
docker run -d -p 9411:9411 openzipkin/zipkinYou can then access the Zipkin UI at http://localhost:9411 in your browser.
Configure Sleuth for Zipkin
Once Zipkin is running, tell your Sleuth-enabled Spring Boot application where to send its trace data. Add this to your application.properties or application.yml.
application.properties:
spring.zipkin.base-url=http://localhost:9411Now, your application will send trace data to the local Zipkin server.
Tracing a Service Call
Let's see how a simple Spring Boot service logs trace IDs. With Sleuth, your logs will automatically include [appName,traceId,spanId,exportable].
Run this simple app, then make a request to /hello. Check the console logs for the added IDs.
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 TracingApp {
public static void main(String[] args) {
SpringApplication.run(TracingApp.class, args);
}
@GetMapping("/hello")
public String hello() {
System.out.println("Hello endpoint called!");
return "Hello from TracingApp!";
}
}Quick Check
Which of the following statements correctly describe the roles of Spring Cloud Sleuth and Zipkin in distributed tracing? (Select all that apply)
Recap & Next Steps
You've learned how distributed tracing helps in microservices, especially with Spring Cloud Sleuth and Zipkin.
- Sleuth: Integrates with Spring apps to inject
traceIdandspanId. - Zipkin: Collects these traces and provides a UI for visualization and analysis.
These tools are crucial for understanding and debugging complex distributed systems.
常见问题解答
「使用 Sleuth/Zipkin 进行分布式追踪」课时是免费的吗?
是的 — 「使用 Sleuth/Zipkin 进行分布式追踪」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程的其余内容,请升级到 CoddyKit PRO。 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程共包含 4 节课。
「使用 Sleuth/Zipkin 进行分布式追踪」这节课中我会学到什么?
集成 Spring Cloud Sleuth 和 Zipkin 进行分布式追踪,了解请求在各个微服务之间的流转情况。 你通过在浏览器中直接运行的动手代码来练习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway),全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用 Sleuth/Zipkin 进行分布式追踪」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课中编写并运行代码吗?
能。每节 API Gateway & Reverse Proxy (Nginx + Spring Cloud Gateway) 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- JWT 身份验证与授权
- 使用 Sleuth/Zipkin 进行分布式追踪
- 使用配置服务器集中管理配置
- 使用 Actuator 与 Prometheus 暴露指标