av一区二区在线观看_亚洲男人的天堂网站_日韩亚洲视频_在线成人免费_欧美日韩精品免费观看视频_久草视

您的位置:首頁技術文章
文章詳情頁

SpringBoot使用prometheus監控的示例代碼

瀏覽:104日期:2023-03-20 17:09:09

本文介紹SpringBoot如何使用Prometheus配合Grafana監控。

1.關于Prometheus

Prometheus是一個根據應用的metrics來進行監控的開源工具。相信很多工程都在使用它來進行監控,有關詳細介紹可以查看官網:https://prometheus.io/docs/introduction/overview/。

2.有關Grafana

Grafana是一個開源監控利器,如圖所示。

SpringBoot使用prometheus監控的示例代碼

從圖中就可以看出來,使用Grafana監控很高大上,提供了很多可視化的圖標。

官網地址:https://grafana.com/

3.SpringBoot使用Prometheus3.1 依賴內容

在SpringBoot中使用Prometheus其實很簡單,不需要配置太多的東西,在pom文件中加入依賴,完整內容如下所示。

<?xml version='1.0' encoding='UTF-8'?><project xmlns='http://maven.apache.org/POM/4.0.0' xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'xsi:schemaLocation='http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd'><modelVersion>4.0.0</modelVersion><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.1.3.RELEASE</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.dalaoyang</groupId><artifactId>springboot2_prometheus</artifactId><version>0.0.1-SNAPSHOT</version><name>springboot2_prometheus</name><description>springboot2_prometheus</description><properties><java.version>1.8</java.version></properties><dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency><dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId><version>1.1.3</version></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>3.2 配置文件

配置文件中加入配置,這里就只進行一些簡單配置,management.metrics.tags.application屬性是本文配合Grafana的Dashboard設置的,如下所示:

spring.application.name=springboot_prometheusmanagement.endpoints.web.exposure.include=*management.metrics.tags.application=${spring.application.name}3.3 設置application

修改啟動類,如下所示.

@SpringBootApplicationpublic class Springboot2PrometheusApplication {public static void main(String[] args) {SpringApplication.run(Springboot2PrometheusApplication.class, args);}@BeanMeterRegistryCustomizer<MeterRegistry> configurer(@Value('${spring.application.name}') String applicationName) {return (registry) -> registry.config().commonTags('application', applicationName);}}

SpringBoot項目到這里就配置完成了,啟動項目,訪問http://localhost:8080/actuator/prometheus,如圖所示,可以看到一些度量指標。

SpringBoot使用prometheus監控的示例代碼

4.Prometheus配置4.1 配置應用

在prometheus配置監控我們的SpringBoot應用,完整配置如下所示。

# my global configglobal: 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 configurationalerting: 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: - job_name: ’prometheus’ static_configs: - targets: [’127.0.0.1:9090’]###以下內容為SpringBoot應用配置 - job_name: ’springboot_prometheus’ scrape_interval: 5s metrics_path: ’/actuator/prometheus’ static_configs: - targets: [’127.0.0.1:8080’]4.2 啟動Prometheus

啟動Prometheus,瀏覽器訪問,查看Prometheus頁面,如圖所示。

SpringBoot使用prometheus監控的示例代碼

點擊如圖所示位置,可以查看Prometheus監控的應用。

SpringBoot使用prometheus監控的示例代碼

列表中UP的頁面為存活的實例,如圖所示。

SpringBoot使用prometheus監控的示例代碼

也可以查看很多指數,如下所示。

SpringBoot使用prometheus監控的示例代碼

5.Grafana配置

啟動Grafana,配置Prometheus數據源,這里以ID是4701的Doshboard為例(地址:https://grafana.com/dashboards/4701)如圖。

SpringBoot使用prometheus監控的示例代碼

在Grafana內點擊如圖所示import按鈕

SpringBoot使用prometheus監控的示例代碼

在如圖所示位置填寫4701,然后點擊load。

SpringBoot使用prometheus監控的示例代碼

接下來導入Doshboard。

SpringBoot使用prometheus監控的示例代碼

導入后就可以看到我們的SpringBoot項目對應的指標圖表了,如圖。

SpringBoot使用prometheus監控的示例代碼

6.源碼

源碼地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_prometheus

到此這篇關于SpringBoot使用prometheus監控的示例代碼的文章就介紹到這了,更多相關SpringBoot prometheus監控內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: Spring
相關文章:
主站蜘蛛池模板: 久久视频免费在线观看 | 丁香激情五月 | 国产福利小视频 | 欧美精品在线观看 | 国产一区二区三区精品视频 | 国产a视频 | 亚洲91精品 | 国产在线观看不卡 | 9.1成人看片| 四虎在线免费观看视频 | 黄色大片在线 | 黄色免费网站视频 | 欧美性生交xxxxx久久久 | 免费看片黄色 | 成人深夜福利视频 | 精品久久一区二区 | 99av| 国产麻豆91 | 黄视频在线播放 | av黄色网 | 亚洲二区在线 | 欧美精品黄色 | 国产一区二区三区在线看 | 欧美综合在线视频 | 成年视频在线观看 | 成人免费网站黄 | 国产区在线观看 | 国产午夜精品久久 | 日韩欧美黄色 | 谁有毛片网站 | 免费a视频 | 国产7777 | 一区二区三区在线免费 | 中文字幕免费观看 | 欧美激情视频一区二区三区 | 中文字幕婷婷 | www.日韩在线 | 日韩欧美在线一区 | 婷婷久久久| 亚洲成人中文字幕 | 在线色网 |