使用 Spring Boot Actuator 进行监控
利用 Spring Boot Actuator 端点在生产环境中监控、管理和检查应用。
使用 Spring Boot Actuator 进行监控 是 CoddyKit 上的免费 Spring Boot 4 Complete Guide 课时。 这是第 2 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Spring Boot 4 Complete Guide 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Spring Boot 4 Complete Guide 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Meet Spring Boot Actuator
Spring Boot Actuator is a powerful tool that helps you monitor and manage your application when it's running in production. Think of it as your app's built-in dashboard!
It provides production-ready features without requiring you to write much code.
- Monitoring: Check health, metrics, and environment details.
- Management: Log levels, shutdown, refresh context.
- Insight: Understand your app's internal workings.
Include Actuator in Your Project
To use Actuator, you just need to add its starter dependency to your project. This brings in all the necessary libraries.
For Maven, add this to your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>Discover Actuator's Endpoints
Once Actuator is added, your application automatically exposes several "endpoints". These are HTTP URLs that provide specific information about your running app.
Some default endpoints include:
/actuator/health: Shows application health status./actuator/info: Displays general application information./actuator/metrics: Provides detailed metrics (CPU, memory, etc.).
By default, only /health and /info are exposed via web.
Run & Access Actuator
Let's create a simple Spring Boot app with Actuator. After running it, you can access the default endpoints in your browser or with a tool like curl.
Try running this and then navigate to http://localhost:8080/actuator/health.
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ActuatorDemoApplication {
public static void main(String[] args) {
SpringApplication.run(ActuatorDemoApplication.class, args);
}
}Deep Dive: The /health Endpoint
The /actuator/health endpoint is crucial for understanding your application's operational status. It aggregates information from various "health indicators".
- DiskSpace: Checks available disk space.
- DataSource: Verifies database connectivity (if configured).
- Liveness/Readiness: For Kubernetes deployments (newer versions).
It typically returns {"status": "UP"} if everything is fine, or details if something is down.
Custom Info with /info
The /actuator/info endpoint provides general information about your application. By default, it's empty, but you can easily populate it with useful details.
Add custom properties to your application.properties or application.yml:
info.app.name=MyActuatorApp
info.app.version=1.0.0
info.app.description=A demo for CoddyKitConfigure & Expose More Endpoints
By default, only /health and /info are exposed over the web. You can expose more endpoints like /beans (list all Spring beans) or /env (environment properties).
To expose all endpoints, add this to application.properties:
management.endpoints.web.exposure.include=*Monitor Performance with /metrics
The /actuator/metrics endpoint is invaluable for performance monitoring. It exposes various metrics about your application, JVM, and system.
Examples of metrics:
jvm.memory.used: Current JVM memory usage.system.cpu.usage: System CPU load.http.server.requests: Details about incoming HTTP requests.
You can query specific metrics like /actuator/metrics/jvm.memory.used for detailed data.
Build Your Own Health Checks
You can create custom health indicators to check the status of specific parts of your application, like an external service dependency or a custom cache.
Implement the HealthIndicator interface:
import org.springframework.boot.SpringApplication;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.stereotype.Component;
@SpringBootApplication
public class CustomHealthApp {
public static void main(String[] args) {
SpringApplication.run(CustomHealthApp.class, args);
}
}
@Component
class MyCustomHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// Simulate checking an external service
boolean serviceIsUp = Math.random() > 0.5; // Randomly up or down
if (serviceIsUp) {
return Health.up().withDetail("myServiceStatus", "OK").build();
} else {
return Health.down().withDetail("myServiceStatus", "Failed").build();
}
}
}Quick Check: Actuator Endpoints
Which of the following Actuator endpoints are exposed by default over the web in a basic Spring Boot application with the Actuator dependency?
Actuator: Your App's Best Friend
Congratulations! You've learned how Spring Boot Actuator transforms your application into a production-ready, observable service.
- It provides crucial insights into your app's health and performance.
- You can easily add it with a single dependency.
- Default endpoints like
/healthand/infoare automatically available. - You can expose more endpoints and even create custom health checks.
Actuator is a must-have for any serious Spring Boot application!
常见问题解答
「使用 Spring Boot Actuator 进行监控」课时是免费的吗?
是的 — 「使用 Spring Boot Actuator 进行监控」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Spring Boot 4 Complete Guide 课程的其余内容,请升级到 CoddyKit PRO。 Spring Boot 4 Complete Guide 课程共包含 4 节课。
「使用 Spring Boot Actuator 进行监控」这节课中我会学到什么?
利用 Spring Boot Actuator 端点在生产环境中监控、管理和检查应用。 你通过在浏览器中直接运行的动手代码来练习 Spring Boot 4 Complete Guide,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Spring Boot 4 Complete Guide 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Spring Boot 4 Complete Guide 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 2 节课,共 4 节。
「使用 Spring Boot Actuator 进行监控」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Spring Boot 4 Complete Guide 课中编写并运行代码吗?
能。每节 Spring Boot 4 Complete Guide 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 将 Spring Boot 应用容器化
- 使用 Spring Boot Actuator 进行监控
- 云部署策略
- 构建 Spring Boot CI/CD 流水线