尧图网络 高端网站定制 · 原创设计
免费咨询热线
400-888-6620
免费获取方案
Spring Boot 应用对接 Prometheus 监控指南
Spring Boot 应用对接 Prometheus 监控指南本文档介绍如何将 Spring Boot 应用接入 Prometheus 监控体系分无认证和Basic Auth 认证两种情况说明。一、技术栈组件用途Micrometer指标采集门面Spring Boot Actuator暴露监控端点Prometheus指标采集 时序数据库Grafana指标可视化二、通用步骤两种场景都需要2.1 引入依赖!-- Spring Boot Actuator --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-actuator/artifactId/dependency!-- Prometheus Registry --dependencygroupIdio.micrometer/groupIdartifactIdmicrometer-registry-prometheus/artifactId/dependency2.2 配置 application.ymlserver:port:8086# 业务端口spring:application:name:janus-service1management:server:port:18086# 管理端口与应用端口隔离endpoints:web:exposure:include:health,info,prometheus# 暴露 Prometheus 端点endpoint:prometheus:enabled:true# 启用 Prometheus 端点metrics:tags:application:${spring.application.name}# 给指标打标签便于区分2.3 K8s Deployment 配置apiVersion:apps/v1kind:Deploymentmetadata:name:janus-service1namespace:gateway-defaultspec:template:metadata:annotations:# # 场景一无认证直接抓取# prometheus.io/scrape:trueprometheus.io/kind:janus-service1prometheus.io/port:18086prometheus.io/path:/actuator/prometheus# # 场景二Basic Auth 认证额外添加# # prometheus.io/auth: basic # ← 有认证时取消注释spec:containers:-name:janus-service1image:your-registry/janus-service1:latestports:-name:httpcontainerPort:8086protocol:TCP-name:managementcontainerPort:18086# 管理端口protocol:TCP三、场景一无认证内网/测试环境3.1 场景说明Prometheus 直接抓取/actuator/prometheus端点无需用户名密码验证适用于内网环境或测试环境3.2 配置清单配置项值说明prometheus.io/scrapetrue允许 Prometheus 抓取prometheus.io/port18086抓取端口prometheus.io/path/actuator/prometheus抓取路径prometheus.io/auth不配置无需认证3.3 验证命令# 1. Pod 内验证curlhttp://localhost:18086/actuator/prometheus|head-10# 2. 集群内验证curlhttp://pod-ip:18086/actuator/prometheus|head-10# 3. 查看 Prometheus Targetshttp://prometheus-host:9090/targets# 预期: State UP ✅四、场景二Basic Auth 认证生产环境4.1 场景说明Prometheus 抓取时需要携带 Basic Auth 凭证应用需要验证请求中的用户名密码适用于生产环境安全性更高4.2 新增依赖!-- 仅场景二需要 --dependencygroupIdorg.springframework.boot/groupIdartifactIdspring-boot-starter-security/artifactId/dependency4.3 配置 application.ymlspring:security:user:# ⚠️ 必须与 Prometheus 配置中的 basic_auth 一致name:usernamepassword:axxxxxxxxxxxxxxxxxxxxxxxmanagement:# ... 与场景一相同保持不变4.4 配置 Security只保护 /actuator 路径importorg.springframework.context.annotation.Bean;importorg.springframework.context.annotation.Configuration;importorg.springframework.core.annotation.Order;importorg.springframework.security.config.Customizer;importorg.springframework.security.config.annotation.web.builders.HttpSecurity;importorg.springframework.security.config.annotation.web.configuration.EnableWebSecurity;importorg.springframework.security.web.SecurityFilterChain;ConfigurationEnableWebSecuritypublicclassManagementSecurityConfig{/** * 只保护 /actuator/** 路径使用 Basic Auth */BeanOrder(1)publicSecurityFilterChainmanagementSecurityFilterChain(HttpSecurityhttp)throwsException{http.securityMatcher(/actuator/**).authorizeHttpRequests(auth-auth.anyRequest().authenticated()).httpBasic(Customizer.withDefaults()).csrf(csrf-csrf.disable());returnhttp.build();}/** * 其他所有请求放行不影响业务接口 */BeanOrder(2)publicSecurityFilterChaindefaultSecurityFilterChain(HttpSecurityhttp)throwsException{http.authorizeHttpRequests(auth-auth.anyRequest().permitAll()).csrf(csrf-csrf.disable());returnhttp.build();}}4.5 K8s 注解添加 auth 标记annotations:prometheus.io/scrape:trueprometheus.io/kind:janus-service1prometheus.io/port:18086prometheus.io/path:/actuator/prometheusprometheus.io/auth:basic# ← 告诉 Prometheus 需要认证4.6 Prometheus 配置运维侧scrape_configs:-job_name:janus-service1kubernetes_sd_configs:-role:podrelabel_configs:# 只抓取 scrapetrue 的 Pod-source_labels:[__meta_kubernetes_pod_annotation_prometheus_io_scrape]action:keepregex:true# 只抓取 authbasic 的 Pod-source_labels:[__meta_kubernetes_pod_annotation_prometheus_io_auth]action:keepregex:basic# 使用自定义路径-source_labels:[__meta_kubernetes_pod_annotation_prometheus_io_path]action:replacetarget_label:__metrics_path__# 使用自定义端口-source_labels:[__address__,__meta_kubernetes_pod_annotation_prometheus_io_port]action:replaceregex:([^:])(?::\d)?;(\d)replacement:$1:$2target_label:__address__# Basic Auth 认证配置basic_auth:username:usernamepassword:a48xxxxxxxxxxxxxxxxxxxxxxxxxx391
RELATED

相关推荐

hubuild中的uniapp项目运行在Android Studio平板模拟器中

hubuild中的uniapp项目运行在Android Studio平板模拟器中

1.下载Android Studio2.打开设备管理器打开后如图打开后这个列表是空的,我的是已经添加了,点击左上角加号,左侧选择Tablet,就是平板的意思,右侧随便选一个,第三个是我自己自定义添加的,是点击左…

📅 2026/8/23 1:46:28
C语言基础:字符数组

C语言基础:字符数组

一维字符数组应用 : 存储字符串。1.定义:类型 数组名[整形常量]; 整形常量 数组的容量,表示可以储存多少个字符 类型 char c语言规定,字符串必须使用\0 作为结束标准。 如果你要在数组中储存一个 hello , hello\0 共计6个…

📅 2026/8/23 1:46:28
GEO优化哪个机构靠谱

GEO优化哪个机构靠谱

在选择 GEO 优化机构时,以下几个方面可以帮助你判断其是否靠谱:机构的背景和资质成立时间:成立时间较长的机构通常在行业内积累了更多的经验和资源。例如合肥拓路人信息科技有限公司,2016 年 4 月成立,十年的时间里专注…

📅 2026/9/17 17:23:55
MORE NEWS

更多资讯

📰

TiXL 导出独立可执行文件(Player)完整指南:从编辑器到一键分发

TiXL 导出独立可执行文件(Player)完整指南:从编辑器到一键分发 【免费下载链接】t3 TiXL is an open source software to create realtime motion graphics. 项目地址: https://gitcode.com/GitHub_Trending/t3/t3 TiXL(T3…

📰

2026年服务好的在线文档编辑中台厂商综合评测

在线文档编辑中台厂商服务能力评价维度当前企业数字化转型进程持续深入,在线文档编辑中台已经成为支撑企业跨部门协作、业务系统集成、数据安全管控的核心办公基础设施。不同厂商的服务能力差异直接影响项目落地效率、使用体验和后续运维成本,建立统一的…

📰

CANN pyasc 算子开发:asc.language.basic.set_atomic_type 原子操作数据类型设置接口详解

CANN pyasc 算子开发:asc.language.basic.set_atomic_type 原子操作数据类型设置接口详解 【免费下载链接】pyasc 本项目为Python用户提供算子编程接口,支持在昇腾AI处理器上加速计算,接口与Ascend C一一对应并遵守Python原生语法。 项目地…

📰

微信聊天记录导出完整指南:用 WeChatMsg 免费把历史对话转成 Word、CSV 和年度聊天报告

微信聊天记录导出完整指南:用 WeChatMsg 免费把历史对话转成 Word、CSV 和年度聊天报告 【免费下载链接】WeChatMsg 提取微信聊天记录,将其导出成HTML、Word、CSV文档永久保存,对聊天记录进行分析生成年度聊天报告 项目地址: https://gitco…

📰

OCC WebGL案例编译指南:FreeType静态库配置与链接实战

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

📰

深圳光影教育AIGC小班培训,从提示词到出图全流程,适合设计师转型

随着AIGC技术的快速落地,AI内容创作已经从行业概念变成了商业市场的刚需,无论是图文设计、短视频制作还是短剧生产,都在通过AIGC技术降低创作成本、提升产出效率。据相关行业数据显示,国内AIGC相关岗位人才缺口已经超过百万&#…

TODAY

今日更新

THIS WEEK

本周精选

THIS MONTH

本月热门

读完文章,想聊聊您的网站?

告诉我们您的行业与需求,资深顾问一对一梳理方案与报价,全程免费。

📞 💬