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

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

spring boot Actuator之自定义Endpoint

發(fā)布時(shí)間:2024/1/1 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 spring boot Actuator之自定义Endpoint 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

本文基于spring boot 2.2.0 release版本。

在上一篇文章《spring boot Actuator原理詳解之啟動(dòng)》詳細(xì)介紹了在web環(huán)境下,Actuator是如何啟動(dòng)的,這里對(duì)流程做一個(gè)簡(jiǎn)單的總結(jié):

  • 掃描所有@Endpoint注解的類,這些類都是Endpoint;
  • 使用過濾器對(duì)Endpoint對(duì)象進(jìn)行過濾,沒有被過濾掉的才可以進(jìn)入下一步;
  • 讀取Endpoint對(duì)象的每個(gè)方法,判斷是否有@ReadOperation、@WriteOperation、@DeleteOperation三個(gè)注解,如果有,則針對(duì)每個(gè)被注解的方法創(chuàng)建操作對(duì)象Operation;
  • 根據(jù)操作對(duì)象、Endpoint對(duì)象、Endpoint名創(chuàng)建為RequestMappingInfo,并將其注冊(cè)到spring mvc中;
  • 注冊(cè)成功之后,Endpoint對(duì)象便可以對(duì)外提供服務(wù)。
  • 明白了上面整個(gè)啟動(dòng)流程之后,我們也可以編寫一個(gè)自定義的Endpoint。
    自定義Endpoint的關(guān)鍵點(diǎn):

  • 類上必須有注解@Endpoint,注解必須設(shè)置id值,否則啟動(dòng)拋出如下異常:Caused by: java.lang.IllegalStateException: No @Endpoint id attribute specified for XXXClass;
  • 必須指定方法注解為下面三個(gè)中一個(gè):@ReadOperation、@WriteOperation、@DeleteOperation;
  • 注解@Selector可以指定路徑參數(shù),比如/actuator/caches/{cache}花括號(hào)里面的內(nèi)容是路徑參數(shù),我們可以在方法入?yún)⑸咸砑?#64;Selector來(lái)指定路徑參數(shù),比如public String counters(@Selector int value);
  • 默認(rèn)Endpoint對(duì)象的方法都是以json格式返回客戶端,可以通過設(shè)置@ReadOperation、@WriteOperation、@DeleteOperation的produces屬性指定其他格式。
  • 下面給出一個(gè)自定義Endpoint的例子:

    /*** 統(tǒng)計(jì)方法該Endpoint對(duì)象的次數(shù)*/ @Component @Endpoint(id="counter") public class Counter {private AtomicInteger cnt=new AtomicInteger(0);@ReadOperationpublic int counter() {return cnt.getAndAdd(1);}@ReadOperationpublic int counters(@Selector int value) {return cnt.getAndAdd(value);} }

    上面這個(gè)類只是一個(gè)示例,并沒有實(shí)際意義。運(yùn)行起來(lái)后,訪問http://localhost:8079/actuator/counter,每次訪問都會(huì)返回一個(gè)比之前大1的數(shù)字。

    我們還可以使用注解@EndpointWebExtension擴(kuò)展已有的Endpoint。
    下面是spring提供的對(duì)EnvironmentEndpoint的擴(kuò)展:

    @EndpointWebExtension(endpoint = EnvironmentEndpoint.class) public class EnvironmentEndpointWebExtension {private final EnvironmentEndpoint delegate;public EnvironmentEndpointWebExtension(EnvironmentEndpoint delegate) {this.delegate = delegate;}@ReadOperationpublic WebEndpointResponse<EnvironmentEntryDescriptor> environmentEntry(@Selector String toMatch) {EnvironmentEntryDescriptor descriptor = this.delegate.environmentEntry(toMatch);return new WebEndpointResponse<>(descriptor, getStatus(descriptor));}private int getStatus(EnvironmentEntryDescriptor descriptor) {if (descriptor.getProperty() == null) {return WebEndpointResponse.STATUS_NOT_FOUND;}return WebEndpointResponse.STATUS_OK;} }

    就web環(huán)境下來(lái)說(shuō),如果擴(kuò)展類的方法有與原類相同的http請(qǐng)求路徑,那么擴(kuò)展類會(huì)替換掉原來(lái)的方法,也就是說(shuō),訪問該路徑時(shí),不會(huì)訪問到原類的方法,而是訪問到擴(kuò)展類。

    總結(jié)

    以上是生活随笔為你收集整理的spring boot Actuator之自定义Endpoint的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。