如何配置 Prometheus 告警?
解读
在国内一线互联网公司与大型国企的 Spring Cloud 体系中,Prometheus 已取代 Zabbix 成为云原生监控事实标准。面试官问“如何配置 Prometheus 告警”,并不是让你背诵 yaml 语法,而是考察你是否能把Spring Boot 指标暴露 → Prometheus 采集 → Alertmanager 路由 → 企业微信/钉钉/飞书 通知这条全链路闭环讲清楚,并体现你对SRE 可观测性体系的理解。回答时务必结合国内网络隔离、多环境、多集群、灰度发布等真实痛点,突出“能落地、能值班、能止损”。
知识点
- Spring Boot Actuator 指标暴露:开启
/actuator/prometheus端点,配置management.metrics.export.prometheus.descriptions=true,并关闭敏感指标防止泄露。 - Prometheus 抓取配置:使用
kubernetes_sd_configs做服务发现(国内集群多为阿里云 ACK、腾讯云 TKE、华为云 CCE),配合relabel_configs做环境隔离(label:env=gray/prod)。 - Recording Rule 与 Alerting Rule 分离:把高频计算提前为 Recording Rule,降低查询延迟;Alerting Rule 遵循**“5 分钟收敛”原则,避免抖动告警**轰炸。
- Alertmanager 路由树:按业务线 + 环境 + 级别三层路由,国内常用钉钉群机器人(加签密钥)、企业微信群机器人(IP 白名单)、飞书卡片消息(支持交互按钮),并做值班组 OnCall 轮换。
- 告警质量治理:使用
inhibit_rules抑制级联告警;设置group_wait/group_interval/repeat_interval实现告警合并;通过告警认领与SLO 燃尽图持续降低误报率。 - 高可用与合规:Prometheus 采用Thanos 或 VictoriaMetrics 集群版做多副本+长期存储;敏感金融系统需国密 TLS 双向认证与日志审计满足等保 3 级。
答案
以Spring Boot 2.7 + Prometheus 2.45 + Alertmanager 0.26 为例,给出可直接落地的最小闭环步骤:
-
Spring Boot 侧暴露指标
引入依赖implementation 'io.micrometer:micrometer-registry-prometheus'配置
application-prod.ymlmanagement: endpoints: web: exposure: include: health,info,prometheus metrics: tags: application: ${spring.application.name} env: prod禁止暴露 env、pod_ip 等敏感 tag,防止蓝绿灰度标签泄露。
-
Prometheus 侧抓取与规则
在 ACK 集群内使用ServiceMonitor(Prometheus Operator 模式):apiVersion: monitoring.coreos.com/v1 kind: ServiceMonitor metadata: name: order-service-monitor namespace: middleware spec: selector: matchLabels: app: order-service endpoints: - port: web path: /actuator/prometheus interval: 15s relabelings: - sourceLabels: [__meta_kubernetes_pod_label_env] targetLabel: env在
prometheus-rules.yml定义黄金指标告警:groups: - name: spring-golden-signals interval: 15s rules: - alert: HighErrorRate expr: | rate(http_server_requests_seconds_count{status=~"5.."}[2m]) / rate(http_server_requests_seconds_count[2m]) > 0.05 for: 3m labels: severity: critical team: order env: prod annotations: summary: "订单服务 5xx 率超过 5%" description: "实例 {{ $labels.instance }} 在过去 2 分钟 5xx 占比 {{ $value | humanizePercentage }}" runbook: "https://wiki.company.com/runbook/high-5xx"注意:
for: 3m避免瞬时抖动;humanizePercentage让值班一眼看懂。 -
Alertmanager 侧路由与通知
配置alertmanager.yml(密钥走 Kubernetes Secret):global: resolve_timeout: 5m route: group_by: [alertname, team, env] group_wait: 30s group_interval: 5m repeat_interval: 4h receiver: 'default' routes: - match: team: order severity: critical receiver: 'order-critical' continue: true receivers: - name: 'order-critical' webhook_configs: - url: 'http://dingtalk-webhook.middleware.svc.cluster.local:8080/dingtalk/order-critical/send' send_resolved: true http_config: headers: Authorization: 'Bearer ${DINGTALK_SIGN}'钉钉 webhook 服务内部做加签验证与**@值班人**,支持告警认领与静默按钮。
-
端到端验证
使用curl -X POST http://order-service/actuator/fail制造 5xx,3 分钟内收到钉钉卡片,点击**“查看 Grafana”跳转至预聚合大盘**,确认错误码分布与TraceID,完成MTTR<15 分钟的闭环。
拓展思考
- 多集群联邦:国内大型电商采用**“华东 2 + 华北 3 + 华南 1”三地域,使用Thanos Query 联邦聚合,Ruler 下沉到边缘集群,中心只存全局 SLO 告警**,避免跨地域网络抖动导致误告。
- 告警风暴治理:引入AI 异常检测(如阿里云 ARMS 智能告警)与自适应阈值,结合变更事件(Kube-events + GitOps)做根因推荐,把P0 告警量从日均 200 条降到 10 条以内。
- 合规与审计:金融场景下,Prometheus 远程写入到国密版 VictoriaMetrics,所有 Alertmanager 通知走内部短信网关并留痕 7 年,满足央行 281 号文与等保 2.0 要求。