SpringBoot使用prometheus监控的示例代码

2025-05-29 0 108

本文介绍SpringBoot如何使用Prometheus配合Grafana监控

1.关于Prometheus

Prometheus是一个根据应用的metrics来进行监控的开源工具。相信很多工程都在使用它来进行监控,有关详细介绍可以查看官网:https://prometheus.io/docs/introduction/overview/。

2.有关Grafana

Grafana是一个开源监控利器,如图所示。

SpringBoot使用prometheus监控的示例代码

从图中就可以看出来,使用Grafana监控很高大上,提供了很多可视化的图标。

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

3.SpringBoot使用Prometheus

3.1 依赖内容

SpringBoot中使用Prometheus其实很简单,不需要配置太多的东西,在pom文件中加入依赖,完整内容如下所示。

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  4. <modelVersion>4.0.0</modelVersion>
  5. <parent>
  6. <groupId>org.springframework.boot</groupId>
  7. <artifactId>spring-boot-starter-parent</artifactId>
  8. <version>2.1.3.RELEASE</version>
  9. <relativePath/> <!– lookup parent from repository –>
  10. </parent>
  11. <groupId>com.dalaoyang</groupId>
  12. <artifactId>springboot2_prometheus</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>springboot2_prometheus</name>
  15. <description>springboot2_prometheus</description>
  16. <properties>
  17. <java.version>1.8</java.version>
  18. </properties>
  19. <dependencies>
  20. <dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-actuator</artifactId>
  23. </dependency>
  24. <dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-web</artifactId>
  27. </dependency>
  28. <dependency>
  29. <groupId>org.springframework.boot</groupId>
  30. <artifactId>spring-boot-starter-test</artifactId>
  31. <scope>test</scope>
  32. </dependency>
  33. <dependency>
  34. <groupId>io.micrometer</groupId>
  35. <artifactId>micrometer-registry-prometheus</artifactId>
  36. <version>1.1.3</version>
  37. </dependency>
  38. </dependencies>
  39. <build>
  40. <plugins>
  41. <plugin>
  42. <groupId>org.springframework.boot</groupId>
  43. <artifactId>spring-boot-maven-plugin</artifactId>
  44. </plugin>
  45. </plugins>
  46. </build>
  47. </project>

3.2 配置文件

配置文件中加入配置,这里就只进行一些简单配置,management.metrics.tags.application属性是本文配合Grafana的Dashboard设置的,如下所示:

  1. spring.application.name=springboot_prometheus
  2. management.endpoints.web.exposure.include=*
  3. management.metrics.tags.application=${spring.application.name}

3.3 设置application

修改启动类,如下所示.

  1. @SpringBootApplication
  2. public class Springboot2PrometheusApplication {
  3. public static void main(String[] args) {
  4. SpringApplication.run(Springboot2PrometheusApplication.class, args);
  5. }
  6. @Bean
  7. MeterRegistryCustomizer<MeterRegistry> configurer(
  8. @Value("${spring.application.name}") String applicationName) {
  9. return (registry) -> registry.config().commonTags("application", applicationName);
  10. }
  11. }

SpringBoot项目到这里就配置完成了,启动项目,访问http://localhost:8080/actuator/prometheus,如图所示,可以看到一些度量指标。

SpringBoot使用prometheus监控的示例代码

4.Prometheus配置

4.1 配置应用

在prometheus配置监控我们的SpringBoot应用,完整配置如下所示。

  1. # my global config
  2. global:
  3. scrape_interval: 15s # Set the scrape interval to every 15 seconds. Default is every 1 minute.
  4. evaluation_interval: 15s # Evaluate rules every 15 seconds. The default is every 1 minute.
  5. # scrape_timeout is set to the global default (10s).
  6. # Alertmanager configuration
  7. alerting:
  8. alertmanagers:
  9. static_configs:
  10. targets:
  11. # – alertmanager:9093
  12. # Load rules once and periodically evaluate them according to the global 'evaluation_interval'.
  13. rule_files:
  14. # – "first_rules.yml"
  15. # – "second_rules.yml"
  16. # A scrape configuration containing exactly one endpoint to scrape:
  17. # Here it's Prometheus itself.
  18. scrape_configs:
  19. job_name: 'prometheus'
  20. static_configs:
  21. targets: ['127.0.0.1:9090']
  22. ###以下内容为SpringBoot应用配置
  23. job_name: 'springboot_prometheus'
  24. scrape_interval: 5s
  25. metrics_path: '/actuator/prometheus'
  26. static_configs:
  27. 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监控内容请搜索快网idc以前的文章或继续浏览下面的相关文章希望大家以后多多支持快网idc!

原文链接:https://blog.csdn.net/qq_33257527/article/details/88294016

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 SpringBoot使用prometheus监控的示例代码 https://www.kuaiidc.com/108222.html

相关文章

发表评论
暂无评论