Spring Boot Actuator is a powerful module that provides production-ready features to help you monitor and manage your Spring Boot application.
📌 What is Actuator?
Spring Boot Actuator exposes several built-in endpoints that give insights into your application:
-
Health
-
Metrics
-
Environment
-
Beans
-
Thread dumps
-
HTTP trace
-
Custom endpoints
✅ Step-by-Step Setup
🧱 1. Add Dependency
Maven:
Gradle:
⚙️ 2. Configuration (application.yml
or .properties
)
application.yml
(Recommended)
If using .properties
🚀 3. Access Endpoints
Once Actuator is enabled and running (on default port 8080), you can hit these endpoints:
Endpoint | Description | URL Example |
---|---|---|
/actuator | List all enabled endpoints | http://localhost:8080/actuator |
/actuator/health | Application health status | http://localhost:8080/actuator/health |
/actuator/metrics | Performance metrics (memory, etc.) | http://localhost:8080/actuator/metrics |
/actuator/beans | All Spring beans | http://localhost:8080/actuator/beans |
/actuator/env | All environment variables | http://localhost:8080/actuator/env |
/actuator/info | Project info (from info.* ) | http://localhost:8080/actuator/info |
/actuator/loggers | Log levels (change at runtime) | http://localhost:8080/actuator/loggers |
🔐 4. Security with Spring Security (Optional)
By default, Actuator endpoints are secured if Spring Security is on the classpath.
To allow all endpoints without auth (not for production):
Login using admin/admin123
in browser when accessing endpoints.
➕ Custom Info Endpoint
Add this to application.yml
:
Then check: http://localhost:8080/actuator/info
➕ Custom Health Indicator
🛠 Expose Actuator on a Different Port (Optional)
Now actuator is accessible at http://localhost:9090/actuator
.
📦 Use Cases
Use Case | Benefit |
---|---|
Kubernetes liveness/readiness | Check app status automatically |
Prometheus/Grafana | Integrate metrics and monitoring |
Debugging | View beans, thread dumps, env vars |
DevOps Friendly | Health, metrics, restart, shutdown |
📌 Summary
Feature | Default Status | Configurable |
---|---|---|
/actuator/health | ✅ Enabled | Yes |
/actuator/info | ✅ Enabled | Yes |
Others like /metrics , /beans | ❌ Disabled by default | ✅ Enable via config |
✅ 2. Expose Custom Metrics using Micrometer
📌 Custom Metric Example
Use orderMetrics.incrementOrderCount()
inside your service when an order is created.
✅ 3. Write a Custom Actuator Endpoint
📦 Step-by-Step Custom Endpoint
a. Add a dependency for custom actuator endpoint support (optional)
No extra dependency needed unless you're using older Spring Boot (<2.0)
b. Custom Endpoint Code
This exposes: http://localhost:8080/actuator/customstatus
You must explicitly expose it:
✅ Bonus: Custom HealthIndicator
It becomes part of /actuator/health
.
No comments:
Post a Comment