非Web型camel-springboot应用如何配置暴露actuator metrics指标
解决方案
根因说明
该场景下访问404的核心原因是:Spring Boot默认非Web应用(
spring.main.web-application-type默认值为NONE)不会启动内嵌Servlet容器,8080端口没有HTTP服务监听,自然无法响应Actuator端点的请求。
具体操作步骤
- 第一步:引入Web容器依赖
确认项目依赖配置中已引入Spring Web起步依赖,用来启动内嵌Servlet容器承载Actuator端点:
若不想使用默认Tomcat,可替换为Jetty、Undertow等其他Servlet容器实现。<!-- Maven 依赖示例 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> - 第二步:修改Spring Boot核心配置
在application.properties或application.yml中添加如下配置:
现有配置# 启用Servlet类型的Web应用,启动内嵌Web容器 spring.main.web-application-type=servlet # 暴露Actuator的metrics端点,多个端点用逗号分隔,配置*则暴露所有端点 management.endpoints.web.exposure.include=health,metrics # 可选配置:单独指定Actuator服务端口,不配置则默认和Web服务共用8080端口 # management.server.port=8081camel.springboot.main-run-controller=true无需调整,启动Web容器和Camel主进程常驻逻辑完全不冲突,原有Kafka消费功能不受任何影响。 - 补充特殊场景配置:如果你手动关闭了端点默认启用开关(配置了
management.endpoints.enabled-by-default=false),需要额外添加配置开启metrics端点:management.endpoint.metrics.enabled=true - 第三步:验证访问
重启应用后,先访问http://localhost:8080/actuator查看返回的端点列表,确认包含metrics项后,即可正常访问http://localhost:8080/actuator/metrics获取指标数据。
内容的提问来源于stack exchange,提问作者srikant_mantha
相关产品推荐
相关产品推荐

