Spring Boot Actuator

Spring boot actuator is one of the most useful starter provided by the Spring framework. Spring boot actuator is used to check the status of our application after the deployment. Or in other words we can Spring Actuator is the sub-module of spring boot that provides the endpoint to check the status of our application. These endpoint allows us to manage our application , We can enable and disable these endpoints as per our requirements.

Let’s say we have a application that is deployed to the production. Now we want to check the health status of this application like it’s UP & running or not. Using Spring boot actuator we can easily check these status of our running application.

To enable spring boot actuator in our spring boot project we have to add the below dependency in our pom.xml file.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

After adding the above dependency in your pom.xml file you just need to start the application. Once your application started just try to call the /actuator endpoint

For example if our application is running on http://localhost:8080. Just append the /actuator at the end and you ‘ll see the actuator endpoint as the response like below:

You can see while calling http://localhost:8080/actuator endpoint we are getting some URL as the response. By default health endpoint is enable in /actuator.

This health point is tell us the status of our running application. Just try calling http://localhost:8080/actuator/health and you ‘ll see status as UP that means our application is UP and running.

As we see by default only health endpoint is enable in actuator. But if we want to see all the endpoint provided by the actuator, We have to add the below property in out application.properties file.

management.endpoints.web.exposure.include=*

After adding above property in your application.properties file just restart your application and try to access the http://localhost:8080/actuator again. This time you ‘ll see all the endpoint provided by actuator in the response.

How to use Specific Endpoints of Spring Boot Actuator

As you see using this management.endpoints.web.exposure.include=* property we can see all the endpoints provided by the actuator. But sometime we only need specific endpoints and don’t want to use other endpoints of actuator. So for example if we want to access only health and info endpoint of actuator, For this we place these endpoints manually instead of * like below:

management.endpoints.web.exposure.include=info,health

In above property we have define info and health endpoint. So if you restart your application and call actuator endpoint you ‘ll see only health and info endpoint details as the response.

Exclude Spring boot actuator endpoints

Sometime we want to exclude or we can say some time we don’t want to enable some endpoints of actuator. For Example we want to enable all the endpoints of actuator but we don’t need the info and beans endpoint. So to disable or exclude them we can use the exclude property in our application.properties file like below.

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=info,beans

Change Context root path of Spring Boot Actuator endpoints

If you observe the context root path of all the actuator endpoints are set to /actuator in spring boot by default. Means if you want to access any actuator endpoint you have to access it via /actuator. For example if we want to check the health endpoint then we have to call the url http://localhost:8080/actuator/health

Now if you want to change this default context root path of actuator, We can change it very easily. Suppose i want to access actuator endpoint by /javadream context path or you can choose any name of your choice. So for this we have to add the below property in our application.properties file.

management.endpoints.web.base-path=/javadream

Now instead of /actuator you have to use the /javadream to access the actuator endpoint. So if we have to access the health endpoint of actuator so our URL ‘ll be http://localhost:8080/javadream/health

Spring boot actuator health
Change Management server port of Spring Boot Actuator endpoints

If you observer till now we are accessing the actuator endpoint on the same port on which our application is running. Say if our application is running on port 8080, So to access actuator endpoint we have to use the same port like http://localhost:8080/actuator URL or if we change the context root path so URL ‘ll be http://localhost:8080/javadream. But port is same on which our application is running. Now if we want to change this port we have to use the below property in our application.properties file.

management.server.port=8083

We have used 8083 port you can use any port of your choice. Now if you restart your application and try to access the old port URL ( http://localhost:8080/javadream/health ) for accessing actuator endpoint you ‘ll get the 404 error because you have change the port. Now try to change the port from 8080 to 8083 in the URL and see the response.

Actuator Context Path

That’s all. If you reached till here so your application.properties file ‘ll be look like below:

Spring Actuator properties

You can see more details about Spring Boot Actuator in it’s official documentation https://docs.spring.io/spring-boot/docs/2.0.x/actuator-api/html/

We are done hope you enjoyed this article. Please share this with others if you like this article 🙂