Spring Boot Actuator is a sub-project of Spring Boot that provides built-in endpoints that help you monitor and manage your application. These endpoints expose information about the runtime state of your application, making it easier to troubleshoot issues and understand how your application behaves in production.
In this tutorial, we will explore how to use Actuator endpoints in a Spring Boot application. We'll cover the basics of setting up Actuator, configuring endpoints, and using them to monitor and manage your application.
Before you start, ensure that you have:
First, let's create a new Spring Boot application. You can use the Spring Initializr to bootstrap your project.
Unzip the downloaded file and import it into your IDE.
By default, Spring Boot Actuator includes several endpoints that are automatically enabled when you add the spring-boot-starter-actuator dependency. These endpoints provide information about the application's health, metrics, and other aspects.
If you want to enable specific endpoints or customize their behavior, you can do so in your application.properties or application.yml file.
application.properties# Enable all Actuator endpoints
management.endpoints.web.exposure.include=*
# Enable only specific endpoints
management.endpoints.web.exposure.include=health,info
# Change the base path for Actuator endpoints
management.endpoints.web.base-path=/actuator
application.ymlmanagement:
endpoints:
web:
exposure:
include: health,info
base-path: /actuator
Let's explore some of the most commonly used Actuator endpoints.
/health EndpointThe /health endpoint provides information about the application's health. By default, it shows basic health status (UP or DOWN).
curl http://localhost:8080/actuator/health
{
"status": "UP"
}
/info EndpointThe /info endpoint provides general information about the application, such as build details.
curl http://localhost:8080/actuator/info
{
"build": {
"version": "1.0-SNAPSHOT",
"artifact": "demo",
"name": "demo"
}
}
/metrics EndpointThe /metrics endpoint provides various metrics about the application, such as memory usage, thread count, and more.
curl http://localhost:8080/actuator/metrics
{
"names": [
"jvm.memory.max",
"jvm.threads.live",
"http.server.requests"
]
}
/env EndpointThe /env endpoint provides information about the application's environment variables and configuration properties.
curl http://localhost:8080/actuator/env
{
"activeProfiles": [],
"propertySources": [
{
"name": "server.ports",
"properties": {
"local.server.port": {
"value": 8080
}
}
},
{
"name": "applicationConfig: [classpath:/application.properties]",
"properties": {
"management.endpoints.web.exposure.include": {
"value": "health,info"
}
}
}
]
}
You can customize Actuator endpoints by adding your own information or modifying existing ones.
To add custom health indicators, create a class that implements the HealthIndicator interface.
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;
@Component
public class CustomHealthIndicator implements HealthIndicator {
@Override
public Health health() {
int errorCode = check(); // perform some specific health checks
if (errorCode != 0) {
return Health.down().withDetail("Error Code", errorCode).build();
}
return Health.up().build();
}
private int check() {
// Implement your custom health checks here
return 0; // Return 0 for healthy, non-zero for unhealthy
}
}
To add a custom endpoint, create a class annotated with @Endpoint and implement the necessary methods.
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;
@Component
@Endpoint(id = "custom")
public class CustomEndpoint {
@ReadOperation
public String customOperation() {
return "This is a custom endpoint response.";
}
}
By default, Actuator endpoints are not secured. To secure them, you can configure Spring Security.
First, add the Spring Security dependency to your pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Then, configure security in a configuration class.
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
@Configuration
@EnableWebSecurity
public class SecurityConfig {
@Bean
public UserDetailsService userDetailsService() {
UserDetails user =
User.withDefaultPasswordEncoder()
.username("user")
.password("password")
.roles("USER")
.build();
return new InMemoryUserDetailsManager(user);
}
@Bean
public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
http.authorizeRequests(authorize -> authorize
.requestMatchers("/actuator/**").authenticated()
.anyRequest().permitAll())
.httpBasic(withDefaults());
return http.build();
}
}
Spring Boot Actuator provides a powerful set of tools for monitoring and managing your Spring Boot applications. By understanding how to use and customize Actuator endpoints, you can gain valuable insights into your application's behavior and ensure it runs smoothly in production.