Springboot搭建JVM監(jiān)控(Springboot + Prometheus + Grafana)
背景
由于項目之前在生產(chǎn)環(huán)境出現(xiàn)過OOM的問題,并且沒有及時發(fā)現(xiàn),導(dǎo)致生產(chǎn)環(huán)境出現(xiàn)了在一定時間內(nèi)不可用的情況,故決定搭建JVM監(jiān)控對微服務(wù)24小時監(jiān)聽,以便于出現(xiàn)問題能夠及時通知相關(guān)人員進(jìn)行服務(wù)降級或解決問題。
監(jiān)控平臺的選擇
經(jīng)過可行性分析,得到目前較為適合的微服務(wù)監(jiān)控為Springboot Admin或者Prometheus,兩者的主要區(qū)別如下:
框架 | 可監(jiān)控對象 | 是否支持集群 |
---|---|---|
Springboot Admin | Springboot微服務(wù) | 是 |
Prometheus | 開源監(jiān)控,不僅僅能夠監(jiān)控微服務(wù) | 是 |
考慮到未來還會有慢SQL、Git等其他類型監(jiān)控,并且Grafana能夠提供優(yōu)秀的監(jiān)控數(shù)據(jù)統(tǒng)計與展示,因此最終選擇了以Prometheus + Grafana的方式搭建監(jiān)控。
搭建微服務(wù)監(jiān)控
Prometheus下載與安裝
下載地址:Prometheus下載
選擇對應(yīng)的操作系統(tǒng)下載壓縮包即可(因目前為調(diào)研階段,本文以windows舉例,未來正式部署會補充linux的)。
安裝包解壓后的目錄如下圖所示,其中,prometheus.yml是Prometheus啟動時讀取的配置文件,有關(guān)監(jiān)聽配置(scrape_configs)全部寫在此文件中,關(guān)于監(jiān)聽配置本文將在微服務(wù)監(jiān)控配置時一起講述。
注:關(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對于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>
因為版本緣故,經(jīng)過多次嘗試發(fā)現(xiàn),Springboot1.4.0可使用Prometheus最高版本為1.0.9,再高版本服務(wù)無法啟動。spring-legacy版本與Prometheus版本對應(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,這里目前只配了實例名 endpoints: web: exposure: include: '*' #開放所有端口
Springboot1.X版本需要將實例名以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)聽配置
對于Springboot2.X版本來說,Prometheus的接入可友好太多了,Metrics可以直接配置在yml文件中,并且無需單獨引入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)對應(yīng)Prometheus版本為1.5.9,pom依賴無需指定版本。yml文件新增配置如下,其中,實例名采集必須配置,否則JVM監(jiān)控數(shù)據(jù)抓取不到實例名,Grafana提供的JVM監(jiān)控模板中有些Metric Query是需要以實例名作為條件的。
management: endpoint: metrics: enabled: true #支持metrics prometheus: enabled: true #支持Prometheus metrics: export: prometheus: enabled: true tags: application: eunomia #實例名采集 endpoints: web: exposure: include: '*' #開放所有端口
Prometheus配置微服務(wù)注冊
完成Springboot的監(jiān)聽基本配置后,需要將其以Job的形式注冊到Prometheus,讓我們再回到Prometheus的配置文件中:
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ù)注冊名 |
metrics_path | 服務(wù)監(jiān)聽路徑,一般為 “/actuator/實例名”的形式 |
static_configs | 靜態(tài)配置目錄 |
targets | 監(jiān)聽目標(biāo)地址 |
完成Prometheus的配置后,便可以啟動Springboot服務(wù)和Prometheus,驗證以上配置是否正確。
Prometheus啟動器在{Prometheus}/bin目錄下,prometheus.exe,啟動后的界面如下圖所示:
兩者均啟動后,可訪問Prometheus平臺頁面查看服務(wù)的注冊情況。
默認(rèn)地址為:http://127.0.0.1:9090/進(jìn)入頁面后,Status -> Target,結(jié)果如下圖所示,可以看到服務(wù)名為apollo-monitor的服務(wù)已經(jīng)注冊在Prometheus上,說明Prometheus已經(jīng)在對其進(jìn)行實時監(jiān)聽。
確認(rèn)服務(wù)注冊成功后,可訪問127.0.0.1:serverport/actuator/prometheus頁面,查看服務(wù)是否報送相關(guān)可監(jiān)控數(shù)據(jù),如下圖所示,監(jiān)控數(shù)據(jù)已經(jīng)全部報送,只是可讀性非常差,無法在短時間內(nèi)獲取到有效的監(jiān)控信息,這時便需要Grafana將監(jiān)控數(shù)據(jù)進(jìn)行統(tǒng)計、分類與可視化。
Prometheus接入Grafana
Grafana
Grafana 是一跨平臺的開源的可視化分析工具。目前網(wǎng)絡(luò)架構(gòu)和應(yīng)用分析中最流行的時序數(shù)據(jù)展示工具,主要用于大規(guī)模指標(biāo)數(shù)據(jù)的可視化展示。其主要作用如下:
1. 將監(jiān)控數(shù)據(jù)以不同維度、不同效果進(jìn)行展示與統(tǒng)計。
2. 關(guān)聯(lián)Alert Management以實現(xiàn)閾值告警功能。3. 支持多種數(shù)據(jù)源的監(jiān)控數(shù)據(jù)收集,如:Prometheus,Mysql,Oracle,ES,ZipKin,Git,Jira等熱門應(yīng)用。
Grafana下載與安裝
官網(wǎng)下載:Grafana官網(wǎng)
如下圖所示,進(jìn)入下載頁面后,選擇對應(yīng)的操作系統(tǒng),本文以windows舉例,點擊下載按鈕即可下載安裝包,下載后本地直接安裝即可。
安裝后Grafana是默認(rèn)啟動的,端口是3000,我們可直接訪問頁面:http://127.0.0.1:3000/login,默認(rèn)用戶名密碼為admin/admin,首次登錄會強制修改密碼,登錄后進(jìn)入Home界面,因為本文的最終目的是實現(xiàn)微服務(wù)的JVM監(jiān)控,而Grafana官網(wǎng)有較為全面的JVM監(jiān)控數(shù)據(jù)面板,故直接選擇import的方式導(dǎo)入即可,模板ID可去官方倉庫查詢,可先訪問Grafana官方倉庫:Grafana模板倉庫地址,搜索欄輸入“JVM”,結(jié)果中第一個就是我們想要的模板,如下圖所示:
點擊后進(jìn)入模板詳情頁面,如下圖所示,兩種方式都可以,分別對應(yīng)Grafana兩種導(dǎo)入方式即可,本文以ID舉例。
獲取到ID后,回到Grafana主頁面,點擊DataSource新建數(shù)據(jù)源,選擇Prometheus,填入之前搭好的Prometheus監(jiān)控地址,Save即可。接下來是最后一步,導(dǎo)入監(jiān)控面板,Home頁面選擇Import Dashboard,其中A方式為Json導(dǎo)入方式,B方式為ID導(dǎo)入方式,分別對應(yīng)Grafana倉庫的兩種模板下載方式,本文選擇ID導(dǎo)入方式,輸入4701,點擊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)控的主要流程,其實這僅僅是Prometheus和Grafana的冰山一角,不僅是JVM監(jiān)控,還可以做服務(wù)數(shù)據(jù)采集進(jìn)行運營(如ip、登錄端、API調(diào)用與耗時等多維度、多方面監(jiān)控)、運維相關(guān)(服務(wù)器監(jiān)控、慢SQL監(jiān)控)的統(tǒng)計,并且,微服務(wù)接入方式也值得深入研究,盡可能地使得業(yè)務(wù)無感知去完成Metric數(shù)據(jù)捕捉。
到此這篇關(guān)于Springboot搭建JVM監(jiān)控(Springboot + Prometheus + Grafana)的文章就介紹到這了,更多相關(guān)Springboot搭建JVM監(jiān)控內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
springboot 2.0 mybatis mapper-locations掃描多個路徑的實現(xiàn)
這篇文章主要介紹了springboot 2.0 mybatis mapper-locations掃描多個路徑的實現(xiàn)方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-07-07基于java file 文件操作operate file of java的應(yīng)用
本篇文章介紹了,基于java file 文件操作operate file of java的應(yīng)用。需要的朋友參考下2013-05-05java線性表的存儲結(jié)構(gòu)及其代碼實現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)筆記第一篇,線性表的存儲結(jié)構(gòu)及其代碼實現(xiàn),具有一定的參考價值,感興趣的小伙伴們可以參考一下2017-09-09配置了jdk的環(huán)境idea卻提示找不到j(luò)dk解決辦法
在使用Java編程語言進(jìn)行開發(fā)時,IDEA是一個非常流行和強大的集成開發(fā)環(huán)境,這篇文章主要給大家介紹了關(guān)于配置了jdk的環(huán)境idea卻提示找不到j(luò)dk的解決辦法,需要的朋友可以參考下2023-12-12談?wù)凧ava中對象,類和this,super,static關(guān)鍵字的使用
對象:對象是類的一個實例,有狀態(tài)和行為。類:類是一個模板,它描述一類對象的行為和狀態(tài)。本文就來和大家聊聊Java中對象,類和關(guān)鍵字的使用,需要的可以參考一下2022-08-08