- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Spring Boot Actuator A Production Grade Feature in Spring Boot
Spring Boot Actuator is one of the greatest and most useful feature in Spring Boot framework. Actuator module in spring boot helps application developers to implement the production grade features like metrics, health check, security, etc. with minimal effort.
This article will guide you through how to enable the spring boot actuator, configure endpoints and how to modify the default settings in the application.properties file. Note that spring boot actuator can work only for the spring boot application, this can not be integrated to the non-spring boot applications.
List of Endpoints Supported
Here is the list of actuator endpoints that is supported at the time of writing this article. There will be new features added to the endpoints for every new release. In this article I would explain few of the endpoints that are very commonly used in the production environment.
- actuator
- autoconfig
- beans
- configprops
- dump
- env
- health
- info
- metrics
- mappings
- shutdown
- trace
- logfile
- flyway
- liquibase
How to enable actuator endpoints?
By default, actuator endpoints are enabled to your spring boot applications. But, it is very simple to enable actuator feature in your application. If you add the required dependency to your pom.xml file, then that will enable the actuator feature to your application.
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
Once you have added the above entries in your pom.xml file, restart the application. It will be enabled. Note that, there are multiple endpoints in the actuator module. Only few of them enabled by default and only few of them are accessible to anonymous user. Some of the endpoints would have security protection to prohibit unauthorized access. This configurations can be modified to change the behaviour of each endpoints.
Endpoints Configuration
Each endpoints have the configurations that are enabled with default configurations at the application start up. One of the important attribute for the endpoint is “sensitive”, if the value of this attribute is “false”, this implies that this endpoint can be accessed without any authentication. If the value is “true”, then you have to add the Spring Security module in the class path to access the endpoints. This values also can be configured in the application.properties file.
Here is an Example of Configurations
endpoints.info.id=information endpoints.info.sensitive=false endpoints.info.enabled=true information.app.name=Actuator Example information.app.description=Actuator Example information.app.version=1.0.0
Metrics
The default URI for this endpoint is /metrics. This endpoint will be enabled by default when you are adding the spring boot actuator module in the classpath. As the name of the endpoint itself indicates that this feature will be useful for finding the information about memory, heap, processor, class loading and thread pool.
We can classify the metrics as system metrics, datasource metrics, cache metrics and session metrics. Also application developer can add their own metrics implementation to their spring boot application.
If you could run the application and access the endpoint URI at /metrics, you would get the JSON response like the one below:
{ "mem":200162, "mem.free":42675, "processors":4, "instance.uptime":11631, "uptime":27519, "systemload.average":-1.0, "heap.committed":153088, "heap.init":65536, "heap.used":110412, "heap":921600, "nonheap.committed":48320, "nonheap.init":2496, "nonheap.used":47075, "nonheap":0, "threads.peak":13, "threads.daemon":10, "threads.totalStarted":16, "threads":12, "classes":5773, "classes.loaded":5773, "classes.unloaded":0, "gc.ps_scavenge.count":8, "gc.ps_scavenge.time":196, "gc.ps_marksweep.count":1, "gc.ps_marksweep.time":127, "httpsessions.max":-1, "httpsessions.active":0 }
Health
It is important for any production application to periodically to check if the application is up or down. This endpoint facilitate that requirement by exposing a endpoint URI /health which returns the status message for your application. When you access the URI, you would get the response message like the one below:
{ "status" : "UP" }
The above message indicates that the application is currently running without any issues. This service can be used by monitoring tools to check and alert if the application is not running.
Info
Info endpoint is exposed by default at URI /info. If you access this endpoint, it displays the information about the application. Look at the following configurations:
endpoints.info.id=information endpoints.info.sensitive=false endpoints.info.enabled=true info.app.name=Spring Boot Actuator Sample Application info.app.description=Spring Boot Actuator Sample Application info.app.version=1.0.0
The above configuration modifies the default endpoint info to information. Also sets the applications details name, description and version. The sensitive attribute of the endpoint ensures that corresponding endpoint can be made public or requires authentication. Another attribute enabled is to ensure if you want to expose that endpoint or not.
If you could run the application and access the endpoint as /information, you would see the following information displayed as JSON format.
{ “app”: { “description”:“Spring Boot Actuator Sample Application”, “name”:“Spring Boot Actuator Sample Application”, “version”:“1.0.0” } }
Example Application
I have written a very simple example application to demonstrate the spring boot actuator functionality. You can just following these steps to complete the setup of your first spring boot actuator application.
Create a Maven Project
First step is to create a maven project. The final package structure for the example application would be similar to the one below:
Create Maven Build File
Create a pom.xml file and add the following entries to the maven build file.
project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>org.spring</groupId> <artifactId>boot-sample</artifactId> <packaging>war</packaging> <version>0.0.1-SNAPSHOT</version> <name>boot-sample Maven Webapp</name> <url>http://maven.apache.org</url> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.3.3.RELEASE</version> </parent> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> </dependencies> <build> <finalName>boot-sample</finalName> </build> </project>
Create Spring Boot Application File
Spring Boot application starter file is used for running the spring boot application. This class has to be annotated with the @SpringBootApplication annotation.
Application.java
package spring.boot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
Create Application Properties File
This file is the important spring boot configuration file for loading custom configurations.
application.properties
server.contextPath=/nature endpoints.info.id=information endpoints.info.sensitive=false endpoints.info.enabled=true info.app.name=Spring Boot Actuator Sample Application info.app.description=Spring Boot Actuator Sample Application info.app.version=1.0.0
Run Spring Boot Actuator Example Application
Once you have build the project, just right click on the Application.java file to start the spring boot application. When you are starting your application, the actuator endpoint will be enabled. Here is the URL to access the actuator endpoints and screenshots of the example application.
http://localhost:8080/nature/health
http://localhost:8080/nature/metrics
http://localhost:8080/nature/info
In this article I have explained the key concepts about the spring boot actuator and provided simple example to demonstrate how to enable the enspoints and also configured few endpoints like infor, metrics and health. In the similar way you will be able to configure any other endpoints supported by the actuator module.