Spring Boot Actuator

Last Updated : 08 Jan, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

Developing and Managing an application are the two most important aspects of the application’s life cycle. It is very crucial to know what’s going on beneath the application. Also when we push the application into production, managing it gradually becomes critically important. Therefore, it is always recommended to monitor the application both while at the development phase and at the production phase. 

For the same use case, Spring Boot provides an actuator dependency that can be used to monitor and manage your Spring Boot application, By /actuator and /actuator/health endpoints you can achieve the purpose of monitoring.

  • With the help of Spring Boot, we can achieve the above objectives.
  • Spring Boot’s ‘Actuator’ dependency is used to monitor and manage the Spring web application.
  • We can use it to monitor and manage the application with the help of HTTP endpoints or with the JMX.
Working of the Spring's Actuator

Working of the Spring’s Actuator

Advantages of Actuator the Application

  1. It increases customer satisfaction.
  2. It reduces downtime.
  3. It boosts productivity.
  4. It improves Cybersecurity Management.
  5. It increases the conversion rate.

1. Configuration for Actuator

In order to use hibernate validators, these configurations are necessary in your Spring Boot project.

1.1 Dependency for Actuator

To use the ‘Actuator’ add the following dependency in your application’s project settings file.

Dependency configuration for both Maven and Gradle build system.

Maven -> pom.xml

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

Gradle

-> build.gradle

dependencies {
implementation 'org.springframework.boot:spring-boot-starter-actuator'
}

1.2 Application Properties configuration for Actuator

There more configurations available for Actuator, few of them are listed:

  • You can also change the default endpoint by adding the following in the application.properties file.
management.endpoints.web.base-path=/details
  • Including IDs/Endpoints

By default, all IDs are set to false except for ‘health’. To include an ID, use the following property in the application.properties file.

management.endpoint.<id>.enabled

Example -> management.endpoint.metrics.enabled=true
  • List down all IDs that you want to include which are separated by a comma.
management.endpoints.web.exposure.include=metrics,info
  • Include only metrics and info IDs and will exclude all others (‘health’ too).

To add/include all ID information about your application, you can do it in the application.properties file by simply adding the following –

management.endpoints.web.exposure.include=*
  • Excluding IDs/Endpoints

To exclude an ID or endpoint, use the following property and list out the respective IDs separated by a comma in the application.properties file.

management.endpoints.web.exposure.exclude

Example -> management.endpoints.web.exposure.exclude=info

Folder structure for projects

The below image demonstrates the picture of how your project must look

Project Structure - Maven

Project Structure – Maven

2. Implementing the project

2.1 Entity

UserEntity.java (Entity class representing the model data) is explained below:

  • This class acts as a simple java bean whose properties are returned as JSON response by the REST API’s get() method.
  • ‘Lombok’ library is used to generate GETTER/SETTER methods automatically at runtime using ‘@Data‘ annotation.
  • @RequiredArgsConstructor‘ annotation is used to generate a zero-argument constructor and if final or ‘@NonNull’ fields are present, then respective arguments constructor is created.
  • To add the ‘Lombok‘ library in your application, add the following dependency in your application’s project build.
  • @Component‘ annotation is used so that this bean automatically gets registered in Spring’s application context.

Java




package gfg;
 
import lombok.Data;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;
 
@Component
@Data
@RequiredArgsConstructor
public class UserEntity {
    String id = "1";
    String name = "Darshan.G.Pawar";
    String userName = "@drash";
    String email = "drash@geek";
    String pincode = "422-009";
}


2.2 Controller

RESTfulController.java (A REST API controller) for defining APIs and testing the program.

This controller’s get() method uses the UserEntity bean to return JSON response. UserEntiy bean is outsourced through ‘@Autowired‘ annotation which was registered in Spring’s application context.

Java




package gfg;
 
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
 
@RestController
@RequestMapping("/get")
public class RESTfulController {
 
    @Autowired
    UserEntity entity;
   
    @GetMapping("/data")
      public UserEntity getEntity(){
        return entity;
    }
}


3. Testing Actuator APIs

3.1 Controller APIs

Here, the JSON Formatter Chrome extension is used to automatically parse the JSON body. Further, it will be required to work with ‘Actuator’.

JSON response returned by REST API

JSON response returned by REST API

3.2 Working with Spring Boot Actuator APIs

To access the ‘Actuator’ services, you will have to use the HTTP endpoint as it becomes reliable to work with.

3.2.1 /actuator

It’s simple just hit the default endpoint ‘/actuator’, ensure that your Application is running.

Example:

You can also change the default endpoint by adding the following in the application.properties file.

management.endpoints.web.base-path=/details

3.2.2 /actuator/health

You can click on these above links and see the respective information. Additionally, you can activate other Actuator IDs and use them after ‘/actuator’ to see more information. For example, ‘health’ ID is activated by default. Therefore you can click the link in the image or directly use ‘http://localhost:8080/actuator/health’.

The health of an application

The health of an application

‘UP’ means the application’s health is good. There are a total of 25 IDs out of which the commonly used are listed out here –

ID

Description         

beans Displays a complete list of all the Spring beans in your application.
caches  Exposes available caches.
conditions Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match.
health Shows application health information.
httptrace Displays HTTP trace information (by default, the last 100 HTTP request-response exchanges). Requires an HttpTraceRepository bean.
loggers Shows and modifies the configuration of loggers in the application.
mappings Displays a collated list of all @RequestMapping paths.
sessions Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Requires a servlet-based web application that uses Spring Session.
threaddump Performs a thread dump.

3.2.3 /actuator/beans

Accessing 'beans' ID of the above project

Accessing ‘beans’ ID of the above project

3.2.4 /actuator/mappings

Accessing 'mappings' ID of the above project

Accessing ‘mappings’ ID of the above project

Including IDs/Endpoints

By default, all IDs are set to false except for ‘health’. To include an ID, use the following property in the application.properties file.

management.endpoint.<id>.enabled

Example -> management.endpoint.metrics.enabled=true

OR, you can just list down all IDs that you want to include which are separated by a comma.

management.endpoints.web.exposure.include=metrics,info

This will include only metrics and info IDs and will exclude all others (‘health’ too). To add/include all ID information about your application, you can do it in the application.properties file by simply adding the following –

management.endpoints.web.exposure.include=*

Output:

All the IDs or the Endpoint are now enabled

All the IDs or the Endpoint are now enabled

Excluding IDs/Endpoints

To exclude an ID or endpoint, use the following property and list out the respective IDs separated by a comma in the application.properties file.

management.endpoints.web.exposure.exclude

Example -> management.endpoints.web.exposure.exclude=info

Use ‘*’ in place of IDs in property to exclude all the IDs or endpoints.

Notes: 

  1. Before setting the management.endpoints.web.exposure.include, ensure that the exposed actuators do not contain sensitive information.
  2. They should be secured by placing them behind a firewall or are secured by something like Spring Security.


Previous Article
Next Article

Similar Reads

Spring Cloud Vs Spring Boot Actuator
Spring CloudSpring Cloud's main purpose is to give tools and services for managing and making distributed systems, especially in a microservices architecture. The developer wants to speed up the process of building patterns in distributed systems, these tools can fulfill their requirement. They include almost all from the management of configuratio
5 min read
Custom Endpoints in Spring boot Actuator
Custom Endpoints in Spring Boot Actuator extends the default functionality provided by the Actuator. It involves defining a custom endpoint class annotated with @Endpoint, which contains methods annotated with @ReadOperation, @WriteOperation, and @DeleteOperation to handle GET, POST, and DELETE requests. Spring Boot Actuator allows you to expose th
4 min read
Difference Between Spring Boot Starter Web and Spring Boot Starter Tomcat
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework
3 min read
Spring Boot – Handling Background Tasks with Spring Boot
Efficiently handling background tasks with Spring Boot is important for providing a smooth user experience and optimizing resource utilization. Background tasks refer to operations that are performed asynchronously or in the background, allowing the main application to continue processing other requests. In a Spring Boot application, background tas
5 min read
Spring Boot – Using Spring Boot with Apache Camel
Apache Camel and Spring Boot are two powerful frameworks that can be seamlessly integrated to build robust, scalable, and efficient applications. Apache Camel is an open-source integration framework that provides an extensive range of components and connectors, enabling developers to integrate different systems, APIs, and applications. It simplifie
5 min read
Spring Boot - Spring JDBC vs Spring Data JDBC
Spring JDBC Spring can perform JDBC operations by having connectivity with any one of jars of RDBMS like MySQL, Oracle, or SQL Server, etc., For example, if we are connecting with MySQL, then we need to connect "mysql-connector-java". Let us see how a pom.xml file of a maven project looks like. C/C++ Code &lt;?xml version=&quot;1.0&quot; encoding=
4 min read
Spring vs Spring Boot vs Spring MVC
Are you ready to dive into the exciting world of Java development? Whether you're a seasoned pro or just starting out, this article is your gateway to mastering the top frameworks and technologies in Java development. We'll explore the Spring framework, known for its versatility and lightweight nature, making it perfect for enterprise-level softwar
8 min read
Java Spring Boot Microservices - Develop API Gateway Using Spring Cloud Gateway
The API Gateway Pattern in some cases stands for “Backend for frontend”. It is basically the entry gate for taking entry into any application by an external source. The pattern is going on in a programmer’s mind while they are making the client’s application. It acts as a medium between the client applications and microservices. For example-Netflix
4 min read
Spring Boot | How to access database using Spring Data JPA
Spring Data JPA is a method to implement JPA repositories to add the data access layer in applications easily. CRUD stands for create, retrieve, update, delete which are the possible operations which can be performed in a database. In this article, we will see an example of how to access data from a database(MySQL for this article) in a spring boot
4 min read
How to Create a Spring Boot Project in Spring Initializr and Run it in IntelliJ IDEA?
Java language is one of the most popular languages among all programming languages. There are several advantages of using the java programming language, whether for security purposes or building large distribution projects. One of the advantages of using Java is that it tries to connect every concept in the language to the real world with the help
3 min read
How to Create and Setup Spring Boot Project in Spring Tool Suite?
Spring Boot is built on the top of the spring and contains all the features of spring. And is becoming a favorite of developers these days because of its rapid production-ready environment which enables the developers to directly focus on the logic instead of struggling with the configuration and setup. Spring Boot is a microservice-based framework
3 min read
How to Run Your First Spring Boot Application in Spring Tool Suite?
Spring Tool Suite (STS) is a java IDE tailored for developing Spring-based enterprise applications. It is easier, faster, and more convenient. And most importantly it is based on Eclipse IDE. STS is free, open-source, and powered by VMware. Spring Tools 4 is the next generation of Spring tooling for the favorite coding environment. Largely rebuilt
3 min read
How to Make a Project Using Spring Boot, MySQL, Spring Data JPA, and Maven?
For the sample project, below mentioned tools got used Java 8Eclipse IDE for developmentHibernate ORM, Spring framework with Spring Data JPAMySQL database, MySQL Connector Java as JDBC driver.Example Project Using Spring Boot, MySQL, Spring Data JPA, and Maven Project Structure: As this is getting prepared as a maven project, all dependencies are s
4 min read
Java Spring Boot Microservices – Integration of Eureka and Spring Cloud Gateway
Microservices are small, loosely coupled distributed services. Microservices architecture evolved as a solution to the scalability, independently deployable, and innovation challenges with Monolithic Architecture. It provides us to take a big application and break it into efficiently manageable small components with some specified responsibilities.
5 min read
What's New in Spring 6 and Spring Boot 3?
Spring and Spring Boot are two of the most popular Java frameworks used by developers worldwide. The Spring team is continuously working on improving and enhancing the frameworks with each new major release. Spring 6 and Spring Boot 3 are expected to bring in significant new features and changes that will further boost development with these techno
5 min read
How to Integrate Keycloak with Spring Boot and Spring Security?
Keycloak is Open Source Identity and Access Management (IAM) solution developed by Red Hat. By using this you can add authentication to applications and secure services with minimum effort. No need to deal with storing users or authenticating users. Keycloak provides user federation, strong authentication, user management, fine-grained authorizatio
2 min read
Java Spring Boot Microservices - Integration of Eureka, Feign & Spring Cloud Load Balancer
Microservices are small, loosely coupled distributed services. Microservices architecture evolved as a solution to the scalability, independently deployable, and innovation challenges with Monolithic Architecture. It provides us to take a big application and break it into efficiently manageable small components with some specified responsibilities.
13 min read
Java Spring Boot Microservices - Client Side Load Balancing with Spring Cloud LoadBalancer
Spring Cloud is a collection of projects like load balancing, service discovery, circuit breakers, routing, micro-proxy, etc will be given by Spring Cloud. So spring Cloud basically provides some of the common tools and techniques and projects to quickly develop some common patterns of the microservices. Basically, there are two ways to load balanc
12 min read
Difference between Spring and Spring Boot
Spring Spring is an open-source lightweight framework that allows Java EE 7 developers to build simple, reliable, and scalable enterprise applications. This framework mainly focuses on providing various ways to help you manage your business objects. It made the development of Web applications much easier compared to classic Java frameworks and Appl
4 min read
Configuring Multiple Spring Batch Jobs in a Spring Boot Application
Spring Batch serves as a robust framework within the Spring ecosystem, specifically tailored for managing batch processing tasks efficiently. It's designed to tackle big data jobs efficiently and comes with handy tools to make batch application development a breeze. In the context of a Spring Boot application, setting up multiple Spring Batch jobs
10 min read
Spring Boot - Reactive Programming Using Spring Webflux Framework
In this article, we will explore React programming in Spring Boot, Reactive programming is an asynchronous, non-blocking programming paradigm for developing highly responsive applications that react to external stimuli. What is Reactive ProgrammingIn reactive programming, the flow of data is asynchronous through push-based publishers and subscriber
4 min read
Spring Boot Batch Processing Using Spring Data JPA to CSV File
The Spring Batch is a framework in the Spring Boot ecosystem It can provide a lot of functionalities for Batch processing. The Spring Batch framework simplifies the batch development of applications by providing reliable components and other patterns for common batch processing concerns. Mostly, batch processing is used to read and write data in bu
7 min read
Show SQL from Spring Data JPA/Hibernate in Spring Boot
In Spring Boot, Spring Data JPA is part of the larger Spring Data Project that can simplify the development of the data access layers in the spring applications using the Java Persistence API and it can provide a higher-level abstraction over the JPA API. It can reduce the boilerplate code and make it easier to work with the database. Spring Data J
6 min read
Spring Boot - Spring Data JPA
Spring Data JPA or JPA stands for Java Persistence API, so before looking into that, we must know about ORM (Object Relation Mapping). So Object relation mapping is simply the process of persisting any java object directly into a database table. Usually, the name of the object being persisted becomes the name of the table, and each field within tha
6 min read
Properties with Spring and Spring Boot
Java-based applications using the Spring framework and its evolution into the Spring Boot and the properties play a crucial role in configuring the various aspects of the application. Properties can allow the developers to externalize the configuration settings from the code. Understanding how to work with properties in the Spring and Spring Boot i
4 min read
Implementing CORS in Spring Boot with Spring Security
CORS issue is one of the common issues being faced in web development. We are here going to configure CORS in our backend application built using Spring Boot and Spring security, being used for security purpose. Before going to fix the issue, lets understand what CORS is and how it works and why browsers show error while sending CORS request. What
6 min read
Spring Boot 3.0 - JWT Authentication with Spring Security using MySQL Database
In Spring Security 5.7.0, the spring team deprecated the WebSecurityConfigurerAdapter, as they encourage users to move towards a component-based security configuration. Spring Boot 3.0 has come with many changes in Spring Security . In this article, we'll learn how to implement JWT authentication and authorization in a Spring Boot 3.0 application u
9 min read
Spring Boot - Dependency Injection and Spring Beans
Spring Boot is a powerful framework for building RESTful APIs and microservices with minimal configuration. Two fundamental concepts within Spring Boot are Dependency Injection (DI) and Spring Beans. Dependency Injection is a design pattern used to implement Inversion of Control (IoC), allowing the framework to manage object creation and dependenci
6 min read
How to Build a RESTful API with Spring Boot and Spring MVC?
RESTful APIs have become the standard for building scalable and maintainable web services in web development. REST (Representational State Transfer) enables a stateless, client-server architecture where resources are accessed via standard HTTP methods. This article demonstrates how to create a RESTful API using Spring Boot and Spring MVC. We will w
7 min read
Spring Security Integration with Spring Boot
Spring Security is a powerful and customizable authentication and access control framework for Java applications. It provides comprehensive security services for Java EE-based enterprise software applications. This article will integrate Spring Security with a Spring Boot application, covering configuration, authentication, and securing RESTful API
5 min read