【spring系列】Actuator 自定义端点

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<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方法
@ReadOperationGET
@WriteOperationPOST
@DeleteOperationDELETE

在方法上配置注解即可通过对应的方式进行访问,访问地址一般是

/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

更详细内容可查看官网

点击进入官网

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

叁滴水

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值