基於 RocketMQ Prometheus Exporter 打造定制化 DevOps 平台


本文將對 RocketMQ-Exporter 的設計實現做一個簡單的介紹,讀者可以通過本文了解到 RocketMQ-Exporter 的實現過程,以及通過 RocketMQ-Exporter 來搭建自己的 RocketMQ 監控系統。該項目的 git 地址 https://github.com/apache/rocketmq-exporter

文章主要內容包含以下幾個方面:

  1. RocketMQ 介紹
  2. Prometheus 簡介
  3. RocketMQ-Exporter 的具體實現
  4. RocketMQ-Exporter 的監控指標和告警指標
  5. RocketMQ-Exporter 使用示例

RocketMQ 介紹

RocketMQ 是一個分布式消息和流數據平台,具有低延遲、高性能、高可靠性、萬億級容量和靈活的可擴展性。簡單的來說,它由 Broker 服務器和客戶端兩部分組成,其中客戶端一個是消息發布者客戶端 (Producer),它負責向 Broker 服務器發送消息;另外一個是消息的消費者客戶端 (Consumer),多個消費者可以組成一個消費組,來訂閱和拉取消費 Broker 服務器上存儲的消息。正由於它具有高性能、高可靠性和高實時性的特點,與其他協議組件在 MQTT 等各種消息場景中的結合也越來越多,應用越來越廣泛。而對於這樣一個強大的消息中間件平台,在實際使用的時候還缺少一個監控管理平台。而當前在開源界,使用最廣泛監控解決方案的就是 Prometheus。與其它傳統監控系統相比較,Prometheus 具有易於管理,監控服務的內部運行狀態,強大的數據模型,強大的查詢語言 PromQL,高效的數據處理,可擴展,易於集成,可視化,開放性等優點。並且借助於 Prometheus 可以很快速的構建出一個能夠監控 RocketMQ 的監控平台。

Prometheus 簡介

下圖展示了 Prometheus 的基本架構:

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

Prometheus Server

Prometheus Server 是 Prometheus 組件中的核心部分,負責實現對監控數據的獲取,存儲以及查詢。 Prometheus Server 可以通過靜態配置管理監控目標,也可以配合使用 Service Discovery 的方式動態管理監控目標,並從這些監控目標中獲取數據。其次 Prometheus Server 需要對采集到的監控數據進行存儲,Prometheus Server 本身就是一個時序數據庫,將采集到的監控數據按照時間序列的方式存儲在本地磁盤當中。最后 Prometheus Server 對外提供了自定義的 PromQL 語言,實現對數據的查詢以及分析。

Exporters

Exporter 將監控數據采集的端點通過 HTTP 服務的形式暴露給 Prometheus Server,Prometheus Server 通過訪問該 Exporter 提供的 Endpoint 端點,即可獲取到需要采集的監控數據。RocketMQ-Exporter 就是這樣一個 Exporter,它首先從 RocketMQ 集群采集數據,然后借助 Prometheus 提供的第三方客戶端庫將采集的數據規范化成符合 Prometheus 系統要求的數據,Prometheus 定時去從 Exporter 拉取數據即可。當前 RocketMQ Exporter 已被 Prometheus 官方收錄,其地址為 https://github.com/apache/rocketmq-exporter

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

RocketMQ-Exporter 的具體實現

當前在 Exporter 當中,實現原理如下圖所示:

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

整個系統基於 spring boot 框架來實現。由於 MQ 內部本身提供了比較全面的數據統計信息,所以對於 Exporter 而言,只需要將 MQ 集群提供的統計信息取出然后進行加工而已。所以 RocketMQ-Exporter 的基本邏輯是內部啟動多個定時任務周期性的從 MQ 集群拉取數據,然后將數據規范化后通過端點暴露給 Prometheus 即可。其中主要包含如下主要的三個功能部分:

  • MQAdminExt 模塊通過封裝 MQ 系統客戶端提供的接口來獲取 MQ 集群內部的統計信息。
  • MetricService 負責將 MQ 集群返回的結果數據進行加工,使其符合 Prometheus 要求的格式化數據。
  • Collect 模塊負責存儲規范化后的數據,最后當 Prometheus 定時從 Exporter 拉取數據的時候,Exporter 就將 Collector 收集的數據通過 HTTP 的形式在 /metrics 端點進行暴露。

RocketMQ-Exporter 的監控指標和告警指標

RocketMQ-Exporter 主要是配合 Prometheus 來做監控,下面來看看當前在 Expoter 中定義了哪些監控指標和告警指標

  • 監控指標
監控指標 含義
rocketmq_broker_tps broker 每秒生產消息數量
rocketmq_broker_qps broker 每秒消費消息數量
rocketmq_producer_tps 某個 topic 每秒生產的消息數量
rocketmq_producer_put_size 某個 topic 每秒生產的消息大小 (字節)
rocketmq_producer_offset 某個 topic 的生產消息的進度
rocketmq_consumer_tps 某個消費組每秒消費的消息數量
rocketmq_consumer_get_size 某個消費組每秒消費的消息大小 (字節)
rocketmq_consumer_offset 某個消費組的消費消息的進度
rocketmq_group_get_latency_by_storetime 某個消費組的消費延時時間
rocketmq_message_accumulation(rocketmq_producer_offset-rocketmq_consumer_offset) 消息堆積量(生產進度 - 消費進度)

rocketmq_message_accumulation 是一個聚合指標,需要根據其它上報指標聚合生成。

  • 告警指標
告警指標 含義
sum(rocketmq_producer_tps) by (cluster) >= 10 集群發送 tps 太高
sum(rocketmq_producer_tps) by (cluster) < 1 集群發送 tps 太低
sum(rocketmq_consumer_tps) by (cluster) >= 10 集群消費 tps 太高
sum(rocketmq_consumer_tps) by (cluster) < 1 集群消費 tps 太低
rocketmq_group_get_latency_by_storetime > 1000 集群消費延時告警
rocketmq_message_accumulation > value 消費堆積告警

消費者堆積告警指標也是一個聚合指標,它根據消費堆積的聚合指標生成,value 這個閾值對每個消費者是不固定的,當前是根據過去 5 分鍾生產者生產的消息數量來定,用戶也可以根據實際情況自行設定該閾值。 告警指標設置的值只是個閾值只是象征性的值,用戶可根據在實際使用 RocketMQ 的情況下自行設定。這里重點介紹一下消費者堆積告警指標,在以往的監控系統中,由於沒有像 Prometheus 那樣有強大的 PromQL 語言,在處理消費者告警問題時勢必需要為每個消費者設置告警,那這樣就需要 RocketMQ 系統的維護人員為每個消費者添加,要么在系統后台檢測到有新的消費者創建時自動添加。在 Prometheus 中,這可以通過一條如下的語句來實現:

(sum(rocketmq_producer_offset) by (topic) - on(topic)  group_right  sum(rocketmq_consumer_offset) by (group,topic)) 
- ignoring(group) group_left sum (avg_over_time(rocketmq_producer_tps[5m])) by (topic)*5*60 > 0

 

借助 PromQL 這一條語句不僅可以實現為任意一個消費者創建消費告警堆積告警,而且還可以使消費堆積的閾值取一個跟生產者發送速度相關的閾值。這樣大大增加了消費堆積告警的准確性。

RocketMQ-Exporter 使用示例

1 啟動 NameServer 和 Broker

要驗證 RocketMQ 的 Spring-Boot 客戶端,首先要確保 RocketMQ 服務正確的下載並啟動。可以參考 RocketMQ 主站的快速開始來進行操作。確保啟動 NameServer 和 Broker 已經正確啟動。

2 編譯 RocketMQ-Exporter

用戶當前使用,需要自行下載 git 源碼編譯

git clone https://github.com/apache/rocketmq-exporter
cd rocketmq-exporter
mvn clean install

 

3 配置和運行

RocketMQ-Exporter 有如下的運行選項

選項 默認值 含義
rocketmq.config.namesrvAddr 127.0.0.1:9876 MQ 集群的 nameSrv 地址
rocketmq.config.webTelemetryPath /metrics 指標搜集路徑
server.port 5557 HTTP 服務暴露端口

以上的運行選項既可以在下載代碼后在配置文件中更改,也可以通過命令行來設置。
編譯出來的 jar 包就叫 rocketmq-exporter-0.0.1-SNAPSHOT.jar,可以通過如下的方式來運行。

java -jar rocketmq-exporter-0.0.1-SNAPSHOT.jar [--rocketmq.config.namesrvAddr="127.0.0.1:9876" ...]

 

4 安裝 Prometheus

首先到 Prometheus 官方下載地址 https://prometheus.io/download/ 去下載 Prometheus 安裝包,當前以 linux 系統安裝為例,選擇的安裝包為 prometheus-2.7.0-rc.1.linux-amd64.tar.gz,經過如下的操作步驟就可以啟動 prometheus 進程。

tar -xzf prometheus-2.7.0-rc.1.linux-amd64.tar.gz
cd prometheus-2.7.0-rc.1.linux-amd64/
./prometheus --config.file=prometheus.yml --web.listen-address=:5555

 

Prometheus 默認監聽端口號為 9090,為了不與系統上的其它進程監聽端口沖突,我們在啟動參數里面重新設置了監聽端口號為 5555。然后通過瀏覽器訪問 http://< 服務器 IP 地址 >:5555, 就可以驗證 Prometheus 是否已成功安裝,顯示界面如下

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

由於 RocketMQ-Exporter 進程已啟動,這個時候可以通過 Prometheus 來抓取 RocketMQ-Exporter 的數據,這個時候只需要更改 Prometheus 啟動的配置文件即可
整體配置文件如下:

# my global config
global:
   scrape_interval:     15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
   evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
   # scrape_timeout is set to the global default (10s).
 
 
 # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
 rule_files:
   # - "first_rules.yml"
   # - "second_rules.yml"
   

 scrape_configs:
   - job_name: 'prometheus'
     static_configs:
     - targets: ['localhost:5555']
   
   
   - job_name: 'exporter'
     static_configs:
     - targets: ['localhost:5557']

 

更改配置文件后,重啟服務即可。重啟后就可以在 Prometheus 界面查詢 RocketMQ-Exporter 上報的指標,例如查詢 rocketmq_broker_tps 指標,其結果如下

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

5 告警規則添加

在 Prometheus 可以展示 RocketMQ-Exporter 的指標后,就可以在 Prometheus 中配置 RocketMQ 的告警指標了。在 Prometheus 的配置文件中添加如下的告警配置項,*.rules 表示可以匹配多個后綴為 rules 的文件。

rule_files:
  # - "first_rules.yml"
  # - "second_rules.yml" 
  - /home/prometheus/prometheus-2.7.0-rc.1.linux-amd64/rules/*.rules

 

當前設置的告警配置文件為 warn.rules,其文件具體內容如下所示。其中的閾值只起一個示例的作用,具體的閾值還需用戶根據實際使用情況來自行設定。

###
# Sample prometheus rules/alerts for rocketmq.
#
###
# Galera Alerts

groups:
- name: GaleraAlerts
  rules:
  - alert: RocketMQClusterProduceHigh
    expr: sum(rocketmq_producer_tps) by (cluster) >= 10
    for: 3m
    labels:
      severity: warning
    annotations:
      description: '{{$labels.cluster}} Sending tps too high.'
      summary: cluster send tps too high
  - alert: RocketMQClusterProduceLow
    expr: sum(rocketmq_producer_tps) by (cluster) < 1
    for: 3m
    labels:
      severity: warning
    annotations:
      description: '{{$labels.cluster}} Sending tps too low.'
      summary: cluster send tps too low
  - alert: RocketMQClusterConsumeHigh
    expr: sum(rocketmq_consumer_tps) by (cluster) >= 10
    for: 3m
    labels:
      severity: warning
    annotations:
      description: '{{$labels.cluster}} consuming tps too high.'
      summary: cluster consume tps too high
  - alert: RocketMQClusterConsumeLow
    expr: sum(rocketmq_consumer_tps) by (cluster) < 1
    for: 3m
    labels:
      severity: warning
    annotations:
      description: '{{$labels.cluster}} consuming tps too low.'
      summary: cluster consume tps too low
  - alert: ConsumerFallingBehind
    expr: (sum(rocketmq_producer_offset) by (topic) - on(topic)  group_right  sum(rocketmq_consumer_offset) by (group,topic)) - ignoring(group) group_left sum (avg_over_time(rocketmq_producer_tps[5m])) by (topic)*5*60 > 0
    for: 3m
    labels:
      severity: warning
    annotations:
      description: 'consumer {{$labels.group}} on {{$labels.topic}} lag behind
        and is falling behind (behind value {{$value}}).'
      summary: consumer lag behind
  - alert: GroupGetLatencyByStoretime
    expr: rocketmq_group_get_latency_by_storetime > 1000
    for: 3m
    labels:
      severity: warning
    annotations:
      description: 'consumer {{$labels.group}} on {{$labels.broker}}, {{$labels.topic}} consume time lag behind message store time
        and (behind value is {{$value}}).'
      summary: message consumes time lag behind message store time too much 

 

最終,可以在 Prometheus 的看一下告警展示效果,紅色表示當前處於告警狀態的項,綠色表示正常狀態。

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

6 Grafana dashboard for RocketMQ

Prometheus 自身的指標展示平台沒有當前流行的展示平台 Grafana 好, 為了更好的展示 RocketMQ 的指標,可以使用 Grafana 來展示 Prometheus 獲取的指標。首先到官網去下載 https://grafana.com/grafana/download , 這里仍以二進制文件安裝為例進行介紹。

wget https://dl.grafana.com/oss/release/grafana-6.2.5.linux-amd64.tar.gz 
tar -zxvf grafana-6.2.5.linux-amd64.tar.gz
cd grafana-5.4.3/

 

同樣為了不與其它進程的使用端口沖突,可以修改 conf 目錄下的 defaults.ini 文件的監聽端口,當前將 grafana 的監聽端口改為 55555,然后使用如下的命令啟動即可

./bin/grafana-server web

 

然后通過瀏覽器訪問 http://< 服務器 IP 地址 >:55555, 就可以驗證 grafana 是否已成功安裝。系統默認用戶名和密碼為 admin/admin,第一次登陸系統會要求修改密碼,修改密碼后登陸,界面顯示如下:

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

點擊 Add data source 按鈕,會要求選擇數據源。

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

選擇數據源為 Prometheus,設置數據源的地址為前面步驟啟動的 Prometheus 的地址

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

回到主界面會要求創建新的 Dashboard

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

點擊創建 dashboard,創建 dashboard 可以自己手動創建,也可以以配置文件導入的方式創建,當前已將 RocketMQ 的 dashboard 配置文件上傳到 Grafana 的官網,這里以配置文件導入的方式進行創建。

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

點擊 New dashboard 下拉按鈕

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

選擇 import dashboard

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

這個時候可以到 Grafana 官網去下載當前已為 RocketMQ 創建好的配置文件,地址為 https://grafana.com/dashboards/10477/revisions ,如下圖所示

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

,點擊 download 就可以下載配置文件,下載配置文件然后,復制配置文件中的內容粘貼到上圖的粘貼內容處。

最后按上述方式就將配置文件導入到 Grafana 了。

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

最終的效果如下所示

基於RocketMQ Prometheus Exporter 打造定制化DevOps平台

 

source: https://www.infoq.cn/article/NcSYj_2PQhBlqveuD1Kw


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM