在以往的分布式開發當中,各個服務節點的監控必不可少。監控包含有很多方面,比如說:內存占用情況,節點是否健康等。在spring-boot會給我們提供相關資源監控叫做spring-boot-actuator, 通過執行器可以幫我管理和監控生產環境下的應用服務。
一。添加SpringBoot執行器的依賴(版本2.0.0.RELEASE)
添加gradle配置依賴:

dependencies { compile('org.springframework.boot:spring-boot-starter-actuator') }
二。關於SpringBoot的端點
端點是一個提供給我們監控應用程序的功能點,SpringBoot提供了一系列內置端點叫我們使用,舉個例子:health端點為我們提供了一個對我們基礎程序的一個健康狀態的監控
每個端點都可以打開或關閉,絕大多數的應用都可以通過http請求進行訪問,springboot包含很多內置內置端點。tips:參考官網的
ID | Description | Enabled by default |
---|---|---|
|
Exposes audit events information for the current application. |
Yes |
|
Displays a complete list of all the Spring beans in your application. |
Yes |
|
Shows the conditions that were evaluated on configuration and auto-configuration classes and the reasons why they did or did not match. |
Yes |
|
Displays a collated list of all |
Yes |
|
Exposes properties from Spring’s |
Yes |
|
Shows any Flyway database migrations that have been applied. |
Yes |
|
Shows application health information. |
Yes |
|
Displays HTTP trace information (by default, the last 100 HTTP request-response exchanges). |
Yes |
|
Displays arbitrary application info. |
Yes |
|
Shows and modifies the configuration of loggers in the application. |
Yes |
|
Shows any Liquibase database migrations that have been applied. |
Yes |
|
Shows ‘metrics’ information for the current application. |
Yes |
|
Displays a collated list of all |
Yes |
|
Displays the scheduled tasks in your application. |
Yes |
|
Allows retrieval and deletion of user sessions from a Spring Session-backed session store. Not available when using Spring Session’s support for reactive web applications. |
Yes |
|
Lets the application be gracefully shutdown. |
No |
|
Performs a thread dump. |
Yes |
有幾點要補充說明一下:
1). 我們訪問的端點監控的地址規范是:/actuator/{ID}的方式訪問,比如說:health端點默認被映射的路徑就是/actuator/health
2) 並不是所有端點都可以通過http請求訪問,以下表格列舉了各個端點的狀態值:
ID | JMX | Web |
---|---|---|
|
Yes |
No |
|
Yes |
No |
|
Yes |
No |
|
Yes |
No |
|
Yes |
No |
|
Yes |
No |
|
Yes |
Yes |
|
N/A |
No |
|
Yes |
No |
|
Yes |
Yes |
|
N/A |
No |
|
N/A |
No |
|
Yes |
No |
|
Yes |
No |
|
Yes |
No |
|
Yes |
No |
|
N/A |
No |
|
Yes |
No |
|
Yes |
No |
|
Yes |
No |
|
Yes |
No |
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
3)當我們訪問端點時,響應的數據會緩存一定的時間,我們可以通過這個屬性進行配置:
management.endpoint.<id>.cache.time-to-live=10s
4) 我們也可以自己定義監視路徑,默認情況是:/actuator/xxx,通過如下屬性可以設置:
management.endpoints.web.base-path=/
5) 關於保護敏感端點,首先我們要添加對spring-security的依賴,並設置進行安全驗證的用戶名,密碼以及角色,如果不使用spring-security就要慎重考慮暴露端口的端點了
三。關於Health端點
- health端點用於檢測我們運行程序的健康狀態,當程序宕機時,可以提供給開發人員相關的提示信息
- 默認情況下該端點只是顯示簡略的監控信息,不過我們可以通過management.endpoint.health.show-details屬性來讓其顯示詳細的監控信息
- 端點有如下幾種狀態: UP DOWN UNKNOW-SERVICE 從字面上我們很好理解
- 實現自定義健康監控:

package com.bdqn.lyrk.springboot.study.monitor; import org.springframework.boot.actuate.health.Health; import org.springframework.boot.actuate.health.HealthIndicator; import org.springframework.stereotype.Component; @Component public class SelfMonitor implements HealthIndicator { @Override public Health health() { return Health.up().withDetail("health","next").build(); } }
例子很簡單,主要是實現HealthIndicator接口,當我們訪問:http://localhost:8080/actuator/health時,我們可以看到相關監控信息,如下圖: