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

Spring Boot 应用对接 Prometheus 监控指南

Spring Boot 应用对接 Prometheus 监控指南

本文档介绍如何将 Spring Boot 应用接入 Prometheus 监控体系,分无认证Basic Auth 认证两种情况说明。


一、技术栈

组件用途
Micrometer指标采集门面
Spring Boot Actuator暴露监控端点
Prometheus指标采集 + 时序数据库
Grafana指标可视化

二、通用步骤(两种场景都需要)

2.1 引入依赖

<!-- Spring Boot Actuator --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-actuator</artifactId></dependency><!-- Prometheus Registry --><dependency><groupId>io.micrometer</groupId><artifactId>micrometer-registry-prometheus</artifactId></dependency>

2.2 配置 application.yml

server: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:"true"prometheus.io/kind:"janus-service1"prometheus.io/port:"18086"prometheus.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/scrape"true"允许 Prometheus 抓取
prometheus.io/port"18086"抓取端口
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 新增依赖

<!-- 仅场景二需要 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency>

4.3 配置 application.yml

spring: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;@Configuration@EnableWebSecuritypublicclassManagementSecurityConfig{/** * 只保护 /actuator/** 路径,使用 Basic Auth */@Bean@Order(1)publicSecurityFilterChainmanagementSecurityFilterChain(HttpSecurityhttp)throwsException{http.securityMatcher("/actuator/**").authorizeHttpRequests(auth->auth.anyRequest().authenticated()).httpBasic(Customizer.withDefaults()).csrf(csrf->csrf.disable());returnhttp.build();}/** * 其他所有请求放行(不影响业务接口) */@Bean@Order(2)publicSecurityFilterChaindefaultSecurityFilterChain(HttpSecurityhttp)throwsException{http.authorizeHttpRequests(auth->auth.anyRequest().permitAll()).csrf(csrf->csrf.disable());returnhttp.build();}}

4.5 K8s 注解(添加 auth 标记)

annotations:prometheus.io/scrape:"true"prometheus.io/kind:"janus-service1"prometheus.io/port:"18086"prometheus.io/path:"/actuator/prometheus"prometheus.io/auth:"basic"# ← 告诉 Prometheus 需要认证

4.6 Prometheus 配置(运维侧)

scrape_configs:-job_name:'janus-service1'kubernetes_sd_configs:-role:podrelabel_configs:# 只抓取 scrape=true 的 Pod-source_labels:[__meta_kubernetes_pod_annotation_prometheus_io_scrape]action:keepregex:true# 只抓取 auth=basic 的 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
http://www.jsqmd.com/news/1298487/

相关文章:

  • 2026年工程项目管理系统推荐甲方视角下的投资管控与合规选型指南
  • USB3.2链路训练与LTSSM状态机:从物理连接到稳定通信的底层原理与调试实战
  • 高并发游戏服务器架构实战:从微变私服到安徒恩攻坚战
  • 2026年7月四川省遂宁市联通融合宽带怎么报装 - 找卡家园
  • C++入门基础:从环境搭建到实战编程,掌握系统级语言核心
  • Python 权限装饰器与日志模块:给函数穿防护马甲 + 让程序写日记
  • DALI调光主控器安装接线与调试全流程实战指南
  • AI表格导出Excel后的日期与编号校验:DS随心转整理方法
  • Unity答题系统架构设计:从动态面板到可复用交互框架
  • 极限学习机(ELM)原理与MATLAB实现:快速神经网络训练算法
  • Python修改Excel数据:从pandas到openpyxl的实战指南
  • 贾子理论:一种系统性真理探索的元科学范式——兼论西方分科体制的认识论局限与学术工业的异化
  • 从0到1:企业级AI项目迭代日记 Vol.78|不只是更名,还有更隐蔽的事
  • MyBatis查询操作实战:从基础配置到动态SQL与性能优化
  • Reaction视频制作全攻略:从技术设备到版权合规
  • 从零实现C++优先队列:深入理解堆算法与STL设计
  • 现代C++网络编程实战:基于Asio构建高性能异步服务器
  • DALI调光主控器安装接线全攻略:从原理到实战,打造稳定智能照明系统
  • 电赛国一报告模板解析:从PID控制到系统测试的工程实践指南
  • Beta分布原理与应用:从概率建模到点击率预测
  • 2026年盘点:那些备受推崇的HDMI矩阵厂家,究竟好在哪里?
  • Firefox网页翻译插件全攻略:从选型配置到高效工作流
  • 带隙基准电路全流程设计:从HSPICE仿真到Cadence版图验证
  • 基于AgentScope与LlamaIndex构建智能消息检索系统
  • 2026年非标五金模胚生产厂家:精密定制与高刚性模具核心供应商深度解析 - 优企名品
  • Verilog延迟语句:仿真与综合的核心差异与实战指南
  • Kettle多表数据抽取:原理、优化与实战
  • 2026年南阳交通事故维权避坑指南:从事故认定到伤残鉴定全流程解析 - 本地品牌推荐
  • 私有CA搭建与Dovecot TLS证书配置实战指南
  • 功率放大器分类全解析:从A类到D类,效率与音质的终极权衡