當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot Endpoint
生活随笔
收集整理的這篇文章主要介紹了
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的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 柔性生产制造实验系统(工程型)
- 下一篇: Spring boot 使用@Endpo