Spring Boot實踐---基於spring-boot-actuator的監控外掛: Spring Boot Admin
一、入門使用:Actuator外掛
Actuator外掛是SpringBoot原生提供的一個服務,可以通過暴露端點路由,用來輸出應用中的諸多 端點資訊。實戰一下!
- pom.xml中新增依賴:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
啟動Spring Boot應用程式之後,只要在瀏覽器中輸入端點資訊就能獲得應用的一些狀態資訊。
常用端點列舉如下,可以一個個詳細試一下:
- /info 應用基本資訊
- /health 健康度資訊
- /metrics 執行指標
- /env 環境變數資訊
- /loggers 日誌相關
- /dump 執行緒相關資訊
- /trace 請求呼叫軌跡
當然此時只能使用/health
和 /info
端點,其他因為許可權問題無法訪問。想訪問指定端點的話可以在yml配置中新增相關的配置項,比如/metrics
endpoints:
metrics:
sensitive: false
此時瀏覽器訪問/metrics端點就能得到諸如下面所示的資訊:
{ "mem": 71529, "mem.free": 15073, "processors": 4, "instance.uptime": 6376, "uptime": 9447, "systemload.average": -1.0, "heap.committed": 48024, "heap.init": 16384, "heap.used": 32950, "heap": 506816, "nonheap.committed": 23840, "nonheap.init": 160, "nonheap.used": 23506, "nonheap": 0, "threads.peak": 25, "threads.daemon": 23, "threads.totalStarted": 28, "threads": 25, "classes": 6129, "classes.loaded": 6129, "classes.unloaded": 0, "gc.copy.count": 74, "gc.copy.time": 173, "gc.marksweepcompact.count": 3, "gc.marksweepcompact.time": 88, "httpsessions.max": -1, "httpsessions.active": 0 }
當然也可以開啟全部端點許可權,只需如下配置即可:
endpoints:
sensitive: false
由於Actuator外掛提供的監控能力畢竟有限,而且UI比較簡陋,因此需要一個更加成熟一點的工具
二、Spring Boot Admin監控系統
SBA則是基於Actuator更加進化了一步,其是一個針對Actuator介面進行UI美化封裝的監控工具。我們來實驗一下。
- 首先來建立一個Spring Boot Admin Server工程作為服務端
pom.xml中加入如下依賴:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server</artifactId>
<version>1.5.7</version>
</dependency>
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-server-ui</artifactId>
<version>1.5.7</version>
</dependency>
然後在應用主類上通過加註解來啟用Spring Boot Admin
@EnableAdminServer
@SpringBootApplication
public class SpringbtAdminServerApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbtAdminServerApplication.class, args);
}
}
啟動程式,瀏覽器開啟 localhost:8081
檢視Spring Boot Admin主頁面:
Spring Boot Admin主頁面
此時Application一欄空空如也,等待待監控的應用加入
- 建立要監控的Spring Boot應用
pom.xml中加入以下依賴
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.7</version>
</dependency>
然後在yml配置中新增如下配置,將應用註冊到Admin服務端去:
spring:
boot:
admin:
url: http://localhost:8081
client:
name: AdminTest
Client應用一啟動,Admin服務立馬推送來了訊息,告訴你AdminTest上線了:
應用上線推送訊息
此時去Admin主介面上檢視,發現Client應用確實已經註冊上來了:
Client應用已註冊上來
- 檢視Detail
Detail資訊
- 檢視 Metrics
Metrics資訊
- 檢視 Enviroment
Enviroment資訊
- 檢視JMX
JMX資訊
- 檢視Threads
Threads資訊
- 檢視Trace與詳情
Trace資訊
點選最上方JOURNAL,會看到被監控應用程式的事件變化:
應用程式的事件變化資訊
圖中可以清晰地看到,應用從 REGISTRATION → UNKNOWN → UP 的狀態跳轉。
這樣就將Actuator外掛提供的所有端點資訊在SBA中全部嘗試了一遍。