javascript
Spring Cloud应用监控与管理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文檔點擊打開鏈接,注意這是Spring Boot2.0.2的文檔,其余版本請去官網自行查找。
開啟和關閉端點
使用management.endpoint.<id>.enabled來修改端點的開啟關閉狀態,如以關閉health端點為例
management.endpoint.health.enabled=false
如果您希望端點啟用選擇加入而不是選擇退出,請將management.endpoints.enabled-by-default屬性設置?為false并使用各個端點?enabled屬性重新加入。以下示例啟用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將會是你自定義得健康指標名稱
* 3.@Component注入到容器內
* 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端點用途并不大。
總結
以上是生活随笔為你收集整理的Spring Cloud应用监控与管理Actuator的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: solr elasticsearch比较
- 下一篇: Spring 处理过程分析