How to Check the Health of a Spring boot Application using Spring Actuator

Tega Isiboge
6 min readMay 18, 2022

Objectives:

  • Learn about Health check indicators in Spring Boot.
  • How to use the actuator library and endpoint “/actuator/health”, to monitor our application health.

Requirements:

Let’s begin by starting a spring boot project.

Step 1: Via IntelliJ

  1. Launch your IntelliJ
  2. Open new from the file menu, click on the project and a new window will appear (as shown below), fill in the necessary information,
  • Name the project (I called mine ‘actuatordemo’)
  • Select the location where you want your project to be housed.
  • Select Java as the programming language
  • Select Maven as the project type.
  • You can call the Group anything (I called mine ‘com.tega’)
  • Let us refer to the Artifact as actuatordemo.
  • Using the dropdown list, select the Java SDK version.
  • Choose your Java version as well. I chose 11
  • Our project will be packaged as a Jar file.
  • JAR is an abbreviation for Java Archive. It is a ZIP file type that is used to combine several files into one.

Then, click on Next,

Starting a Maven project from IntelliJ IDEA
  1. Select the Spring Boot version (mine is 2.6.7).
  2. Select any other dependencies needed for your project. The appropriate Spring Boot starters will be inserted based on the dependencies specified in the pom.xml file.
  3. Following the selection of these dependencies, click the Finish button as shown:
Dependencies required for the sping boot application

Let’s Understand the dependencies chosen;

Spring Web: Converts your project into a web application. The spring-boot-starter-web dependency transitively pulls in all web development dependencies. Spring MVC, REST, and Tomcat as the default embedded servers are used.

Spring Starter Actuator: Actuator is a production grade monitoring tool. It’s a library mainly used to expose operational information about the running application — health, metrics, info, dump, env, etc. It uses HTTP endpoints or JMX beans to enable us to interact with it.

Once this dependency is on the class-path, several endpoints are available for us out of the box. Monitoring our app, gathering metrics, understanding traffic, or the state of our database become trivial with this dependency. To Learn more about Spring Actuator Library, please visit here.

Step 2: Using Spring Initializer:

1. Visit start.spring.io

2. Enter the Maven project properties, including dependencies, as shown below, then click the Generate button. This will create a zip folder for your project.

Start a Spring boot application using Spring Initializer

3. Locate the folder, Unzip it, then open it in your preferred code editor to allow maven index the dependencies with Maven and setup your project for start up.

4. Open your IDE (mine is IntelliJ), and click on the file button on the navigation menu and click open, it will take you to your folder structure, locate the unzipped project and click open.

Opening a spring boot project from file

Maven Management

The dependencies and Maven plugins in our project are housed in the pom.xml file.

The dependency section contains the dependencies we added to the project, the dependencies are Spring Web and Spring Starter Actuator.

POM.xml file for Project dependences

RUNNING A SPRINGBOOT APPLICATION

Upon start-up, Spring boot creates the initial context and launches the Spring Boot application via the @SpringBootApplication annotation and a single line of code as shown below:

Spring boot starter application

Ideally, by default your application is configured to start on server port 8080, however, I configured mine to start on port 8081 because my port 8080 is already in use by another application. See below for how I configured a new port in my applications.properties file

Application Configuration file (application.properties)

Next

Run the application by pressing the play button at the top right corner (I have underlined mine with blue) as shown below:

When the application has started (this should happen in a few seconds), open your browser (Chrome or Microsoft Edge) and visit http://localhost:8080/health/actuator (do not forget I am running mine on port 8081, so my URL would look like http://localhost:8081/health/actuator).

and you should see this

Internet browser showing the health of the spring application using the actuator/health

By interpreting the results, It’s a 200 OK response.

The response says that the status is UP.

If we want to see more information about these health checks, we can simply configure the library in our applications.properties file.

We would change management.endpoint.health.show-details to always.

Run your application again to allow the new configuration propagate, then visit the same endpoint http://localhost:8080/health/actuator

Now the response would look like below

Health Check for Starters

Usually, starters come with their own health indicator auto-configured. For Example, adding spring-boot-starter-jdbc adds a database Health check. Let’s add the following dependencies to our spring boot project and restart the application.

Now restart your application when the dependencies have been completely propagated with the application.

When we go back to our browser and hit the actuator health check endpoint (http://localhost:8080/actuator/health), it also returns information about the database as well.

See that after the first status, it shows the components of the application and it shows the database and the details of the db.

Conclusion:

In this article we learnt how to use the spring actuator library to access the health endpoint of actuator and check the health of our application and its components.

We have used IntelliJ IDE, for writing our small pieces of code, and running our spring boot application.

I hope this has been helpful.

Please leave me a comment below if you might require some guidance using achieving this..

The code as mentioned in this post is available here in my github repository.

--

--