SpringBoot使用prometheus監(jiān)控的示例代碼
本文介紹SpringBoot如何使用Prometheus配合Grafana監(jiān)控。
1.關(guān)于Prometheus
Prometheus是一個(gè)根據(jù)應(yīng)用的metrics來(lái)進(jìn)行監(jiān)控的開源工具。相信很多工程都在使用它來(lái)進(jìn)行監(jiān)控,有關(guān)詳細(xì)介紹可以查看官網(wǎng):https://prometheus.io/docs/introduction/overview/。
2.有關(guān)Grafana
Grafana是一個(gè)開源監(jiān)控利器,如圖所示。
從圖中就可以看出來(lái),使用Grafana監(jiān)控很高大上,提供了很多可視化的圖標(biāo)。
官網(wǎng)地址:https://grafana.com/
3.SpringBoot使用Prometheus
3.1 依賴內(nèi)容
在SpringBoot中使用Prometheus其實(shí)很簡(jiǎn)單,不需要配置太多的東西,在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)行一些簡(jiǎ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
修改啟動(dòng)類,如下所示.
@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)目到這里就配置完成了,啟動(dòng)項(xiàng)目,訪問(wèn)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 啟動(dòng)Prometheus
啟動(dòng)Prometheus,瀏覽器訪問(wèn),查看Prometheus頁(yè)面,如圖所示。
點(diǎn)擊如圖所示位置,可以查看Prometheus監(jiān)控的應(yīng)用。
列表中UP的頁(yè)面為存活的實(shí)例,如圖所示。
也可以查看很多指數(shù),如下所示。
5.Grafana配置
啟動(dòng)Grafana,配置Prometheus數(shù)據(jù)源,這里以ID是4701的Doshboard為例(地址:https://grafana.com/dashboards/4701)如圖。
在Grafana內(nèi)點(diǎn)擊如圖所示import按鈕
在如圖所示位置填寫4701,然后點(diǎn)擊load。
接下來(lái)導(dǎo)入Doshboard。
導(dǎo)入后就可以看到我們的SpringBoot項(xiàng)目對(duì)應(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)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- 使用Prometheus+Grafana的方法監(jiān)控Springboot應(yīng)用教程詳解
- SpringBoot+Prometheus+Grafana實(shí)現(xiàn)應(yīng)用監(jiān)控和報(bào)警的詳細(xì)步驟
- springboot2.X整合prometheus監(jiān)控的實(shí)例講解
- Prometheus監(jiān)控Springboot程序的實(shí)現(xiàn)方法
- SpringBoot集成Prometheus實(shí)現(xiàn)監(jiān)控的過(guò)程
- SpringBoot使用Prometheus實(shí)現(xiàn)監(jiān)控
- 使用SpringBoot+Prometheus+Grafana實(shí)現(xiàn)可視化監(jiān)控
- SpringBoot使用Prometheus采集自定義指標(biāo)數(shù)據(jù)的方法詳解
- SpringBoot集成 Prometheus進(jìn)行高效監(jiān)控的實(shí)現(xiàn)
相關(guān)文章
Java 異步編程實(shí)踐_動(dòng)力節(jié)點(diǎn)Java學(xué)院整理
異步編程提供了一個(gè)非阻塞的,事件驅(qū)動(dòng)的編程模型。下面通過(guò)本文給大家介紹Java 異步編程實(shí)踐,感興趣的的朋友一起看看吧2017-05-05Java DriverManager.getConnection()獲取數(shù)據(jù)庫(kù)連接
這篇文章主要介紹了Java DriverManager.getConnection()獲取數(shù)據(jù)庫(kù)連接,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01深入了解Java核心類庫(kù)--Date,Calendar,DateFormat類
這篇文章主要為大家詳細(xì)介紹了javaDate,Calendar,DateFormat類定義與使用的相關(guān)資料,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能給你帶來(lái)幫助2021-07-07區(qū)塊鏈常用數(shù)據(jù)庫(kù)leveldb用java來(lái)實(shí)現(xiàn)常規(guī)操作的方法
這篇文章主要介紹了區(qū)塊鏈常用數(shù)據(jù)庫(kù)leveldb用java來(lái)實(shí)現(xiàn)常規(guī)操作,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02java Hibernate save()與persist()區(qū)別
本文章來(lái)給各位同學(xué)介紹一下Hibernate save()與persist()區(qū)別,希望此文章能對(duì)各位同學(xué)對(duì)于Hibernate save()與persist()有所理解2016-01-01Spring動(dòng)態(tài)注冊(cè)多數(shù)據(jù)源的實(shí)現(xiàn)方法
這篇文章主要介紹了Spring動(dòng)態(tài)注冊(cè)多數(shù)據(jù)源的實(shí)現(xiàn)方法,小編覺的挺不錯(cuò)的,現(xiàn)分享到腳本之家平臺(tái),需要的朋友可以參考下2018-01-01