Springboot應用監控之Actuator提高

Springboot應用監控之Actuator提高

Springboot之Actuator提高一點點。

自定義已有Endpoint

springboot允許我們擴展已有的Endpoint內容,以Health為例。

創建類文件CustomHealth.java,內容如下

Springboot應用監控之Actuator提高

Springboot應用監控之Actuator提高

重啟web應用,訪問health接口:

Springboot應用監控之Actuator提高

其它endpoint也可以類似的進行擴展。只需要把繼承的接口換一下就可以。

自定義新的Endpoint

如果自定義新的Endpoint需要用到幾個特定的註解,例如@Endpoint、@ReadOperation、@WriteOperation和@DeleteOperation註解。

新建類文件MyEndpoint.java,內容如下:

Springboot應用監控之Actuator提高

Springboot應用監控之Actuator提高

重啟應用,訪問自定義Endpoint的id即可看到結果

Springboot應用監控之Actuator提高

備註:如果需要了解自定義endpoint如何接收參數,可以查閱官方文檔53.7小節。本文就不再介紹了。

Actuator Security

默認endpoint是不需要鑑權就可以訪問的,如果是在生產環境中肯定是很危險的,所以我們需要給actuator訪問添加身份認證。

常用的方法是添加springsecurity,修改pom.xml文件:

Springboot應用監控之Actuator提高

重啟應用,訪問已有的endpoint,經測試,訪問/health和/info沒有變化,說明默認這兩個沒有加權限認證,但是當訪問其它的例如我們自己添加的endpoint,頁面會自動跳轉到登錄頁:

Springboot應用監控之Actuator提高

說明actuator自動使用了springsecurity的鑑權流程,我們可以增加一個配置,開啟對特定endpoint的訪問權限。

修改application.properties文件,添加內容:

Springboot應用監控之Actuator提高

新增自定義類文件ActuatorSecurityConfig.java文件:

Springboot應用監控之Actuator提高

重啟應用,訪問endpoint,如果未授權的將跳轉到登錄頁面,授權的可以直接訪問。

注:目前對springsecurity瞭解不多,所以關於這部分內容不再多說,後續有機會再單獨介紹,也歡迎大家補充。


分享到:


相關文章: