Spring Boot访问actuator/prometheus端点出现404错误的解决求助
修复Spring Boot Actuator Prometheus端点404问题
1. 先修正重复的配置项
你的application.properties里存在重复的management.endpoints.web.exposure.include配置,后一行会覆盖前一行,虽然你已经包含了prometheus,但先清理配置避免潜在冲突:
management.endpoint.info.enabled=true management.security.enabled=false # 保留唯一的端点暴露配置 management.endpoints.web.exposure.include=health,info,prometheus # 显式启用prometheus端点(默认已启用,显式配置更稳妥) management.endpoint.prometheus.enabled=true
2. 确认依赖是否完整且兼容
检查你的构建文件,确保依赖引入正确:
Maven(pom.xml):
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>io.micrometer</groupId> <artifactId>micrometer-registry-prometheus</artifactId> <scope>runtime</scope> </dependency> </dependencies>
Gradle(build.gradle):
dependencies { implementation 'org.springframework.boot:spring-boot-starter-actuator' runtimeOnly 'io.micrometer:micrometer-registry-prometheus' }
注意:Spring Boot版本和Micrometer要匹配——Spring Boot 2.x对应Micrometer 1.x,Spring Boot 3.x对应Micrometer 1.10+或2.x,不要手动指定冲突版本。
3. 检查启动日志
启动应用后,在控制台搜索prometheus关键词:
- 确认日志中出现类似
Exposing 3 endpoint(s) beneath base path '/actuator'的内容,且包含prometheus - 如果看不到prometheus端点被暴露,说明依赖未正确加载,检查是否有其他依赖排除了micrometer组件
4. 彻底排除Spring Security影响
即使你禁用了安全配置,可能仍有自动配置残留,在启动类添加排除:
@SpringBootApplication(exclude = {SecurityAutoConfiguration.class, ManagementWebSecurityAutoConfiguration.class}) public class YourApplication { public static void main(String[] args) { SpringApplication.run(YourApplication.class, args); } }
验证是否是安全配置导致的端点无法访问。
5. 检查自定义端点路径
如果你配置了management.endpoints.web.base-path,端点路径会变更,比如配置了management.endpoints.web.base-path=/monitor,则访问路径应为http://localhost:8080/monitor/prometheus,确认你未修改过该配置。
如果以上步骤无效,建议创建一个最小化测试项目:只引入actuator和prometheus依赖,配置好暴露规则,启动后测试端点。若测试项目正常,再逐步添加原项目的依赖和配置,排查冲突组件。
内容的提问来源于stack exchange,提问作者SharpCoder
相关产品推荐
相关产品推荐

