Actuator 自定义端点

测试版本:Spring Boot 2.2.0.RELEASE

官网中提出

​If you add a @Bean annotated with @Endpoint, any methods annotated with @ReadOperation, @WriteOperation, or @DeleteOperation are automatically exposed over JMX and, in a web application, over HTTP as well. Endpoints can be exposed over HTTP using Jersey, Spring MVC, or Spring WebFlux.

You can also write technology-specific endpoints by using @JmxEndpoint or @WebEndpoint. These endpoints are restricted to their respective technologies. For example, @WebEndpoint is exposed only over HTTP and not over JMX.

其中就提到如何自定义端点,带有@Endpoint注解的bean即可为自定义端点可通过JMX或者HTTP进行访问,@JmxEndpoint和@WebEndpoint 注解的bean只能使用该有的方式进行访问。

创建测试端点

import org.springframework.boot.actuate.endpoint.annotation.ReadOperation;
import org.springframework.boot.actuate.endpoint.web.annotation.WebEndpoint;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
@WebEndpoint(id = "testEndpoint")
public class WebEndpointTest {

  @ReadOperation
  public Map test() {
    Map result = new HashMap();
    result.put("name", "hello");
    result.put("age", "18");
    return result;
  }
}


配置文件中记得公开端点(官网说明会自动暴露,可是并没有自动暴露,不知道哪里配置错了)

  management.endpoints.web.exposure.include=testEndpoint
配置好即可通过 http://{url}:{port}/actuator/testEndpoint 进行访问

注解说明
端点注解
@Endpoint可通过JMX和web应用程序访问(spring-boot-actuator包中HealthEndpoint类中可见例子)
@JmxEndpoint只能通过JMX访问
@WebEndpoint只能通过web访问
@ServletEndpointServlet端点
@RestControllerEndpoint控制器端点(spring-cloud-gateway包中GatewayControllerEndpoint可见例子)
方法注解
运作方式 HTTP方法
@ReadOperation GET
@WriteOperation POST
@DeleteOperation DELETE
在方法上配置注解即可通过对应的方式进行访问,访问地址一般是

/actuator/{EndpointID}

@RestControllerEndpoint 注解中还是用的SpringMVC的标准注释例如@RequestMapping

HTTP监控管理
自定义端点管理路径
修改/actuator路径为/base

management.endpoints.web.base-path=/base
那么修改后之前的/actuator/{ID}就变成了/base/{ID}

/actuator/info /base/info

注意这句话

​Unless the management port has been configured to expose endpoints by using a different HTTP port, management.endpoints.web.base-path is relative to server.servlet.context-path. If management.server.port is configured, management.endpoints.web.base-path is relative to management.server.servlet.context-path.

例如:修改actuator/health 为 /healthcheck

  management.endpoints.web.base-path=/
  management.endpoints.web.path-mapping.health=healthcheck
自定义端口
  management.server.port=8081

你可能感兴趣的:(Actuator 自定义端点)