Spring Boot Starter Actuator
Spring Boot Actuator
Spring Boot Actuator is a sub-project of the Spring Boot Framework. It includes a number of additional features that help us to monitor and manage the Spring Boot application. It contains the actuator endpoints (the place where the resources live). We can use HTTP and JMX endpoints to manage and monitor the Spring Boot application. If we want to get production-ready features in an application, we should use the Spring Boot actuator.
Spring Boot Actuator Features
There are three main features of Spring Boot Actuator:
- Endpoints
- Metrics
- Audit
Endpoint: The actuator endpoints allows us to monitor and interact with the application. Spring Boot provides a number of built-in endpoints. We can also create our own endpoint. We can enable and disable each endpoint individually. Most of the application choose HTTP, where the Id of the endpoint, along with the prefix of /actuator, is mapped to a URL.
For example, the /health endpoint provides the basic health information of an application. The actuator, by default, mapped it to /actuator/health.
Metrics: Spring Boot Actuator provides dimensional metrics by integrating with the micrometer. The micrometer is integrated into Spring Boot. It is the instrumentation library powering the delivery of application metrics from Spring. It provides vendor-neutral interfaces for timers, gauges, counters, distribution summaries, and long task timers with a dimensional data model.
Audit: Spring Boot provides a flexible audit framework that publishes events to an AuditEventRepository. It automatically publishes the authentication events if spring-security is in execution.
Enabling Spring Boot Actuator
We can enable actuator by injecting the dependency spring-boot-starter-actuator in the pom.xml file.
Spring Boot Actuator Endpoints
The actuator endpoints allow us to monitor and interact with our Spring Boot application. Spring Boot includes number of built-in endpoints and we can also add custom endpoints in Spring Boot application.
The following table describes the widely used endpoints.
Id | Usage | Default |
---|---|---|
actuator | It provides a hypermedia-based discovery page for the other endpoints. It requires Spring HATEOAS to be on the classpath. | True |
auditevents | It exposes audit events information for the current application. | True |
autoconfig | It is used to display an auto-configuration report showing all auto-configuration candidates and the reason why they ‘were’ or ‘were not’ applied. | True |
beans | It is used to display a complete list of all the Spring beans in your application. | True |
configprops | It is used to display a collated list of all @ConfigurationProperties. | True |
dump | It is used to perform a thread dump. | True |
env | It is used to expose properties from Spring’s ConfigurableEnvironment. | True |
flyway | It is used to show any Flyway database migrations that have been applied. | True |
health | It is used to show application health information. | False |
info | It is used to display arbitrary application info. | False |
loggers | It is used to show and modify the configuration of loggers in the application. | True |
liquibase | It is used to show any Liquibase database migrations that have been applied. | True |
metrics | It is used to show metrics information for the current application. | True |
mappings | It is used to display a collated list of all @RequestMapping paths. | True |
shutdown | It is used to allow the application to be gracefully shutdown. | True |
trace | It is used to display trace information. | True |
For Spring MVC, the following additional endpoints are used.
Id | Description | Default |
---|---|---|
docs | It is used to display documentation, including example requests and responses for the Actuator’s endpoints. | False |
heapdump | It is used to return a GZip compressed hprof heap dump file. | True |
jolokia | It is used to expose JMX beans over HTTP (when Jolokia is on the classpath). | True |
logfile | It is used to return the contents of the logfile. | True |
prometheus | It is used to expose metrics in a format that can be scraped by a prometheus server. It requires a dependency on micrometer-registry- prometheus. | True |
Spring Boot actuator properties
Spring Boot enables security for all actuator endpoints. It uses form-based authentication that provides user Id as the user and a randomly generated password. We can also access actuator-restricted endpoints by customizing basicauth security to the endpoints. We need to override this configuration by management.security.roles property. For example:
Spring Boot Actuator Example
Let’s understand the concept of the actuator through an example.
Step 1: Open Spring Initializr https://start.spring.io/ and create a Maven project.
Step 2: Provide the Group name. We have provided com.tutoraspire.
Step 3: Provide the Artifact Id. We have provided the spring-boot-actuator-example.
Step 4: Add the following dependencies: Spring Web, Spring Boot Starter Actuator, and Spring Data Rest HAL Browser.
Step 5: Click on the Generate button. When we click on the Generate button, it wraps all the specifications related to the project into a Jar file and downloads it to our local system.
Step 6: Extract the Jar file and paste it into the STS workspace.
Step 7: Import the project folder.
File -> Import -> Existing Maven Projects -> Browse -> Select the folder spring-boot-actuator-example -> Finish
It takes some time to import. After importing the project, we can see the project directory in the package explorer section.
Step 8: Create a Controller class. We have created the controller class with the name DemoRestController.
DemoRestController.java
Step 9: Open the application.properties file and disable the security feature of the actuator by adding the following statement.
application.properties
Step 10: Run the SpringBootActuatorExampleApplication.java file.
Step 11: Open the browser and invoke the URL http://localhost:8080/actuator. It returns the following page:
The application runs on port 8080 by default. Once the actuator has started, we can see the list of all the endpoints exposed over HTTP.
Let’s invoke the health endpoint by invoking the URL http://localhost:8080/actuator/health. It denotes the status UP. It means the application is healthy and running without any interruption.
Similarly, we can invoke other endpoints that helps us to monitor and manage the Spring Boot application.