• 15 heures
  • Moyenne

Ce cours est visible gratuitement en ligne.

course.header.alt.is_video

course.header.alt.is_certifying

J'ai tout compris !

Mis à jour le 03/07/2020

Get started with Spring Boot Actuator

Our Watchlist application is ready to be deployed in production and enjoyed by many users. We know it will be successful and attract many movie lovers from around the world, but there’s a catch. The more users we have, the more sophisticated our production environment should be to cope with the load of user requests.

We'll need to use techniques like clustering, which refers to deploying the application on multiple servers and dividing the load between them. At this point, which is very common for enterprise applications, people who create lists of movies to watch won’t be the only users.

We will have another set of users called system admins or DevOps engineers. These people are usually responsible for designing and managing production environments.  For them to do their job, they need to be able to communicate with an application and get some information about how it's running, and if any special attention is needed.

We can give systems admins or DevOps engineers this information using a set of features called Spring Boot Actuator, which comes ready to go in Spring Boot.  These features add several production-grade services to our application with very little effort. They also help monitor and manage applications using HTTP endpoints when pushed to production. Let's see how to activate this superpower of Spring Boot! 

Enabling Actuator

Add Spring Boot Actuator project through Maven starters like the ones we used in Part 1 of the course. It's been a while, I know! Grab the Maven dependency of Spring Actuator from Spring Boot documentation website, and add it to the POM file of the project. Then check out two of the default end-points.

Thanks to Spring Boot auto-configuration, you can add Actuator to your project with two working endpoints by adding its started dependency to your pom.xml file. So add this inside dependencies tag of your pom.xml file:

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

Restart the server and checkout  http://localhost/actuator/info  and  http://localhost/actuator/health URLs in your browser. The information returned is quite minimal, and that's what we are going to fix now!

Adding custom info

The default  /info  response wasn’t very helpful, was it? We are going to add some information to be returned by this endpoint.  /info endpoint is a way of asking a running instance of the application: "Who are you?" This is particularly useful for sysadmin to make sure a running instance is the right version of the right application and so on. Let's see how we could add more information to it.

Enriching the   /info  endpoint requires adding whatever information about the app you want to return when the /info URL is invoked in the application.properties file. Remember that variable names should start with info.app. For example:

info.app.name=Watchlist
info.app.description=A must have for any movie freak!
info.app.version=1.0.0

Implement a custom health indicator

People always ask, “How are you?” when they see each other. The Actuator health endpoint is a way to ask a Spring Boot application the same question.

Health checks are very important in a production environment. A web server could be up, but not really healthy enough to serve the incoming requests. It could not have enough memory or disk space, unavailable external services or databases, and so on. Spring Boot Actuator lets you check whatever condition you want before you decide if the application is up and running. For our application, one of the factors that can determine its health is if the OMDb API is accessible. Let's check before we return the status "Up" when the  /health  endpoint is hit.

First of all, create a new package and call it actuator for actuator related classes. Any custom health checker needs to implement  HealthIndicator interface, so create a new class in it and call it  HealthCheck. Check if the OMDb API service is available as a sign of overall health of the application.

package com.openclassrooms.watchlist.actuator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.stereotype.Component;

import com.openclassrooms.watchlist.service.MovieRatingService;

@Component 
public class HealthCheck implements HealthIndicator {

	@Autowired
	MovieRatingService movieRatingService;
	
	@Override
	public Health health() {
		
		if (! movieRatingService.getMovieRating("Up").isPresent()) {
			return Health.down().withDetail("Cause", "OMDb API is not available").build();
		}

		return Health.up().build();
	}
}

Now you can hit  http://localhost/actuator/health  endpoint again. Now the communication with OMDb will be checked before returning "Up" as the status. Now disconnect your WiFi, and check the endpoint again and see what status is returned. 

Let's recap!

Before moving on to the next chapter, ask yourself if you remember the steps for how to:

  • Add Spring Boot Actuator to our application.

  • Check the general information and health status of our application.

  • Add custom information to the  /info endpoint.

  • Write a custom health checker using HealthIndicator interface. 

If you're not sure, step back through the chapter and review.  If you're feeling pretty confident, then join me in the next chapter to find out about a few more endpoints. 😛

Exemple de certificat de réussite
Exemple de certificat de réussite