集成 Prometheus 和 Grafana
设置 Prometheus 抓取 Kafka 和 Spring Boot 应用的指标,然后使用 Grafana 仪表板将其可视化
集成 Prometheus 和 Grafana 是 CoddyKit 上的免费 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 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,prometheusThis exposes metrics at /actuator/prometheus.
management.endpoints.web.exposure.include=health,info,prometheusYour 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 portIntroducing 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_totalfor 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 导师)并解锁 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 导师会在你学习这节课的过程中回答你的问题。
学习 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Advanced Spring Boot 4: Event-Driven Architecture (Kafka) 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「集成 Prometheus 和 Grafana」课时需要多长时间?
大多数 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 进行分布式追踪
- 监控消费者滞后并设置告警