Springboot應用監控之Actuator入門

Springboot應用監控之Actuator入門

基本環境搭建

為了滿足Springboot在生產環境中的需要,官方提供了一個特殊的模塊Actuator,這個模塊可以收集springboot應用的各項指標信息,可以看作是對應用的一個監控功能。此模塊監控的信息可以通過兩種方式獲取:JMX或HTTP,方便不同場景下的需要。本文主要以HTTP為主介紹。

另外需要注意的是,springboot的1.x和2.x區別較大,本文介紹的是2.1.0版本。

代碼部分為了防止顯示錯亂,都使用了截圖。

要使用Actuator模塊很簡單,首先你需要有一個springboot應用並且啟用了web功能(本文以http方式為例),springboot應用如下:

Springboot應用監控之Actuator入門

pom.xml內容(截取部分):

Springboot應用監控之Actuator入門

application.properties內容如下:

Springboot應用監控之Actuator入門

main方法啟動文件SpdemoApplication.java內容:

Springboot應用監控之Actuator入門

測試服務正常的DemoController.java內容:

Springboot應用監控之Actuator入門

啟動SpdemoApplication類的main方法,啟動日誌(部分):

Springboot應用監控之Actuator入門

瀏覽器訪問http://localhost:8080/demo/test

Springboot應用監控之Actuator入門

說明springboot搭建成功,運行正常。接下來我們就要添加監控模塊。

---------------------劃重點-----------------------

pom.xml文件添加actuator模塊依賴:

Springboot應用監控之Actuator入門

然後重新啟動項目,看日誌內容:

Springboot應用監控之Actuator入門

日誌中多了兩行內容,意思是在/actuator路徑下發布了兩個endpoint(s),這個概念後邊介紹。

打開瀏覽器訪問http://localhost:8080/demo/actuator/health

Springboot應用監控之Actuator入門

這個就是actuator給我們發佈出的一個接口,另外一個是

Springboot應用監控之Actuator入門

返回內容為空,我們後續可以通過配置添加返回的內容。

截止目前,我們的項目中已經成功的添加了actuator模塊,那麼接下來,我們先簡單瞭解下actuator的一些細節。

Actuator入門

Actuator

spring官網是這麼介紹Actuator的:

Definition of Actuator

An actuator is a manufacturing term that refers to a mechanical device for moving or controlling something. Actuators can generate a large amount of motion from a small change.

這是機械行業的一個術語,一般指作動器,是實施主動控制的關鍵部件~~~我也不懂機械行業。按我的理解就是一個控制組件,可以根據系統的一些變化產生一系列的動作。

在springboot中,整個監控組件就是Actuator,理解到這裡就可以了。

Endpoint基礎

Endpoint才是我們重點關注的對象。

在springboot中,endpoint就是一系列可以監控我們系統各項指標的監控點,官方默認給我們提供了二十多個endpoint,而且我們還可以很容易的擴展自己的endpoint。

每一個endpoint都可以單獨控制enabled或者disabled。這裡的disabled是指的不會加載對應的bean,不會進行初始化。每一個endpoint的名字也是配置所用的id名稱。endpoint同時提供了JMX和Web兩種操作方式。

例如上一步中我們訪問的/health和/info就是兩個endpoint,所有的endpoint如下表:

Springboot應用監控之Actuator入門

Springboot應用監控之Actuator入門

如果是web應用,官方還額外提供了幾個專用的endpoint:

Springboot應用監控之Actuator入門

部分節點功能說明如下(參考網絡資料翻譯):

Endpoint數據結構說明,參考官網文檔:

https://docs.spring.io/spring-boot/docs/2.1.0.RELEASE/actuator-api//html/

Endpoint配置

系統默認只發布了兩個endpoint,想查看更多的,需要配置

修改application.properties文件:

Springboot應用監控之Actuator入門

重啟應用,查看日誌:

Springboot應用監控之Actuator入門

根據日誌可以看出這次發佈了16個節點,並且啟動了8081端口(看配置文件)

查看health接口數據:

Springboot應用監控之Actuator入門

endpoint的配置項還有很多,可參考官方文檔:

https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready-endpoints-enabling-endpoints


分享到:


相關文章: