作者:zhaoyh
http://zhaoyh.com.cn/
Spring Boot特別適合團隊構建各種可快速迭代的微服務,同時為了減少程序本身監控系統的開發量,Spring Boot 提供了 actuator 模塊,可以很方便的對你的 Spring Boot 程序做監控。
1. actuator接口說明
Spring Boot 應用中加入監控很簡單,只需要在pom.xml文件中加入以下依賴:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
為了保證actuator接口的敏感性,在配置文件中,開放安全屬性配置:
management.security.enabled=false
這樣就能允許你查看 Spring Boot 進程的actuator信息了。
啟動Spring Boot程序,在啟動日志里看到可訪問的actuator接口:
通過這些,我們可以實時的獲取應用的各項監控指標。另外,關注微信公眾號:Java技術棧,在后台回復:boot,可以獲取我整理的 N 篇 Spring Boot 教程,都是干貨。
actuator的接口分為原生接口和用戶自定義接口。
原生接口主要有如下幾個:
如果你想關閉某個接口,比如關閉health接口,可以直接設置:
endpoints.health.enabled=false
2. 監控展示
2.1 JConsole
JConsole是一個內置Java性能分析器,如果你本機已經配置了jdk的話,可直接命令行輸入jconsole,打開后的頁面如下圖所示:
選擇你要監控的進程,點擊連接即可進入該Java進程的監控首頁,如下圖所示:
可以很詳細地展示進程的內存、CPU、類信息。
2.2 Spring Boot Admin
Spring Boot Admin是一款監控和管理 Spring Boot 應用程序的開源軟件。Spring Boot Admin讀取actuator的接口數據,並通過 Spring Boot Admin UI 將實時數據展示在前端。
創建一個Spring Boot Admin Server,首先需要創建一個基本的 Spring Boot 應用程序,這個就不做贅述了,並加入以下依賴:
<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>
server.port端口設置為8090,並注冊到你的eureka服務上。
啟動類中使用注解開啟服務:
@SpringBootApplication
@EnableDiscoveryClient
@EnableAdminServer
@EnableTurbine
public class HtsApplication {
public static void main(String[] args) {
SpringApplication.run(HtsApplication.class, args);
}
}
瀏覽器訪問:http://localhost:8090 即可看到如下Spring Boot Admin Server的頁面:
在任意需要被監控的Spring Boot應用程序上,添加依賴:
<dependency>
<groupId>de.codecentric</groupId>
<artifactId>spring-boot-admin-starter-client</artifactId>
<version>1.5.7</version>
</dependency>
啟動客戶端后,稍等片刻,Spring Boot Admin Server從Eureka上拿到注冊信息后,即可在 http://localhost:8090 上看到應用程序的監控信息了:
3. FAQ
-
Spring Boot Admin Server上可以配置郵件告警信息,添加自己的Email即可收到告警信息。
-
Spring Boot Admin Server監控的應用程序服務,需要和Spring Boot Admin Server都注冊到Eureka上。
關注公眾號Java技術棧回復"面試"獲取我整理的2020最全面試題及答案。
推薦去我的博客閱讀更多:
2.Spring MVC、Spring Boot、Spring Cloud 系列教程
3.Maven、Git、Eclipse、Intellij IDEA 系列工具教程
覺得不錯,別忘了點贊+轉發哦!