1、概述
????????Spring Boot Admin 是一款用于監控 Spring Boot 應用程序的開源工具,可幫助開發者實時監控應用的運行狀態、性能指標、日志信息等。
2、核心功能
應用狀態監控
- 顯示應用是否在線、啟動時間、運行時長等基礎信息。
- 監控 JVM 相關指標:內存使用情況(堆內存、非堆內存)、線程狀態(活躍線程數、峰值線程數)、類加載數量等。
- 查看 CPU 使用率、垃圾回收(GC)頻率及耗時。
應用詳情查看
- 展示應用的配置屬性(如?
application.properties
?中的參數)。 - 查看 HTTP 端點(Endpoints)信息,如健康檢查(
/health
)、環境變量(/env
)、日志級別(/loggers
)等。
日志管理
- 實時查看和修改應用的日志級別(如 DEBUG、INFO、WARN)。
- 支持按條件過濾日志內容。
告警與通知
- 結合第三方工具(如 Email、Slack、釘釘等)實現告警通知,當應用狀態異常或指標超出閾值時觸發提醒。
批量監控
- 支持同時監控多個 Spring Boot 應用,通過統一的 Web 界面集中管理。
3、搭建監控系統
配置監控端(Admin Server)
添加依賴
在?pom.xml
?中引入 Spring Boot Admin Server:
<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-server</artifactId>
</dependency>
啟用 Admin Server
在主類上添加?@EnableAdminServer
?注解:
package com.ybw;import de.codecentric.boot.admin.server.config.EnableAdminServer;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;/*** 啟動類** @author ybw* @version V1.0* @className SpringAdminServerDemoApplication* @date 2025/6/9**/
@SpringBootApplication
@EnableAdminServer
public class SpringAdminServerDemoApplication {public static void main(String[] args) {SpringApplication.run(SpringAdminServerDemoApplication.class, args);}}
配置端口和安全(可選)
server.port=9090 # 監控端端口
spring.security.user.name=admin # 認證用戶名
spring.security.user.password=123456 # 認證密碼
配置被監控應用(Admin Client)
添加依賴
在被監控應用的?pom.xml
?中引入 Spring Boot Admin Client:
<dependency><groupId>de.codecentric</groupId><artifactId>spring-boot-admin-starter-client</artifactId>
</dependency>
配置監控端地址
在被監控應用的?application.properties
?中指定監控端的 URL:
spring.boot.admin.client.url=http://localhost:9090 # 監控端地址
management.endpoints.web.exposure.include=* # 暴露所有監控端點
啟動并訪問監控界面
????????先啟動監控端(Admin Server),訪問?http://localhost:9090
,輸入認證信息(若配置了安全策略)。
4、擴展功能與最佳實踐?
自定義監控指標
????????通過 Spring Boot 的?Micrometer
?框架添加自定義指標(如數據庫連接數、業務接口響應時間):
import io.micrometer.core.annotation.Timed;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class CustomMetricsController {@Timed(value = "api.request.time", description = "請求處理耗時")@GetMapping("/custom-api")public String customApi() {// 業務邏輯return "Hello, Admin!";}
}
- 寫在被監控端:admin client。?
告警集成
????????使用 Spring Boot Admin 的?Notifier
?接口集成告警服務,例如通過郵件通知:
import de.codecentric.boot.admin.server.notify.AbstractEventNotifier;
import de.codecentric.boot.admin.server.notify.StatusChangeEvent;
import org.springframework.stereotype.Component;@Component
public class EmailNotifier extends AbstractEventNotifier {@Overrideprotected void doNotify(StatusChangeEvent event) {// 發送郵件邏輯(調用郵件服務 API)String message = String.format("應用 %s 狀態變更為 %s", event.getApplication().getName(), event.getStatusInfo());sendEmail("admin@example.com", "Spring Boot Admin 告警", message);}private void sendEmail(String to, String subject, String content) {// 具體郵件發送實現}
}
- 寫在監控端:admin server。
- 觸發條件:基于狀態變化觸發,默認情況下,Spring Boot Admin 會在應用狀態發生變化時觸發告警(如?
UP
?→?DOWN
、OFFLINE
?→?UP
?等)。常見狀態如下:UP
:應用正常運行DOWN
:應用健康檢查失敗OFFLINE
:應用注冊后斷開連接UNKNOWN
:狀態未知
5、常用端點(Endpoints)
????????Spring Boot Admin 通過訪問應用的監控端點獲取數據,常用端點如下:
端點路徑 | 描述 |
---|---|
/health | 健康檢查狀態(是否存活) |
/info | 應用信息(如版本、構建時間) |
/metrics | 指標數據(JVM、CPU、自定義指標) |
/loggers | 日志級別管理 |
/env | 環境變量和配置屬性 |
/threaddump | 線程 dump 信息 |
6、持久化?
默認內存存儲(無持久化)
Spring Boot Admin?默認使用內存存儲監控數據,這意味著:
- 數據生命周期:監控數據僅在 Admin Server 運行期間有效,重啟后數據丟失。
- 短期數據:默認只保留最近的狀態變化和指標,例如:
- 應用狀態(UP/DOWN):僅保留最新狀態。
- 指標數據(堆內存、線程數):僅保留最近幾次采樣值(通常為幾分鐘內)。