Table of contents
1.
Introduction
2.
Spring Boot Actuator
3.
How to Use the Production Ready Features
3.1.
➡️Steps to Add Monitoring Service in our Spring Boot Project
3.1.1.
1️⃣Add the following Dependencies in pom.xml
3.1.2.
⭐Spring Boot Starter Actuator
3.1.3.
⭐Spring Data Rest HAL Browser
3.1.4.
2️⃣Restart the Application
3.1.5.
3️⃣Enter the URL
4.
HAL Browser
5.
Frequently Asked Questions
5.1.
What is Spring Boot Actuator?
5.2.
What are the two protocols you can use to access actuator endpoints? 
5.3.
What value does Spring Boot Actuator provide?
5.4.
What are metrics?
5.5.
What is Health Indicator?
6.
Conclusion
Last Updated: Mar 27, 2024
Easy

Monitoring APIs with Spring Boot Actuator

Author Akshat
0 upvote

Introduction

Spring Boot Actuator is a component of the Spring Boot Framework. It exposes operational data about any application currently running using HTTP endpoints. The primary advantage of utilizing this library is that it provides health and monitoring metrics from applications ready for production. 

Monitoring APIs with Spring Boot Actuator

In this article, we will discuss Monitoring APIs with Spring Boot Actuator.

Spring Boot Actuator

With the help of Spring Boot, applications can be effectively managed and monitored. This tool has HTTP endpoints (the place where the resource lives). It is a Spring Boot subsidiary project. It more easily adds several production-grade services to our application. We should find out the cause if a service's performance declines or failure as soon as possible. The API needs to be monitored, especially when we create microservices. Spring Boot has excellent monitoring support.

Here's your problem of the day

Solving this problem will increase your chance to get selected in this company

Skill covered: Programming

How do you create a function in JavaScript?

Choose another skill to practice

How to Use the Production Ready Features

We will add the spring-boot-actuator dependency to the pom.xml file to use the production-ready features.

➡️Steps to Add Monitoring Service in our Spring Boot Project

Let's discuss the steps to add a monitoring service to our Spring Boot project

1️⃣Add the following Dependencies in pom.xml

⭐Spring Boot Starter Actuator

It offers numerous monitoring options for your services.
 

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

 

⭐Spring Data Rest HAL Browser

We can export our API using HAL Browser, and the documentation is accessible from the API itself.
 

<dependency>  
<groupId>org.springframework.data</groupId>  
<artifactId>spring-data-rest-hal-browser</artifactId>  
</dependency>  

 

The Hypertext Application Language (HAL) is a straightforward language that makes it simple and consistent to link between API resources. The spring boot starter actuator is in HAL format. The HAL browser looks for APIs and finds the links. The link is displayed on the screen so we can quickly browse the API.

2️⃣Restart the Application

Restart the HAL Browser application.

3️⃣Enter the URL

In the browser, type localhost:8080/actuator and press the enter key. Use localhost:8080/application if it doesn't work.

The actuator is launched and displays the three URLs—self, health, and information.
 

{"_links":{"self":{"href":"http://localhost:8080/actuator","templated":false},"health":
{"href":"http://localhost:8080/actuator/health","templated":false},"health-component":
{"href":"http://localhost:8080/actuator/health/{component}","templated":true},"health-component-instance":
{"href":"http://localhost:8080/actuator/health/{component}/{instance}","templated":true},"info":
{"href":"http://localhost:8080/actuator/info","templated":false}}}  

 

The application's health is displayed when we click on the URL for health. The status up in the following image indicates that the application is active.
 

{“status”:”UP”}

 

The info URL displays the application's information when we click on it. Blank curly braces indicate that no data is available.
 

{}

 

We must configure the property to make the information available.
 

💯Enable web exposure by opening the application.properties file by writing the following code.

management.endpoints.web.exposure.include=*  

💯Restart the application.

💯Use the URL localhost:8080/actuator to restart the actuator.

HAL Browser

We can now use the HAL browser to access the actuator. In the text field of the Explorer, type "actuator" and press the Go button.

 

HAL Browser

It displays everything associated with the actuator. Beans are the most crucial component of the actuator.

 

The spring boot project's beans are displayed when we click the bean's arrow.

 

HAL Browser

 

Clicking on the health link will allow us to see the application's current status.

Bean arrow

It displays the health of the application.
 

Metrics is a different link in the actuator. It displays a list of valid metrics.

 

Let's say we want to know how much memory the application uses. We have accessed /jvm.memory.max.
 

The value in the following image represents the total memory the application will use.
 

Total memory

 

There are two significant links in the actuator: httptrace and mapping.

 

http and mapping

 

All the requests we have previously made are displayed in the httptrace. All the information about previously completed requests is visible, as shown below.

http method

 

The mapping displays all items that are mapped to a URL. We map many URLs whenever we develop web services or web applications.

Frequently Asked Questions

What is Spring Boot Actuator?

Spring Boot Actuator is a component of the Spring Boot Framework. It exposes operational data about any application currently running using HTTP endpoints.

What are the two protocols you can use to access actuator endpoints? 

You can use JMX and HTTP to connect to actuator endpoints with Spring Boot.

What value does Spring Boot Actuator provide?

For a production application, being able to monitor and interact with your application through the Spring Boot actuator is crucial. Using JMX without a spring boot actuator, you must create your monitoring and interaction system.

What are metrics?

This Spring Boot actuator endpoint displays metrics data for your Spring Boot application, allowing you to see how much memory it is using, how much memory is free, how long it has been running, how many threads are currently active, and other information.

What is Health Indicator?

A Spring Boot actuator health indicator indicates an application's health. Monitoring software can access and use this health information at the /health endpoint. Numerous health indicators are automatically configured by default.

Conclusion

In the article Monitoring APIs with Spring Boot Actuator, we have started our discussion with the Spring Boot Actuator. Further continued our conversation with how to use the production-ready features and HAL Browser at the end. You can refer to similar articles for further information.

 

You can also consider our Spring Boot Course to give your career an edge over others.

Happy Learning Ninja! 🥷

Live masterclass