Spring Boot Actuator与Micrometer中@Counted注解失效,@Timed正常
问题分析与解决
@Counted指标不显示的原因及解决
@Timed在Spring Boot中默认通过自动配置启用代理支持,但@Counted需要额外配置才能生效:
- 需在配置类上添加
@EnableMetrics注解,开启Metrics注解的代理支持; - 确保被注解方法通过Spring代理对象调用:如果是类内部直接调用方法(而非通过@Autowired注入的Bean调用),代理不会生效,@Counted也无法触发计数;
- Spring Boot 2.1+版本可通过配置
management.metrics.annotations.counted.enabled=true全局启用@Counted注解支持。
Prometheus端点404的原因及解决
你访问的端点存在两处错误:路径重复了actuator,且拼写错误(应为prometheus而非promethues),正确路径是http://<server>:<port>/actuator/prometheus。即使路径正确仍404,通常是以下原因:
- 未引入Prometheus依赖:需在项目中添加
micrometer-registry-prometheus依赖(Maven或Gradle); - 未暴露Prometheus端点:在配置文件(application.properties/yml)中设置
management.endpoints.web.exposure.include=prometheus,metrics,将端点对外暴露。
内容的提问来源于stack exchange,提问作者Gil Shapir
相关产品推荐
相关产品推荐

