RocketMQ生产02:监控体系搭建 - Prometheus + Grafana 实战
引言:没有监控的生产环境是裸奔 凌晨 3 点,手机疯狂震动,告警短信轰炸:“RocketMQ 消息堆积 100 万条!“你迅速爬起来,打开电脑,却不知道从哪里开始排查… 这就是没有监控的痛: ❌ 故障发现滞后,用户先于运维发现问题 ❌ 排查无从下手,缺少历史数据 ❌ 性能瓶颈不可见,容量规划靠猜 ❌ 告警不及时,小问题变大故障 本文目标: ✅ 搭建完整的 RocketMQ 监控体系 ✅ 实现核心指标的可视化 ✅ 配置智能告警规则 ✅ 掌握故障排查方法 一、监控体系架构 1.1 整体架构图 ┌─────────────────────────────────────────────────────────┐ │ RocketMQ 集群 │ │ ┌──────────┐ ┌──────────┐ ┌──────────┐ │ │ │ Broker-A │ │ Broker-B │ │ Broker-C │ │ │ └────┬─────┘ └────┬─────┘ └────┬─────┘ │ │ │ │ │ │ │ └─────────────┼─────────────┘ │ │ │ │ │ ┌──────▼──────┐ │ │ │ Exporter │ ← 暴露 Prometheus 指标 │ │ └──────┬──────┘ │ └─────────────────────┼────────────────────────────────────┘ │ ┌──────▼──────┐ │ Prometheus │ ← 采集、存储、查询 └──────┬──────┘ │ ┌─────────────┼─────────────┐ │ │ │ ┌──────▼──────┐ ┌───▼────┐ ┌──────▼──────┐ │ Grafana │ │AlertMgr│ │ 其他工具 │ │ (可视化) │ │(告警) │ │ (日志等) │ └─────────────┘ └────────┘ └─────────────┘ 1.2 核心组件 组件 作用 端口 RocketMQ Exporter 采集 RocketMQ 指标,暴露给 Prometheus 5557 Prometheus 时序数据库,存储监控指标 9090 Grafana 可视化平台,展示监控大盘 3000 AlertManager 告警管理,通知到钉钉/邮件/微信 9093 二、安装部署 2.1 安装 Prometheus # 1. 下载 Prometheus cd /opt wget https://github.com/prometheus/prometheus/releases/download/v2.45.0/prometheus-2.45.0.linux-amd64.tar.gz tar -xzf prometheus-2.45.0.linux-amd64.tar.gz mv prometheus-2.45.0.linux-amd64 prometheus # 2. 配置 Prometheus cd prometheus cat > prometheus.yml <<EOF global: scrape_interval: 15s # 采集间隔 evaluation_interval: 15s # 告警规则评估间隔 # 采集任务配置 scrape_configs: # Prometheus 自身监控 - job_name: 'prometheus' static_configs: - targets: ['localhost:9090'] # RocketMQ 监控 - job_name: 'rocketmq' static_configs: - targets: ['192.168.1.100:5557'] # Exporter 地址 labels: cluster: 'rocketmq-prod' env: 'production' EOF # 3. 启动 Prometheus nohup ./prometheus --config.file=prometheus.yml \ --storage.tsdb.path=./data \ --web.listen-address=:9090 > prometheus.log 2>&1 & # 4. 验证 curl http://localhost:9090/-/healthy # 浏览器访问:http://your-ip:9090 2.2 安装 RocketMQ Exporter # 1. 下载 Exporter cd /opt wget https://github.com/apache/rocketmq-exporter/releases/download/rocketmq-exporter-0.0.2/rocketmq-exporter-0.0.2-bin.tar.gz tar -xzf rocketmq-exporter-0.0.2-bin.tar.gz cd rocketmq-exporter-0.0.2 # 2. 配置 application.properties cat > application.properties <<EOF # RocketMQ NameServer 地址 rocketmq.config.namesrvAddr=192.168.1.100:9876;192.168.1.101:9876 # Exporter 监听端口 server.port=5557 # 采集指标的消费组(用于监控消费进度) rocketmq.config.consumerGroups=consumer_group_1,consumer_group_2 # 是否启用 ACL(如果 RocketMQ 开启了 ACL) rocketmq.config.enableACL=false EOF # 3. 启动 Exporter nohup java -jar rocketmq-exporter-0.0.2.jar \ --spring.config.location=application.properties > exporter.log 2>&1 & # 4. 验证指标暴露 curl http://localhost:5557/metrics 成功输出示例: ...