Spring Boot应用整合Prometheus

news/2024/5/19 1:32:50 标签: spring boot, prometheus, grafana

Spring Boot Actuator 提供了一组用于监控和管理 Spring Boot 应用程序的端点,而 Prometheus 是一个开源的监控和告警工具。通过将这两者结合起来,您可以实时监控您的应用程序的性能指标,并通过 Prometheus 提供的丰富的查询语言来分析和可视化这些指标。

Spring Boot 应用整合 Prometheus

1)添加依赖
首先,您需要在您的 Spring Boot 项目中添加以下依赖:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
    <groupId>io.micrometer</groupId>
    <artifactId>micrometer-registry-prometheus</artifactId>
</dependency>

这将添加 Actuator 和 Prometheus 的依赖项到您的项目中。

2)配置 Actuator,启用 Prometheus 端点
application.yml文件中添加以下配置:

spring:
  application:
    name: PrometheusApp

# Prometheus springboot监控配置
management:
  endpoints:
    web:
      exposure:
        include: '*'
    tags:
      application: ${spring.application.name} # 暴露的数据中添加application label

include=* 配置为开启 Actuator 服务,Spring Boot Actuator 自带了一个/actuator/Prometheus的监控端点供给 Prometheus 抓取数据。不过默认该服务是关闭的,所以,使用该配置将打开所有的 Actuator 服务。

Actuator 默认的端点很多,详见:
https://docs.spring.io/spring-boot/docs/2.4.3/reference/html/production-ready-features.html#production-ready-endpoints

3)启动应用程序
现在,您可以启动您的 Spring Boot 应用程序了。
您可以通过访问[http://localhost:8080/actuator/prometheus](http://localhost:8080/actuator/prometheus)来查看 Prometheu 实时监控的指标数据。
以上就是使用 Spring Boot Actuator 和 Prometheus 监控应用程序的整个过程。

将应用添加到 Prometheus

首先,修改 Prometheus 的配置文件 prometheus.yml ,添加上边启动的服务地址来执行监控

scrape_configs:
  - job_name: 'prometheusapp'
    metrics_path: '/actuator/prometheus'
    static_configs:
      - targets: ['192.168.2.234:8080'] 

上面的 prometheusapp 就是前面创建的 Spring Boot 应用程序,也就是 Prometheus 需要监控的服务地址。
然后,重启 Prometheus 服务,查看 Prometheus UI 界面确认 Target 是否添加成功。

使用 Grafana Dashboard 展示应用数据

[https://grafana.com/grafana/dashboards](https://grafana.com/grafana/dashboards)下载 Spring Boot 的模板(这里使用的是编号4701)。
根据 ID 导入模板,导入完毕后,就可以看到 JVM 的各项监控指标,如果有多个应用,可以通过 Application 选择我们想要查看的应用即可。


http://www.niftyadmin.cn/n/5297294.html

相关文章

使用docker build构建image

文章目录 环境步骤准备例1&#xff1a;基本用法例2&#xff1a;缓存layer例3&#xff1a;Multi-stage例4&#xff1a;Mountcache mountbind mount 例5&#xff1a;参数例6&#xff1a;Export文件例7&#xff1a;测试 参考 环境 RHEL 9.3Docker Community 24.0.7 步骤 在Dock…

YHZ010 Python 的类型转换

&#x1f436; 类型转换 资源编号&#xff1a;YHZ010 配套视频&#xff1a;https://www.bilibili.com/video/BV1zy4y1Z7nk?p11 &#x1f439; 检查变量类型 在 Python 中可以使用type函数对变量的类型进行检查。程序设计中函数的概念跟数学上函数的概念是一致的&#xff0c;数…

什么是IDE?新手用哪个IDE比较

IDE代表集成开发环境&#xff08;Integrated Development Environment&#xff09;&#xff0c;它是一种软件应用程序&#xff0c;提供了一套工具&#xff0c;用于编写、调试和运行软件程序。一个IDE通常包含代码编辑器、编译器、调试器和其他各种工具&#xff0c;以便开发人员…

Django Rest Framework(DRF)框架搭建步骤,包含部分错误解决

一、初步搭建项目 1.使用PyCharm 2021创建Djiango项目&#xff0c;配置如下&#xff08;假设应用名叫djiango_python) Python &#xff08;3.6&#xff0c; 3.7&#xff0c; 3.8&#xff0c; 3.9&#xff0c; 3.10&#xff0c; 3.11&#xff09;> 当前版本 3.8.6Django &a…

Ubuntu 常用命令之 locate 命令用法介绍

🔥Linux/Ubuntu 常用命令归类整理 locate命令是在Ubuntu系统下用于查找文件或目录的命令。它使用一个预先构建的数据库(通常由updatedb命令创建)来查找文件或目录,因此它的查找速度非常快。 plocate 安装 locate 不是 Ubuntu 系统的原生命令/功能,要想在 Ubuntu 系统中…

基于SpringBoot的宠物商城管理系统

基于SpringBootVue的宠物商城管理系统的设计与实现~ 开发语言&#xff1a;Java数据库&#xff1a;MySQL技术&#xff1a;SpringBootVueMyBatis工具&#xff1a;IDEA/Ecilpse、Navicat、Maven 系统展示 管理员界面 宠物商品信息 用户信息管理 用户界面 商品信息 个人中心 摘要…

毫秒格式化

## 计算当前毫秒数&#xff1a; const [start,setStart] useState(new Date().getTime())useEffect(()>{setInterval(()>{setCurrMill(new Date().getTime()-start)},1)},[]) ## 格式化毫秒 function formatMilliseconds(milliseconds) {const totalSeconds Math.flo…

Debian安装k8s记录

Debian安装k8s记录 在master和node上安装kube安装master安装node遇到的问题汇总1、kubelet.service报错 failed to pull image "registry.k8s.io/pause:3.6"2、node重启后报错&#xff0c;failed: open /run/flannel/subnet.env: no such file or directory 在master…