Table of Contents
- What is Spring Boot Actuator?
- Why Use Actuator?
- Adding Actuator to Your Project
- Exposing Actuator Endpoints
- Commonly Used Endpoints
- Customizing Endpoint Exposure
- Securing Actuator Endpoints
- Actuator and Health Monitoring
- Metrics and Integration with Prometheus/Grafana
- Creating Custom Actuator Endpoints
- Summary
1. What is Spring Boot Actuator?
Spring Boot Actuator is a powerful feature of the Spring Boot ecosystem that enables production-ready features for monitoring and managing applications. It exposes various REST endpoints to monitor the internal state and behavior of your application without changing the code logic.
2. Why Use Actuator?
- Health Checks – Monitor application readiness and liveness.
- Metrics – View memory usage, CPU load, and JVM statistics.
- Audit Events – Track important system events.
- Thread Dumps – Analyze running threads.
- Environment Info – View environment variables, active profiles, etc.
- Integration – Easily integrate with Prometheus, Micrometer, and other monitoring tools.
3. Adding Actuator to Your Project
Add the Spring Boot Actuator dependency in your pom.xml
:
xmlCopyEdit<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Or if you’re using Gradle:
groovyCopyEditimplementation 'org.springframework.boot:spring-boot-starter-actuator'
4. Exposing Actuator Endpoints
By default, only the /actuator/health
and /actuator/info
endpoints are exposed over HTTP. To expose all or specific endpoints, configure your application.properties
or application.yml
:
propertiesCopyEditmanagement.endpoints.web.exposure.include=*
Or selectively:
propertiesCopyEditmanagement.endpoints.web.exposure.include=health,info,metrics,env
5. Commonly Used Endpoints
Endpoint | Description |
---|---|
/actuator/health | Application health status |
/actuator/info | App info like version, build |
/actuator/metrics | Detailed metrics (JVM, CPU, etc.) |
/actuator/env | Environment variables |
/actuator/beans | List of all Spring beans |
/actuator/loggers | View/change logging levels |
/actuator/mappings | Show request-to-handler mappings |
To get a full list, visit /actuator
after exposing endpoints.
6. Customizing Endpoint Exposure
You can include/exclude endpoints individually:
propertiesCopyEditmanagement.endpoints.web.exposure.include=health,metrics
management.endpoints.web.exposure.exclude=beans,env
To change the base path for all actuator endpoints:
propertiesCopyEditmanagement.endpoints.web.base-path=/management
7. Securing Actuator Endpoints
Actuator endpoints can expose sensitive application internals. Use Spring Security to restrict access:
javaCopyEdit@Configuration
public class SecurityConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
http
.authorizeRequests()
.antMatchers("/actuator/health", "/actuator/info").permitAll()
.antMatchers("/actuator/**").hasRole("ADMIN")
.anyRequest().authenticated()
.and()
.httpBasic();
}
}
You can also use application.properties
to enable HTTP Basic authentication.
8. Actuator and Health Monitoring
The /actuator/health
endpoint provides insight into the application’s status. You can include detailed health info:
propertiesCopyEditmanagement.endpoint.health.show-details=always
Spring Boot integrates with various health indicators:
- Database health
- Disk space
- Redis/Mongo/RabbitMQ connections
- Custom health indicators (covered below)
Custom Health Indicator
javaCopyEdit@Component
public class CustomHealthIndicator implements HealthIndicator {
@Override
public Health health() {
// You can add logic here to check custom conditions
return Health.up().withDetail("customService", "Available").build();
}
}
9. Metrics and Integration with Prometheus/Grafana
Spring Boot integrates with Micrometer, which supports metrics exporting.
To use Prometheus:
xmlCopyEdit<dependency>
<groupId>io.micrometer</groupId>
<artifactId>micrometer-registry-prometheus</artifactId>
</dependency>
Expose the Prometheus endpoint:
propertiesCopyEditmanagement.endpoints.web.exposure.include=prometheus
Then access metrics at /actuator/prometheus
and configure Prometheus to scrape it.
10. Creating Custom Actuator Endpoints
You can define your own actuator endpoints:
javaCopyEdit@Component
@Endpoint(id = "custom")
public class CustomEndpoint {
@ReadOperation
public Map<String, String> customEndpoint() {
return Map.of("status", "Custom actuator endpoint working");
}
}
The above creates an endpoint at /actuator/custom
.
11. Summary
Spring Boot Actuator offers comprehensive monitoring and management capabilities for Spring Boot applications. By exposing standardized and custom endpoints, you gain real-time insights into application behavior, making it invaluable for debugging, observability, and DevOps automation.