• Skip to main content
  • Skip to primary sidebar
  • Home
  • Java
    • Spring
      • Spring Cloud
    • Design Patterns
    • Java Miscellaneous Tips

Coding Strain

All about code

Spring Boot for Cloud – Actuator

November 6, 2022 by mario.casari@gmail.com Leave a Comment

Introduction

In previous articles, we talked about some features of Spring Boot that are very useful in the development phase of our system: we described simplified dependency management and API development, for instance. In this article, we are going to cover functionalities that are related to a production environment.

When a Spring Boot application is finally in production, it needs all the tools to manage and monitor it. This is especially important in complex scenarios like those related to micro-service systems. Spring Boot provides an effective solution to provide the required functionalities, the Actuator. The Actuator is a Spring Boot module with a set of features available as HTTP or JMX endpoints, or even with a remote SSH shell.

The HTTP endpoints are available if we use a Spring Boot Web dependency, by the spring-boot-starter-web starter. In this article, we will show how to activate the Actuator module and describe the main features available with it, in the context of a web application.

Spring Boot Actuator

Activating the Spring Boot Actuator

The Spring Boot Actuator is a set of functionalities that are very useful in a production environment. It can be introduced in our project by the following starter:

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

A Spring Boot application configured with a spring-boot-starter-web and spring-boot-starter-actuator dependencies will expose a set of URL endpoints to manage, monitor, and gather statistics of the running system. We show here some of the endpoints available:

  • /actuator: it shows a list of the other endpoints
  • /env: shows properties from the application environment
  • /health: provides health info on system components
  • /metrics: shows statistics about memory, threads, and so on.
  • /heapdump: executes a dump of Java heap memory
  • /shutdown: can be used to perform a “graceful” shutdown. It is not available by default.

In the following sections, we will make a quick review focused mainly on the /health and /metrics endpoints, and some configurations required to properly use the actuator features.

Configuring the Actuator to show all the endpoints

To access the single endpoints we need a path starting with /actuator. For instance, to access the /health endpoint we have to use the following address:

http://localhost:8080/actuator/health

The /actuator path itself is supposed to show a list of all the available endpoints. With the actuator starter dependency and no other additional configuration in place, we will see that if we try to access the actuator endpoint only the /health endpoint will be available. In fact, If we run the application with the “mvn spring-boot:run” Maven command and enter the http://localhost:8080/actuator/health address in our browser, we will have the following result:

{
    "_links": {
       "self": {
          "href": "http://localhost:8080/actuator",
          "templated": false
       },
       "health": {
          "href": "http://localhost:8080/actuator/health",
          "templated": false
      },
      "health-path": {
         "href": "http://localhost:8080/actuator/health/{*path}",
         "templated": true
      }
   }
}

In order to show all the endpoints, we must include the following peace of configuration in the application.yaml file:

management:
   endpoints:
      web:
         exposure:
            include: '*'

Or, in case we have an application.properties file instead, we will use the corresponding plain text version: “management.endpoints.web.exposure.include=’*’ “.

With the above configuration, if we start the application and access again /actuator endpoint we will see a complete list, as we can see in the following screenshot:

Complete list of endpoints shown by /actuator path
Complete list of endpoints shown by /actuator path

Securing the Spring Boot Actuator endpoints

By default, the endpoints are not secured and are freely available without providing any credentials. To add a security layer we have to add the following starter to the POM:

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

A further required step would be to configure a username and password in the application.yaml file (or application.properties):

spring:
   security:
      user:
         name: username
         password: pass

If we start the application again with the above configuration and try to access, for instance, the actuator endpoint, we will get the following:

Login page to access actuator endpoints
Login page to access actuator endpoints

By entering the username and password and clicking on the “Sing in” button, we will get again the list described in the previous section.

About the Spring Boot Actuator /health Endpoint, and How to Make it Show Detailed Information

The /health endpoint plays an important role in a production scenario: it allows a system administrator to quickly check if the application is in a healthy condition: if all its components are up, and if all the required resources, like for instance disk space, are available. If we start the application without any additional setup, we will see that by default the /health endpoint shows only generic status information:

{
   "status": "UP"
}

The status will be “UP” as long as all the application components and resources are up and available. In order to get more detailed health information we have to add the following peace of configuration:

management:
   endpoint: 
      health:
         show-details: always

If we then restart the application and access the /health endpoint again, we will obtain something like this:

Health endpoint with detailed information
Health endpoint with detailed information

As we can see, with the additional configuration described above, we have some more information, in this case about the status of an H2 DB instance and disk space. The service behind the /health endpoint uses specific health indicator classes to show all the information available for a particular component or resource. A number of predefined indicators are available, for example DataSourceHealthIndicator and DiskSpaceHealthIndicator.

It is also possible to define custom indicators, by extending the AbstractHealthIndicator abstract class and overriding the doHealthCheck method.

The Spring Boot Actuator /metrics endpoint

The /metrics endpoint allows us to obtain various statistics about our running application. The main path http://localhost:8080/actuator/metrics shows a list of available metrics. As we can see in the following screenshot, we obtain a list of names.

List of /metric items
List of /metric items

In order to access the details of a single metric from the list we must complete the path with its name. For instance, if we want to check how many classes were loaded into the Java Virtual Machine, we can use the following address: http://localhost:8080/actuator/metrics/jvm.classes.loaded, as shown in the screenshot below.

Information detail of a single /metrics item.

Conclusion

Moving a Spring boot application into production requires all the functionalities to manage, assess the status and health of the running application, and gather detailed statistics. Spring Boot provides the features mentioned above through the Actuator module described in this article.

A sample application is available at the following GitHub address: https://github.com/mcasari/codingstrain/tree/main/spring-boot-minimal-rest-h2-actuator.

Filed Under: Spring Cloud

Reader Interactions

Leave a Reply Cancel reply

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

Primary Sidebar

Recent Posts

  • Spring Cloud: How to Deal with Microservice Configuration (Part I)
  • Java Map: Preserve Insertion Order
  • Java Double: Parse String with Comma
  • Spring Boot for Cloud – Actuator
  • Spring Boot for Cloud – REST API Development

Recent Comments

    Copyright © 2023 · Magazine Pro on Genesis Framework

    We use cookies on our website to give you the most relevant experience by remembering your preferences and repeat visits. By clicking “Accept All”, you consent to the use of ALL the cookies. However, you may visit "Cookie Settings" to provide a controlled consent.
    Cookie SettingsAccept All
    Manage consent

    Privacy Overview

    This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.
    Necessary
    Always Enabled
    Necessary cookies are absolutely essential for the website to function properly. These cookies ensure basic functionalities and security features of the website, anonymously.
    CookieDurationDescription
    cookielawinfo-checkbox-analytics11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Analytics".
    cookielawinfo-checkbox-functional11 monthsThe cookie is set by GDPR cookie consent to record the user consent for the cookies in the category "Functional".
    cookielawinfo-checkbox-necessary11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookies is used to store the user consent for the cookies in the category "Necessary".
    cookielawinfo-checkbox-others11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Other.
    cookielawinfo-checkbox-performance11 monthsThis cookie is set by GDPR Cookie Consent plugin. The cookie is used to store the user consent for the cookies in the category "Performance".
    viewed_cookie_policy11 monthsThe cookie is set by the GDPR Cookie Consent plugin and is used to store whether or not user has consented to the use of cookies. It does not store any personal data.
    Functional
    Functional cookies help to perform certain functionalities like sharing the content of the website on social media platforms, collect feedbacks, and other third-party features.
    Performance
    Performance cookies are used to understand and analyze the key performance indexes of the website which helps in delivering a better user experience for the visitors.
    Analytics
    Analytical cookies are used to understand how visitors interact with the website. These cookies help provide information on metrics the number of visitors, bounce rate, traffic source, etc.
    Advertisement
    Advertisement cookies are used to provide visitors with relevant ads and marketing campaigns. These cookies track visitors across websites and collect information to provide customized ads.
    Others
    Other uncategorized cookies are those that are being analyzed and have not been classified into a category as yet.
    SAVE & ACCEPT