15 Mart 2023 Çarşamba

SpringBoot Actuator @EndpointWebExtension - Customizing inbuilt endpoints

Giriş
Açıklaması şöyle
It may be the case that you want to change the functionality of an inbuild endpoint such as changing its output. Spring boot actuator allows you to modify its inbuilt endpoints by extending them.

Following are the steps involved:
  1. Create a class with @EndpointWebExtension annotation. You can also annotate it with @EndpointExtension.
  2. @EndpointWebExtension is a specialization of @EndpointExtension for web applications.
  3. For JMX, use @EndpointJmxExtension or @EndpointExtension annotation.
  4. This annotation should have an endpoint property. Its value should be the class name of the endpoint that you want to extend.
  5. For this example, we will be extending info endpoint, so its value will be InfoEndpoint.class.
  6. As with creating a custom endpoint, provide methods annotated with @ReadOperation, @DeleteOperation or @WriteOperation.
Örnek
Şöyle yaparız
@Component
@EndpointWebExtension(endpoint=InfoEndpoint.class)
public class EnvEndpointCustomizer {
  @ReadOperation
  public Environment getEnvironmentInfo() {
    return new Environment();
  }
  // model class for info output
  class Environment {
    String ram;
    String hdd;
    public String getRam() {
      return ram;
    }
    public void setRam(String ram) {
      this.ram = ram;
    }
    public String getHdd() {
      return hdd;
    }
    public void setHdd(String hdd) {
      this.hdd = hdd;
    }
    // constructor
    public Environment() {
      this.ram = "8GB";
      this.hdd = "500GB";
    }
  }
}
http://localhost:8080/actuator/info adresine gidersek çıktısı şöyle olur
{"ram":"8GB","hdd":"500GB"}



Hiç yorum yok:

Yorum Gönder