日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

大牛带你深入SpringBoot:自定义Endpoint 及实现原理

發布時間:2025/4/5 javascript 54 豆豆
生活随笔 收集整理的這篇文章主要介紹了 大牛带你深入SpringBoot:自定义Endpoint 及实现原理 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

https://www.jianshu.com/p/fd93d941bf3b

Endpoint

SpringBoot的Endpoint主要是用來監控應用服務的運行狀況,并集成在Mvc中提供查看接口。內置的Endpoint比如HealthEndpoint會監控dist和db的狀況,MetricsEndpoint則會監控內存和gc的狀況。 Endpoint的接口如下,其中*invoke()*是主要的方法,用于返回監控的內容,*isSensitive()*用于權限控制。

public interface Endpoint {

String getId();

boolean isEnabled();

boolean isSensitive();

T invoke();

}

Endpoint的加載還是依靠spring.factories實現的。spring-boot-actuator包下的META-INF/spring.factories配置了EndpointAutoConfiguration。

org.springframework.boot.autoconfigure.EnableAutoConfiguration=\

...

org.springframework.boot.actuate.autoconfigure.EndpointAutoConfiguration,\

...

EndpointAutoConfiguration就會注入必要的Endpoint。有些Endpoint需要外部的收集類,比如TraceEndpoint。

@Bean

@ConditionalOnMissingBean

public TraceEndpoint traceEndpoint() {

return new TraceEndpoint(this.traceRepository);

}

TraceEndpoint會記錄每次請求的Request和Response的狀態,需要嵌入到Request的流程中,這里就主要用到了3個類。

1.TraceRepository用于保存和獲取Request和Response的狀態。

public interface TraceRepository {

List findAll();

void add(Map traceInfo);

}

2.WebRequestTraceFilter用于嵌入web request,收集請求的狀態并保存在TraceRepository中。

3.TraceEndpoint,invoke()方法直接調用TraceRepository保存的數據。

Endpoint的Mvc接口主要是通過EndpointWebMvcManagementContextConfiguration實現的,這個類的配置也放在spring.factories中。

...

org.springframework.boot.actuate.autoconfigure.ManagementContextConfiguration=\

org.springframework.boot.actuate.autoconfigure.EndpointWebMvcManagementContextConfiguration,\

org.springframework.boot.actuate.autoconfigure.EndpointWebMvcHypermediaManagementContextConfiguration

EndpointWebMvcManagementContextConfiguration注入EndpointHandlerMapping來實現Endpoint的Mvc接口。

自定義Endpoint

自定義Endpoint也是類似的原理。這里自定義Endpoint實現應用內存的定時收集。完整的代碼放在Github上了。

收集內存,MemStatus是內存的存儲結構,MemCollector是內存的收集類,使用Spring內置的定時功能,每5秒收集當前內存。

2.自定義Endpoint,getId是EndPoint的唯一標識,也是Mvc接口對外暴露的路徑。invoke方法,取出maxMemory和totalMemory和對應的時間。

3.AutoConfig,注入了MyEndPoint,和MemCollector。

public static class EndPointAutoConfig {

private List status = new ArrayList();

@Bean

public MyEndPoint myEndPoint() {

return new MyEndPoint(status);

}

@Bean

public MemCollector memCollector() {

return new MemCollector(status);

}

}

4.程序入口,運行后訪問http://localhost:8080/my 就可以看到了。

@Configuration

@EnableAutoConfiguration

public class CustomizeEndPoint {

public static void main(String[] args) {

SpringApplication application = new SpringApplication(CustomizeEndPoint.class);

application.run(args);

}

}

結語

Endpoint也是通過spring.factories實現擴展功能,注入了對應的Bean來實現應用監控的功能。

轉載于:https://www.cnblogs.com/davidwang456/articles/10268127.html

總結

以上是生活随笔為你收集整理的大牛带你深入SpringBoot:自定义Endpoint 及实现原理的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。