Spring boot’s actuator module allows us to monitor and manage application usages in production environment, without coding and configuration for any of them. These monitoring and management information is exposed via REST like endpoint URLs.
The simplest way to enable the features is to add a dependency to the spring-boot-starter-actuator starter pom file.
In this Spring boot actuator tutorial, learn about in-built HTTP endpoints available for any boot application for different monitoring and management purposes. Before the spring framework, if we had to introduce this type of monitoring functionality in our applications then we had to manually develop all those components and that too was very specific to our need. But with spring boot we have Actuator module which makes it very easy.
We just need to configure a few things and we are done – all the management and monitoring related information is easily available. Let’s learn to configure Spring boot 2 actuator endpoints.
1. Spring Boot Actuator Module
Spring boot’s module Actuator allows you to monitor and manage application usages in production environment, without coding and configuration for any of them. These monitoring and management information is exposed via REST like endpoint URLs.
Most applications exposes endpoints via HTTP, where the ID of the endpoint along with a prefix of /actuator is mapped to a URL. For example, by default, the health endpoint is mapped to /actuator/health.
By default, only /health and /info are exposed via Web APIs. Rest are exposed via JMX. Use management.endpoints.web.exposure.include=* to expose all endpoints through the Web APIs.
management.endpoints.web.exposure.include=*# To expose only selected endpoints#management.endpoints.jmx.exposure.include=health,info,env,beans
Some of important and widely used actuator endpoints are given below:
ENDPOINT
USAGE
/auditevents
Returns all auto-configuration candidates and the reason why they ‘were’ or ‘were not’ applied.
/beans
Returns a complete list of all the Spring beans in your application.
/mappings
Displays a collated list of all @RequestMapping paths..
/env
Returns list of properties in current environment
/health
Returns application health information.
/caches
It exposes available caches.
/conditions
Shows the conditions that were evaluated on configuration and auto-configuration.
/configprops
It displays a collated list of all @ConfigurationProperties.
/integrationgraph
It shows the Spring Integration graph. Requires a dependency on spring-integration-core.
/loggers
The configuration of loggers in the application..
/scheduledtasks
Displays the scheduled tasks in the application.
/sessions
Returns trace logs (by default the last 100 HTTP requests). Requires an HttpTraceRepository bean.
/httptrace
It allows retrieval and deletion of user sessions from a Spring Session-backed session store. Requires a Servlet-based web application using Spring Session.
/shutdown
Lets the application be gracefully shutdown. Disabled by default.
/threaddump
It performs a thread dump.
/metrics
It shows several useful metrics information like JVM memory used, system CPU usage, open files, and much more.
The Spring web application (Spring MVC, Spring WebFlux, or Jersey) provide the following additional endpoints:
ENDPOINT
USAGE
/heapdump
Returns an hprof heap dump file.
/logfile
Returns the contents of the logfile if logging.file.name or logging.file.path properties have been set.
1.3. Securing Endpoints
By default, Spring Security is enabled for all actuator endpoints if it available in the classpath.
If you wish to configure custom security for HTTP endpoints, for example, only allow users with a certain role to access then configure WebSecurityConfigurerAdapter in following manner:
Actuator endpoints automatically cache the responses to read operations that do not take any parameters. Use cache.time-to-live property to configure the amount of time for which an endpoint will cache the response.
management.endpoint.beans.cache.time-to-live=20s
2. Spring Boot Actuator Endpoint Example
In this example, we will create a simple spring boot application and access the actuator endpoints to know more about them.
2.1. Development environment
JDK 1.8, Eclipse, Maven – Development environment
Spring-boot – Underlying application framework
Spring-boot Actuator – Management endpoints
2.2. Create Maven Project
Start with creating one spring boot project from Spring Initializer site with Web, Rest Repositories and Actuator dependencies. Download project in zipped format. Unzip and then import project in eclipse as maven project.
2.3. Add simple Rest endpoint
Now add one simple Rest endpoint /example to the application.
packagecom.example.springbootmanagementexample;importjava.util.Date;importorg.springframework.web.bind.annotation.GetMapping;importorg.springframework.web.bind.annotation.RestController;@RestControllerpublicclassSimpleRestController {@GetMapping("/example")publicString example() {return"Hello User !! "+ newDate();}}
3. Spring Boot Actuator Endpoints Demo
I have added management.security.enabled=false entry to the application.properties file to disable actuator security. Here I am more interested in actuator endpoints responses.
Do maven build using mvn clean install and start the application using java -jar target\spring-boot-actuator-example-0.0.1-SNAPSHOT.jar command. This will bring up one tomcat server in default port 8080 and application will be deployed in it.
Access /example API in browser to generate few monitoring information on server.
http://localhost:8080/actuator/envThis will give all the environmental configuration about the server.Endpoint env output
http://localhost:8080/actuator/beansThis will give all the spring beans loaded in the context.Endpoint beans output
http://localhost:8080/actuator/threaddumpThis will give the current server thread dump.Endpoint threaddump output
Those endpoints will give standard information in the browser. These are the basic important endpoints we generally refer, but spring boot provides many more endpoints as mentioned in this link
4. Actuator Advance Configuration Options
4.1. Change the Management endpoint context path
By default all endpoints comes in default context path of the application, suffixed with /actuator. If for some reason, we have existing endpoints in application starting with /actuator then we can customize the base path to something else.
All we need to specify the new base path in the application.properties.
management.endpoints.web.base-path=/manage
Now you will be able to access all actuator endpoints under a new URL. e.g.
/manage/health
/manage/dump
/manage/env
/manage/beans
4.2. Customize the management server port
To customize the management endpoint port, we need to add this entry in the application.properties file.
management.server.port=8081
5. Summary
In this spring boot actuator example, we learned to configure management and monitoring endpoints with few easy configurations. So next time, you need to add application health checks or add monitoring support, you should consider adding the Spring actuator project and use these endpoints.
Feel free to drop your questions in the comments section.