spring boot Actuator之自定义Endpoint
生活随笔
收集整理的這篇文章主要介紹了
spring boot Actuator之自定义Endpoint
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
本文基于spring boot 2.2.0 release版本。
在上一篇文章《spring boot Actuator原理詳解之啟動》詳細介紹了在web環境下,Actuator是如何啟動的,這里對流程做一個簡單的總結:
明白了上面整個啟動流程之后,我們也可以編寫一個自定義的Endpoint。
自定義Endpoint的關鍵點:
下面給出一個自定義Endpoint的例子:
/*** 統計方法該Endpoint對象的次數*/ @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);} }上面這個類只是一個示例,并沒有實際意義。運行起來后,訪問http://localhost:8079/actuator/counter,每次訪問都會返回一個比之前大1的數字。
我們還可以使用注解@EndpointWebExtension擴展已有的Endpoint。
下面是spring提供的對EnvironmentEndpoint的擴展:
就web環境下來說,如果擴展類的方法有與原類相同的http請求路徑,那么擴展類會替換掉原來的方法,也就是說,訪問該路徑時,不會訪問到原類的方法,而是訪問到擴展類。
總結
以上是生活随笔為你收集整理的spring boot Actuator之自定义Endpoint的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 小试牛刀——JS基础
- 下一篇: 考研翻译笔记