loader
banner

The Spring Boot Actuator provides production-ready features for our Spring Boot application. Additionally, this library provides all endpoints for production — without having to write a single line of code just by adding the proper dependency.

The Spring Boot Actuator mainly exposes endpoints like health, metrics, logger, info, thread dump, and more. Let’s take a closer look!

Dependencies

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

Endpoints Provided by the Spring Boot Actuator

  1.  /auditevents — lists security audit-related events such as user login/logout. Also, we can use this to filter by principal or type among other fields.
  2.  /beans — returns all available beans in our BeanFactory. Unlike /auditevents, it doesn’t support filtering.
  3.   /conditions — formerly known as  /autoconfig, this endpoint builds a report of conditions around auto-configuration.
  4.  /configprops — allows us to fetch all @ConfigurationProperties beans.
  5.  /env — returns the current environment properties. Additionally, we can retrieve single properties.
  6.  /flyway — provides details about our Flyway database migrations.
  7.  /health – summarizes the health status of our application.
  8.  /heapdump — builds and returns a heap dump from the JVM used by our application.
  9.  /info — returns general information. It might be custom data, build information, or details about the latest commit.
  10.  /liquibase — behaves like /flyway, but for Liquibase.
  11.  /logfile — returns ordinary Java application logs.
  12.  /loggers — enables us to query and modify the logging level of our application.
  13.  /metrics — details the metrics of our application. This might include generic metrics as well as custom ones.
  14.  /prometheus — returns metrics like the previous one, but formatted to work with a Prometheus server.
  15.  /scheduledtasks — provides details about every scheduled task within our application.
  16.  /sessions — lists HTTP sessions, given we are using Spring Session.
  17.  /shutdown — performs a graceful shutdown of the application.
  18.  /threaddump — dumps the thread information of the underlying JVM.

By default, all of these endpoints are exposed as REST APIs and as JMX endpoints. Spring Boot provides a set of properties for controlling the endpoints available to REST APIs and also for JMX.

management.endpoints.jmx.exposure.include and management.endpoints.jmx.exposure.exclude  are two properties for controlling the exposed endpoints for JMX.

management.endpoints.web.exposure.exclude and  management.endpoints.web.exposure.include are two properties for controlling the exposed endpoints for the REST API.

Configuring Endpoints

By default, for performance, all of the actuator’s endpoints responses are cached for reading operations, which do not take any parameters. We can also set cache time using property below:

management.endpoint.beans.cache 

Instead, we can also set time-to-live at each endpoint level, like so:

management.endpoint.loggers.cache

CORS Support

The Spring Boot Actuator provides a set of properties for allowing requests from a specified host. It also provides the type of request methods to support methods from remote hosts:

management.endpoints.web.cors.://example.com 

management.endpoints.web.cors.,POST 

Implementing Custom Endpoints

The Spring Boot Actuator provides the @Endpoint annotation to specify the endpoint.

By default, this endpoint is allowed for both HTTP and JMX.

The @Endpoint-annotated class will have  @ReadOperation,  @WriteOperation, and  @DeleteOperation. These annotated methods will provide a new endpoint for all of these endpoints.

@Selector is an annotation to mark the method argument as part of the request URI.

If we cleanly observe the HealthEndpoint class, it is annotated with @Endpoint, and it has one of the methods annotated with @ReadOperation.

@ReadOperation
public Health healthForComponentInstance(@Selector String component, @Selector String instance) {
.
}

In the above code, method, component, and instance are annotated with @Selector, and a result of the REST API syntax is:

http://localhost:8080/actuator/health/{component}/{instance}

If we want to restrict endpoints to web or JMX, we can use @JMXEnpoint or  @WebEndpoint; these endpoints are restricted to only specified technology.

@EndpointExtension is used as meta-annotation for @Endpoint to indicate the extension support for an endpoint. These extensions support filters where these endpoints need to execute.

@EndpointWebExtensionand @EndpointJmxExtension are two annotation extensions for a specific technology.

Customizing the Management Server Port

By default, management endpoints will use the HTTP protocol, but it is sensible for choice for cloud-based deployments.

The Spring Boot Actuator provides configuration property to expose management endpoints in a different port:

management.server

It also provides the configuration for enabling SSL for management endpoints.

Customizing the Management Server Address

management.server.address is the property used to specify the list of addresses from where we want to listen.

If we want to listen to management requests only from the localhost, then specify the property value as 127.0.0.1. For this to work, the management endpoint must be configured to a different port from the HTTP port.

Disabling HTTP Endpoints

We can disable the management endpoint in two ways:

  1. We can set management.server.port to -1
  2. management.endpoints.web.exposure.exclude and  management.endpoints.jmx.exposure.exclude set these two properties to *  to exclude all properties.

Conclusion

Today, we learned more about the Spring Boot Actuator, endpoints provided by the Actuator, its use cases, and how to customize the Spring Boot Actuator.

Trả lời

Email của bạn sẽ không được hiển thị công khai. Các trường bắt buộc được đánh dấu *