日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

springboot集成prometheus

發布時間:2025/3/21 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot集成prometheus 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

1 Maven pom.xml引入依賴

<dependency><groupId>io.prometheus</groupId><artifactId>simpleclient_spring_boot</artifactId> </dependency>

2 啟動類引入注解

import io.prometheus.client.spring.boot.EnablePrometheusEndpoint; import io.prometheus.client.spring.boot.EnableSpringBootMetricsCollector; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication @EnablePrometheusEndpoint @EnableSpringBootMetricsCollector public class Application {public static void main(String[] args) {SpringApplication.run(Application.class, args);}

3 Controller類寫需要監控的指標,比如Counter

import io.prometheus.client.Counter; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import java.util.Random;@RestController public class SampleController {private static Random random = new Random();private static final Counter requestTotal = Counter.build().name("my_sample_counter").labelNames("status").help("A simple Counter to illustrate custom Counters in Spring Boot and Prometheus").register();@RequestMapping("/endpoint")public void endpoint() {if (random.nextInt(2) > 0) {requestTotal.labels("success").inc();} else {requestTotal.labels("error").inc();}} }

4 設置springboot應用的服務名和端口,在application.properties

spring.application.name=mydemo server.port=8888

5 配置prometheus.yml

global:scrape_interval: 15s # By default, scrape targets every 15 seconds.evaluation_interval: 15s # By default, scrape targets every 15 seconds.# scrape_timeout is set to the global default (10s).# Attach these labels to any time series or alerts when communicating with# external systems (federation, remote storage, Alertmanager).external_labels:monitor: 'codelab-monitor'# Load rules once and periodically evaluate them according to the global 'evaluation_interval'. rule_files:# - "first.rules"# - "second.rules"# 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# metrics_path defaults to '/metrics'# scheme defaults to 'http'.static_configs:- targets: ['localhost:9090']- job_name: 'mydemo'# Override the global default and scrape targets from this job every 5 seconds.scrape_interval: 5smetrics_path: '/prometheus'# scheme defaults to 'http'.static_configs:- targets: ['10.94.20.52:8888']

最關鍵的配置就是targets: [‘10.94.20.52:8888’],就是springboot應用的ip和端口

注:在application.xml里設置屬性:spring.metrics.servo.enabled=false,去掉重復的metrics,不然在prometheus的控制臺的targets頁簽里,會一直顯示此endpoint為down狀態。

6 多次訪問 http://localhost:8888/mydemo/endpoint,然后在prometheus控制臺查看相關metrics信息,my_sample_counter,2個頁簽:Graph,Console

總結

以上是生活随笔為你收集整理的springboot集成prometheus的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。