Spring Boot与Actuator指标数据为空问题#28266
Hey there, let's work through this metrics issue you're hitting—since even a fresh Spring Boot 2.5.5 project has the same problem, it's likely a configuration oversight rather than a complex bug. Here's what to check step by step:
1. Verify Actuator Endpoint Exposure Configuration
Spring Boot 2.x locks down most Actuator endpoints by default for security. You need to explicitly expose the metrics endpoint (or all endpoints) in your application.properties or application.yml:
For application.properties:
# Expose all Actuator endpoints (adjust if you want to limit to specific ones) management.endpoints.web.exposure.include=* # Ensure the metrics endpoint is enabled (it's enabled by default, but good to confirm) management.endpoint.metrics.enabled=true
For application.yml:
management: endpoints: web: exposure: include: '*' endpoint: metrics: enabled: true
2. Confirm Dependencies Are Correct
Double-check your build file to make sure you've included the necessary starters without conflicts.
Maven (pom.xml):
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies>
Gradle (build.gradle):
dependencies { implementation 'org.springframework.boot:spring-boot-starter-web' implementation 'org.springframework.boot:spring-boot-starter-actuator' }
3. Ensure You're Accessing the Correct Path
Spring Boot 2.x uses /actuator as the default base path for all endpoints. So instead of hitting /metrics, you should visit:
http://localhost:8080/actuator/metrics
This should return a list of available metrics (like JVM memory stats, Tomcat session metrics, etc.) even without Spring Data/JPA. To see details for a specific metric, append its name—e.g., http://localhost:8080/actuator/metrics/jvm.memory.used.
4. Check Startup Logs for Endpoint Registration
When your app starts, look for log lines like this:
Exposing X endpoint(s) beneath base path '/actuator'
Make sure metrics is listed in the exposed endpoints. If it's missing, double-check your configuration for typos (common issues include misspelling management or exposure).
5. Clear Build Cache (If Needed)
For fresh projects that still have issues, sometimes cached dependencies can cause weird behavior. Try cleaning your Maven/Gradle cache and rebuilding the project:
- Maven:
mvn clean install - Gradle:
./gradlew clean build
If none of these steps fix the problem, let me know what you see when you visit /actuator/metrics (e.g., a blank response, 404 error, or empty JSON) and we can dig deeper!
内容的提问来源于stack exchange,提问作者user14774749

