Introduction to Spring Boot Actuator
Spring Boot actuator helps us to manage and monitor our application when we deploy our application to production. It contains various endpoints by which we can see where the resource is up, it has two types of endpoints which we can use to monitor our spring boot application which are JMX and HTTP, it provides us the production-ready application by which we can monitor the health of our application we should use this feature. It also applied some more things automatically in applications like metrics, health, and auditing.
Features of Spring Boot Actuator
The spring-boot-actuator module provides all of Spring Boot’s production-ready features. The simplest way to enable the features is to add a dependency to the spring-boot-starter-actuator ‘Starter’.
1. Provide Production Ready Feature
We have one module which provides us all the production ready features. To enable this feature into the project we need to add one dependency i.e. spring-boot-starter-actuator ‘Starter’.
We all have spring security in our project for authentication so this actuator has audit framework as well which manage various events by default like access denied, failures and authentication success. So this feature is very important and it implements lockout for user based on a number of failures in the authentication. Now to enable this we need to configure AuditEventRepository type bean in our spring boot application. Spring boot also provides one InMemoryAuditEventRepository but this has very limited features so this can only recommend using in a development phase, if we want to move our application to production then we should go for AuditEventRepository configuration.
3. Customer Auditing
we can also implement customize security events but fir this we need to provide implementations of the interface which are:
Spring boot actuator provides us various built-in endpoints to manage and monitor our application and by this, we can also interact with our application. It has so many built-in endpoints as well as we can create our endpoint also. We can also disable and enable this endpoint according to the need. Suppose we want to access these endpoints so in order to access them we need to expose them on to the network via HTTP or JMX. Here application prefers HTTP over JMX URL starts with / actuator and after this ID we can add to access them.
One main reason for this feature is it allows us to configure our loggers in our application at runtime. We can also view the individual logger or the entire list configuration. This configuration are made both implicitly and explicitly. Implicitly are once which are provided by the login framework and explicitly which we have done. Various level mentioned below:
As discussed actuator provide us auto-configuration and management for micrometer, it is a facade which provides support for various monitoring purpose that includes:
- Simple (in-memory)
- New Relic
JMX stands for Java Management Extensions, it allows us to manage and monitor our spring boot applications. To enable this feature we need to do configuration in an out project like set the configuration property spring.jmx.enabled = true. If we want to disable this property or we do not want to expose our endpoints using JMX then we need to configure like below:
This feature is used to expose our endpoints over the network via HTTP this is enabled by default. We just need to prefix /actuator followed by the ID of the endpoint. It can happen sometimes that we have already used / actuator for other purposes so we can also change the prefix by setting some property which shows below:
9. Process Monitoring
Spring boot provides us two classes that are very useful for process monitoring which are as follows :
- WebServerPortFileWriter: This class used to create a file which contains information about the server port on which the application is up and running. By default, it creates with name application.port
- ApplicationPidFileWriter: This class creates a file that conation the application PDI. By default, it creates with name application.pid
10. Cloud Foundry Support
Spring boot actuator also provides us support when we deploy our application on the cloud. It provides us /cloudfoundryapplication path which provides us a secure route to all our endpoints.
11. HTTP Tracing
This we can enable by configuring HttpTraceRepository type bean into our application. Spring boot provides us InMemoryHttpTraceRepository but it has limited support which can be used in development. For the production-ready application, we can go for HttpTraceRepository.
Spring Boot Actuator endpoints
We have so many inbuilt endpoints available in spring boot actuator.
Spring Boot Actuator Properties:
Examples of Spring Boot Actuator
Follow the below mentioned steps to create your first spring boot project using an actuator.
- First, we need to create the spring boot project for that you can follow this link https://start.spring.io/
- Here mention the group names, artifact id and add the necessary dependency. The dependency for the actuator is mentioned below.
- Add this to pom.xml
- Java Class:
public class DemoActuator
public String demoAc()
return "Demo actuator!!";
- For now, we are disabling the security feature of a spring boot actuator. For this we will set one property which is mentioned below:
So actuator provides us many features to make our spring boot application production ready which helps us to monitor and manage our application very easily also it reduces the maintenance task for us. We can also trace the exception and errors in production very easily.
This is a guide to Spring Boot Actuator. Here we discuss the Introduction and features of spring boot actuator along with an example and its code implementation. you may also have a look at the following articles to learn more –