Spring Boot Actuator Endpoints lets us monitor and interact with our application. Spring Actuator is a Spring Boot sub-module and provides built-in endpoints that we can enable and disable for our application.
Spring Boot Actuator Endpoints are exposed over JMX and HTTP, most of the times we use HTTP based Actuator endpoints because they are easy to access over the browser, CURL command, shell scripts etc. Some of the useful actuator endpoints are:
You can get the complete list of spring actuator endpoints from here.
Only “health” and “info” endpoints are exposed without any security, for accessing all other endpoints we need to configure our application for spring security. This is very easy to achieve, we will get to it in the later part of the tutorial.
When we add Spring Actuator Dependencies to our spring boot project, it automatically enables actuator endpoints. Add below dependencies to your spring application to enable spring boot actuator endpoints.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
Now when you will run the application, you will see actuator endpoints being mapped in the logs.
2018-06-19 15:23:20.715 INFO 6493 --- [main] o.s.b.a.e.web.EndpointLinksResolver: Exposing 2 endpoint(s) beneath base path '/actuator'
2018-06-19 15:23:20.723 INFO 6493 --- [main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/health],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2018-06-19 15:23:20.724 INFO 6493 --- [main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/actuator/info],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
Notice that only two endpoints - health
and info
has been mapped. Below image shows the output of these actuator endpoints. Did you noticed that there is no data in the /actuator/info
, it’s because we haven’t configured them. Just add following properties to your application.properties
file.
info.app.name=Spring Actuator Example
info.app.java.version=10
info.app.type=Spring Boot
Restart the application and you will get following output:
By default base-path of actuator endpoints is /actuator
, we can change it to any other value by setting management.endpoints.web.base-path
in application properties file.
management.endpoints.web.base-path=/management
We can enable and disable other actuator endpoints through property files. If you want to enable all actuator endpoints, then add following property.
management.endpoints.web.exposure.include=*
To enable only specific actuator endpoints, provide the list of endpoint id.
management.endpoints.web.exposure.include=health,info,beans,env
Note that we need to add Spring Security to our application for enabling additional endpoints because all other endpoints need at least basic authentication. Add following dependency for spring security in your application.
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
Also, add spring security username and password in application properties file.
spring.security.user.name=pankaj
spring.security.user.password=pankaj
Restart the application and you will see additional endpoints being mapped.
2018-06-19 16:18:22.211 INFO 6627 --- [main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/management/beans],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2018-06-19 16:18:22.212 INFO 6627 --- [ main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/management/env],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
2018-06-19 16:18:22.212 INFO 6627 --- [ main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/management/env/{toMatch}],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
Now when you will try to access the secured actuator endpoints, you will have to provide login credentials. Below images shows the response of beans
and env/java.home
endpoints.
One of the great features of Spring Framework is that it’s very easy to extend. We can create our own custom actuator endpoints using @Endpoint
annotation on a class. Then we have to use @ReadOperation
, @WriteOperation
, or @DeleteOperation
annotations on the methods to expose them as actuator endpoint bean. We can create technology-specific Endpoints using @JmxEndpoint
and @WebEndpoint
annotations. Here is an example of our own custom spring actuator endpoint.
package com.journaldev.spring;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;
@Endpoint(id="myendpoint")
@Component
public class MyCustomEndpoints {
@ReadOperation
@Bean
public String hi() {
return "Hi from custom endpoint";
}
}
Did you notice the endpoint id? We also need to configure this in the list of actuator endpoints to be enabled. Update following properties in application.properties
file.
management.endpoints.web.exposure.include=health,info,beans,env,myendpoint
Now when you will start the application, check for this new endpoint being mapped in the logs.
2018-06-19 17:06:59.743 INFO 6739 --- [main] s.b.a.e.w.s.WebMvcEndpointHandlerMapping : Mapped "{[/management/myendpoint],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}" onto public java.lang.Object org.springframework.boot.actuate.endpoint.web.servlet.AbstractWebMvcEndpointHandlerMapping$OperationHandler.handle(javax.servlet.http.HttpServletRequest,java.util.Map<java.lang.String, java.lang.String>)
Below image shows the output when we invoke our custom actuator endpoint.
Spring Boot Actuator is a production ready management and information module. We can easily extend it to add our own APIs and manage our application.
You can download the complete project from our GitHub Repository.
Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.
While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Hello, How can I map my custom liveness checking function to he existing “actuator/health/liveness” endpoint of Spring Boot?
- Barna
Hey, if I want to change health endpoint to heartbeat,how I can do that. Please help
- Neha Gour
"Only “health” and “info” endpoints are exposed without any security, for accessing all other endpoints we need to configure our application for spring security. " -------------- I’m able to access other endpoints without adding spring security.
- JJose
Hi! I uses spring-boot-starter-actuator:1.5.2.RELEASE and the only thing for configuring the base-path (in application.YML) which worked was: ``` management: contextPath: /actuator ``` @see org.springframework.boot.actuate.autoconfigure.ManagementServerProperties#contextPath Otherwise the endpoints of Actuator have been mapped to the application BasePath – in my case ROOT (“/”) Greats!
- count023
Really awesome.
- MKhan
Hi Sir, I need full Spring Boot Tutorials.
- praveen verma
Hi Pankaj, Help me to get entire console log thru endpoint if it is possible using actuator or by other means.
- xaan