springboot集成普羅米修斯(Prometheus)的方法
Prometheus 是一套開源的系統(tǒng)監(jiān)控報(bào)警框架。它由工作在 SoundCloud 的 員工創(chuàng)建,并在 2015 年正式發(fā)布的開源項(xiàng)目。2016 年,Prometheus 正式加入 Cloud Native Computing Foundation,非常的受歡迎。
簡介
Prometheus 具有以下特點(diǎn):
- 一個(gè)多維數(shù)據(jù)模型,其中包含通過度量標(biāo)準(zhǔn)名稱和鍵/值對(duì)標(biāo)識(shí)的時(shí)間序列數(shù)據(jù)
- PromQL,一種靈活的查詢語言,可利用此維度
- 不依賴分布式存儲(chǔ); 單服務(wù)器節(jié)點(diǎn)是自治的
- 時(shí)間序列收集通過HTTP上的拉模型進(jìn)行
- 通過中間網(wǎng)關(guān)支持推送時(shí)間序列
- 通過服務(wù)發(fā)現(xiàn)或靜態(tài)配置發(fā)現(xiàn)目標(biāo)
- 多種圖形和儀表板支持模式
Prometheus 組成及架構(gòu)
聲明:該小節(jié)參考了文章[Prometheus 入門與實(shí)踐]
Prometheus 生態(tài)圈中包含了多個(gè)組件,其中許多組件是可選的:
- Prometheus Server: 用于收集和存儲(chǔ)時(shí)間序列數(shù)據(jù)。
- Client Library: 客戶端庫,為需要監(jiān)控的服務(wù)生成相應(yīng)的 metrics 并暴露給 Prometheus server。當(dāng) Prometheus server 來 pull 時(shí),直接返回實(shí)時(shí)狀態(tài)的 metrics。
- Push Gateway: 主要用于短期的 jobs。由于這類 jobs 存在時(shí)間較短,可能在 Prometheus 來 pull 之前就消失了。為此,這次 jobs 可以直接向 Prometheus server 端推送它們的 metrics。這種方式主要用于服務(wù)層面的 metrics,對(duì)于機(jī)器層面的 metrices,需要使用 node exporter。
- Exporters: 用于暴露已有的第三方服務(wù)的 metrics 給 Prometheus。
- Alertmanager: 從 Prometheus server 端接收到 alerts 后,會(huì)進(jìn)行去除重復(fù)數(shù)據(jù),分組,并路由到對(duì)收的接受方式,發(fā)出報(bào)警。常見的接收方式有:電子郵件,pagerduty,OpsGenie, webhook 等。
一些其他的工具。
其大概的工作流程是:
1.Prometheus server 定期從配置好的 jobs 或者 exporters 中拉 metrics,或者接收來自 Pushgateway 發(fā)過來的 metrics,或者從其他的 Prometheus server 中拉 metrics。
2.Prometheus server 在本地存儲(chǔ)收集到的 metrics,并運(yùn)行已定義好的 alert.rules,記錄新的時(shí)間序列或者向 Alertmanager 推送警報(bào)。
3.Alertmanager 根據(jù)配置文件,對(duì)接收到的警報(bào)進(jìn)行處理,發(fā)出告警。
4.在圖形界面中,可視化采集數(shù)據(jù)。
springboot 集成prometheus
在spring boot工程中引入actuator的起步依賴,以及micrometer-registry-prometheus的依賴。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>
暴露prometheus的接口;暴露metrics.tags,和spring.application.name一致。
server: port: 8081 spring: application: name: my-prometheus management: endpoints: web: exposure: include: 'prometheus' metrics: tags: application: ${spring.application.name}
寫一個(gè)API接口,用作測(cè)試。代碼如下:
@RestController public class TestController { Logger logger = LoggerFactory.getLogger(TestController.class); @GetMapping("/test") public String test() { logger.info("test"); return "ok"; } @GetMapping("") public String home() { logger.info("home"); return "ok"; } }
在瀏覽器上訪問http://localhost:8081/actuator/prometheus,展示的信息如下,這些信息都是actuator的一些監(jiān)控信息。
# 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="my-prometheus",} 2.863661056E9 # HELP http_server_requests_seconds # TYPE http_server_requests_seconds summary http_server_requests_seconds_count{application="my-prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 1.0 http_server_requests_seconds_sum{application="my-prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.018082327 # HELP http_server_requests_seconds_max # TYPE http_server_requests_seconds_max gauge http_server_requests_seconds_max{application="my-prometheus",exception="None",method="GET",outcome="CLIENT_ERROR",status="404",uri="/**",} 0.018082327 # HELP jvm_threads_states_threads The current number of threads having NEW state # TYPE jvm_threads_states_threads gauge jvm_threads_states_threads{application="my-prometheus",state="waiting",} 12.0 jvm_threads_states_threads{application="my-prometheus",state="runnable",} 8.0 jvm_threads_states_threads{application="my-prometheus",state="timed-waiting",} 2.0 jvm_threads_states_threads{application="my-prometheus",state="terminated",} 0.0 jvm_threads_states_threads{application="my-prometheus",state="blocked",} 0.0 jvm_threads_states_threads{application="my-prometheus",state="new",} 0.0 # HELP process_files_open_files The open file descriptor count # TYPE process_files_open_files gauge ...省略更多
安裝Prometheus
安裝Prometheus很簡單,在linux系統(tǒng)上安裝,執(zhí)行以下的安裝命令。其他的操作系統(tǒng),比如windows、mac等在官網(wǎng)上(https://prometheus.io/download/)下載并安裝。
wget https://github.com/prometheus/prometheus/releases/download/v2.19.2/prometheus-2.19.2.darwin-amd64.tar.gz tar xvfz prometheus-*.tar.gz cd prometheus-*
修改Prometheus的配置文件prometheus.yml,代碼如下:
global: scrape_interval: 15s # By default, scrape targets every 15 seconds. # Attach these labels to any time series or alerts when communicating with # external systems (federation, remote storage, Alertmanager). external_labels: monitor: 'codelab-monitor' # 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' # Override the global default and scrape targets from this job every 5 seconds. scrape_interval: 5s static_configs: - targets: ['localhost:9090'] - job_name: 'springboot_prometheus' scrape_interval: 5s metrics_path: '/actuator/prometheus' static_configs: - targets: ['127.0.0.1:8081']
- config.job_name,配置job的名稱
- config.scrape_interval,配置多久抓一次監(jiān)控信息
- config.metrics_path,獲取監(jiān)控信息的接口
- config.static_configs.targets配置獲取監(jiān)控信息的地址。
使用以下的命令啟動(dòng)prometheus,并通過–config.file指定配置文件
./prometheus --config.file=prometheus.yml
多次請(qǐng)求springboot項(xiàng)目的接口http://localhost:8081/test , 并訪問prometheus的控制臺(tái)http://localhost:9090/,展示的界面如下:
prometheus提供了一些可視化圖,比如使用柱狀圖來展示每秒請(qǐng)求數(shù):
安裝grafana
grafana 是一款采用 go 語言編寫的開源應(yīng)用,主要用于大規(guī)模指標(biāo)數(shù)據(jù)的可視化展現(xiàn),是網(wǎng)絡(luò)架構(gòu)和應(yīng)用分析中最流行的時(shí)序數(shù)據(jù)展示工具,目前已經(jīng)支持絕大部分常用的時(shí)序數(shù)據(jù)庫。使用grafana去展示prometheus上的數(shù)據(jù)。先安裝,安裝命令如下:
wget https://dl.grafana.com/oss/release/grafana-7.0.4.darwin-amd64.tar.gz tar -zxvf grafana-7.0.4.darwin-amd64.tar.gz ./grafana-server
訪問http://localhost:3000/,初始密碼:admin/admin。
配置數(shù)據(jù)源,如圖:
配置prometheus的地址:http://localhost:9090 ,如圖所示:
在dashboard界面新建panel,展示的metrics為http_server_request_seconds_count,即展示了以時(shí)間為橫軸,請(qǐng)求數(shù)為縱軸的請(qǐng)求曲線,如圖所示:
參考資料
[Prometheus 入門與實(shí)踐]
到此這篇關(guān)于springboot集成普羅米修斯(Prometheus)的方法的文章就介紹到這了,更多相關(guān)springboot集成普羅米修斯內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot使用Prometheus采集自定義指標(biāo)數(shù)據(jù)的方法詳解
- Springboot搭建JVM監(jiān)控(Springboot + Prometheus + Grafana)
- SpringBoot 使用Prometheus采集自定義指標(biāo)數(shù)據(jù)的方案
- springboot2.X整合prometheus監(jiān)控的實(shí)例講解
- SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟
- Prometheus 入門教程之SpringBoot 實(shí)現(xiàn)自定義指標(biāo)監(jiān)控
- 使用Prometheus+Grafana的方法監(jiān)控Springboot應(yīng)用教程詳解
- springboot整合prometheus實(shí)現(xiàn)資源監(jiān)控的詳細(xì)步驟
相關(guān)文章
Java中json格式化BigDecimal保留2位小數(shù)
這篇文章主要給大家介紹了關(guān)于Java中json格式化BigDecimal保留2位小數(shù)的相關(guān)資料,BigDecimal是Java中的一個(gè)數(shù)學(xué)庫,可以實(shí)現(xiàn)高精度計(jì)算,文中給出了詳細(xì)的代碼實(shí)例,需要的朋友可以參考下2023-09-09詳解JAVA中的Collection接口和其主要實(shí)現(xiàn)的類
這篇文章主要介紹了JAVA中的Collection接口和其主要實(shí)現(xiàn)的類,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-03-03Spring Security實(shí)現(xiàn)驗(yàn)證碼登錄功能
這篇文章主要介紹了Spring Security實(shí)現(xiàn)驗(yàn)證碼登錄功能,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-01-01SpringBoot整合Mybatis-plus實(shí)現(xiàn)多級(jí)評(píng)論功能
本文介紹了如何使用SpringBoot整合Mybatis-plus實(shí)現(xiàn)多級(jí)評(píng)論功能,同時(shí)提供了數(shù)據(jù)庫的設(shè)計(jì)和詳細(xì)的后端代碼,前端界面使用的Vue2,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2023-05-05java8 多個(gè)list對(duì)象用lambda求差集操作
這篇文章主要介紹了java8 多個(gè)list對(duì)象用lambda求差集操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2020-09-09