The Spring Boot Actuator provides production-ready features for our Spring Boot application. Additionally, this library provides all endpoints for production — without having to write a single line of code just by adding the proper dependency.
The Spring Boot Actuator mainly exposes endpoints like health, metrics, logger, info, thread dump, and more. Let’s take a closer look!
Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Endpoints Provided by the Spring Boot Actuator
-
/auditevents
— lists security audit-related events such as user login/logout. Also, we can use this to filter by principal or type among other fields. -
/beans
— returns all available beans in ourBeanFactory
. Unlike/auditevents
, it doesn’t support filtering. -
/conditions
— formerly known as/autoconfig
, this endpoint builds a report of conditions around auto-configuration. -
/configprops
— allows us to fetch all@ConfigurationProperties
beans. -
/env
— returns the current environment properties. Additionally, we can retrieve single properties. -
/flyway
— provides details about our Flyway database migrations. -
/health
– summarizes the health status of our application. -
/heapdump
— builds and returns a heap dump from the JVM used by our application. -
/info
— returns general information. It might be custom data, build information, or details about the latest commit. -
/liquibase
— behaves like/flyway
, but for Liquibase. -
/logfile
— returns ordinary Java application logs. -
/loggers
— enables us to query and modify the logging level of our application. -
/metrics
— details the metrics of our application. This might include generic metrics as well as custom ones. -
/prometheus
— returns metrics like the previous one, but formatted to work with a Prometheus server. -
/scheduledtasks
— provides details about every scheduled task within our application. -
/sessions
— lists HTTP sessions, given we are using Spring Session. -
/shutdown
— performs a graceful shutdown of the application. -
/threaddump
— dumps the thread information of the underlying JVM.
By default, all of these endpoints are exposed as REST APIs and as JMX endpoints. Spring Boot provides a set of properties for controlling the endpoints available to REST APIs and also for JMX.
management.endpoints.jmx.exposure.include
and management.endpoints.jmx.exposure.exclude
are two properties for controlling the exposed endpoints for JMX.
management.endpoints.web.exposure.exclude
and management.endpoints.web.exposure.include
are two properties for controlling the exposed endpoints for the REST API.
Configuring Endpoints
By default, for performance, all of the actuator’s endpoints responses are cached for reading operations, which do not take any parameters. We can also set cache time using property below:
management.endpoint.beans.cache
Instead, we can also set time-to-live at each endpoint level, like so:
management.endpoint.loggers.cache
CORS Support
The Spring Boot Actuator provides a set of properties for allowing requests from a specified host. It also provides the type of request methods to support methods from remote hosts:
management.endpoints.web.cors.://example.com
management.endpoints.web.cors.,POST
Implementing Custom Endpoints
The Spring Boot Actuator provides the @Endpoint
annotation to specify the endpoint.
By default, this endpoint is allowed for both HTTP and JMX.
The @Endpoint
-annotated class will have @ReadOperation
, @WriteOperation
, and @DeleteOperation
. These annotated methods will provide a new endpoint for all of these endpoints.
@Selector
is an annotation to mark the method argument as part of the request URI.
If we cleanly observe the HealthEndpoint
class, it is annotated with @Endpoint
, and it has one of the methods annotated with @ReadOperation
.
public Health healthForComponentInstance( String component, String instance) {
….
}
In the above code, method, component, and instance are annotated with @Selector
, and a result of the REST API syntax is:
http://localhost:8080/actuator/health/{component}/{instance}
If we want to restrict endpoints to web or JMX, we can use @JMXEnpoint
or @WebEndpoint
; these endpoints are restricted to only specified technology.
@EndpointExtension
is used as meta-annotation for @Endpoint
to indicate the extension support for an endpoint. These extensions support filters where these endpoints need to execute.
@EndpointWebExtension
and @EndpointJmxExtension
are two annotation extensions for a specific technology.
Customizing the Management Server Port
By default, management endpoints will use the HTTP protocol, but it is sensible for choice for cloud-based deployments.
The Spring Boot Actuator provides configuration property to expose management endpoints in a different port:
management.server
It also provides the configuration for enabling SSL for management endpoints.
Customizing the Management Server Address
management.server.address
is the property used to specify the list of addresses from where we want to listen.
If we want to listen to management requests only from the localhost, then specify the property value as 127.0.0.1. For this to work, the management endpoint must be configured to a different port from the HTTP port.
Disabling HTTP Endpoints
We can disable the management endpoint in two ways:
- We can set
management.server.port
to -1 management.endpoints.web.exposure.exclude
andmanagement.endpoints.jmx.exposure.exclude
set these two properties to*
to exclude all properties.
Conclusion
Today, we learned more about the Spring Boot Actuator, endpoints provided by the Actuator, its use cases, and how to customize the Spring Boot Actuator.