การติดตามแบบกระจายด้วย Sleuth/Zipkin
นำการติดตามแบบกระจายมาใช้ด้วย Spring Cloud Sleuth และ Zipkin เพื่อติดตามการไหลของเหตุการณ์ผ่านไมโครเซอร์วิสหลายตัว
การติดตามแบบกระจายด้วย Sleuth/Zipkin เป็นบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Why Trace Distributed Systems?
In a microservices architecture, a single user request often travels through many different services. This distributed nature makes it incredibly hard to track the flow of requests and pinpoint where issues occur.
Distributed tracing helps you visualize the full journey of a request across all services involved, making debugging and performance monitoring much easier.
Trace and Span: Your Navigation Tools
Distributed tracing relies on two core concepts:
- Trace: Represents the complete journey of a request through a system, from start to finish. It's like a story of one operation.
- Span: A single, logical unit of work within a trace. Each operation (e.g., an HTTP request, a database call, sending a Kafka message) gets its own span. Spans have parent-child relationships, showing cause and effect.
Meet Spring Cloud Sleuth
Spring Cloud Sleuth is a powerful library for Spring Boot applications that automatically adds distributed tracing capabilities. It instruments your application to generate, collect, and propagate trace information.
- It automatically adds trace and span IDs to your logs.
- It propagates these IDs across service boundaries (HTTP, messaging, etc.).
This means you don't have to manually manage trace IDs in most cases!
Getting Started: Add Sleuth & Zipkin
To integrate Spring Cloud Sleuth into your Spring Boot project, add the following dependencies to your pom.xml:
spring-cloud-starter-sleuth: The core tracing library.spring-cloud-sleuth-zipkin: Integrates with Zipkin for trace visualization.
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-sleuth</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-sleuth-zipkin</artifactId>
</dependency>Minimal Sleuth Configuration
After adding the dependencies, a minimal configuration in your application.properties is usually enough to get started. Sleuth will automatically detect and configure itself.
To send traces to a Zipkin server, specify its URL:
spring.application.name=my-kafka-producer-app
spring.zipkin.base-url=http://localhost:9411
spring.sleuth.sampler.probability=1.0Tracing Across Kafka Messages
One of Sleuth's key features is its ability to propagate trace context across messaging systems like Kafka. When you use Spring's KafkaTemplate to send messages:
- Sleuth automatically injects trace and span IDs into the Kafka message headers.
- When a
@KafkaListenerreceives the message, Sleuth extracts these headers and continues the trace, linking the producer's span to the consumer's span.
This creates a continuous trace, even across asynchronous Kafka message flows.
Zipkin: See Your Traces
While Sleuth generates and propagates trace data, Zipkin is the distributed tracing system that collects, stores, and visualizes this data. It provides a user interface where you can:
- Search for traces by service name, timestamp, or trace ID.
- View a Gantt chart representation of a trace, showing the sequence and duration of spans.
- Identify performance bottlenecks and errors across your microservices.
Spin Up Zipkin Locally
For local development and testing, you can easily run a Zipkin server using Docker. This provides a quick way to see your traces without complex setup.
docker run -d -p 9411:9411 openzipkin/zipkinProducer with Sleuth Instrumentation
Here's a simple Spring Boot Kafka producer. With Sleuth and Zipkin configured (as shown in earlier scenes), when you run this, Sleuth will automatically add tracing headers to the Kafka message. You would see the traceId and spanId in your application logs and in the Zipkin UI.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.kafka.core.KafkaTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
public class SleuthKafkaProducerApplication {
@Autowired
private KafkaTemplate<String, String> kafkaTemplate;
private static final String TOPIC = "my-traceable-topic";
public static void main(String[] args) {
SpringApplication.run(SleuthKafkaProducerApplication.class, args);
}
@GetMapping("/send")
public String sendMessage(@RequestParam("message") String message) {
kafkaTemplate.send(TOPIC, message);
return "Message sent with trace context: " + message;
}
}Quick Check: Tracing Concepts
When a Spring Boot application with Spring Cloud Sleuth sends a message to Kafka, which of the following statements are true about trace information?
Lesson Summary: Tracing Your Flow
You've learned about distributed tracing, a critical technique for understanding complex microservice interactions. We covered:
- The concepts of Trace (full request journey) and Span (individual operation).
- How Spring Cloud Sleuth automatically instruments Spring Boot applications, including Kafka producers and consumers, to propagate tracing context.
- The role of Zipkin in collecting and visualizing these traces, providing invaluable insights into your system's behavior.
With these tools, you can effectively monitor and debug your event-driven microservices!
คำถามที่พบบ่อย
บทเรียน “การติดตามแบบกระจายด้วย Sleuth/Zipkin” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การติดตามแบบกระจายด้วย Sleuth/Zipkin” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การติดตามแบบกระจายด้วย Sleuth/Zipkin”
นำการติดตามแบบกระจายมาใช้ด้วย Spring Cloud Sleuth และ Zipkin เพื่อติดตามการไหลของเหตุการณ์ผ่านไมโครเซอร์วิสหลายตัว คุณปฏิบัติ Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน
บทเรียน “การติดตามแบบกระจายด้วย Sleuth/Zipkin” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) นี้ได้ไหม
ได้ บทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- เมตริก Kafka (JMX) และการตรวจสอบสุขภาพ
- การผสานรวมกับ Prometheus และ Grafana
- การติดตามแบบกระจายด้วย Sleuth/Zipkin
- การเฝ้าติดตามความล่าช้าของผู้บริโภคและการตั้งการแจ้งเตือน