{{announcement.body}}
{{announcement.title}}

Spring Boot Actuator

DZone 's Guide to

Spring Boot Actuator

An actuator is a spring boot sub-project that helps to expose production-ready support features against Spring boot application.

· Java Zone ·
Free Resource

Key Features Offered by an Actuator

  • Health check: You can use a health endpoint to check the status of your running application.
  • Monitoring and Management over HTTP/JMX: Actuator support HTTP endpoint as well as Java Management Extensions (JMX) to provide a standard mechanism to monitor and manage applications.
    • Logger: It provides a feature to view and update the logs level.
    • Metrics: Spring Boot Actuator provides dependency management and auto-configuration for Micrometer, an application metrics facade that supports numerous monitoring systems.
    • Auditing: Once Spring Security is in play, Spring Boot Actuator has a flexible audit framework that publishes events (by default, “authentication success”, “failure” and “access denied” exceptions). This feature can be very useful for reporting and for implementing a lock-out policy based on authentication failures.
    • Http Tracing: HTTP Tracing can be enabled by providing a bean of type HttpTraceRepository in your application’s configuration. For convenience, Spring Boot offers an InMemoryHttpTraceRepository that store traces for the last 100 request-response exchanges
    • Process Monitoring

Enable Actuator Into Spring Boot Project

You can enable Actuator into Spring boot project by including below dependency.

Java
 




x


 
1
//Gradle
2
org.springframework.boot:spring-boot-starter-actuator:2.3.1.RELEASE
3
//Maven
4
<dependency>
5
    <groupId>org.springframework.boot</groupId>
6
    <artifactId>spring-boot-starter-actuator</artifactId>
7
    <version>2.3.1.RELEASE</version>
8
</dependency>



Endpoint Offer by Actuator

By default ‘health’ and ‘info’ endpoint is enabled

localhost

Default exposed endpoint.

Other endpoints are sensitive and not advisable to expose to the production environment without security. As we are demonstrating, let’s expose all the APIs.

Java
 




x


 
1
management.endpoints.web.exposure.include=*



localhost

Include and Exclude Endpoint.

Even you can include or exclude endpoint by defining below properties

Plain Text
 




x


 
1
# wild card to include/exclude all
2
management.endpoints.web.exposure.include=* 
3
management.endpoints.web.exposure.exclude=* 
4
 
          
5
# you can include specific properties like below
6
management.endpoints.web.exposure.include=env,beans
7
management.endpoints.web.exposure.exclude=heapdump



Customize Management Server Address

You can customize the management server port, it will help you define the limited scope to the ports.

management.server.port=8081
management.server.address=127.0.0.1

Expose Custom Endpoint

Any methods annotated with @ReadOperation, @WriteOperation, or @DeleteOperation are automatically exposed over JMX and HTTP. Even you can expose technology-specific endpoint by using @JmxEndpoint or @WebEndpoint.

Here I’m sharing you example for exposing endpoint using Spring boot 2.x

Java
 




xxxxxxxxxx
1
14


 
1
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
2
import org.springframework.stereotype.Component;
3
 
4
@Component
5
@org.springframework.boot.actuate.endpoint.annotation.Endpoint(id = "say-hello")
6
public class Endpoint {
7
 
8
    @ReadOperation
9
    public String sayHello()
10
    {
11
        return "Hello World";
12
    }
13
 
14
}



Summary: Spring boot actuator is one of the best libraries you can add in your application to enable production-ready features in less effort. it offers key features that can be used in day to day production support.

Topics:
actuator, java, production, rest api, spring boot 2.1, support

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}