Actuator in Spring Boot

Actuator in Spring Boot

0 Comments

Spring Boot Actuator is a sub-project of the Spring Boot Framework. It uses HTTP endpoints to expose operational information about any running application.

The main benefit of using this library is that we get health and monitoring metrics from production-ready applications. Moreover, the gathering of metrics, understanding traffic, or knowing the state of the database, becomes tremendously easy with Actuator.

In this post, you will learn how to use and configure Spring Boot Actuator.

Maven Dependency for Spring Boot Actuator

To use Actuator in your application, you need to enable the spring-boot-actuator dependency in pom.xml.

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

Example to Demonstrate Spring Boot Actuator

Spring Boot Actuator provides us with a list of available built-in endpoints which get enabled with the addition of the actuator dependency.

However, in the latest versions, only two endpoints are enabled by default, /health and /info.

To understand the various endpoints, let us create a REST Controller class.

This is the code of the DemoController class.

DemoController.java

package guru.springframework.controller;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DemoController {
    @GetMapping("/get-info")
    public String getStringMessage() {
        return "Hi ! You will be able to monitor health of the application !!" ;
    }
}

The preceding code shows a  Rest Controller class annotated with @RestController. This class contains a request handler method that returns a String message.

Build and Run the Application

Run the main class of the application and open the browser to invoke the URL http://localhost:8080/actuator

This is the output that you will see in the browser. The output shows three endpoint URLs.

Let us access the default /health endpoint by pointing the browser to http://localhost:8080/health

The output is this.

Spring Boot Actuator Status

The above output denotes the status UP. It means the application is healthy and running without any interruption.

You can enable all of the built-in endpoints of Actuator. To do so, set the configuration in the application.properties file, like this.

management.endpoints.web.exposure.include=*.

Open the browser and now access the http://localhost:8080/actuator URL.

You will get this output.

 

Notice that we now have a number of endpoints.  Each of these endpoints has its own function. We can access each of these endpoints individually.

From the browser access the /env endpoint by pointing to http://localhost:8080/actuator/env

Notice that this endpoint provides information on the environmental configurations about the server, such as the Operating System, version, CPU type, encoding type, and so on.

Another endpoint that you can check is /metrics through the URL http://localhost:8080/actuator/metrics

This is how the output looks like.

It lists down all the metrics that are available for you to track such as system.cpu.usage, jvm.memory.max and so on.

To get the details of an individual metric, you need to pass the metric name in the URL like this:

http://localhost:8080/actuator/metrics/{MetricName}

For Example, to get the details of system.cpu.usage metric, use the URL:

http://localhost:8080/actuator/metrics/system.cpu.usage

The Output for the above endpoint is this.

Spring Boot Actuator metrics

There are several other endpoints that you can check such as:

  • /beans: It displays a complete list of all the Spring beans in your application.
  • /mappings: This shows a collated list of all @RequestMapping paths.
  • /caches: Exposes available caches.
  • /loggers: Lists down a list of all the configured loggers in your application with their corresponding log levels.
  • /sessions:  Allows retrieval and deletion of user sessions from a Spring Session-backed session store.

Summary

In this Spring Boot Actuator post, we saw a few endpoints with few easy configurations. There are advanced configuration options too like changing the Management endpoint context path. We can change the default context path from /actuator to something else. There is also a configuration option to customize the management server port.

You can find the source code of this post here on Github.

For in-depth knowledge on Actuator in Spring Boot Applications check my Udemy Best Seller Course

About SFG Contributor

Staff writer account for Spring Framework Guru

    You May Also Like

    Leave a Reply

    Your email address will not be published. Required fields are marked *

    This site uses Akismet to reduce spam. Learn how your comment data is processed.