SpringBoot Endpoint

介绍

SpringBoot的Endpoint主要是用来监控应用服务的运行状况,并集成在Mvc中提供查看接口。内置的Endpoint比如HealthEndpoint会监控dist和db的状况,MetricsEndpoint则会监控内存和gc的状况。

Endpoint的接口如下,其中invoke()是主要的方法,用于返回监控的内容,isSensitive()用于权限控制。

public interface Endpoint<T> {
     String getId();
      boolean isEnabled();
      boolean isSensitive();
      T invoke();
}

Endpoint的加载还是依靠spring.factories实现的。spring-boot-actuator包下的META-INF/spring.factories配置了EndpointAutoConfiguration.

自定义Endpoint

以监控内存为例

定义内存实体类
public class MemoInfo {
    private long maxMemo;

    public long getMaxMemo() {
        return maxMemo;
    }

    public void setMaxMemo(long maxMemo) {
        this.maxMemo = maxMemo;
    }   
}
编写Endpoint类
import org.springframework.boot.actuate.endpoint.Endpoint; 
public class MyEndpoint implements Endpoint<MemoInfo> {
    public String getId() {
        return "myendpoint";
    }
    public MemoInfo invoke() {
        MemoInfo memInfo = new MemoInfo();  
        Runtime runtime = Runtime.getRuntime();  
        memInfo.setMaxMemo(runtime.maxMemory());
        return memInfo;
    }
    public boolean isEnabled() {
        return true;
    }
    public boolean isSensitive() {
        return false;
    }
}

getId()是Endpoint唯一的标识,另外也是MVC接口对外暴露的路径,以上代码对外访问路径就是:http://localhost:8080/myendpoint

编写配置类
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MyEndPointAutoConfig {
    @Bean
     public MyEndpoint myEndPoint() {  
        return new MyEndpoint();  
    }  
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值