欢迎访问 生活随笔!

生活随笔

当前位置: 首页 > 前端技术 > javascript >内容正文

javascript

SpringBoot Endpoint

发布时间:2024/1/1 javascript 41 豆豆
生活随笔 收集整理的这篇文章主要介绍了 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 {@Beanpublic MyEndpoint myEndPoint() { return new MyEndpoint(); } }

总结

以上是生活随笔为你收集整理的SpringBoot Endpoint的全部内容,希望文章能够帮你解决所遇到的问题。

如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。