Understanding, Spring Boot Actuator
Posted By : Yasir Zuberi | 30-Dec-2018
I had a need while working on a Spring Boot project to monitor logs.
So I came across this library called Spring Boot Actuator, which provides production-ready features for Spring Boot application. Like monitoring, access logs, collecting health metrics, database state, environment and much more.
Once the application is deployed on production, it's really important to keep health check and
ensure
application is up and running. Specifically for those applications where we need zero downtime.
Earlier we had to write code for checking application's health. But Spring Boot Actuator comes with some out of the box endpoints for this purpose.
In order to enable Spring Boot Actuator, simply add spring-boot-actuator dependency to package manager.
In Gradle:
compile('org.springframework.boot:spring-boot-starter-actuator')
In Maven:
org.springframework.boot
spring-boot-starter-actuator
Few useful and handy endpoint:-
- health - Shows application health information.
- info - Displays application info.
- loggers - Shows and modifies a configuration of loggers in an application.
- metrics - Shows several useful metrics info like system CPU usage, JVM memory used, and much more.
- shutdown - To shutdown application gracefully.
- env - Information on Spring Environment properties.
By default, only two available are /health and /info.
To enable all endpoints, need to set below the property
//To expose all endpoint
management.endpoints.web.exposure.include=*
//To Disable exposing specific endpoint like shutdown and beans
management.endpoints.web.exposure.exclude=shutdown,beans
In order to use Info endpoint, need to configure below properties in an application.properties file
info.app.name=Spring Boot Actuator Example
info.app.java.version=1.8.0
info.app.type=Spring Boot Application
Management endpoint context path customization
By default, endpoints are in default context path of application with /actuator. But if there's a need to expose endpoints in different endpoint, we need to specify that in our application.properties file.
management.context-path=/manage-my-application
After updating above you will be able to access all spring boot actuator endpoints by new URL.
/manage-my-application/health
/manage-my-application/dump
/manage-my-application/env
/manage-my-application/beans
Management server port customization
For customizing management endpoint port, need to add below entry in our application.properties file.
management.port=8081
Spring Security for Actuator Endpoints
We also need to add Spring Security to our application for enabling additional endpoints as other endpoints need at least basic authentication.
Simply add below dependency for spring security to your application.
//Gradle
compile('org.springframework.boot:spring-boot-starter-security')
//Maven
org.springframework.boot
spring-boot-starter-security
Now, add below properties for spring security username and password in the application properties file.
spring.security.user.name=username
spring.security.user.password=admin
So now if you try to access secured actuator endpoints, you need to provide login credentials.
Summary
Spring Boot Actuator library gives production ready management and information module. Easily extend it to add your own APIs and manage your spring boot applications.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Yasir Zuberi
Yasir is Lead Developer. He is a bright Java and Grails developer and have worked on development of various SaaS applications using Grails framework.