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:
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.
For Spring MVC, the following additional endpoints are used.
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.javatpoint.
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.
Step 9: Open the application.properties file and disable the security feature of the actuator by adding the following statement.
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.