SpringBoot集成Prometheus實(shí)現(xiàn)監(jiān)控的過(guò)程
一.SpringBoot配置Prometheus
- pom.xml 引入監(jiān)控以及prometheus依賴(lài)
<dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
- 自定義指標(biāo)
引入上面兩個(gè)依賴(lài)之后,SpringBoot的/actuator/prometheus路徑會(huì)默認(rèn)暴露一些指標(biāo)。
prometheus通過(guò)Http協(xié)議拉取的指標(biāo)數(shù)據(jù)格式為
指標(biāo)名 {標(biāo)簽} 值
如 jvm_memory_max_bytes{application=“blog”,area=“heap”,id=“Eden Space”,} 7.1630848E7
這個(gè)指標(biāo)的指標(biāo)名是jvm_memory_max_bytes,標(biāo)簽是 {application=“blog”,area=“heap”,id=“Eden Space”,},而指標(biāo)值是 7.1630848E7,將來(lái)使用PromQL查詢(xún)時(shí),標(biāo)簽可以起到篩選條件的作用。
除了引入依賴(lài)所提供的指標(biāo)外,還可以自定義指標(biāo)。
@SuppressWarnings("all") @Component public class MetricsCounter { private static Counter loginCounter = null; private static Counter registerCounter = null; private static AtomicInteger atomicInteger; public MetricsCounter(MeterRegistry registry) { loginCounter = registry.counter("login_nums"); registerCounter = registry.counter("register_nums"); atomicInteger = registry.gauge("ssl_expire_days", new AtomicInteger(10)); } /** * 此方法可能會(huì)被多線(xiàn)程執(zhí)行,需要考慮線(xiàn)程安全問(wèn)題 */ public synchronized static void incrLogin() { loginCounter.increment(); } public synchronized static void incrRegister() { registerCounter.increment(); } public static void updateSslExpireDays(){ atomicInteger.set(new Random().nextInt(100)); } }
通過(guò)拿到MeterRegistry 自定義指標(biāo),這里定義了兩種類(lèi)型的指標(biāo),一種是Counter 計(jì)數(shù)器,值只增不減,一種是gauge,gauge類(lèi)型可以隨意修改。
2.編寫(xiě)一個(gè)接口,改變指標(biāo)
@RestController public class TestRest { @GetMapping("t1") public String t1(){ MetricsCounter.incrLogin(); MetricsCounter.incrRegister(); MetricsCounter.updateSslExpireDays(); return "t1"; } }
3.訪(fǎng)問(wèn)路徑
可以看到最新的指標(biāo)值。
二 、 Prometheus 端配置
SpringBoot將指標(biāo)暴露出去后,還需要配置Prometheus 的配置文件,讓Prometheus 定時(shí)去訪(fǎng)問(wèn)路徑拉取到指標(biāo)。
# 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: "nodeExporter" # metrics_path defaults to '/metrics' # scheme defaults to 'http'. static_configs: - targets: ["192.168.240.130:9100"] #監(jiān)控自己主機(jī)上的端口 - job_name: "springboot" scrape_interval: 3s # 多久采集一次數(shù)據(jù) scrape_timeout: 3s # 采集時(shí)的超時(shí)時(shí)間 metrics_path: '/actuator/prometheus' # 采集的路徑 static_configs: # 采集服務(wù)的地址,設(shè)置成Springboot應(yīng)用所在服務(wù)器的具體地址 - targets: ["192.168.1.103:8188"]
alerting : 配置告警管理器地址
rule_files : 配置告警 規(guī)則
scrape_configs : 配置指標(biāo)抓取規(guī)則,在這個(gè)配置項(xiàng)下配置SpringBoot的指標(biāo)路徑。
- 啟動(dòng)promteus
nohup ./prometheus --config.file=./prometheus.yml &
啟動(dòng)后的端口默認(rèn)是 9090
可以在上述的input輸入框中輸入PromQL進(jìn)行對(duì)指標(biāo)的查詢(xún)。
具體的算術(shù)運(yùn)算符、關(guān)系運(yùn)算符以及內(nèi)置函數(shù)等 可參考 Prometheus官網(wǎng)。
到此SpringBoot已完成與Prometheus的整合。
到此這篇關(guān)于SpringBoot集成Prometheus實(shí)現(xiàn)監(jiān)控的文章就介紹到這了,更多相關(guān)SpringBoot Prometheus實(shí)現(xiàn)監(jiān)控內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用Prometheus+Grafana的方法監(jiān)控Springboot應(yīng)用教程詳解
- SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟
- springboot2.X整合prometheus監(jiān)控的實(shí)例講解
- SpringBoot使用prometheus監(jiān)控的示例代碼
- Prometheus監(jiān)控Springboot程序的實(shí)現(xiàn)方法
- SpringBoot使用Prometheus實(shí)現(xiàn)監(jiān)控
- 使用SpringBoot+Prometheus+Grafana實(shí)現(xiàn)可視化監(jiān)控
- SpringBoot使用Prometheus采集自定義指標(biāo)數(shù)據(jù)的方法詳解
- SpringBoot集成 Prometheus進(jìn)行高效監(jiān)控的實(shí)現(xiàn)
相關(guān)文章
Spring MVC Controller返回值及異常的統(tǒng)一處理方法
這篇文章主要給大家介紹了關(guān)于Spring MVC Controller返回值及異常的統(tǒng)一處理方法,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者使用Spring MVC具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11ThreadLocal原理介紹及應(yīng)用場(chǎng)景
本文詳細(xì)講解了ThreadLocal原理介紹及應(yīng)用場(chǎng)景,對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-12-12Spring Boot Filter 過(guò)濾器的使用方式
這篇文章主要介紹了Spring Boot Filter 過(guò)濾器的使用方式,文章通過(guò)圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫(kù)的配置方法
這篇文章主要介紹了SpringBoot使用log4j2將日志記錄到文件及自定義數(shù)據(jù)庫(kù)的配置方法,本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2024-03-03SpringCloud使用Feign實(shí)現(xiàn)服務(wù)調(diào)用
這篇文章主要為大家詳細(xì)介紹了SpringCloud使用Feign實(shí)現(xiàn)服務(wù)調(diào)用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2019-04-04使用JAVA8 filter對(duì)List多條件篩選的實(shí)現(xiàn)
這篇文章主要介紹了使用JAVA8 filter對(duì)List多條件篩選的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-03-03springboot使用com.github.binarywang包實(shí)現(xiàn)微信網(wǎng)頁(yè)上的支付和退款
最近做項(xiàng)目需要實(shí)現(xiàn)在pc端需要實(shí)現(xiàn)微信的支付,本文主要介紹了springboot使用com.github.binarywang包實(shí)現(xiàn)微信網(wǎng)頁(yè)上的支付和退款,具有一定的參考價(jià)值,感興趣的可以了解一下2024-05-05idea如何為java程序添加啟動(dòng)參數(shù)
文章介紹了如何在Java程序中添加啟動(dòng)參數(shù),包括program arguments、VM arguments和Environment variables,并解釋了如何在代碼中使用System類(lèi)獲取這些參數(shù)2025-01-01MyBatis通用Mapper中的通用example(排序)詳解
這篇文章主要介紹了MyBatis通用Mapper中的通用example(排序)詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-12-12