[英]How to inject Prometheus metrics bean in Spring Boot Application?
[英]Prometheus UI, Grafana settings for spring boot application
我正在使用 spring boot 2.0 並在 POM 中添加了以下依賴項
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
我的 application.yml 看起來像這樣
management.endpoints.web.base-path = /manage management.endpoints.web.exposure.include = "*" endpoints.prometheus.enabled = true
當我訪問 Prometheus 時
本地主機/管理/普羅米修斯
我能夠看到所有指標。
接下來,我的目標是在 Prometheus UI 中查看上述指標。 為此,我在 POM 中添加了以下依賴項
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_spring_boot</artifactId>
<version>${prometheus.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_hotspot</artifactId>
<version>${prometheus.version}</version>
</dependency>
<dependency>
<groupId>io.prometheus</groupId>
<artifactId>simpleclient_servlet</artifactId>
<version>${prometheus.version}</version>
</dependency>
在 Prometheus UI 中查看指標的下一步是什么,最終目標是將 Prometheus 集成到 Grafana。
PS:我在 google 上進行了搜索,並嘗試添加 prometheus.yml 並添加諸如 @EnablePrometheusEndpoint 之類的注釋,因為所有文章都是舊的,因此沒有任何效果。
編輯:如果 spring boot jar 托管在不同的主機(Azure/AWS)和 prometheus 服務器在不同的主機中,那么如何配置 prometheus.yml(metrics_path,targets)。
如果您使用的是 Spring Boot 2 和 micrometer,則不需要添加額外的依賴項,它們會在您添加micrometer-registry-prometheus
時導入。 如果您能夠在 localhost/manage/prometheus 上看到指標,那么您在 spring-boot 端的配置就可以了。 無需再配置任何東西。
要查看 Prometheus 中的指標,您需要:
scrape_configs: - job_name: 'mySpringBoot' metrics_path: '/manage/prometheus' static_configs: - targets: ['springBootHost:springBootPort']
UP
- http://localhost:9090/targets (假設 Prometheus 在 localhost 上運行)DOWN
,則說明存在配置或網絡問題。以下步驟很簡單,其他地方有很多文檔:
Spring boot api 應用程序在 windows 上配置了 prometheus 和 grafana。
- 創建 spring boot 應用程序並在 pom.xml 中添加依賴項 -
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-core</artifactId>
</dependency>
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
如果它正在運行,請轉到您的瀏覽器並輸入“ http://localhost:8080/actuator/prometheus ”,您將獲得所有指標。
下載並安裝 Prometheus Server https://prometheus.io/download/並解壓 zip 並運行 prometheus exe。
為此,您需要通過添加新作業來修改 prometheus.yml 文件(不要忘記在更改 yml 文件后重新啟動 Prometheus)
注意:使用 prometheus 配置 spring 應用程序。
scrape_configs: - job_name: 'SpringBootApplicationName metrics_path: ‘actuator-prometheus' static_configs: - targets: [‘IPADDRESS:springBootApplicationPort]
配置完成后,轉到 Prometheus UI,檢查目標是否為UP
- http://localhost:9090/targets (假設 Prometheus 在 localhost 上運行)
轉到“ http://localhost:8080/actuator/prometheus ”並選擇一個指標並粘貼 - http://localhost:9090/graph並單擊執行。 例如,您可以選擇一種 CPU 和 HTTP 指標,例如 - 'http_server_request_seconds_max'
以下步驟很簡單,其他地方有很多文檔:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.