Microservices Communication Patterns (Saga, Circuit Breaker) · บทเรียน

เมตริกและการตรวจสอบสถานะ

ใช้เมตริกและการตรวจสอบสถานะเพื่อเฝ้าติดตามประสิทธิภาพและความพร้อมใช้งานแบบเรียลไทม์ของไมโครเซอร์วิสและรูปแบบต่าง ๆ

บทเรียน 3 จาก 411 ขั้นตอน

เมตริกและการตรวจสอบสถานะ เป็นบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Microservices Communication Patterns (Saga, Circuit Breaker) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Welcome to Monitoring

In distributed systems, knowing what's happening is vital. That's where metrics and health checks come in!

They help us understand how our services are performing and if they're even alive. This lesson will show you how to use them effectively.

Understanding Metrics

Metrics are numerical measurements collected over time. Think of them as vital signs for your applications.

  • Counters: Count events (e.g., requests received).
  • Gauges: Show a current value (e.g., current CPU usage).
  • Histograms/Timers: Measure distributions and durations (e.g., request latency).

How Metrics are Collected

Metrics need to be gathered from your services. This often involves libraries or frameworks that expose data in a standard format.

Tools like Prometheus or Micrometer (for Java) help instrument your code to collect and expose these metrics for monitoring systems.

Measuring Request Count

Let's see a simple example of a counter. This Java code simulates incrementing a request counter each time a "request" is processed. In a real app, this would be part of a web framework.

public class Main {
  static int requestCounter = 0;

  public static void processRequest() {
    requestCounter++;
    System.out.println("Requests processed: " + requestCounter);
  }

  public static void main(String[] args) {
    processRequest();
    processRequest();
    processRequest();
  }
}

Measuring Request Latency

Measuring how long an operation takes is crucial. This example uses a basic timer to show the duration of a simulated task. Real-world solutions use more sophisticated timer metrics.

public class Main {
  public static void simulateWork() throws InterruptedException {
    long startTime = System.nanoTime();
    Thread.sleep(100); // Simulate work
    long endTime = System.nanoTime();
    long durationMs = (endTime - startTime) / 1_000_000;
    System.out.println("Work took: " + durationMs + " ms");
  }

  public static void main(String[] args) throws InterruptedException {
    simulateWork();
    simulateWork();
  }
}

Dashboards for Metrics

Raw metrics aren't very useful on their own. They need to be visualized!

Tools like Grafana allow you to create powerful dashboards. These dashboards transform raw metric data into graphs and charts, making it easy to spot trends, anomalies, and performance issues at a glance.

Understanding Health Checks

While metrics tell you about performance, health checks tell you if a service is operational.

They are simple endpoints (e.g., /health) that a monitoring system or orchestrator (like Kubernetes) can call to determine if your application is ready to serve traffic or if it needs to be restarted.

Implementing a Basic Health Check

A basic health check might just return an "OK" status if the application process is running. In a real microservice, this would be an HTTP endpoint.

public class Main {
  public static boolean isServiceHealthy() {
    // In a real app, this might check database connection,
    // external service reachability, etc.
    return true; // For now, always healthy
  }

  public static void main(String[] args) {
    if (isServiceHealthy()) {
      System.out.println("Service is HEALTHY");
    } else {
      System.out.println("Service is UNHEALTHY");
    }
  }
}

Liveness vs. Readiness Checks

There are two main types of health checks:

  • Liveness Check: Determines if your application is still running and able to make progress. If it fails, the application might be restarted.
  • Readiness Check: Determines if your application is ready to accept traffic. If it fails, traffic won't be routed to it (e.g., during startup or while recovering).

Quick Check on Monitoring

You've learned about metrics and health checks. Let's test your understanding!

Recap: Metrics & Health Checks

Congratulations! You've learned how metrics provide deep insights into your service's performance, using counters, gauges, and timers.

You also discovered how health checks (liveness and readiness) are crucial for ensuring your services are operational and available. Together, they form the foundation of robust observability!

เริ่มต้นได้ฟรี

เรียนรู้ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “เมตริกและการตรวจสอบสถานะ” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “เมตริกและการตรวจสอบสถานะ” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Microservices Communication Patterns (Saga, Circuit Breaker) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “เมตริกและการตรวจสอบสถานะ”

ใช้เมตริกและการตรวจสอบสถานะเพื่อเฝ้าติดตามประสิทธิภาพและความพร้อมใช้งานแบบเรียลไทม์ของไมโครเซอร์วิสและรูปแบบต่าง ๆ คุณปฏิบัติ Microservices Communication Patterns (Saga, Circuit Breaker) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Microservices Communication Patterns (Saga, Circuit Breaker) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Microservices Communication Patterns (Saga, Circuit Breaker) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “เมตริกและการตรวจสอบสถานะ” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) นี้ได้ไหม

ได้ บทเรียน Microservices Communication Patterns (Saga, Circuit Breaker) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. แนวคิดการติดตามแบบกระจาย
  2. กลยุทธ์การบันทึกข้อมูลแบบรวมศูนย์
  3. เมตริกและการตรวจสอบสถานะ
  4. การแจ้งเตือนและ SLO
← กลับไปที่ Microservices Communication Patterns (Saga, Circuit Breaker)