首页 > 其他 > 详细

Actuator 自定义端点

时间:2020-06-23 22:55:44      阅读:167      评论:0      收藏:0      [点我收藏+]

测试版本: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<String, String> test() {
    Map<String, String> result = new HashMap<String, String>();
    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 自定义端点

原文:https://www.cnblogs.com/gaojiaqi4433/p/13184746.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!