Spring Boot Actuator:自定义端点

要在Spring Boot Actuator中实现自定义端点,可以按照以下步骤进行操作:

1.创建一个自定义端点类

该类需要使用`@Endpoint`注解进行标记,并使用`@Component`注解将其作为Spring Bean进行管理。

package com.example.highactuator.point;

import lombok.extern.apachecommons.CommonsLog;
import org.springframework.boot.actuate.endpoint.annotation.Endpoint;
import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.stereotype.Component;

@Component
@Endpoint(id = "customEndpoint")
public class CustomEndpoint {
    @ReadOperation
    public String customEndpoint() {
        return "Custom endpoint response";
    }
}

在上述示例中,我们创建了一个自定义端点类`CustomEndpoint`,并使用`@Endpoint`注解指定了端点的ID为`customEndpoint`。`@ReadOperation`注解用于标识该方法处理读取操作的请求。在本例中,该方法返回一个自定义的响应字符串。

2. 在application.yml`中配置端点的暴露路径和敏感性。

主要是下方图片中圈中的配置,其他内容忽略一下

Spring Boot Actuator:自定义端点_第1张图片

management:
  endpoint:
    customEndpoint:
      sensitive: false #标记为非敏感
    health:
      show-components: when_authorized
  endpoints:
    web:
      base-path: /high
      path-mapping:
         health: heal
      exposure:
        include: health,customEndpoint #暴露路径

 上述配置指定了只暴露`customEndpoint`端点,并将其设置为非敏感。

3. 启动应用程序

可以通过`/actuator/customEndpoint`路径访问自定义端点。

Spring Boot Actuator:自定义端点_第2张图片

通过以上步骤,你可以创建和暴露自定义的端点,并在访问对应的路径时获取自定义的响应。请根据实际需求来实现自定义端点的方法和逻辑,在方法中返回所需的监控数据或其他自定义操作。

你可能感兴趣的:(spring,boot,后端,java)