opencodez

Simple Guide to Learn Spring Boot Actuators with Example – Source Code on GitHub

In this article, we will understand the Spring Boot Actuators. We will go through the implementation and basic usage of this fantastic Spring Boot feature.

Softwares used

What is Spring Boot Actuators?

When a large scale enterprise application is running then the operations team is always worried about its health parameters. They have to keep a constant tab on whats working and what is not working? Whether the application is up or down. In general scenario development team can write logic for such endpoints and can expose them but it takes a lot of efforts.

For such scenarios, Spring Boot Actuators comes very handily. Just by adding some dependencies in your POM, your application can have out of the box features that will help you monitor your application environment. You can expose these features as HTTP endpoints and access via the web.

Enabling Actuators

For enabling Spring boot actuator following dependencies need to be added in the POM file. By adding below dependency, a user can access all 16 actuator endpoints.

xdependencyx
	xgroupIdxorg.springframework.bootx/groupIdx
	xartifactIdxspring-boot-starter-actuatorx/artifactIdx
x/dependencyx

Accessing Actuator Endpoints

After you add above dependency and start your application, you can access the list of all endpoints at http://localhost:8080/actuator

x

Below are some major endpoints:

All endpoints and detail information for those can be furnished from official page.

Changes have been introduced in Spring boot 2.0.0

Here is a catch since the release of Spring boot 2.0.0. Spring ensures that all points cannot be accessed directly and includes some other securities relevant updates as well. Following changes have been introduced in Spring boot 2.0.0.

Please refer migration guide for detailed information.

Security for Endpoints

There are some endpoints those are pertaining to critical information and we don’t want to expose for other roles who are not intended to have such information.

We can add authorization here or we can stop exposing sensitive endpoints by making it false. For authorization we have to use spring security for that we need to add following dependency.

xdependencyx    
	xgroupIdxorg.springframework.bootx/groupIdx    
	xartifactIdxspring-boot-starter-securityx/artifactIdx
x/dependencyx

Including above dependency, Spring security start acting and use in-built form based authorization with the configured user and a password.

So due to this, we can have prime role of ADMIN and ACTUATOR who are authorized to get access of sensitive endpoints. You also need to add below supporting configuration in an application.properties file.

management.endpoints.web.exposure.include=*
management.security.enabled=true
security.basic.enabled=true
security.user.name=act
security.user.password=act
management.security.roles=ACTUATOR
management.context-path=/actuator

You can also include a list of required endpoints separated by a comma (,). as

management.endpoints.web.expose= health, beans, trace, info, metrices

Customizing an Endpoint

Spring also provides adding custom endpoints if you have any application specific feature that you want to exapose and monitor. Just add a @Bean annotated with @Endpoint, In this class any methods annotated with @ReadOperation, @WriteOperation, or @DeleteOperation are automatically exposed over JMX and, in a web application, over HTTP as well. Endpoints can be exposed over HTTP using Jersey, Spring MVC, or Spring WebFlux.

@Component
@Endpoint(id = "myfeatures")
public class CustomFeatureEndPoint {
	
	private MapxString, Objectx features = new ConcurrentHashMapxx();
	 
    @ReadOperation
    public MapxString, Objectx features() {
    	features.put("customFeature", "Hello World!");
        return features;
    }
}

Output:

Conclusion

This article briefly explains What is Spring Boot Actuators, What is itxs use and how you can integrate with your application easily.

You can download the reference code from our GitHub repo.

Download Code