
在微服务和分布式架构成为主流的今天,系统的复杂度呈指数级增长。线上服务突然卡顿、接口响应超时、服务器资源耗尽……这些问题如果不能及时发现和定位,小则影响用户体验,大则导致业务瘫痪。而传统的日志排查、人工巡检方式,早已跟不上分布式系统的节奏——你需要的是一套实时、可视化、可告警的监控体系,这正是Prometheus+Grafana组合的核心价值所在。
Prometheus(普罗米修斯)是由SoundCloud开源的时序数据库监控系统,天生为云原生环境设计;Grafana则是一款开源的可视化面板工具,能将Prometheus收集的时序数据转化为直观的图表。二者结合,就像给你的系统装上了"智能仪表盘"和"预警雷达",既能实时看到系统的运行状态,又能在问题发生前发出警报。
本文将从底层原理到实战落地,手把手教你搭建、配置、优化Prometheus+Grafana监控体系,所有示例均经过实际验证,可直接落地生产环境。
在动手搭建前,先搞懂Prometheus的核心概念,避免只会"照抄配置"。
Prometheus存储的核心是时序数据——带时间戳的键值对,格式为:指标名{标签名=标签值,...} 数值 时间戳。 例如:http_requests_total{method="GET",path="/api/user"} 12589 1710000000,表示在1710000000这个时间点,GET /api/user接口的请求总数是12589。
Prometheus定义了4种核心指标类型,这是监控的"基础积木":
http_requests_total(总请求数)。node_memory_usage_percent(内存使用率)。http_request_duration_seconds_bucket(请求延迟分桶统计)。http_request_duration_seconds_summary。Prometheus采用"拉模式"采集数据:Prometheus Server主动向被监控目标(Exporter)发送HTTP请求,获取指标数据(默认端口9090)。这种模式的优势是:
PromQL是Prometheus的查询语言,能对时序数据进行多维度的筛选、聚合、计算,是实现监控告警的核心。例如:
http_requests_total{method="GET"}rate(http_requests_total[5m])histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[5m])) by (le))
Prometheus的核心架构包含以下组件:
本文以Linux(CentOS 7/8)为例,所有操作均为root用户执行,确保网络通畅,关闭防火墙或开放对应端口:
yum install -y wget tar vim
# 下载最新稳定版(可替换为最新版本号)
wget https://github.com/prometheus/prometheus/releases/download/v2.53.1/prometheus-2.53.1.linux-amd64.tar.gz
# 解压
tar -zxvf prometheus-2.53.1.linux-amd64.tar.gz
# 移动到/usr/local目录
mv prometheus-2.53.1.linux-amd64 /usr/local/prometheus
# 创建数据目录(TSDB存储位置)
mkdir -p /data/prometheus
编辑/usr/local/prometheus/prometheus.yml,这是Prometheus的核心配置文件:
global:
scrape_interval:15s# 全局采集频率,默认15秒
evaluation_interval:15s# 告警规则评估频率,默认15秒
alerting:
alertmanagers:
-static_configs:
-targets:
# 后续会配置Alertmanager,此处先留空
# - localhost:9093
rule_files:
# 告警规则文件路径,后续配置
# - "alert_rules.yml"
scrape_configs:
# 监控Prometheus自身
-job_name:"prometheus"
static_configs:
-targets:["localhost:9090"]
# 监控Node Exporter(后续安装)
-job_name:"node_exporter"
static_configs:
-targets:["localhost:9100"]
配置文件核心说明:
global:全局配置,定义采集和规则评估频率;alerting:配置Alertmanager地址;rule_files:指定告警规则文件;scrape_configs:定义监控任务(job),每个job对应一组监控目标。为了让Prometheus开机自启,创建systemd服务文件/usr/lib/systemd/system/prometheus.service:
[Unit]
Description=Prometheus Server
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/prometheus/prometheus \
--config.file=/usr/local/prometheus/prometheus.yml \
--storage.tsdb.path=/data/prometheus \
--web.listen-address=0.0.0.0:9090
Restart=on-failure
[Install]
WantedBy=multi-user.target
# 重新加载systemd配置
systemctl daemon-reload
# 启动Prometheus
systemctl start prometheus
# 设置开机自启
systemctl enable prometheus
# 检查状态
systemctl status prometheus
访问http://服务器IP:9090,进入Prometheus Web界面:
prometheus和node_exporter两个job(node_exporter暂未安装,状态为down);prometheus_build_info,点击「Execute」,可看到Prometheus的版本信息,说明自身监控正常。Node Exporter是Prometheus官方提供的Exporter,用于采集Linux/Windows服务器的CPU、内存、磁盘、网络等指标。
wget https://github.com/prometheus/node_exporter/releases/download/v1.8.2/node_exporter-1.8.2.linux-amd64.tar.gz
tar -zxvf node_exporter-1.8.2.linux-amd64.tar.gz
mv node_exporter-1.8.2.linux-amd64 /usr/local/node_exporter
编辑/usr/lib/systemd/system/node_exporter.service:
[Unit]
Description=Node Exporter
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/node_exporter/node_exporter
Restart=on-failure
[Install]
WantedBy=multi-user.target
systemctl daemon-reload
systemctl start node_exporter
systemctl enable node_exporter
systemctl status node_exporter
访问http://服务器IP:9100/metrics,可看到大量服务器指标(如node_cpu_usage、node_memory_MemTotal_bytes)。 回到Prometheus Web界面,刷新Targets,node_exporter状态变为UP,说明采集成功。
Grafana是可视化工具,能将Prometheus的时序数据转化为直观的仪表盘,支持自定义图表、告警、多数据源。
# 添加Grafana源
cat > /etc/yum.repos.d/grafana.repo << EOF
[grafana]
name=grafana
baseurl=https://packages.grafana.com/oss/rpm
repo_gpgcheck=1
enabled=1
gpgcheck=1
gpgkey=https://packages.grafana.com/gpg.key
sslverify=1
sslcacert=/etc/pki/tls/certs/ca-bundle.crt
EOF
# 安装Grafana
yum install -y grafana
# 启动并设置开机自启
systemctl start grafana-server
systemctl enable grafana-server
systemctl status grafana-server
http://服务器IP:3000,默认账号/密码:admin/admin,首次登录需修改密码;http://localhost:9090(Prometheus Server地址);Grafana社区提供了大量现成的仪表盘模板,无需从零制作。Node Exporter的官方模板ID是1860:
PromQL是Prometheus的核心,掌握它才能真正玩转监控。以下是常用的PromQL查询示例,均经过验证可直接使用。
# 筛选所有GET请求的总请求数
http_requests_total{method="GET"}
# 筛选指定路径的POST请求数
http_requests_total{method="POST",path="/api/order"}
# 排除某个标签值(如排除测试环境)
http_requests_total{env!="test"}
Counter是只增不减的,直接查询无意义,需用rate()或irate()计算增长率:
rate():计算指定时间范围内的平均增长率(适合长期趋势);irate():计算最新两个数据点的瞬时增长率(适合实时监控)。# 计算5分钟内HTTP请求的平均QPS
rate(http_requests_total[5m])
# 计算1分钟内POST请求的瞬时QPS
irate(http_requests_total{method="POST"}[1m])
# 统计5分钟内错误请求(status=5xx)的占比
sum(rate(http_requests_total{status=~"5.."}[5m])) / sum(rate(http_requests_total[5m])) * 100
Gauge可直接查询,也可结合avg()、max()等函数聚合:
# 查询当前服务器内存使用率
node_memory_usage_percent
# 查询集群中所有节点的平均CPU负载(1分钟)
avg(node_load1) by (instance)
# 查询磁盘使用率超过80%的节点
node_filesystem_usage_percent{mountpoint="/"} > 80
Histogram通过bucket(分桶)存储数据,需用histogram_quantile()计算分位数:
# 计算95%的请求延迟(单位:秒)
histogram_quantile(0.95, sum(rate(http_request_duration_seconds_bucket[5m])) by (le))
# 计算99%的请求延迟,并按接口路径分组
histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket[5m])) by (le, path))
告警规则是Prometheus的核心功能之一,通过PromQL定义告警条件,满足条件时触发告警。
编辑/usr/local/prometheus/alert_rules.yml:
groups:
-name:server_alerts
rules:
# 告警规则1:CPU使用率超过80%持续5分钟
-alert:HighCPUUsage
expr:avg(node_cpu_usage_percent)by(instance)>80
for:5m
labels:
severity:warning
env:production
annotations:
summary:"服务器{{ $labels.instance }} CPU使用率过高"
description:"服务器{{ $labels.instance }} CPU使用率已超过80%(当前值:{{ $value }}%),持续时间5分钟。"
# 告警规则2:内存使用率超过90%持续3分钟
-alert:HighMemoryUsage
expr:node_memory_usage_percent>90
for:3m
labels:
severity:critical
env:production
annotations:
summary:"服务器{{ $labels.instance }} 内存使用率过高"
description:"服务器{{ $labels.instance }} 内存使用率已超过90%(当前值:{{ $value }}%),持续时间3分钟。"
# 告警规则3:磁盘根分区使用率超过85%
-alert:HighDiskUsage
expr:node_filesystem_usage_percent{mountpoint="/"}>85
for:1m
labels:
severity:critical
env:production
annotations:
summary:"服务器{{ $labels.instance }} 磁盘根分区使用率过高"
description:"服务器{{ $labels.instance }} 磁盘根分区使用率已超过85%(当前值:{{ $value }}%)。"
修改/usr/local/prometheus/prometheus.yml,在rule_files中添加告警规则文件:
rule_files:
- "alert_rules.yml"
systemctl restart prometheus
访问Prometheus Web界面,点击「Alerts」,可看到定义的3条告警规则,状态为「Inactive」(未触发)。若服务器CPU使用率超过80%持续5分钟,状态会变为「Firing」(触发)。
Alertmanager负责接收Prometheus的告警,进行去重、分组、静默、路由,最终发送到邮件、钉钉、微信等渠道。
wget https://github.com/prometheus/alertmanager/releases/download/v0.27.0/alertmanager-0.27.0.linux-amd64.tar.gz
tar -zxvf alertmanager-0.27.0.linux-amd64.tar.gz
mv alertmanager-0.27.0.linux-amd64 /usr/local/alertmanager
# 创建数据目录
mkdir -p /data/alertmanager
编辑/usr/local/alertmanager/alertmanager.yml:
global:
resolve_timeout:5m# 告警恢复后,5分钟内不再发送恢复通知
route:
group_by:['alertname','instance']# 按告警名称和实例分组
group_wait:10s# 分组等待时间,收集同组告警后一起发送
group_interval:10s# 同组告警再次发送的间隔
repeat_interval:1h# 重复发送告警的间隔
receiver:'email-receiver'# 默认接收者
receivers:
-name:'email-receiver'
email_configs:
-to:'your-email@example.com'# 接收告警的邮箱
from:'alertmanager@example.com'# 发件人邮箱
smarthost:'smtp.example.com:587'# SMTP服务器地址和端口
auth_username:'alertmanager@example.com'# SMTP用户名
auth_password:'your-smtp-password'# SMTP密码
require_tls:true# 启用TLS
inhibit_rules:
-source_match:
severity:'critical'
target_match:
severity:'warning'
equal:['alertname','instance']# 当critical告警触发时,抑制同实例的warning告警
编辑/usr/lib/systemd/system/alertmanager.service:
[Unit]
Description=Alertmanager
After=network.target
[Service]
Type=simple
User=root
ExecStart=/usr/local/alertmanager/alertmanager \
--config.file=/usr/local/alertmanager/alertmanager.yml \
--storage.path=/data/alertmanager
Restart=on-failure
[Install]
WantedBy=multi-user.target
修改/usr/local/prometheus/prometheus.yml,补充Alertmanager地址:
alerting:
alertmanagers:
- static_configs:
- targets:
- localhost:9093
systemctl daemon-reload
systemctl start alertmanager
systemctl enable alertmanager
systemctl status alertmanager
访问http://服务器IP:9093,进入Alertmanager Web界面,可查看告警状态、接收者配置等。当Prometheus触发告警时,Alertmanager会按配置发送邮件通知。
作为Java开发者,监控Spring Boot应用是高频需求。以下是完整的实现步骤。
在Spring Boot项目的pom.xml中添加依赖(以Spring Boot 3.x为例):
<!-- Prometheus监控核心依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
spring:
application:
name:demo-app
management:
endpoints:
web:
exposure:
include:prometheus,health,info# 暴露prometheus端点
metrics:
tags:
application:${spring.application.name}# 为指标添加应用名称标签
export:
prometheus:
enabled:true
endpoint:
prometheus:
enabled:true
启动Spring Boot应用,访问http://应用IP:端口/actuator/prometheus,可看到应用的指标(如jvm_memory_used_bytes、http_server_requests_seconds),说明指标暴露成功。
修改/usr/local/prometheus/prometheus.yml,添加Spring Boot应用的监控任务:
scrape_configs:
# 省略已有配置...
-job_name:"spring_boot_app"
static_configs:
-targets:["应用IP:端口"]# Spring Boot应用地址
metrics_path:"/actuator/prometheus"# 指标路径
scrape_interval:10s# 采集频率
重启Prometheus:
systemctl restart prometheus
在Prometheus Web界面的Targets中,可看到spring_boot_app状态为UP,说明采集成功。
Grafana社区提供了Spring Boot应用的仪表盘模板,ID为12900:
storage.tsdb.retention.time,根据业务需求设置保留时间(如15d),避免数据过多占用磁盘;group_by、group_wait、inhibit_rules配置,减少重复告警;curl 目标IP:端口/metrics,确认能获取指标数据。