Springboot搭建JVM監(jiān)控(Springboot + Prometheus + Grafana)
背景
由于項(xiàng)目之前在生產(chǎn)環(huán)境出現(xiàn)過(guò)OOM的問(wèn)題,并且沒(méi)有及時(shí)發(fā)現(xiàn),導(dǎo)致生產(chǎn)環(huán)境出現(xiàn)了在一定時(shí)間內(nèi)不可用的情況,故決定搭建JVM監(jiān)控對(duì)微服務(wù)24小時(shí)監(jiān)聽(tīng),以便于出現(xiàn)問(wèn)題能夠及時(shí)通知相關(guān)人員進(jìn)行服務(wù)降級(jí)或解決問(wèn)題。
監(jiān)控平臺(tái)的選擇
經(jīng)過(guò)可行性分析,得到目前較為適合的微服務(wù)監(jiān)控為Springboot Admin或者Prometheus,兩者的主要區(qū)別如下:
框架 | 可監(jiān)控對(duì)象 | 是否支持集群 |
---|---|---|
Springboot Admin | Springboot微服務(wù) | 是 |
Prometheus | 開(kāi)源監(jiān)控,不僅僅能夠監(jiān)控微服務(wù) | 是 |
考慮到未來(lái)還會(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舉例,未來(lái)正式部署會(huì)補(bǔ)充linux的)。
安裝包解壓后的目錄如下圖所示,其中,prometheus.yml是Prometheus啟動(dòng)時(shí)讀取的配置文件,有關(guān)監(jiān)聽(tīng)配置(scrape_configs)全部寫(xiě)在此文件中,關(guān)于監(jiān)聽(tīng)配置本文將在微服務(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)聽(tīng)數(shù)據(jù)源來(lái)自于Springboot的actuator,但是1.X版本和2.X版本的監(jiān)聽(tīng)配置還是有一些區(qū)別的,本文將分別以Springboot 1.4.0和Springboot 2.3.7為例,闡述監(jiān)聽(tīng)配置過(guò)程
Springboot 1.4.0監(jiān)聽(tīng)配置
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)過(guò)多次嘗試發(fā)現(xiàn),Springboot1.4.0可使用Prometheus最高版本為1.0.9,再高版本服務(wù)無(wú)法啟動(dòng)。spring-legacy版本與Prometheus版本對(duì)應(yīng)即可,yml文件新增配置如下:
server: port: 8001 #服務(wù)端口,若management沒(méi)有配置監(jiān)聽(tīng)端口,則默認(rèn)采用服務(wù)端口作為監(jiān)聽(tīng)端口 spring: application: name: apollo-monitor management: context-path: /actuator #監(jiān)聽(tīng)服務(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: '*' #開(kāi)放所有端口
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)聽(tīng)配置
對(duì)于Springboot2.X版本來(lái)說(shuō),Prometheus的接入可友好太多了,Metrics可以直接配置在yml文件中,并且無(wú)需單獨(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依賴無(wú)需指定版本。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: '*' #開(kāi)放所有端口
Prometheus配置微服務(wù)注冊(cè)
完成Springboot的監(jiān)聽(tīng)基本配置后,需要將其以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)聽(tīng)路徑,一般為 “/actuator/實(shí)例名”的形式 |
static_configs | 靜態(tài)配置目錄 |
targets | 監(jiān)聽(tīng)目標(biāo)地址 |
完成Prometheus的配置后,便可以啟動(dòng)Springboot服務(wù)和Prometheus,驗(yàn)證以上配置是否正確。
Prometheus啟動(dòng)器在{Prometheus}/bin目錄下,prometheus.exe,啟動(dòng)后的界面如下圖所示:
兩者均啟動(dòng)后,可訪問(wèn)Prometheus平臺(tái)頁(yè)面查看服務(wù)的注冊(cè)情況。
默認(rèn)地址為:http://127.0.0.1:9090/進(jìn)入頁(yè)面后,Status -> Target,結(jié)果如下圖所示,可以看到服務(wù)名為apollo-monitor的服務(wù)已經(jīng)注冊(cè)在Prometheus上,說(shuō)明Prometheus已經(jīng)在對(duì)其進(jìn)行實(shí)時(shí)監(jiān)聽(tīng)。
確認(rèn)服務(wù)注冊(cè)成功后,可訪問(wèn)127.0.0.1:serverport/actuator/prometheus頁(yè)面,查看服務(wù)是否報(bào)送相關(guān)可監(jiān)控?cái)?shù)據(jù),如下圖所示,監(jiān)控?cái)?shù)據(jù)已經(jīng)全部報(bào)送,只是可讀性非常差,無(wú)法在短時(shí)間內(nèi)獲取到有效的監(jiān)控信息,這時(shí)便需要Grafana將監(jiān)控?cái)?shù)據(jù)進(jìn)行統(tǒng)計(jì)、分類與可視化。
Prometheus接入Grafana
Grafana
Grafana 是一跨平臺(tái)的開(kā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等熱門(mén)應(yīng)用。
Grafana下載與安裝
官網(wǎng)下載:Grafana官網(wǎng)
如下圖所示,進(jìn)入下載頁(yè)面后,選擇對(duì)應(yīng)的操作系統(tǒng),本文以windows舉例,點(diǎn)擊下載按鈕即可下載安裝包,下載后本地直接安裝即可。
安裝后Grafana是默認(rèn)啟動(dòng)的,端口是3000,我們可直接訪問(wèn)頁(yè)面: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可去官方倉(cāng)庫(kù)查詢,可先訪問(wèn)Grafana官方倉(cāng)庫(kù):Grafana模板倉(cāng)庫(kù)地址,搜索欄輸入“JVM”,結(jié)果中第一個(gè)就是我們想要的模板,如下圖所示:
點(diǎn)擊后進(jìn)入模板詳情頁(yè)面,如下圖所示,兩種方式都可以,分別對(duì)應(yīng)Grafana兩種導(dǎo)入方式即可,本文以ID舉例。
獲取到ID后,回到Grafana主頁(yè)面,點(diǎn)擊DataSource新建數(shù)據(jù)源,選擇Prometheus,填入之前搭好的Prometheus監(jiān)控地址,Save即可。接下來(lái)是最后一步,導(dǎo)入監(jiān)控面板,Home頁(yè)面選擇Import Dashboard,其中A方式為Json導(dǎo)入方式,B方式為ID導(dǎo)入方式,分別對(duì)應(yīng)Grafana倉(cāng)庫(kù)的兩種模板下載方式,本文選擇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)營(yíng)(如ip、登錄端、API調(diào)用與耗時(shí)等多維度、多方面監(jiān)控)、運(yùn)維相關(guān)(服務(wù)器監(jiān)控、慢SQL監(jiān)控)的統(tǒng)計(jì),并且,微服務(wù)接入方式也值得深入研究,盡可能地使得業(yè)務(wù)無(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)文章
SpringAOP核心對(duì)象的創(chuàng)建圖解
這篇文章主要介紹了SpringAOP核心對(duì)象的創(chuàng)建詳解,通過(guò)使用AOP,我們可以將橫切關(guān)注點(diǎn)(如日志記錄、性能監(jiān)控、事務(wù)管理等)從業(yè)務(wù)邏輯中分離出來(lái),使得代碼更加模塊化、可維護(hù)性更高,需要的朋友可以參考下2023-10-10springboot 2.0 mybatis mapper-locations掃描多個(gè)路徑的實(shí)現(xiàn)
這篇文章主要介紹了springboot 2.0 mybatis mapper-locations掃描多個(gè)路徑的實(shí)現(xiàn)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-07-07基于java file 文件操作operate file of java的應(yīng)用
本篇文章介紹了,基于java file 文件操作operate file of java的應(yīng)用。需要的朋友參考下2013-05-05java線性表的存儲(chǔ)結(jié)構(gòu)及其代碼實(shí)現(xiàn)
這篇文章主要為大家詳細(xì)介紹了Java數(shù)據(jù)結(jié)構(gòu)學(xué)習(xí)筆記第一篇,線性表的存儲(chǔ)結(jié)構(gòu)及其代碼實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-09-09配置了jdk的環(huán)境idea卻提示找不到j(luò)dk解決辦法
在使用Java編程語(yǔ)言進(jìn)行開(kāi)發(fā)時(shí),IDEA是一個(gè)非常流行和強(qiáng)大的集成開(kāi)發(fā)環(huán)境,這篇文章主要給大家介紹了關(guān)于配置了jdk的環(huán)境idea卻提示找不到j(luò)dk的解決辦法,需要的朋友可以參考下2023-12-12談?wù)凧ava中對(duì)象,類和this,super,static關(guān)鍵字的使用
對(duì)象:對(duì)象是類的一個(gè)實(shí)例,有狀態(tài)和行為。類:類是一個(gè)模板,它描述一類對(duì)象的行為和狀態(tài)。本文就來(lái)和大家聊聊Java中對(duì)象,類和關(guān)鍵字的使用,需要的可以參考一下2022-08-08