0Pricing
Advanced Spring Boot 4: Event-Driven Architecture (Kafka) · บทเรียน

การผสานรวมกับ Prometheus และ Grafana

ตั้งค่า Prometheus ให้ดึงเมตริกจาก Kafka และแอป Spring Boot จากนั้นแสดงผลด้วยแดชบอร์ด Grafana

การผสานรวมกับ Prometheus และ Grafana เป็นบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน

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

Why Monitor with P&G?

In modern systems, understanding what your applications and infrastructure are doing is critical. This is where monitoring comes in!

We'll explore how Prometheus and Grafana form a powerful duo for observing your Spring Boot Kafka applications and the Kafka cluster itself.

Introducing Prometheus

Prometheus is an open-source monitoring system that collects metrics from configured targets at given intervals, evaluates rule expressions, displays the results, and can trigger alerts.

  • It uses a pull model: Prometheus scrapes (pulls) metrics from your services.
  • It stores data as time-series data, making it excellent for tracking changes over time.

Spring Boot & Micrometer

Spring Boot applications can expose metrics using Actuator, Spring's production-ready features. Micrometer is an instrumentation library that Actuator uses to provide a vendor-neutral metrics facade.

This allows you to export metrics to various monitoring systems, including Prometheus, with minimal code changes.

Enabling Prometheus Endpoint

To make your Spring Boot app's metrics available for Prometheus, you need to add dependencies and configure Actuator.

First, add spring-boot-starter-actuator and micrometer-registry-prometheus to your pom.xml.

Then, enable the Prometheus endpoint in application.properties:

management.endpoints.web.exposure.include=health,info,prometheus

This exposes metrics at /actuator/prometheus.

management.endpoints.web.exposure.include=health,info,prometheus

Your First Metric

This minimal Spring Boot application demonstrates how to expose a custom counter. When you hit the /hello endpoint, the app.hello.requests counter increments, which Prometheus can then scrape.

import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.Metrics;
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 MetricsApp {

  private final Counter helloCounter =
      Metrics.counter("app.hello.requests");

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

  @GetMapping("/hello")
  public String hello() {
    helloCounter.increment();
    return "Hello CoddyKit!";
  }
}

Kafka Metrics with Exporter

Kafka brokers don't natively expose metrics in a Prometheus-friendly format. We use a separate component called Kafka Exporter.

Kafka Exporter is a lightweight service that scrapes JMX metrics from Kafka brokers and re-exposes them in a format that Prometheus can easily consume.

Prometheus Scrape Config

Prometheus needs to know where to find your metrics. You configure scrape targets in its prometheus.yml file. Here's an example:

scrape_configs:
  - job_name: 'spring-boot-app'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['localhost:8080']

  - job_name: 'kafka-exporter'
    static_configs:
      - targets: ['localhost:9308'] # Kafka Exporter port

Introducing Grafana

Grafana is an open-source platform for monitoring and observability. It allows you to query, visualize, alert on, and understand your metrics no matter where they are stored.

Think of it as the display panel for all the data Prometheus collects. It uses dashboards with various panels to present metrics in an easy-to-understand way.

Grafana Data Sources

Before visualizing, Grafana needs to connect to a data source. Here, Prometheus will be our data source.

In Grafana's UI, navigate to "Connections" -> "Data sources" -> "Add new data source". Select "Prometheus" and configure its URL (e.g., http://localhost:9090, Prometheus' default port).

Creating a Dashboard

With Prometheus connected, you can start building dashboards.

  • Create a new dashboard and add a panel.
  • Select Prometheus as the data source.
  • Write a PromQL query (Prometheus Query Language) to select your metric, e.g., app_hello_requests_total for our Spring Boot app.
  • Choose a visualization type like "Graph" or "Stat".

This brings your metrics to life!

Prometheus & Grafana Check

You've learned how Prometheus scrapes metrics and Grafana visualizes them. What is the primary role of Kafka Exporter in this monitoring setup?

Recap: P&G Power

Great job! You've learned how to integrate Prometheus and Grafana into your monitoring strategy for Spring Boot Kafka applications and Kafka itself.

  • Prometheus scrapes metrics from endpoints.
  • Micrometer/Actuator expose Spring Boot metrics.
  • Kafka Exporter makes Kafka broker metrics available.
  • Grafana visualizes these collected metrics.

This powerful combination provides deep insights into your system's health and performance!

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

บทเรียน “การผสานรวมกับ Prometheus และ Grafana” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การผสานรวมกับ Prometheus และ Grafana” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Advanced Spring Boot 4: Event-Driven Architecture (Kafka) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การผสานรวมกับ Prometheus และ Grafana”

ตั้งค่า Prometheus ให้ดึงเมตริกจาก Kafka และแอป Spring Boot จากนั้นแสดงผลด้วยแดชบอร์ด Grafana คุณปฏิบัติ 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 ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การผสานรวมกับ Prometheus และ Grafana” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) นี้ได้ไหม

ได้ บทเรียน Advanced Spring Boot 4: Event-Driven Architecture (Kafka) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. เมตริก Kafka (JMX) และการตรวจสอบสุขภาพ
  2. การผสานรวมกับ Prometheus และ Grafana
  3. การติดตามแบบกระจายด้วย Sleuth/Zipkin
  4. การเฝ้าติดตามความล่าช้าของผู้บริโภคและการตั้งการแจ้งเตือน
← กลับไปที่ Advanced Spring Boot 4: Event-Driven Architecture (Kafka)