DEV Community

Marcos Henrique
Marcos Henrique

Posted on

Health Check with Java Spring easier than walking backwards

🚨 Direct to the point, the solution is Spring Actuator.

For this post I'll assume that you already familiarized with Spring Ecossystem, but if you aren't I recommends to you Marcos Maia's [post].
(https://dev.to/thegroo/spring-boot-crash-course-21nm)

We have two ways to implement this little guy in our project:

  • Creating a Spring Boot application from zero;
  • Adding Spring Boot Actuator to an existing application;

Creating a Spring Boot application with Actuator

Let’s create a simple Spring Boot application with actuator dependency.

You can create the app using Spring Boot CLI like so:

spring init -d=web,actuator -n=actuator-demo actuator-demo
Enter fullscreen mode Exit fullscreen mode

Another way would be generate the app from Spring Initializr website:

Adding Spring Boot Actuator to an existing application

You can add spring-boot-actuator module to an existing spring boot application using the following dependency.
If you are using maven:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>
</dependencies>
Enter fullscreen mode Exit fullscreen mode

For Gradle, add the dependency like this example bellow

dependencies {
    compile("org.springframework.boot:spring-boot-starter-actuator")
}
Enter fullscreen mode Exit fullscreen mode

Now let's check or verify the integrity of our API

Let’s explore the health endpoint by opening the http://localhost:<your port>/actuator/health URL.

The endpoint should display the following if everything goes well:

{
    "status": "UP"
}
Enter fullscreen mode Exit fullscreen mode

The status will be UP as long as the application is healthy.

It will show DOWN if the application gets unhealthy due to any issue like connectivity with the database or lack of disk space etc.

But Spring Actuator is not limited to just that with it we can do Health check, Auditing, Metrics gathering and Monitoring, so it is a powerful tool for the general monitoring of our application and you can learn more about it here.

Thank you for your time and reading, until next time.

Top comments (0)