恒美微站 Logo 恒美微站
  • 首页
  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心
  • 联系我们

Spring Boot 应用对接 Prometheus 监控指南

  • 首页
  • 资讯中心
  • /
  • Spring Boot 应用对接 Prometheus 监控指南

相关资讯

hubuild中的uniapp项目运行在Android Studio平板模拟器中 2026/8/2 17:19:17
C语言基础:字符数组 2026/8/2 17:19:17
GEO优化哪个机构靠谱 2026/8/2 17:19:19

最新资讯

面试官:你的Agent是怎么应对高并发的?
深度解析宿迁市建设局网站首页:从官方门户看城市建设的温度与力度
WarcraftHelper魔兽争霸3辅助工具完整指南:一次解决宽屏、锁帧与中文路径三大难题
C++多态核心:从virtual关键字到虚函数表的底层实现
从全能Agent到专业组件:可观测性数据采集的架构演进与实践
Cowabunga Lite完整指南:不越狱也能实现iOS界面自由定制

今日推荐

青岛煜鹏网站建设公司如何帮助传统企业实现数字化转型破局与增长路径
内蒙古生产建设兵团四师三十四团知青网站:承载岁月记忆与青春荣耀的精神家园
梅州市住房与城乡建设局官网:获取权威建筑信息、政策解读与民生服务的最佳平台入口

本周热门

5分钟告别提取码焦虑:baidupankey如何智能破解百度网盘资源锁
如何快速生成中国车牌图片:Python开源工具完整指南
当 LLM 遇见大文档:主流开源项目如何处理上下文超限

本月精选

如何用DamaiHelper实现演唱会门票的智能自动化抢购:完整技术解决方案指南
第4篇:59 倍性能差距的索引瓶颈定位——一次教科书级的全表扫描调优
终极歌词批量下载神器:5分钟解决离线音乐库歌词同步难题

Spring Boot 应用对接 Prometheus 监控指南

发布时间:2026/8/14 9:31:26
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

关于恒美微站

恒美微站专注于为个体商户、工作室提供极简自助建站服务,让每个人都能轻松拥有专业网站。

快速链接

  • 关于我们
  • 建站服务
  • 主题模板
  • 案例展示
  • 资讯中心

服务项目

  • 可视化建站
  • 拖拽编辑
  • 主题定制
  • SEO 优化
  • 网站托管

联系方式

  • 📍 地址:北京市朝阳区建国路 88 号
  • 📞 电话:400-888-8888
  • ✉️ 邮箱:info@hmyw.cn
  • 🕐 时间:周一至周日 9:00-18:00

© 2024 恒美微站 hmyw.cn 版权所有 | 京 ICP 备 12345678 号