Prometheus + Grafana 监控搭建:从入门到生产可用
Prometheus + Grafana Monitoring Setup: From Basics to Production Ready
| David | 2026-08-29T13:54:44
花了两天时间给我们的微服务搭了一套完整的监控体系。这篇文章从零开始讲解搭建过程,包括自定义指标和告警规则配置。
Built a complete monitoring system for our microservices in two days. Step-by-step guide including custom metrics and alert rules.
之前我们的服务出了问题都是靠用户反馈或者看日志发现的,非常被动。花了两天时间搭了 Prometheus + Grafana 的监控体系,终于能主动发现问题了。 架构概览 整体架构很简单: 各服务暴露 /metrics 端点(Spring Boot Actuator 自带) Prometheus 定时抓取指标数据 Grafana 从 Prometheus 查询数据并可视化 AlertManager 接收 Prometheus 的告警并推送到钉钉 Spring Boot 集成 Spring Boot 集成 Prometheus 非常简单: <!-- pom.xml --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> # application.yml management: endpoints: web: exposure: include: prometheus,health,info metrics: tags: application: ${spring.application.name} 自定义业务指标 除了 JVM、HTTP 这些默认指标,我们还加了业务指标: @Component public class OrderMetrics { private final Counter orderCounter; private final Timer orderProcessTimer; public OrderMetrics(MeterRegistry registry) { this.orderCounter = Counter.builder("order.created.total") .description("Total orders created") .tag("channel", "unknown") .register(registry); this.orderProcessTimer = Timer.builder("order.process.duration") .description("Order processing time") .register(registry); } public void recordOrder(String channel) { orderCounter.increment(); } public Timer.Sample startTimer() { return Timer.start(); } } 告警规则 配了几个核心告警规则: groups: - name: service-alerts rules: - alert: HighErrorRate expr: rate(http_server_requests_seconds_count{status=~"5.."}[5m]) / rate(http_server_requests_seconds_count[5m]) > 0.05 for: 5m labels: severity: critical annotations: summary: "High error rate on {{ $labels.instance }}" - alert: HighLatency expr: histogram_quantile(0.95, rate(http_server_requests_seconds_bucket[5m])) > 2 for: 5m labels: severity: warning 效果 上线第一周就通过监控发现了两个问题:一个是数据库连接池快用完了(连接数持续增长),另一个是某个接口的 P95 延迟在特定时间段飙升(跟定时任务冲突了)。这要是没监控,不知道要等用户反馈多久才能发现。
Built a Prometheus + Grafana monitoring system in two days to replace our reactive problem discovery approach. Architecture Services expose /metrics (Spring Boot Actuator) → Prometheus scrapes → Grafana visualizes → AlertManager pushes to DingTalk. Key Setup Spring Boot integration with micrometer-registry-prometheus, custom business metrics (order counters, processing timers), and alert rules for error rate and latency. Impact Found two issues in the first week: a database connection pool leak and a periodic latency spike from a cron job conflict.