Spring Boot 4 Microservices & REST APIs · 课时

使用 Zipkin 进行分布式追踪

集成 Zipkin,跨微服务实现端到端分布式追踪,以调试复杂的交互过程。

第 3 / 3 课12 个步骤

使用 Zipkin 进行分布式追踪 是 CoddyKit 上的免费 Spring Boot 4 Microservices & REST APIs 课时。 这是第 3 节课,共 3 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Boot 4 Microservices & REST APIs 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Boot 4 Microservices & REST APIs 课程共包含 3 节课。

本课时的部分内容尚未翻译,以英文显示。

The Microservice Debugging Maze

In a microservices architecture, a single user request might travel through many different services. When something goes wrong, it's like finding a needle in a haystack!

Traditional logging only shows what happened within one service. We need a way to see the entire journey.

What is Distributed Tracing?

Distributed tracing is a technique to monitor and observe requests as they flow through different components of a distributed system.

  • It helps you understand how services interact.
  • It pinpoints performance bottlenecks.
  • It makes debugging across service boundaries much easier.

Introducing Zipkin

Zipkin is an open-source distributed tracing system.

It helps you gather timing data needed to troubleshoot latency problems in microservice architectures. It manages both the collection and lookup of this data.

Core Concepts: Trace & Span

Two fundamental concepts in distributed tracing are Traces and Spans:

  • A Trace represents a single request or workflow as it flows through your entire system.
  • A Span is a single operation within a trace. It has a name, a unique ID, and a parent ID (unless it's the root span).

Think of a Trace as a story, and Spans as the individual chapters.

How Zipkin Works

Zipkin works by collecting data from your instrumented services.

Here's the basic flow:

  1. Your services send tracing data (spans) to the Zipkin Collector.
  2. The Collector stores this data (e.g., in memory, MySQL, Elasticsearch).
  3. You use the Zipkin UI to visualize and analyze the traces.

Spring Cloud Sleuth Magic

For Spring Boot applications, Spring Cloud Sleuth makes integration with Zipkin incredibly easy.

Sleuth automatically adds tracing information to your logs and HTTP headers, and sends it to Zipkin without much manual coding from you.

Setting Up Zipkin Locally

The easiest way to run a Zipkin server locally is using Docker. If you have Docker installed, just run this command in your terminal:

docker run -p 9411:9411 openzipkin/zipkin

Once running, you can access the Zipkin UI at http://localhost:9411.

Simple Service Instrumentation

To enable tracing, add Sleuth and Zipkin dependencies (spring-cloud-starter-sleuth and spring-cloud-sleuth-zipkin). Then, configure your application.properties to point to the Zipkin server:

spring.application.name=my-traced-service
spring.zipkin.base-url=http://localhost:9411
spring.sleuth.sampler.probability=1.0 (trace all requests)

Now, let's look at a simple service that sends traces!

Tracing an Endpoint Call

Here's a basic Spring Boot service. With the properties from the last scene and Sleuth dependencies, it will automatically send trace data to Zipkin when you hit its /hello endpoint.

Try running this and then visiting http://localhost:8080/hello. Check your Zipkin UI!

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 MyTracedServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(MyTracedServiceApplication.class, args);
    }

    @GetMapping("/hello")
    public String hello() {
        // Sleuth automatically adds tracing to this request
        System.out.println("Processing /hello request...");
        return "Hello from Traced Service!";
    }
}

Exploring the Zipkin UI

After making requests to your traced service, open http://localhost:9411 in your browser.

You can search for traces by service name or trace ID. The UI will show you a visual timeline of each trace, including its spans, duration, and dependencies between services.

Quick Check: Tracing Terms

Test your understanding of distributed tracing with Zipkin.

Recap: Debugging with Zipkin

Great job! You've learned how distributed tracing helps debug microservices, and how Zipkin and Spring Cloud Sleuth make it easy.

  • Distributed tracing visualizes request flow.
  • Zipkin collects and displays trace data.
  • Sleuth automates instrumentation for Spring Boot.

This helps you quickly find issues and optimize performance across your services.

免费开始

用 AI 导师学习 Java — 免费

在浏览器中编写并运行真实代码,获得全天候 AI 导师的即时帮助,并在网页或应用中继续学习。

课程
24
课程
93

常见问题解答

「使用 Zipkin 进行分布式追踪」课时是免费的吗?

是的 — 「使用 Zipkin 进行分布式追踪」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Boot 4 Microservices & REST APIs 课程的其余内容,请升级到 CoddyKit PRO。 Spring Boot 4 Microservices & REST APIs 课程共包含 3 节课。

「使用 Zipkin 进行分布式追踪」这节课中我会学到什么?

集成 Zipkin,跨微服务实现端到端分布式追踪,以调试复杂的交互过程。 你通过在浏览器中直接运行的动手代码来练习 Spring Boot 4 Microservices & REST APIs,全天候 AI 导师会在你学习这节课的过程中回答你的问题。

学习 Spring Boot 4 Microservices & REST APIs 需要有经验吗?

无需任何先前经验。CoddyKit 上的 Spring Boot 4 Microservices & REST APIs 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 3 节。

「使用 Zipkin 进行分布式追踪」课时需要多长时间?

大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。

我能在这节 Spring Boot 4 Microservices & REST APIs 课中编写并运行代码吗?

能。每节 Spring Boot 4 Microservices & REST APIs 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。

此课程中的所有课时

  1. 使用 Resilience4j 实现熔断器
  2. 实现故障回退与超时
  3. 使用 Zipkin 进行分布式追踪
← 返回 Spring Boot 4 Microservices & REST APIs