Springboot搭建JVM監(jiān)控(Springboot + Prometheus + Grafana)
背景
由于項(xiàng)目之前在生產(chǎn)環(huán)境出現(xiàn)過OOM的問題,并且沒有及時(shí)發(fā)現(xiàn),導(dǎo)致生產(chǎn)環(huán)境出現(xiàn)了在一定時(shí)間內(nèi)不可用的情況,故決定搭建JVM監(jiān)控對(duì)微服務(wù)24小時(shí)監(jiān)聽,以便于出現(xiàn)問題能夠及時(shí)通知相關(guān)人員進(jìn)行服務(wù)降級(jí)或解決問題。
監(jiān)控平臺(tái)的選擇
經(jīng)過可行性分析,得到目前較為適合的微服務(wù)監(jiān)控為Springboot Admin或者Prometheus,兩者的主要區(qū)別如下:
| 框架 | 可監(jiān)控對(duì)象 | 是否支持集群 |
|---|---|---|
| Springboot Admin | Springboot微服務(wù) | 是 |
| Prometheus | 開源監(jiān)控,不僅僅能夠監(jiān)控微服務(wù) | 是 |
考慮到未來還會(huì)有慢SQL、Git等其他類型監(jiān)控,并且Grafana能夠提供優(yōu)秀的監(jiān)控?cái)?shù)據(jù)統(tǒng)計(jì)與展示,因此最終選擇了以Prometheus + Grafana的方式搭建監(jiān)控。
搭建微服務(wù)監(jiān)控

Prometheus下載與安裝
下載地址:Prometheus下載
選擇對(duì)應(yīng)的操作系統(tǒng)下載壓縮包即可(因目前為調(diào)研階段,本文以windows舉例,未來正式部署會(huì)補(bǔ)充linux的)。

安裝包解壓后的目錄如下圖所示,其中,prometheus.yml是Prometheus啟動(dòng)時(shí)讀取的配置文件,有關(guān)監(jiān)聽配置(scrape_configs)全部寫在此文件中,關(guān)于監(jiān)聽配置本文將在微服務(wù)監(jiān)控配置時(shí)一起講述。
注:關(guān)于配置文件的解讀可以參考另外一位大神的文章:prometheus.yml解讀

# 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).
# Alertmanager configuration
alerting:
alertmanagers:
- static_configs:
- targets:
# - alertmanager:9093
# Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
rule_files:
# - "first_rules.yml"
# - "second_rules.yml"
# A scrape configuration containing exactly one endpoint to scrape:
# Here it's Prometheus itself.
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "eunomia"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
metrics_path: /actuator/prometheus
static_configs:
- targets: ["localhost:8000"]
- job_name: "apollo-monitor"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
metrics_path: /actuator/prometheus
static_configs:
- targets: ["localhost:8001"]
Springboot微服務(wù)添加監(jiān)控配置
Prometheus對(duì)于Springboot微服務(wù)的監(jiān)聽數(shù)據(jù)源來自于Springboot的actuator,但是1.X版本和2.X版本的監(jiān)聽配置還是有一些區(qū)別的,本文將分別以Springboot 1.4.0和Springboot 2.3.7為例,闡述監(jiān)聽配置過程
Springboot 1.4.0監(jiān)聽配置
pom文件添加如下依賴:
<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.0.9</version>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-spring-legacy</artifactId>
<version>1.0.9</version>
</dependency>
因?yàn)榘姹揪壒?,?jīng)過多次嘗試發(fā)現(xiàn),Springboot1.4.0可使用Prometheus最高版本為1.0.9,再高版本服務(wù)無法啟動(dòng)。spring-legacy版本與Prometheus版本對(duì)應(yīng)即可,yml文件新增配置如下:
server:
port: 8001 #服務(wù)端口,若management沒有配置監(jiān)聽端口,則默認(rèn)采用服務(wù)端口作為監(jiān)聽端口
spring:
application:
name: apollo-monitor
management:
context-path: /actuator #監(jiān)聽服務(wù)上下文配置
endpoint:
metrics:
enabled: true #支持metrics
prometheus:
enabled: true #支持Prometheus
metrics:
export:
prometheus:
enabled: true
tags:
application: apollo-monitor #可自定義tag,這里目前只配了實(shí)例名
endpoints:
web:
exposure:
include: '*' #開放所有端口
Springboot1.X版本需要將實(shí)例名以metrics的形式發(fā)出,才能被Prometheus獲取(2.X可直接讀yml文件),故需要添加如下配置:
@Value("${spring.application.name}")
private String applicationName;
@Bean
MeterRegistryCustomizer<MeterRegistry> metricsCommonTags() {
return registry -> registry.config().commonTags("application", applicationName);
}
Springboot 2.3.7 監(jiān)聽配置
對(duì)于Springboot2.X版本來說,Prometheus的接入可友好太多了,Metrics可以直接配置在yml文件中,并且無需單獨(dú)引入spring-legency即可完成數(shù)據(jù)采集。
pom文件添加如下依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
<scope>runtime</scope>
</dependency>
Springboot2.3.7默認(rèn)對(duì)應(yīng)Prometheus版本為1.5.9,pom依賴無需指定版本。yml文件新增配置如下,其中,實(shí)例名采集必須配置,否則JVM監(jiān)控?cái)?shù)據(jù)抓取不到實(shí)例名,Grafana提供的JVM監(jiān)控模板中有些Metric Query是需要以實(shí)例名作為條件的。
management:
endpoint:
metrics:
enabled: true #支持metrics
prometheus:
enabled: true #支持Prometheus
metrics:
export:
prometheus:
enabled: true
tags:
application: eunomia #實(shí)例名采集
endpoints:
web:
exposure:
include: '*' #開放所有端口
Prometheus配置微服務(wù)注冊(cè)
完成Springboot的監(jiān)聽基本配置后,需要將其以Job的形式注冊(cè)到Prometheus,讓我們?cè)倩氐絇rometheus的配置文件中:
scrape_configs:
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "prometheus"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
static_configs:
- targets: ["localhost:9090"]
# The job name is added as a label `job=<job_name>` to any timeseries scraped from this config.
- job_name: "eunomia"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
metrics_path: /actuator/prometheus
static_configs:
- targets: ["localhost:8000"]
- job_name: "apollo-monitor"
# metrics_path defaults to '/metrics'
# scheme defaults to 'http'.
metrics_path: /actuator/prometheus
static_configs:
- targets: ["localhost:8001"]
其中,job_name為apollo-monitor的即為上文的Springboot1.4.0服務(wù),將其以配置的形式引入,主要屬性如下表所示:
| 屬性 | 作用 |
|---|---|
| job_name | 服務(wù)注冊(cè)名 |
| metrics_path | 服務(wù)監(jiān)聽路徑,一般為 “/actuator/實(shí)例名”的形式 |
| static_configs | 靜態(tài)配置目錄 |
| targets | 監(jiān)聽目標(biāo)地址 |
完成Prometheus的配置后,便可以啟動(dòng)Springboot服務(wù)和Prometheus,驗(yàn)證以上配置是否正確。
Prometheus啟動(dòng)器在{Prometheus}/bin目錄下,prometheus.exe,啟動(dòng)后的界面如下圖所示:

兩者均啟動(dòng)后,可訪問Prometheus平臺(tái)頁面查看服務(wù)的注冊(cè)情況。
默認(rèn)地址為:http://127.0.0.1:9090/進(jìn)入頁面后,Status -> Target,結(jié)果如下圖所示,可以看到服務(wù)名為apollo-monitor的服務(wù)已經(jīng)注冊(cè)在Prometheus上,說明Prometheus已經(jīng)在對(duì)其進(jìn)行實(shí)時(shí)監(jiān)聽。

確認(rèn)服務(wù)注冊(cè)成功后,可訪問127.0.0.1:serverport/actuator/prometheus頁面,查看服務(wù)是否報(bào)送相關(guān)可監(jiān)控?cái)?shù)據(jù),如下圖所示,監(jiān)控?cái)?shù)據(jù)已經(jīng)全部報(bào)送,只是可讀性非常差,無法在短時(shí)間內(nèi)獲取到有效的監(jiān)控信息,這時(shí)便需要Grafana將監(jiān)控?cái)?shù)據(jù)進(jìn)行統(tǒng)計(jì)、分類與可視化。

Prometheus接入Grafana
Grafana
Grafana 是一跨平臺(tái)的開源的可視化分析工具。目前網(wǎng)絡(luò)架構(gòu)和應(yīng)用分析中最流行的時(shí)序數(shù)據(jù)展示工具,主要用于大規(guī)模指標(biāo)數(shù)據(jù)的可視化展示。其主要作用如下:
1. 將監(jiān)控?cái)?shù)據(jù)以不同維度、不同效果進(jìn)行展示與統(tǒng)計(jì)。
2. 關(guān)聯(lián)Alert Management以實(shí)現(xiàn)閾值告警功能。3. 支持多種數(shù)據(jù)源的監(jiān)控?cái)?shù)據(jù)收集,如:Prometheus,Mysql,Oracle,ES,ZipKin,Git,Jira等熱門應(yīng)用。
Grafana下載與安裝
官網(wǎng)下載:Grafana官網(wǎng)
如下圖所示,進(jìn)入下載頁面后,選擇對(duì)應(yīng)的操作系統(tǒng),本文以windows舉例,點(diǎn)擊下載按鈕即可下載安裝包,下載后本地直接安裝即可。

安裝后Grafana是默認(rèn)啟動(dòng)的,端口是3000,我們可直接訪問頁面:http://127.0.0.1:3000/login,默認(rèn)用戶名密碼為admin/admin,首次登錄會(huì)強(qiáng)制修改密碼,登錄后進(jìn)入Home界面,因?yàn)楸疚牡淖罱K目的是實(shí)現(xiàn)微服務(wù)的JVM監(jiān)控,而Grafana官網(wǎng)有較為全面的JVM監(jiān)控?cái)?shù)據(jù)面板,故直接選擇import的方式導(dǎo)入即可,模板ID可去官方倉庫查詢,可先訪問Grafana官方倉庫:Grafana模板倉庫地址,搜索欄輸入“JVM”,結(jié)果中第一個(gè)就是我們想要的模板,如下圖所示:

點(diǎn)擊后進(jìn)入模板詳情頁面,如下圖所示,兩種方式都可以,分別對(duì)應(yīng)Grafana兩種導(dǎo)入方式即可,本文以ID舉例。

獲取到ID后,回到Grafana主頁面,點(diǎn)擊DataSource新建數(shù)據(jù)源,選擇Prometheus,填入之前搭好的Prometheus監(jiān)控地址,Save即可。接下來是最后一步,導(dǎo)入監(jiān)控面板,Home頁面選擇Import Dashboard,其中A方式為Json導(dǎo)入方式,B方式為ID導(dǎo)入方式,分別對(duì)應(yīng)Grafana倉庫的兩種模板下載方式,本文選擇ID導(dǎo)入方式,輸入4701,點(diǎn)擊Load,完成導(dǎo)入并進(jìn)入數(shù)據(jù)源選擇界面,JVM監(jiān)控模板固定數(shù)據(jù)源類型是Prometheus,因此只需選擇之前添加的Prometheus數(shù)據(jù)源即可完成JVM監(jiān)控面板的搭建,成品如最后所示。







總結(jié)
本文講述以Prometheus+Grafana搭建微服務(wù)JVM監(jiān)控的主要流程,其實(shí)這僅僅是Prometheus和Grafana的冰山一角,不僅是JVM監(jiān)控,還可以做服務(wù)數(shù)據(jù)采集進(jìn)行運(yùn)營(如ip、登錄端、API調(diào)用與耗時(shí)等多維度、多方面監(jiān)控)、運(yùn)維相關(guān)(服務(wù)器監(jiān)控、慢SQL監(jiān)控)的統(tǒng)計(jì),并且,微服務(wù)接入方式也值得深入研究,盡可能地使得業(yè)務(wù)無感知去完成Metric數(shù)據(jù)捕捉。
到此這篇關(guān)于Springboot搭建JVM監(jiān)控(Springboot + Prometheus + Grafana)的文章就介紹到這了,更多相關(guān)Springboot搭建JVM監(jiān)控內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合Prometheus如何實(shí)現(xiàn)資源監(jiān)控
- SpringBoot結(jié)合prometheus自定義埋點(diǎn)方式
- springboot整合prometheus實(shí)現(xiàn)資源監(jiān)控的詳細(xì)步驟
- SpringBoot集成 Prometheus進(jìn)行高效監(jiān)控的實(shí)現(xiàn)
- SpringBoot使用Prometheus采集自定義指標(biāo)數(shù)據(jù)的方法詳解
- Java服務(wù)端服務(wù)監(jiān)控:Prometheus與Spring Boot Actuator的集成方式
相關(guān)文章
Idea如何導(dǎo)入java mysql驅(qū)動(dòng)包
本文介紹了如何在IntelliJ IDEA中配置MySQL數(shù)據(jù)庫連接,首先下載MySQL Connector/J驅(qū)動(dòng)并解壓,然后在Idea項(xiàng)目中創(chuàng)建lib文件夾并將.jar文件復(fù)制到該文件夾,接著,將.jar文件添加為項(xiàng)目庫,通過這些步驟,可以成功配置MySQL數(shù)據(jù)庫連接2024-12-12
SpringBoot使用AOP統(tǒng)一日志管理的方法詳解
這篇文章主要為大家分享一個(gè)干貨:超簡(jiǎn)潔SpringBoot使用AOP統(tǒng)一日志管理,文中的示例代碼講解詳細(xì),感興趣的小伙伴快跟隨小編一起學(xué)習(xí)學(xué)習(xí)吧2022-05-05
tomcat部署java web項(xiàng)目遇到的問題及解決方法
這篇文章主要介紹了tomcat部署java web項(xiàng)目遇到的問題及解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-07-07
深入學(xué)習(xí)MyBatis中的參數(shù)(推薦)
大家日常使用MyBatis經(jīng)常會(huì)遇到一些異常,想要避免參數(shù)引起的錯(cuò)誤,我們需要深入了解參數(shù)。想了解參數(shù),我們首先看MyBatis處理參數(shù)和使用參數(shù)的全部過程。下面這篇文章主要給大家介紹了MyBatis中參數(shù)的的相關(guān)資料,需要的朋友可以參考借鑒,下面來一起看看吧。2017-06-06

