Spring Cloud系列(二) 應用監控與管理Actuator


轉自:https://blog.csdn.net/WYA1993/article/details/80540981

前言:要想使用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

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端點並禁用所有其他端點:

  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。

  1.  
    management.endpoints.web.base-path=/
  2.  
    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.  
    * 3.@Component注入到容器內
  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前綴的屬性來完善

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端點用途並不大。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM