Spring Cloud系列(二) 應用監控與管理Actuator
前言:要想使用Spring Cloud ,Spring Boot 提供的spring-boot-starter-actuator模組是必須瞭解的,這篇文章就先介紹一下actuator的使用。
由於我們把一個複雜高耦合的單體系統拆分成了多個小型服務,所以部署應用的數量在不斷增長,造成維護複雜度大大提升。所以我們需要一套自動化的監控運維機制,這套運維機制可以不間斷的獲取每個服務應用的各種指標,並根據這些指標資訊來制定監控預警規則。
Spring Boot提供了一個依賴模組:spring-boot-starter-actuator,這個模組可以自動為Spring Boot建立的應用構建一系列的用於監控的端點,而且Spring Cloud還在這個基礎上進行了擴充套件,當然在不滿足我們業務需求時也需要對這個模組進行擴充套件。
接下來建立一個Spring Boot專案命名actuator,勾選Actuator依賴
或者在你現有的Spring Boot專案裡新增依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
專案建立完畢後的pom檔案:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.2.RELEASE</version> <relativePath/> <!-- lookup parent from repository --> </parent> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build>
說一下我的版本:jdk1.8、Spring Boot 2.0.2。
接下來就可以啟動應用了,發現控制檯列印如下資訊:
/actuator/health和/actuator/info以及/actuator這三個就是actuator提供的端點,注意以前的版本是沒有/actuator字首的,2.0以後的版本都加了/actuator字首,而且看官方文件actuator提供瞭如下端點:
我們只有health和info端點是因為actuator預設只暴露了health和info端點,我們可以選擇全部暴露或者指定暴露部分端點,修改application.yml
management: endpoints: web: exposure: include: "*" #暴露所有端點 預設是info,health
重新啟動應用,控制檯打印發生了變化,其餘的端點也被暴露出來了:
下面是對部分常用端點的簡要說明
auditevents | 公開當前應用程式的審計事件資訊。 |
beans | 顯示應用程式中所有Spring bean的完整列表。 |
configprops | 顯示應用中配置的屬性資訊報告。 |
env | 顯示應用中所有可用的環境屬性報告,包括環境變數、JVM屬性、應用的配置屬性、命令列的引數。 |
health | 顯示應用健康資訊。 |
httptrace | 顯示HTTP跟蹤資訊(預設情況下為最後100個HTTP請求 - 響應交換)。 |
info | 顯示應用的自定義資訊,預設是空。 |
metrics | 顯示當前應用程式的“指標”資訊,如記憶體資訊、執行緒資訊。 |
mappings | 顯示所有url對映。 |
scheduledtasks | 顯示應用程式中的計劃任務。 |
shutdown | 讓應用程式正常關機。 |
threaddump | 程式執行中的執行緒資訊。 |
詳細說明請檢視actuator-api文件actuator-api,注意這是Spring Boot2.0.2的文件,其餘版本請去官網自行查詢。
開啟和關閉端點
使用management.endpoint.<id>.enabled來修改端點的開啟關閉狀態,如以關閉health端點為例
management.endpoint.health.enabled=false
如果希望端點啟用選擇加入而不是選擇退出,請將management.endpoints.enabled-by-default屬性設定為false並設定想選擇加入端點的enabled=true重新加入。以下示例啟用info端點並禁用所有其他端點:
management.endpoints.enabled-by-default = false
management.endpoint.info.enabled = true
修改路徑
- 修改字首:現在所有端點的字首預設是/actuator,如果想修改的話用management.endpoints.web.base-path屬性。
- 修改路徑:如果想修改端點的路徑,可以用management.endpoints.web.path-mapping屬性。
比如我們想把/autuator/health修改為/healthcheck。
management.endpoints.web.base-path=/
management.endpoints.web.path-mapping.health=healthcheck
重啟專案後所有端點都去掉了/actuator字首,並且health端點的路徑變成了healthcheck
當然,如果你想修改端點的埠,也是可以的,可以通過以下屬性修改
management.server.port = 8081
如果您不想通過HTTP公開端點,則可以將管理埠設定為-1
management.server.port = -1
關於shutdown端點
shutdown端點可以用來遠端關閉應用,此端點預設是關閉的,如果使用的話,需要開啟,使用以下屬性
management.endpoint.shutdown.enabled = true
你就可以在應用啟動後遠端通過呼叫/actuator/shutdown來關閉應用,注意只能POST請求呼叫。
關於health端點
我們嘗試訪問/actuator/health端點,返回
{"status":"UP"}
只有status一個屬性,檢視官方文件health端點的management.endpoint.health.show-details屬性預設不展示細節,我們可以修改一下
management:
endpoints:
web:
exposure:
include: "*" #暴露所有端點 預設是info和health
endpoint:
health:
show-details: always #預設是never
重新啟動再次請求,會發現多了一個磁碟空間的狀態資訊,返回
{"status":"UP","details":{"diskSpace":{"status":"UP","details":{"total":169917878272,"free":138603999232,"threshold":10485760}}}}
health端點預設自帶了一些常用資源的健康指標檢測器,只要你引入了以下依賴就會自動新增到health裡
我們也可以自己擴充套件一個健康指標檢測器
/**
* 1.實現HealthIndicator介面
* 2.類名要求 xxxHealthIndicator xxx將會是你自定義得健康指標名稱
* [email protected]注入到容器內
* 4.重寫health()方法
* @author Administrator
*
*/
@Component
public class MyAppHealthIndicator implements HealthIndicator{
@Override
public Health health() {
if(check()!=0){
return Health.up().build();
}
return Health.down().withDetail("error", "出錯了").build();
}
private int check(){
// 檢測是否健康的自定義邏輯
return 0;
}
}
然後重啟應用發現多了自定義的健康指標
關於info端點
info端點預設是空的,我們可以在application配置檔案中配置info字首的屬性來完善
info:
app:
version: 1.1
name: aut #/actuator/info 自定義的info端點 否則是空的
訪問/actuator/info
我們也可以用info端點描述Git版本資訊,在application.yml或者application.properties同級目錄建立git.properties,新增屬性git.branch=master,再次重啟訪問/actuator/info。
git.屬性名是來自於GitProperties類,eclipse中使用ctrl+shift+t輸入GitProperties就可以查看了,前提是你下載了原始碼,當然你也可以引入git的外掛,具體我就不介紹了,想了解的可以看下這篇文章http://blog.didispace.com/spring-boot-actuator-info-git/,總的來說info端點用途並不大。