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項目裡添加依賴
1 <dependency>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-actuator</artifactId>
4 </dependency>
項目建立完畢後的pom檔案:
1 <parent>
2 <groupId>org.springframework.boot</groupId>
3 <artifactId>spring-boot-starter-parent</artifactId>
4 <version>2.0.2.RELEASE</version>
5 <relativePath/> <!-- lookup parent from repository -->
6 </parent>
7
8 <properties>
9 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
10 <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
11 <java.version>1.8</java.version>
12 </properties>
13
14 <dependencies>
15 <dependency>
16 <groupId>org.springframework.boot</groupId>
17 <artifactId>spring-boot-starter-actuator</artifactId>
18 </dependency>
19 <dependency>
20 <groupId>org.springframework.boot</groupId>
21 <artifactId>spring-boot-starter-web</artifactId>
22 </dependency>
23
24 <dependency>
25 <groupId>org.springframework.boot</groupId>
26 <artifactId>spring-boot-starter-tomcat</artifactId>
27 <scope>provided</scope>
28 </dependency>
29 <dependency>
30 <groupId>org.springframework.boot</groupId>
31 <artifactId>spring-boot-starter-test</artifactId>
32 <scope>test</scope>
33 </dependency>
34 </dependencies>
35
36 <build>
37 <plugins>
38 <plugin>
39 <groupId>org.springframework.boot</groupId>
40 <artifactId>spring-boot-maven-plugin</artifactId>
41 </plugin>
42 </plugins>
43 </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
1 management:
2 endpoints:
3 web:
4 exposure:
5 include: "*" #暴露所有端點 預設是info,health
重新啟動應用,控制台列印發生了變化,其餘的端點也被暴露出來了:
下面是對部分常用端點的簡要說明
詳細說明請檢視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端點并禁用所有其他端點:
1 management.endpoints.enabled-by-default = false
2 management.endpoint.info.enabled = true
修改路徑
1、修改字首:現在所有端點的字首預設是/actuator,如果想修改的話用management.endpoints.web.base-path屬性。
2、修改路徑:如果想修改端點的路徑,可以用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 /**
2 * 1.實作HealthIndicator接口
3 * 2.類名要求 xxxHealthIndicator xxx将會是你自定義得健康名額名稱
4 * [email protected]注入到容器内
5 * 4.重寫health()方法
6 * @author Administrator
7 *
8 */
9 @Component
10 public class MyAppHealthIndicator implements HealthIndicator{
11
12 @Override
13 public Health health() {
14 if(check()!=0){
15 return Health.up().build();
16 }
17 return Health.down().withDetail("error", "出錯了").build();
18 }
19
20 private int check(){
21 // 檢測是否健康的自定義邏輯
22 return 0;
23 }
24 }
然後重新開機應用發現多了自定義的健康名額
關于info端點
info端點預設是空的,我們可以在application配置檔案中配置info字首的屬性來完善
1 info:
2 app:
3 version: 1.1
4 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端點用途并不大。
---------------------
原文:https://blog.csdn.net/WYA1993/article/details/80540981
版權聲明:本文為部落客原創文章,轉載請附上博文連結!
轉載于:https://www.cnblogs.com/holly8/p/SpringCloud.html