Prometheus爲你的微服務保駕護航

前面我們介紹了 Prometheus 的作用和整體的架構,相信大家對 Prometheus 有了一定的瞭解。

具體可以查看這篇文章:《天天CRUD的我,也想玩玩高大上的Prometheus》

今天着重介紹下如何在項目中將 Prometheus 用起來,結合漂亮的圖表做數據展示,真的非常帥氣。

使用之前先介紹一個 Micrometer,Micrometer 是一款監控指標的度量類庫,提供了對各種指標的監控。比如 JVM, 線程池,數據庫連接池等。

官方網站:https://micrometer.io/[1]

項目集成

首先在項目中添加下面的 Maven 依賴,如下:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<version>1.5.9</version>
</dependency>

增加對應的配置,如下:

management:
endpoints:
web:
exposure:
include: "*"
metrics:
tags:
application: ${spring.application.name}

exposure.include 配置你要暴露的端點信息,全部就配置成 * 號。

tags.application 配置成跟服務名一樣即可。

訪問/actuator/prometheus 就可以看到很多指標數據了,至於這些數據是怎麼出來的就不做過多講解,對應的代碼都在 micrometer-registry-prometheus 包中,大家可以自己去研究下。

# HELP hikaricp_connections_max Max connections
# TYPE hikaricp_connections_max gauge
hikaricp_connections_max{application="haomai-customer",pool="HikariPool-1",} 10.0
# HELP process_start_time_seconds Start time of the process since unix epoch.
# TYPE process_start_time_seconds gauge
process_start_time_seconds{application="haomai-customer",} 1.611642684781E9
# HELP jvm_gc_max_data_size_bytes Max size of old generation memory pool
# TYPE jvm_gc_max_data_size_bytes gauge
jvm_gc_max_data_size_bytes{application="haomai-customer",} 2.68435456E8
# HELP tomcat_sessions_created_sessions_total
# TYPE tomcat_sessions_created_sessions_total counter
tomcat_sessions_created_sessions_total{application="haomai-customer",} 0.0

數據採集

如果沒有做服務動態發現,那就手動修改 Prometheus 配置文件,新增一個任務進行抓取。

 - job_name: 'haomai-customer-beta'
    scrape_interval: 5s
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['192.168.1.49:8099']

在 Targets 中查看是否成功。

圖片

數據展示

先去 grafana 搜一個帥氣的圖表,地址如下:

https://grafana.com/grafana/dashboards?search=spring%20boot[2]

選第一個就行了,星星數量多點。


點進去複製圖表的編號 12856,去 grafana 中導入即可,選擇數據源就可以展示了。


當然像數據庫連接之類的也可以去搜專門的圖表來展示,或者自定義圖表,這個後面再給大家介紹。

同樣還有告警也是需要單獨做的,可以用 grafana 自帶的告警來做,也可以單獨部署 Alertmanager 來做告警。後續再單獨再介紹哈。

關於作者:尹吉歡,簡單的技術愛好者,《Spring Cloud 微服務-全棧技術與案例解析》, 《Spring Cloud 微服務 入門 實戰與進階》作者, 公衆號猿天地發起人。

參考資料

[1]

https://micrometer.io/: https://micrometer.io/

[2]

https://grafana.com/grafana/dashboards?search=spring%20boot: https://grafana.com/grafana/dashboards?search=spring%20boot

- END -


後臺回覆 學習資料 領取學習視頻


如有收穫,點個在看,誠摯感謝

本文分享自微信公衆號 - 猿天地(cxytiandi)。
如有侵權,請聯繫 [email protected] 刪除。
本文參與“OSC源創計劃”,歡迎正在閱讀的你也加入,一起分享。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章