TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Spring Boot Actuator

Spring Boot Actuator with features, project, starter project wizard, cli, application, annotations, dm, properties, actuator, thymeleaf view, jpa, jdbc

<< Back to SPRING

Spring Boot Starter Actuator

Spring Boot Actuator

Spring Boot Actuator is a sub-project of the Spring Boot Framework. It includes a number of additional features that help us to monitor and manage the Spring Boot application. It contains the actuator endpoints (the place where the resources live). We can use HTTP and JMX endpoints to manage and monitor the Spring Boot application. If we want to get production-ready features in an application, we should use the Spring Boot actuator.

Spring Boot Actuator Features

There are three main features of Spring Boot Actuator:

  • Endpoints
  • Metrics
  • Audit

Endpoint: The actuator endpoints allows us to monitor and interact with the application. Spring Boot provides a number of built-in endpoints. We can also create our own endpoint. We can enable and disable each endpoint individually. Most of the application choose HTTP, where the Id of the endpoint, along with the prefix of /actuator, is mapped to a URL.

For example, the /health endpoint provides the basic health information of an application. The actuator, by default, mapped it to /actuator/health.  

Metrics: Spring Boot Actuator provides dimensional metrics by integrating with the micrometer. The micrometer is integrated into Spring Boot. It is the instrumentation library powering the delivery of application metrics from Spring. It provides vendor-neutral interfaces for timers, gauges, counters, distribution summaries, and long task timers with a dimensional data model.

Audit: Spring Boot provides a flexible audit framework that publishes events to an AuditEventRepository. It automatically publishes the authentication events if spring-security is in execution.

Enabling Spring Boot Actuator

We can enable actuator by injecting the dependency spring-boot-starter-actuator in the pom.xml file.

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

Spring Boot Actuator Endpoints

The actuator endpoints allow us to monitor and interact with our Spring Boot application. Spring Boot includes number of built-in endpoints and we can also add custom endpoints in Spring Boot application.

The following table describes the widely used endpoints.

Id Usage Default
actuator It provides a hypermedia-based discovery page for the other endpoints. It requires Spring HATEOAS to be on the classpath. True
auditevents It exposes audit events information for the current application. True
autoconfig It is used to display an auto-configuration report showing all auto-configuration candidates and the reason why they 'were' or 'were not' applied. True
beans It is used to display a complete list of all the Spring beans in your application. True
configprops It is used to display a collated list of all @ConfigurationProperties. True
dump It is used to perform a thread dump. True
env It is used to expose properties from Spring's ConfigurableEnvironment. True
flyway It is used to show any Flyway database migrations that have been applied. True
health It is used to show application health information. False
info It is used to display arbitrary application info. False
loggers It is used to show and modify the configuration of loggers in the application. True
liquibase It is used to show any Liquibase database migrations that have been applied. True
metrics It is used to show metrics information for the current application. True
mappings It is used to display a collated list of all @RequestMapping paths. True
shutdown It is used to allow the application to be gracefully shutdown. True
trace It is used to display trace information. True

For Spring MVC, the following additional endpoints are used.

Id Description Default
docs It is used to display documentation, including example requests and responses for the Actuator's endpoints. False
heapdump It is used to return a GZip compressed hprof heap dump file. True
jolokia It is used to expose JMX beans over HTTP (when Jolokia is on the classpath). True
logfile It is used to return the contents of the logfile. True
prometheus It is used to expose metrics in a format that can be scraped by a prometheus server. It requires a dependency on micrometer-registry- prometheus. True

Spring Boot actuator properties

Spring Boot enables security for all actuator endpoints. It uses form-based authentication that provides user Id as the user and a randomly generated password. We can also access actuator-restricted endpoints by customizing basicauth security to the endpoints. We need to override this configuration by management.security.roles property. For example:

management.security.enabled=true
management.security.roles=ADMIN
security.basic.enabled=true
security.user.name=admin
security.user.passowrd=admin

Spring Boot Actuator Example

Let's understand the concept of the actuator through an example.

Step 1: Open Spring Initializr https://start.spring.io/ and create a Maven project.

Step 2: Provide the Group name. We have provided com.TheDeveloperBlog.

Step 3: Provide the Artifact Id. We have provided the spring-boot-actuator-example.

Step 4: Add the following dependencies: Spring Web, Spring Boot Starter Actuator, and Spring Data Rest HAL Browser.

Step 5: Click on the Generate button. When we click on the Generate button, it wraps all the specifications related to the project into a Jar file and downloads it to our local system.

Spring Boot Starter Actuator

Step 6: Extract the Jar file and paste it into the STS workspace.

Step 7: Import the project folder.

File -> Import -> Existing Maven Projects -> Browse -> Select the folder spring-boot-actuator-example -> Finish

It takes some time to import. After importing the project, we can see the project directory in the package explorer section.

Spring Boot Starter Actuator

Step 8: Create a Controller class. We have created the controller class with the name DemoRestController.

DemoRestController.java

package com.TheDeveloperBlog;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoRestController 
{
@GetMapping("/hello")
public String hello() 
{
return "Hello User!";
}
}

Step 9: Open the application.properties file and disable the security feature of the actuator by adding the following statement.

application.properties

management.security.enabled=false

Step 10: Run the SpringBootActuatorExampleApplication.java file.

Step 11: Open the browser and invoke the URL http://localhost:8080/actuator. It returns the following page:

{"_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},"info":{"href":"http://localhost:8080/actuator/info","templated":false}}}

The application runs on port 8080 by default. Once the actuator has started, we can see the list of all the endpoints exposed over HTTP.

Let's invoke the health endpoint by invoking the URL http://localhost:8080/actuator/health. It denotes the status UP. It means the application is healthy and running without any interruption.

Spring Boot Starter Actuator

Similarly, we can invoke other endpoints that helps us to monitor and manage the Spring Boot application.







Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf