Spring Boot Actuator is a powerful tool that provides production-ready features to help you monitor and manage your application. It includes a set of endpoints that expose operational information about the running application, such as health status, metrics, and more. However, sometimes you need to create custom endpoints tailored to your specific requirements.
In this tutorial, we will walk through the process of creating custom Actuator endpoints in Spring Boot. We'll cover the necessary steps, best practices, and provide real-world code examples to help you get started.
Before diving into the implementation, ensure that you have the following prerequisites:
First, let's set up a basic Spring Boot project. You can use Spring Initializr to bootstrap your project quickly.
Go to Spring Initializr.
Configure your project with the following settings:
com.examplecustom-actuator-endpointsCustom Actuator EndpointsDemo project for custom actuator endpointscom.example.customactuatorendpointsAdd the following dependencies:
Click on "Generate" to download the project zip file.
Extract the zip file and import it into your IDE.
Now, let's create a custom Actuator endpoint that exposes some operational information about our application.
Create a new Java class in the com.example.customactuatorendpoints package. Let's call it CustomEndpoint.
package com.example.customactuatorendpoints;
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 customEndpoint() {
return "This is a custom Actuator endpoint!";
}
}
CustomEndpoint class as a Spring component, making it eligible for auto-detection when using component scanning.custom. The endpoint will be accessible at /actuator/custom.@WriteOperation for write operations.Ensure that Actuator is enabled in your Spring Boot application. By default, it should be enabled if you added the Spring Boot Actuator dependency. However, you can explicitly enable it by adding the following property to your application.properties file:
management.endpoints.web.exposure.include=custom,health,info
This configuration exposes the custom endpoint along with the default health and info endpoints.
Run your Spring Boot application. You can do this by executing the main class or using the following Maven command:
mvn spring-boot:run
Once the application is running, you can access your custom endpoint at http://localhost:8080/actuator/custom. You should see the response "This is a custom Actuator endpoint!".
You can return structured data from your endpoint by using a map or a custom class. Let's modify our endpoint to return some JSON data.
package com.example.customactuatorendpoints;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;
import java.util.HashMap;
import java.util.Map;
@Component
@Endpoint(id = "custom")
public class CustomEndpoint {
@ReadOperation
public Map<String, Object> customEndpoint() {
Map<String, Object> response = new HashMap<>();
response.put("message", "This is a custom Actuator endpoint!");
response.put("timestamp", System.currentTimeMillis());
return response;
}
}
Now, when you access the /actuator/custom endpoint, you will receive JSON data:
{
"message": "This is a custom Actuator endpoint!",
"timestamp": 1633072800000
}
You can conditionally expose your endpoints based on certain conditions. For example, you might want to expose the endpoint only in production.
package com.example.customactuatorendpoints;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.context.annotation.ConditionalOnProperty;
import org.springframework.stereotype.Component;
@Component
@Endpoint(id = "custom")
@ConditionalOnProperty(name = "management.endpoints.web.exposure.include", havingValue = "custom")
public class CustomEndpoint {
@ReadOperation
public String customEndpoint() {
return "This is a custom Actuator endpoint!";
}
}
In this example, the CustomEndpoint will only be exposed if the property management.endpoints.web.exposure.include is set to custom.
In this tutorial, we have learned how to create custom Actuator endpoints in Spring Boot. We covered the basics of defining and exposing endpoints, returning JSON data, and applying conditional exposure. By following these steps and best practices, you can effectively monitor and manage your application with custom Actuator endpoints tailored to your specific needs.
Feel free to explore more advanced features of Spring Boot Actuator and customize your endpoints further based on your application's requirements.