当前位置: 首页 > news >正文

Spring Boot + Prometheus 实现应用监控(基于 Actuator 和 Micrometer)

文章目录

  • Spring Boot + Prometheus 实现应用监控(基于 Actuator 和 Micrometer)
  • 环境准备
  • 示例结构
  • 启动和验证
  • 验证 Spring Boot 应用
  • Prometheus 抓取配置(静态方式)
  • Grafana 面板配置
  • 总结


Spring Boot + Prometheus 实现应用监控(基于 Actuator 和 Micrometer)

在微服务架构中,监控是保障系统稳定运行的关键组成部分。本文将介绍如何通过 Spring Boot 的 actuatormicrometer 组件,将应用的运行指标暴露出来,并使用 Prometheus 定时采集这些指标数据。

环境准备

  • JDK:21
  • Spring Boot:3.2.5
  • 构建工具:Maven
  • 监控工具:Prometheus 2.53.4 安装参考
  • 面板工具:Grafana v9.1.2 安装参考

示例结构

test/
├── pom.xml                                           # Maven 项目对象模型文件
├── src
│   └── main
│       ├── java
│       │   └── com
│       │       └── example
│       │           └── DemoMonitorApplication.java   # 主类(示例控制器)
│       └── resources
│           └── application.yml                       # 应用配置文件

pom.xml 文件内容如下:

<?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><!-- ✅ 加入 Spring Boot 官方 parent,自动管理版本号 --><parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>3.2.5</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>demo-monitor</artifactId><version>1.0.0</version><packaging>jar</packaging><name>demo-monitor</name><properties><java.version>21</java.version></properties><dependencies><!-- Web 服务 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Actuator 监控端点 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- Prometheus 指标导出 --><dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId></dependency></dependencies><build><plugins><!-- Spring Boot 插件 --><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>
模块用途
spring-boot-starter-web提供 Web 支持
spring-boot-starter-actuator暴露监控端点(如 /actuator/health
micrometer-registry-prometheus让 Prometheus 能采集指标
spring-boot-maven-pluginMaven 构建可执行 jar 的插件

src/main/java/com/example/DemoMonitorApplication.java 内容如下:

package com.example;import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;@SpringBootApplication
public class DemoMonitorApplication {public static void main(String[] args) {SpringApplication.run(DemoMonitorApplication.class, args);}
}

src/main/resources/application.yml 内容如下:

server:port: 8080management:endpoints:web:exposure:include: "*"endpoint:health:show-details: alwaysmetrics:export:prometheus:enabled: true

说明:

  • management.endpoints.web.exposure.include: 暴露的端点,必须包含 prometheus
  • management.metrics.export.prometheus.enabled: 启用 Prometheus 导出
  • 默认 Prometheus 指标路径为 /actuator/prometheus
  • 注意:上述依赖中未显式声明版本号,是因为使用了 Spring Boot 的官方 parent

启动和验证

使用 Maven 启动项目:

./mvnw spring-boot:run

或打包后运行:

mvn clean package
java -jar target/demo-monitor-1.0.0.jar

然后就可以看到有个 8080 端口启动了


验证 Spring Boot 应用

启动应用后,访问指标接口:

http://localhost:8080/actuator/prometheus

你可以看到形如以下格式的指标输出:

在这里插入图片描述

Prometheus 抓取配置(静态方式)

Prometheus 配置示例(prometheus.yml):

scrape_configs:- job_name: 'spring-boot-app'metrics_path: '/actuator/prometheus'static_configs:- targets: ['localhost:8080']

启动 Prometheus 后,访问 http://localhost:9090 ,你就可以查询该 Spring Boot 应用的各类指标了,如:
在这里插入图片描述
在这里插入图片描述

Grafana 面板配置

表盘市场导入仪表盘:访问地址
ID:14370

在这里插入图片描述

总结

  • 依赖配置简单:通过引入 spring-boot-starter-actuatormicrometer-registry-prometheus,即可在 Spring Boot 中集成监控能力。
  • 指标暴露统一:所有 JVM、应用及自定义指标统一暴露在 /actuator/prometheus 接口上。
  • Prometheus 易于集成:通过 Prometheus 的 scrape_configs 采集配置,即可定时拉取指标数据。
  • 适用于单体与微服务架构:无论是本地部署还是容器化,Spring Boot + Prometheus 都是轻量而强大的监控方案。

若后续部署在 Kubernetes 集群中采集多实例指标。欢迎继续探讨 👇

http://www.lqws.cn/news/192673.html

相关文章:

  • Langgraph实战--在Agent中加入人工反馈
  • 13.MySQL用户管理
  • 力扣100-移动0
  • Android Test3 获取的ANDROID_ID值不同
  • ​​TPS3808​​低静态电流、可编程延迟电压监控电路,应用笔记
  • 初识AI Agent
  • Rust 开发环境搭建
  • 精益数据分析(95/126):Socialight的定价转型启示——B2B商业模式的价格策略与利润优化
  • 超声波清洗设备的清洗效果如何?
  • CMA软件产品测试报告在哪申请?
  • AI对测试行业的应用
  • 中医的十问歌和脉象分类
  • 基于KNN算法的入侵检测模型设计与实现【源码+文档】
  • 【深度学习新浪潮】RoPE对大模型的外推性有什么影响?
  • yolov8自训练模型作为预训练权重【增加新类别】注意事项
  • 事件监听 ——CAD C#二次开发
  • react 常见的闭包陷阱深入解析
  • 几何引擎对比:OpenCasCade、ACIS、Parasolid和CGM
  • n皇后问题的 C++ 回溯算法教学攻略
  • 比对++Hex or Bin文件
  • Xilinx 325T FPGA 中的 GT(GTP 或 GTX)收发器和普通 LVDS 接口的差模和共模电压
  • 逻辑回归与Softmax
  • [蓝桥杯]防御力
  • hg38与hg38相互转换:使用LiftOver在线工具
  • 《架构即未来》笔记
  • LinkedBlockingQueue、ConcurrentLinkedQueue和ArrayBlockingQueue深度解析
  • 单片机0-10V电压输出电路分享
  • 11.RV1126-ROCKX项目
  • 12.6Swing控件4 JSplitPane JTabbedPane
  • Lrc歌词分析