Lesson 2: Actuators in Boot 2

1. Goals

In this lesson, we'll focus on a core feature of Spring Boot - actuators.


2. Lesson Notes

The relevant module you need to import when you're starting with this lesson is: m4-actuators-in-boot-2-start

If you want have a look at the fully implemented lesson, as a reference, feel free to import: m4-actuators-in-boot-2-end


2.1. Actuators

Simply put, actuators are monitoring tools. They bring production ready features into our app for very low effort.

More specifically, they provide various endpoints mainly exposed via HTTP (but also JMX) which basically help in monitoring and, to some extent, managing our application.


2.2. Actuators in Spring Boot

Through actuators, Spring Boot provides these built-in endpoints focused on auditing, health checks, and displaying metrics information.

The best way to enable actuators is to add the spring-boot-starter-actuator dependency:

Boot provides a number of built-in endpoints, well documented in the official reference.

Out of these, two endpoints are enabled by default: the /health and /info endpoints.


2.3. The /health Endpoint

Let’s run the application and have a look at the /health endpoint first. We can open up the browser and access this at: http://localhost:8080/actuator/health.

Naturally, the health endpoint shows information about the health of the application:

{"status":"UP"}

The default output is, of course, the bare minimum - just enough to know our application is up and running.


2.4. The /info Endpoint

Let’s hit the /info endpoint: http://localhost:8080/actuator/info

This endpoint shows information about our application. At this point, we haven’t configured or defined any, so the endpoint by default, won’t contain any data:

{}

Of course, the format is JSON as we expect.

Populating the response of this endpoint is quite easy, as everything is configurable via properties.

Let’s add some info about our app name and description:

Now, when we hit the info endpoint, we’ll see:

{

   "lsapp": {

       "name": "Learn Spring Application",

       "description": "Learn Spring Application Developed With Spring Boot 2"

   }

}

Next, let’s do some simple configuration of the actuators.


2.5. Actuators Context Path

By default, all actuators are available at /actuators/{endpoint_name} path. This can be easily configured via properties.

Let’s open up application.properties and change the base path of all actuators:

Now all endpoints will be available at: /monitoring/{endpoint_name}.

We can further change paths of specific actuators also. For example, if we want to make our info actuator available at /monitoring/information, we’ll do:

LS - M4 - L2.pdf