Prometheus 监控 ActiveMQ 全栈实战:从队列深度到消费者延迟的实时可观测性
Prometheus 监控 ActiveMQ 全栈实战:从队列深度到消费者延迟的实时可观测性
ActiveMQ 是 Java 消息中间件的元老,至今仍被大量企业用于异步通信。其 队列堆积、消费者数量、内存与存储使用、消息入队/出队速率以及 连接数 等指标,直接关系到系统的可用性和性能。Prometheus 根据 ActiveMQ 版本提供不同的监控方案:ActiveMQ Classic (5.x) 通过 JMX Exporter 暴露指标,而 ActiveMQ Artemis (2.x+) 则内置了优雅的 Prometheus 插件。本文将覆盖两种主流架构,带你从零实现 ActiveMQ 的全维度可观测。
1. 方案选型:JMX Exporter vs Artemis 内置插件
| ActiveMQ 版本 | 监控方式 | 特点 |
|---|---|---|
| ActiveMQ Classic (5.x) | 使用 jmx_prometheus_javaagent 随 JVM 启动,抓取 JMX MBean |
灵活,可定制,但需编写 YAML 规则;适用于所有 5.x 版本 |
| ActiveMQ Artemis (2.x+) | 内置 metrics 插件,直接暴露 /metrics 端点 |
官方支持,开箱即用,指标丰富;强烈推荐 |
建议:如果是 Artemis,直接使用内置插件;如果还在 Classic,采用 JMX Exporter。下文将分别给出配置。
2. 监控 ActiveMQ Classic (5.x) 使用 JMX Exporter
2.1 下载并配置 JMX 代理
在 ActiveMQ 服务器上下载 jmx_prometheus_javaagent:
wget https://repo1.maven.org/maven2/io/prometheus/jmx/jmx_prometheus_javaagent/0.20.0/jmx_prometheus_javaagent-0.20.0.jar
sudo mkdir /opt/activemq/prometheus
sudo mv jmx_prometheus_javaagent-0.20.0.jar /opt/activemq/prometheus/
创建规则文件 /opt/activemq/prometheus/activemq_jmx.yml,采集核心 MBean:
startDelaySeconds: 0
hostPort: 127.0.0.1:1099 # ActiveMQ JMX 端口(默认 1099)
ssl: false
rules:
# 队列指标
- pattern: "org.apache.activemq<type=Broker, brokerName=(.+), destinationType=Queue, destinationName=(.+)><>(QueueSize|EnqueueCount|DequeueCount|ConsumerCount|AverageEnqueueTime)"
name: activemq_queue_$3
labels:
broker: $1
destination: $2
type: COUNTER
# 主题指标
- pattern: "org.apache.activemq<type=Broker, brokerName=(.+), destinationType=Topic, destinationName=(.+)><>(EnqueueCount|DequeueCount|ConsumerCount)"
name: activemq_topic_$3
labels:
broker: $1
destination: $2
type: COUNTER
# 连接数
- pattern: "org.apache.activemq<type=Broker, brokerName=(.+)><>(CurrentConnectionsCount|TotalConnectionsCount)"
name: activemq_connections_$2
labels:
broker: $1
# 存储使用
- pattern: "org.apache.activemq<type=Broker, brokerName=(.+)><>(StorePercentUsage|MemoryPercentUsage|TempPercentUsage)"
name: activemq_usage_$2
labels:
broker: $1
# JVM 内存与 GC
- pattern: "java.lang<type=Memory><HeapMemoryUsage>(used|max)"
name: jvm_memory_heap_$1
- pattern: "java.lang<type=GarbageCollector, name=(.+)><>(CollectionCount|CollectionTime)"
name: jvm_gc_$1_$2
可根据需要添加更多 MBean(如 SlowConsumer)。
2.2 修改启动参数
编辑 bin/env 或 activemq 启动脚本,在 ACTIVEMQ_OPTS 中添加 Java Agent:
ACTIVEMQ_OPTS="$ACTIVEMQ_OPTS -javaagent:/opt/activemq/prometheus/jmx_prometheus_javaagent-0.20.0.jar=9096:/opt/activemq/prometheus/activemq_jmx.yml"
这里让代理监听 9096 端口。重启 ActiveMQ 后,curl http://localhost:9096/metrics 应返回指标。
3. 监控 ActiveMQ Artemis (2.x+) 使用内置插件
Artemis 的监控极其简单,只需在 broker.xml 中启用 metrics 插件。
编辑 etc/broker.xml,在 <metrics> 部分添加:
<metrics>
<jvm-gc>true</jvm-gc>
<plugin class-name="org.apache.activemq.artemis.core.server.metrics.plugins.ArtemisPrometheusMetricsPlugin"/>
</metrics>
如果需要暴露具体的队列/地址指标,可以增加:
<metrics>
<jvm-gc>true</jvm-gc>
<plugin class-name="org.apache.activemq.artemis.core.server.metrics.plugins.ArtemisPrometheusMetricsPlugin">
<property key="metrics-path" value="/metrics"/>
</plugin>
<metric name="address.size" />
<metric name="queue.message.count" />
<metric name="queue.consumer.count" />
</metrics>
默认会在管理 HTTP 端口(通常是 8161)上提供 /metrics 端点。也可以单独配置 Prometheus 端口(例如在 etc/bootstrap.xml 中配置 Netty Acceptor)。重启后访问 http://localhost:8161/metrics 验证。
4. 配置 Prometheus 抓取
4.1 对于 ActiveMQ Classic (JMX Exporter)
scrape_configs:
- job_name: 'activemq-classic'
scrape_interval: 30s
static_configs:
- targets: ['activemq-host:9096']
labels:
instance: 'mq-prod'
version: '5.x'
4.2 对于 ActiveMQ Artemis
- job_name: 'activemq-artemis'
scrape_interval: 30s
static_configs:
- targets: ['artemis-host:8161']
labels:
instance: 'mq-prod'
version: 'artemis'
metrics_path: '/metrics'
如果 Artemis 使用独立的 Prometheus 端口(如 9404),直接抓取该端口。
5. 核心监控指标与 PromQL
5.1 Classic (基于 JMX Exporter 规则)
| 指标 | 含义 | PromQL 示例 |
|---|---|---|
activemq_queue_QueueSize |
队列中待消费消息数 | activemq_queue_QueueSize > 1000 |
activemq_queue_EnqueueCount (Counter) |
入队总数 | rate(activemq_queue_EnqueueCount[1m]) |
activemq_queue_DequeueCount (Counter) |
出队总数 | rate(activemq_queue_DequeueCount[1m]) |
activemq_queue_ConsumerCount |
消费者数量 | 为 0 且有 QueueSize 则告警 |
activemq_connections_CurrentConnectionsCount |
当前连接数 | 直接查看 |
activemq_usage_StorePercentUsage |
存储使用百分比 | > 80 需告警 |
activemq_usage_MemoryPercentUsage |
内存使用百分比 | > 80 需告警 |
jvm_memory_heap_used / jvm_memory_heap_max |
堆内存使用率 | > 85% 告警 |
5.2 Artemis (内置指标)
Artemis 的指标名称前缀为 artemis_,如:
| 指标 | 含义 | PromQL 示例 |
|---|---|---|
artemis_address_messages_total |
地址下消息总数 | 可求和看全局堆积 |
artemis_queue_message_count |
特定队列消息数 | artemis_queue_message_count{queue="myQueue"} |
artemis_consumer_count |
消费者数量 | artemis_consumer_count == 0 |
artemis_messages_added_total |
入队速率 | rate(artemis_messages_added_total[1m]) |
artemis_messages_acknowledged_total |
已确认速率 | 对比入队看积压趋势 |
artemis_connection_count |
连接数 | 监控连接数抖动 |
artemis_total_connection_count |
累计连接 | rate(artemis_total_connection_count[5m]) |
artemis_disk_store_usage_ratio |
磁盘存储使用率 | > 0.8 告警 |
artemis_memory_usage / artemis_memory_max |
内存使用/上限 | artemis_memory_usage / artemis_memory_max |
具体指标名以
/metrics输出为准,不同 Artemis 版本可能略有变化,但都遵循artemis_前缀。
6. Grafana 仪表盘推荐
- ActiveMQ Artemis Dashboard:Dashboard ID 12729(专为 Artemis Prometheus 插件设计),展示队列、地址、消费者、内存、磁盘等。
- ActiveMQ Classic (JMX):ID 10944,基于 JMX Exporter 指标,适用于 Classic 5.x。
- 通用 JVM 监控:可与 Spring Boot 或 Node Exporter 面板配合使用。
导入后选择数据源,将 instance 或 broker 变量映射至你的 ActiveMQ 实例。
7. 告警规则实战
以下规则可适配 Classic(调整指标名)与 Artemis:
Classic 版本告警:
groups:
- name: activemq_classic_alerts
rules:
- alert: ActiveMQDown
expr: up{job="activemq-classic"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "ActiveMQ 实例 {{ $labels.instance }} 宕机"
- alert: ActiveMQQueueDepthHigh
expr: activemq_queue_QueueSize > 5000
for: 5m
labels:
severity: warning
annotations:
summary: "队列 {{ $labels.destination }} 深度超过 5000"
- alert: ActiveMQNoConsumer
expr: activemq_queue_QueueSize > 0 and activemq_queue_ConsumerCount == 0
for: 2m
labels:
severity: critical
annotations:
summary: "队列 {{ $labels.destination }} 有消息但没有消费者"
- alert: ActiveMQStoreUsageHigh
expr: activemq_usage_StorePercentUsage > 80
for: 5m
labels:
severity: critical
annotations:
summary: "ActiveMQ 存储使用率超过 80%"
Artemis 版本告警:
groups:
- name: activemq_artemis_alerts
rules:
- alert: ArtemisDown
expr: up{job="activemq-artemis"} == 0
for: 1m
labels:
severity: critical
annotations:
summary: "Artemis 实例 {{ $labels.instance }} 宕机"
- alert: ArtemisQueueDepthHigh
expr: artemis_queue_message_count > 5000
for: 5m
labels:
severity: warning
annotations:
summary: "Artemis 队列 {{ $labels.queue }} 消息堆积超过 5000"
- alert: ArtemisNoConsumer
expr: artemis_queue_message_count > 0 and artemis_consumer_count == 0
for: 2m
labels:
severity: critical
annotations:
summary: "Artemis 队列 {{ $labels.queue }} 有消息但无消费者"
- alert: ArtemisDiskUsageHigh
expr: artemis_disk_store_usage_ratio > 0.8
for: 5m
labels:
severity: critical
annotations:
summary: "Artemis 磁盘使用率超过 80%"
8. 进阶:安全、多实例与性能
8.1 保护指标端点
- Classic (JMX Exporter):通过防火墙限制 9096 端口,或通过 Nginx 反代添加 Basic Auth。
- Artemis:在
broker.xml中配置metrics-security或使用管理 HTTP 端口的安全配置,限制访问。
8.2 多 Broker 监控
ActiveMQ Classic 支持主从(Master/Slave),只需为每个实例配置不同的 JMX Exporter 端口,分别加入 Prometheus targets。Artemis 集群的每个节点也只需各自暴露 /metrics。
8.3 性能开销
JMX Exporter 本身开销极小(内存 ~50MB),Artemis 内置插件零额外成本。抓取间隔 30s 完全足够。
部署完成后,ActiveMQ 的队列健康、消息流转、资源消耗一目了然。任何消息堆积、消费者消失、存储爆满都能立即触发告警,让消息中间件从“盲区”变为可靠的可观测组件,与你的服务器、应用监控融为一体,保障消息驱动的业务永续运行。
更多推荐




所有评论(0)