SpringBoot如何使用Micrometer上报指标给prometheus
liuian 2025-09-18 22:48 21 浏览
在 Spring Boot 应用中使用 Micrometer 将指标上报给 Prometheus 是一个非常标准且常见的做法。整个过程主要分为添加依赖、配置端点和**(可选)自定义指标**三个步骤。
步骤 1:添加必要的依赖
需要在 pom.xml(Maven)或 build.gradle(Gradle)中添加以下依赖:
Maven (pom.xml)
<dependencies>
<!-- Spring Boot Actuator (核心,提供健康检查、指标等端点) -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Micrometer Registry for Prometheus (将指标转换为 Prometheus 格式) -->
<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
<!-- (可选) 如果你使用 Spring Web (MVC), 会自动收集 HTTP 请求指标 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>注意:micrometer-registry-prometheus 依赖会自动引入 Micrometer 的核心库,所以你不需要单独引入 micrometer-core。
步骤 2:配置 application.yml 或 application.properties
为了让 Prometheus 能够抓取指标,需要暴露 /actuator/prometheus 端点。
application.yml
management:
endpoints:
web:
exposure:
# 暴露 prometheus 端点,让 Prometheus 可以访问
include: health,info,prometheus
endpoint:
prometheus:
enabled: true
health:
show-details: always # 可选:显示健康详情application.properties
# 暴露 prometheus 端点
management.endpoints.web.exposure.include=health,info,prometheus
# 启用 prometheus 端点
management.endpoint.prometheus.enabled=true
# (可选) 显示健康详情
management.endpoint.health.show-details=always重要:/actuator/prometheus 是 Prometheus 抓取数据的标准端点。
步骤 3:启动应用并验证
- 启动Spring Boot 应用。
- 访问 http://localhost:8080/actuator,应该能看到 prometheus 端点。
- 访问 http://localhost:8080/actuator/prometheus,应该能看到类似如下的 Prometheus 格式指标:
# HELP http_server_requests_seconds
# TYPE http_server_requests_seconds summary
http_server_requests_seconds_count{method="GET",uri="/actuator/prometheus",...} 1.0
http_server_requests_seconds_sum{method="GET",uri="/actuator/prometheus",...} 0.055231211
# HELP jvm_memory_used_bytes
# TYPE jvm_memory_used_bytes gauge
jvm_memory_used_bytes{area="heap",id="G1 Young Generation",} 12345678.0
...这些就是 Micrometer 收集并格式化后供 Prometheus 抓取的数据。
步骤 4:(可选)自定义业务指标
可以轻松地创建自己的指标。Spring Boot 会自动注入 MeterRegistry。
import io.micrometer.core.instrument.Counter;
import io.micrometer.core.instrument.MeterRegistry;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class MyController {
private final Counter myCustomCounter;
@Autowired
public MyController(MeterRegistry registry) {
// 创建一个自定义计数器
this.myCustomCounter = Counter.builder("my.custom.requests")
.description("A custom counter for demo")
.tag("type", "demo") // 可以添加标签(tags)
.register(registry);
}
@GetMapping("/demo")
public String demo() {
// 每次调用此方法,计数器 +1
myCustomCounter.increment();
return "Hello from custom metric!";
}
}访问 /demo 接口几次后,再访问 /actuator/prometheus,就能看到类似:
my_custom_requests_total{type="demo",} 3.0步骤 5:配置 Prometheus 抓取
最后,在prometheus.yml 配置文件中,添加一个 job 来抓取这个应用的指标:
scrape_configs:
- job_name: 'spring-boot-app'
metrics_path: '/actuator/prometheus'
static_configs:
- targets: ['localhost:8080'] # 替换为你的应用实际地址启动 Prometheus 后,它就会定时从Spring Boot 应用拉取指标。
总结
- 加依赖:spring-boot-starter-actuator + micrometer-registry-prometheus
- 配端点:在 application.yml 中暴露 /actuator/prometheus
- (可选)写代码:通过 MeterRegistry 创建自定义指标
- 配 Prometheus:在 prometheus.yml 中添加抓取任务
完成以上步骤,Spring Boot 应用就能通过 Micrometer 将丰富的指标数据上报给 Prometheus 了,为后续的监控、告警和可视化(如 Grafana)打下基础。
相关推荐
- win7正版怎么下载(我告诉你怎么下载win7)
-
如题,题主想在红警之家下载红警,很简单,可以用迅雷下载软件下载,下载以后用解压软件解压就行了,但是红警这款软件在winxp系统下,是最理想的,7系统下面会出现闪退的情况。下载很容易的,闪退的时候,设置...
- 电脑关机脚本bat命令(win10关机bat脚本)
-
bat关机命令需要使用文本文档。使用文本文档制作bat关机命令步骤如下所示:1、在电脑桌面空白处点击鼠标右键。2、在鼠标右键菜单中,选择新建文本文档。3、点击打开文本文档。4、在新建文本文档中输入关机...
- fat32格式是什么意思(u盘fAT32格式是什么意思)
-
是一种分区格式。这种格式使用32位文件分配表,大大提高了磁盘的管理能力,打破了每个分区只有4GB的FAT16限制。对于使用FAT32文件系统的每个逻辑盘内部空间又可划分为三部分,依次是引导区(BOO...
-
- 手机系统更新软件(手机系统更新软件不更新会怎样)
-
第一步:打开苹果手机的设置,点击“通用”选项进入。第二步:选择“软件更新”选项进入第三步:在软件更新界面,如果有新的更新,点击“下载并安装”即可!应用商店里一键更新。在手机软件里,为了更新最新版本可以进软件商店里面找一下,更新点一下,然后它...
-
2026-01-14 09:37 liuian
- 联想小新如何重装系统(联想小新重装系统怎么操作)
-
联想小新重装系统,方法∶在关机状态下,按下电脑的一键恢复按钮(需确保笔记本屏盖在打开状态)。首先找到按键孔,用针对准插孔,捅一下,电脑启动,进入启动选择界面,选择systemrecovery,按回车...
- 麦克风没声音(win11麦克风没声音)
-
一.先确保你的麦克风能正常使用。请确保麦克风本身是好的,连接线没有问题,请确保你的测试软件已正确设置,如YY之类的软件。二.确认你的麦克风是否插入正确的插孔一般麦克风是插入红色插孔中。三.确认你...
- vs2015官网下载(vs2015 下载)
-
VisualStudio2015下载完成之后,会有一个名为“vs2015.pro_chs.iso”的光盘镜像文件。光盘镜像文件将光盘镜像文件在虚拟光驱中加载之后,可以打开查看光盘内容。安装文件双...
- u盘自我保护怎么解除(怎么样取消u盘的自我保护)
-
要解除U盘保护,首先插入U盘后打开“我的电脑”,右击选择U盘图标,点击“属性”。在弹出的对话框中,选择“安全”标签,然后点击“编辑”按钮,根据自己的需要选择或取消“对于系统用户完全控制”权限,点击“确...
- 如何设置自动关机win10(windows 10如何设置自动关机)
-
Win10设置自动关机,需要以下步骤:1.按“Win+R”组合键,呼出“运行”;2.将定时关机命令设置为“shutdown-s-t7200”;3.在“运行”内输入命令,点击“确定”即可;4.如果设置错...
- 公版驱动(公版驱动和专用驱动的区别)
-
公版这个名词特指显卡本身,与驱动无关。一般采用芯片制造商自己设计的显卡,称为公版。显卡驱动只有WHQL版本、Beta版本、兼容版、定制版之分。兼容版一般称为万能驱动,不会给显卡带来多少优化,只是让你能...
- nod32是什么软件(nod32是哪个国家的)
-
起源于捷克斯洛伐克总部现在美国下面是nod32的由来:nod是根据一部电视剧(城市边缘的医院)起的,原意是“磁盘边的医院”32是源于当16-bitNOD-ICE很成熟的时候32位处理器出来了升级适应3...
- 欧拉linux系统官网(欧拉系统命令)
-
在华为欧拉服务器上配置Linux网络,首先需要编辑网络配置文件,位于/etc/sysconfig/network-scripts目录下,根据网络需求配置对应的网络接口,IP地址、子网掩码、网关等信息,...
- deepin安装显卡驱动(deepin安装显卡驱动后无法进入图形界面)
-
1、首先必须使用rufus制作U盘启动,必须选择DD格式2、从其他linux镜像比如Ubantu或其他拷贝出EFI的引导文件,具体是镜像中的EFI—boot—grubx86.efi这个文件,把这个文件...
- 一周热门
-
-
飞牛OS入门安装遇到问题,如何解决?
-
如何在 iPhone 和 Android 上恢复已删除的抖音消息
-
Boost高性能并发无锁队列指南:boost::lockfree::queue
-
大模型手册: 保姆级用CherryStudio知识库
-
用什么工具在Win中查看8G大的log文件?
-
如何在 Windows 10 或 11 上通过命令行安装 Node.js 和 NPM
-
威联通NAS安装阿里云盘WebDAV服务并添加到Infuse
-
Trae IDE 如何与 GitHub 无缝对接?
-
idea插件之maven search(工欲善其事,必先利其器)
-
如何修改图片拍摄日期?快速修改图片拍摄日期的6种方法
-
- 最近发表
- 标签列表
-
- python判断字典是否为空 (50)
- crontab每周一执行 (48)
- aes和des区别 (43)
- bash脚本和shell脚本的区别 (35)
- canvas库 (33)
- dataframe筛选满足条件的行 (35)
- gitlab日志 (33)
- lua xpcall (36)
- blob转json (33)
- python判断是否在列表中 (34)
- python html转pdf (36)
- 安装指定版本npm (37)
- idea搜索jar包内容 (33)
- css鼠标悬停出现隐藏的文字 (34)
- linux nacos启动命令 (33)
- gitlab 日志 (36)
- adb pull (37)
- python判断元素在不在列表里 (34)
- python 字典删除元素 (34)
- vscode切换git分支 (35)
- python bytes转16进制 (35)
- grep前后几行 (34)
- hashmap转list (35)
- c++ 字符串查找 (35)
- mysql刷新权限 (34)
