SpringBoot使用prometheus監(jiān)控的示例代碼
本文介紹SpringBoot如何使用Prometheus配合Grafana監(jiān)控。
1.關(guān)于Prometheus
Prometheus是一個根據(jù)應(yīng)用的metrics來進(jìn)行監(jiān)控的開源工具。相信很多工程都在使用它來進(jìn)行監(jiān)控,有關(guān)詳細(xì)介紹可以查看官網(wǎng):https://prometheus.io/docs/introduction/overview/。
2.有關(guān)Grafana
Grafana是一個開源監(jiān)控利器,如圖所示。

從圖中就可以看出來,使用Grafana監(jiān)控很高大上,提供了很多可視化的圖標(biāo)。
官網(wǎng)地址:https://grafana.com/
3.SpringBoot使用Prometheus
3.1 依賴內(nèi)容
在SpringBoot中使用Prometheus其實(shí)很簡單,不需要配置太多的東西,在pom文件中加入依賴,完整內(nèi)容如下所示。
<?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 配置文件
配置文件中加入配置,這里就只進(jìn)行一些簡單配置,management.metrics.tags.application屬性是本文配合Grafana的Dashboard設(shè)置的,如下所示:
spring.application.name=springboot_prometheus
management.endpoints.web.exposure.include=*
management.metrics.tags.application=${spring.application.name}
3.3 設(shè)置application
修改啟動類,如下所示.
@SpringBootApplication
public class Springboot2PrometheusApplication {
public static void main(String[] args) {
SpringApplication.run(Springboot2PrometheusApplication.class, args);
}
@Bean
MeterRegistryCustomizer<MeterRegistry> configurer(
@Value("${spring.application.name}") String applicationName) {
return (registry) -> registry.config().commonTags("application", applicationName);
}
}
SpringBoot項(xiàng)目到這里就配置完成了,啟動項(xiàng)目,訪問http://localhost:8080/actuator/prometheus,如圖所示,可以看到一些度量指標(biāo)。

4.Prometheus配置
4.1 配置應(yīng)用
在prometheus配置監(jiān)控我們的SpringBoot應(yīng)用,完整配置如下所示。
# my global config global: 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 configuration alerting: 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'] ###以下內(nèi)容為SpringBoot應(yīng)用配置 - job_name: 'springboot_prometheus' scrape_interval: 5s metrics_path: '/actuator/prometheus' static_configs: - targets: ['127.0.0.1:8080']
4.2 啟動Prometheus
啟動Prometheus,瀏覽器訪問,查看Prometheus頁面,如圖所示。

點(diǎn)擊如圖所示位置,可以查看Prometheus監(jiān)控的應(yīng)用。

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

也可以查看很多指數(shù),如下所示。

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

在Grafana內(nèi)點(diǎn)擊如圖所示import按鈕

在如圖所示位置填寫4701,然后點(diǎn)擊load。

接下來導(dǎo)入Doshboard。

導(dǎo)入后就可以看到我們的SpringBoot項(xiàng)目對應(yīng)的指標(biāo)圖表了,如圖。

6.源碼
源碼地址:https://gitee.com/dalaoyang/springboot_learn/tree/master/springboot2_prometheus
到此這篇關(guān)于SpringBoot使用prometheus監(jiān)控的示例代碼的文章就介紹到這了,更多相關(guān)SpringBoot prometheus監(jiān)控內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- SpringBoot整合Prometheus如何實(shí)現(xiàn)資源監(jiān)控
- springboot整合prometheus實(shí)現(xiàn)資源監(jiān)控的詳細(xì)步驟
- SpringBoot集成 Prometheus進(jìn)行高效監(jiān)控的實(shí)現(xiàn)
- SpringBoot使用Prometheus實(shí)現(xiàn)監(jiān)控
- SpringBoot集成Prometheus實(shí)現(xiàn)監(jiān)控的過程
- Prometheus監(jiān)控Springboot程序的實(shí)現(xiàn)方法
- SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報警的詳細(xì)步驟
- SpringBoot Admin與Prometheus集成監(jiān)控
相關(guān)文章
Jmeter壓力測試簡單教程(包括服務(wù)器狀態(tài)監(jiān)控)
Jmeter是一個非常好用的壓力測試工具。Jmeter用來做輕量級的壓力測試,非常合適,本文詳細(xì)的介紹了Jmeter的使用,感性的可以了解一下2021-11-11
Spring?Data?JPA實(shí)現(xiàn)審計(jì)功能過程詳解
Spring?Data?JPA為跟蹤持久性層的變化提供了很好的支持。通過使用審核,我們可以存儲或記錄有關(guān)實(shí)體更改的信息,例如誰創(chuàng)建或更改了實(shí)體以及何時進(jìn)行更改2023-02-02
FastJson時間格式化問題避坑經(jīng)驗(yàn)分享
這篇文章主要為大家介紹了FastJson時間格式化問題避坑經(jīng)驗(yàn)分享,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08
Java實(shí)現(xiàn)文件名倒序排序的技術(shù)指南
在實(shí)際開發(fā)過程中,我們經(jīng)常需要對文件進(jìn)行操作和處理,一個常見的需求是按文件名倒序排列文件列表,以便于文件的管理和查找,本文將介紹如何在Java中實(shí)現(xiàn)文件名倒序排序,并提供詳細(xì)的代碼案例,需要的朋友可以參考下2024-08-08
JVM分配和回收堆外內(nèi)存的方式與注意點(diǎn)
JVM啟動時分配的內(nèi)存稱為堆內(nèi)存,與之相對的,在代碼中還可以使用堆外內(nèi)存,比如Netty,廣泛使用了堆外內(nèi)存,下面這篇文章主要給大家介紹了關(guān)于JVM分配和回收堆外內(nèi)存的方式與注意點(diǎn),需要的朋友可以參考下2022-07-07
Java并發(fā)編程之關(guān)鍵字volatile的深入解析
提高java的并發(fā)編程,就不得不提volatile關(guān)鍵字,不管是在面試還是實(shí)際開發(fā)中volatile都是一個應(yīng)該掌握的技能,這篇文章主要給大家介紹了關(guān)于Java并發(fā)編程之關(guān)鍵字volatile的相關(guān)資料,需要的朋友可以參考下2021-09-09

