Prometheus為你的微服務(wù)保駕護(hù)航
前面我們介紹了 Prometheus 的作用和整體的架構(gòu),相信大家對 Prometheus 有了一定的了解。
具體可以查看這篇文章:《天天CRUD的我,也想玩玩高大上的Prometheus》
今天著重介紹下如何在項(xiàng)目中將 Prometheus 用起來,結(jié)合漂亮的圖表做數(shù)據(jù)展示,真的非常帥氣。
使用之前先介紹一個 Micrometer,Micrometer 是一款監(jiān)控指標(biāo)的度量類庫,提供了對各種指標(biāo)的監(jiān)控。比如 JVM, 線程池,數(shù)據(jù)庫連接池等。
官方網(wǎng)站:https://micrometer.io/[1]
項(xiàng)目集成
首先在項(xiàng)目中添加下面的 Maven 依賴,如下:
org.springframework.boot
spring-boot-starter-actuator
io.micrometer
micrometer-registry-prometheus
1.5.9
增加對應(yīng)的配置,如下:
management:
endpoints:
web:
exposure:
include: "*"
metrics:
tags:
application: ${spring.application.name}
exposure.include 配置你要暴露的端點(diǎn)信息,全部就配置成 * 號。
tags.application 配置成跟服務(wù)名一樣即可。
訪問/actuator/prometheus 就可以看到很多指標(biāo)數(shù)據(jù)了,至于這些數(shù)據(jù)是怎么出來的就不做過多講解,對應(yīng)的代碼都在 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
數(shù)據(jù)采集
如果沒有做服務(wù)動態(tài)發(fā)現(xiàn),那就手動修改 Prometheus 配置文件,新增一個任務(wù)進(jìn)行抓取。
?- job_name: 'haomai-customer-beta'
? ? scrape_interval: 5s
? ? metrics_path: '/actuator/prometheus'
? ? static_configs:
? ? ? - targets: ['192.168.1.49:8099']
在 Targets 中查看是否成功。

數(shù)據(jù)展示
先去 grafana 搜一個帥氣的圖表,地址如下:
https://grafana.com/grafana/dashboards?search=spring%20boot[2]
選第一個就行了,星星數(shù)量多點(diǎn)。

點(diǎn)進(jìn)去復(fù)制圖表的編號 12856,去 grafana 中導(dǎo)入即可,選擇數(shù)據(jù)源就可以展示了。

當(dāng)然像數(shù)據(jù)庫連接之類的也可以去搜專門的圖表來展示,或者自定義圖表,這個后面再給大家介紹。
同樣還有告警也是需要單獨(dú)做的,可以用 grafana 自帶的告警來做,也可以單獨(dú)部署 Alertmanager 來做告警。后續(xù)再單獨(dú)再介紹哈。
關(guān)于作者:尹吉?dú)g,簡單的技術(shù)愛好者,《Spring Cloud 微服務(wù)-全棧技術(shù)與案例解析》, 《Spring Cloud 微服務(wù) 入門 實(shí)戰(zhàn)與進(jìn)階》作者, 公眾號猿天地發(fā)起人。
參考資料
https://micrometer.io/: https://micrometer.io/
[2]https://grafana.com/grafana/dashboards?search=spring%20boot: https://grafana.com/grafana/dashboards?search=spring%20boot
后臺回復(fù)?學(xué)習(xí)資料?領(lǐng)取學(xué)習(xí)視頻
如有收獲,點(diǎn)個在看,誠摯感謝
