日韩av黄I国产麻豆传媒I国产91av视频在线观看I日韩一区二区三区在线看I美女国产在线I麻豆视频国产在线观看I成人黄色短片

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 >

springboot集成prometheus

發(fā)布時(shí)間:2025/3/21 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 springboot集成prometheus 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

1 Maven pom.xml引入依賴

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

2 啟動(dòng)類引入注解

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類寫需要監(jiān)控的指標(biāo),比如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 設(shè)置springboot應(yīng)用的服務(wù)名和端口,在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']

最關(guān)鍵的配置就是targets: [‘10.94.20.52:8888’],就是springboot應(yīng)用的ip和端口

注:在application.xml里設(shè)置屬性:spring.metrics.servo.enabled=false,去掉重復(fù)的metrics,不然在prometheus的控制臺(tái)的targets頁簽里,會(huì)一直顯示此endpoint為down狀態(tài)。

6 多次訪問 http://localhost:8888/mydemo/endpoint,然后在prometheus控制臺(tái)查看相關(guān)metrics信息,my_sample_counter,2個(gè)頁簽:Graph,Console

總結(jié)

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

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。