Spring Boot 3响应Prometheus采集请求返回HTTP 406错误的解决方法
解决方案
1. 确认依赖配置正确
确保项目构建文件中包含Spring Boot监控所需的核心依赖:
Maven(pom.xml):
<!-- Spring Boot Actuator 基础监控组件 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <!-- Micrometer Prometheus 注册器,提供Prometheus兼容的指标格式 --> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> </dependency>
Gradle(build.gradle):
implementation 'org.springframework.boot:spring-boot-starter-actuator' implementation 'io.micrometer:micrometer-registry-prometheus'
2. 配置Actuator端点暴露
在application.yml或application.properties中开启并暴露Prometheus端点:
application.yml:
management: endpoints: web: exposure: include: prometheus,health # 按需添加其他需要暴露的端点 endpoint: prometheus: enabled: true
application.properties:
management.endpoints.web.exposure.include=prometheus,health management.endpoint.prometheus.enabled=true
3. 修复消息转换器冲突(核心解决步骤)
从日志来看,Spring Boot已返回兼容的媒体类型,但仍报错,大概率是自定义消息转换器覆盖了默认配置,导致无法处理application/openmetrics-text或text/plain格式的响应。
解决方式:
如果项目中自定义了WebMvcConfigurer,避免清空默认消息转换器,改用添加模式:
@Configuration public class WebConfig implements WebMvcConfigurer { @Override public void extendMessageConverters(List<HttpMessageConverter<?>> converters) { // 在这里添加自定义转换器,不会覆盖Spring Boot默认的转换器 // converters.add(new CustomHttpMessageConverter()); } }
如果之前用了configureMessageConverters方法,不要调用converters.clear(),确保保留默认转换器。
4. 手动验证端点可用性
启动应用后,用curl命令手动测试:
curl -H "Accept: application/openmetrics-text;version=1.0.0" http://localhost:8080/actuator/prometheus
若返回正常的Prometheus指标数据,说明配置生效。
5. 检查Prometheus采集配置
确保Prometheus的prometheus.yml中配置了正确的采集路径:
scrape_configs: - job_name: 'spring-boot-app' scrape_interval: 15s static_configs: - targets: ['localhost:8080'] metrics_path: '/actuator/prometheus'
内容的提问来源于stack exchange,提问作者Kalpadiptya Roy
相关产品推荐
相关产品推荐

