Prometheus為你的SpringBoot應(yīng)用保駕護航

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

具體可以查看這篇文章:https://mp.weixin.qq.com/s/QoAs0-AYy8krWTa3HbmJZA

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

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

官方網(wǎng)站:https://micrometer.io/

項目集成

首先在項目中添加下面的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>

增加對應(yīng)的配置,如下:

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

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

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ù)進行抓取。

 - 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

選第一個就行了,星星數(shù)量多點。

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

當(dāng)然像數(shù)據(jù)庫連接之類的也可以去搜專門的圖表來展示,或者自定義圖表,這個后面再給大家介紹。

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

關(guān)于作者:尹吉歡,簡單的技術(shù)愛好者,《Spring Cloud微服務(wù)-全棧技術(shù)與案例解析》, 《Spring Cloud微服務(wù) 入門 實戰(zhàn)與進階》作者, 公眾號猿天地發(fā)起人。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容