認識Actuator

在SpringBoot應用中引入spring-boot-starter-actuator依賴,它可以為開發團隊提供系統運行的各項監控指標。

在項目中引入依賴配置如下:

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>

application.properties屬性配置文件,添加如下配置:

#開放所有的web Endpoints
management.endpoints.web.exposure.include= *

spring-boot-starter-actuator模塊根據應用依賴和配置自動創建出來的監控和管理端點。通過這些端點,我們可以實時獲取系統應用的各項監控指標。

訪問:localhost:8081/actuator

所有端點詳細信息如下:

// http://localhost:8081/actuator

{
"_links": {
"self": {
"href": "http://localhost:8081/actuator",
"templated": false
},
"auditevents": {
"href": "http://localhost:8081/actuator/auditevents",
"templated": false
},
"beans": {
"href": "http://localhost:8081/actuator/beans",
"templated": false
},
"caches-cache": {
"href": "http://localhost:8081/actuator/caches/{cache}",
"templated": true
},
"caches": {
"href": "http://localhost:8081/actuator/caches",
"templated": false
},
"health-component-instance": {
"href": "http://localhost:8081/actuator/health/{component}/{instance}",
"templated": true
},
"health-component": {
"href": "http://localhost:8081/actuator/health/{component}",
"templated": true
},
"health": {
"href": "http://localhost:8081/actuator/health",
"templated": false
},
"conditions": {
"href": "http://localhost:8081/actuator/conditions",
"templated": false
},
"configprops": {
"href": "http://localhost:8081/actuator/configprops",
"templated": false
},
"env": {
"href": "http://localhost:8081/actuator/env",
"templated": false
},
"env-toMatch": {
"href": "http://localhost:8081/actuator/env/{toMatch}",
"templated": true
},
"info": {
"href": "http://localhost:8081/actuator/info",
"templated": false
},
"loggers": {
"href": "http://localhost:8081/actuator/loggers",
"templated": false
},
"loggers-name": {
"href": "http://localhost:8081/actuator/loggers/{name}",
"templated": true
},
"heapdump": {
"href": "http://localhost:8081/actuator/heapdump",
"templated": false
},
"threaddump": {
"href": "http://localhost:8081/actuator/threaddump",
"templated": false
},
"metrics": {
"href": "http://localhost:8081/actuator/metrics",
"templated": false
},
"metrics-requiredMetricName": {
"href": "http://localhost:8081/actuator/metrics/{requiredMetricName}",
"templated": true
},
"scheduledtasks": {
"href": "http://localhost:8081/actuator/scheduledtasks",
"templated": false
},
"httptrace": {
"href": "http://localhost:8081/actuator/httptrace",
"templated": false
},
"mappings": {
"href": "http://localhost:8081/actuator/mappings",
"templated": false
}
}
}

可以將spring-boot-starter-actuator模塊生成的原生端點,分為三大類:

  1. 應用配置類:獲取應用程序中載入的應用配置、環境變數、自動化配置報告等與SpringBoot應用密切相關的配置類信息;這類端點可以幫助我們獲取一系列關於Spring應用配置內容的詳細報告,比如自動化配置的報告、Bean創建的報告、環境屬性的報告等。
  2. 度量指標類:獲取應用程序運行過程中用於監控的度量指標,比如內存信息、線程池信息、HTTP請求統計等;
  3. 操作監控類:提供了對應用的關閉等操作類功能;以上三類端點的詳細信息,下面我們詳細來說明。

端點信息詳述

應用配置類:默認啟用

  1. /autoconfig: 該端點用來獲取應用的自動化配置報告,其中包括所有自動化配置的候選項,同時列舉了每個候選項是否滿足自動化配置的各個先決條件。該端點可以幫助我們方便地找到一些自動化配置為什麼沒有生效的具體原因。報告內容分為兩部分,如下:(1)positiveMatches中返回的是條件匹配成功的自動化配置;(2)negativeMathches中返回的是條件匹配不成功的自動化配置。
  2. /beans: 該端點用來獲取應用上下文中創建的所有Bean。包含的具體信息如下:(1)bean:Bean的名稱;(2)scope: Bean的作用域;(3)type: Bean的Java類型;(4)resource: class文件的具體路徑;

    (5)dependencies: 依賴的Bean的名稱;

  3. /configprops: 該端點用來獲取應用中配置的屬性信息報告。我們可以通過該報告來看到各個屬性的配置路徑,比如我們要關閉端點,就可以使用endpoints.configprops.enabled=false來完成設置。
  4. /env: 該端點用來獲取應用所有可用的環境屬性報告,具體包括環境變數、JVM屬性、應用的配置屬性、命令行中的參數。通過該端點返回的信息,我們可以看到當前應用載入的配置信息,可以結合@ConfigurationProperties註解將它們引入到應用程序中使用。對於一些敏感屬性信息,比如在屬性名中包含password,secret,key這些關鍵詞,在返回的時候會使用*來替換。
  5. /mappings: 該端點用來返回所有Spring MVC的控制器映射關係報告。返回的信息有:(1)bean屬性: 標識該映射關係的請求處理器;(2)method屬性:標識該映射關係的具體處理類和處理函數;
  6. /info:該端點用來返回一些應用自定義的信息。默認情況下,該端點只會返回一個空的JSON內容。我們可以在application.properties配置文件中設置一些以info為前綴的屬性配置信息,就能看到效果。

度量指標類:默認啟用

  1. /metrics: 該端點用來返回當前應用的各類重要度量指標,比如內存信息、線程信息、垃圾回收信息等。具體信息包含如下:(1)系統信息:包括處理器數量processors、運行時間uptime和instance.uptime、系統平均負載systemload.average;(2)mem.*: 內存概要信息,包括分配給應用的總內存數量以及當前空閑的內存數量,這些信息來自java.lang.Runtime;(3)heap.*: 堆內存使用情況。這些信息來自java.lang.management.MemoryMXBean介面中getHeapMemoryUsage方法獲取的java.lang.management.MemoryUsage;(4)nonheap.*: 非堆內存使用情況。這些信息來自java.lang.management.MemoryMXBean介面中getNonHeapMemoryUsage方法獲取java.lang.management.MemoryUsage;(5)threads.*: 線程使用情況,包括線程數、守護線程(daemon)、線程峰值(peak)等,這些信息來自java.lang.management.ThreadMXBean;(6)classes.*: 應用載入和卸載的類統計,這些信息來自java.lang.managemeng.ClassLoadingMXBean;(7)gc.*: 垃圾收集器的詳細信息,包括垃圾回收次數gc.ps_scavenge.count、垃圾回收消耗時間gc.ps_scavenge.time、標記-清除演算法的次數gc.ps_marksweep.count、標記-清除演算法的消耗時間gc.ps_marksweep.time。這些信息來自java.lang.management.GarbageCollectorMXBean;

    (8)httpsessions.*: Tomcat容器的會話使用情況。包括最大會話數httpsessions.max和活躍會話數httpsessions.active。該度量指標信息僅在引入嵌入式Tomcat作為應用容器的時候才會提供;

    (9)gauge.*: HTTP請求的性能指標之一,它主要用來反映一個絕對值。(10)counter.*: HTTP請求的性能指標之一,它主要作為計數器來使用,記錄了增加量和減少量。報告的具體內容如下:
  2. /health: 該端點用來獲取應用的各類健康指標信息。在spring-boot-starter-actuator模塊中自帶實現了一些常用資源的健康指標檢測器,這些健康指標檢測器都是通過HealthIndicator介面實現,並且會根據依賴關係的引入實現自動化配置。常見的一些健康指標檢測器如下:(1)DiskSpaceHealthIndicator: 低磁碟空間檢測;(2)DataSourceHealthIndicator: 檢測DataSource的鏈接是否成功;(3)MongoHealthIndicator: 檢測Mongo資料庫是否可用;(4)RabbitHealthIndicator: 檢測Rabbit伺服器是否可用;(5)RedisHealthIndicator: 檢測Redis伺服器是否可用;(6)SolrHealthIndicator: 檢測Solr伺服器是否可用;

    我們也可用通過實現HealthIndicator介面來自定義自己的健康指標檢測器;

    報告的具體內容如下:
  3. /dump: 該端點用來暴露程序運行中的線程信息。它使用java.lang.management.ThreadMXBean的dumpAllThreads方法來返回所有含有同步信息的活動線程詳情;
  4. /trace: 該端點用來返回基本的HTTP跟蹤信息。默認情況下,跟蹤信息的存儲採用org.springframework.boot.actuate.trace.InMemoryTraceRepository實現的內存方式,始終保留最近的100條請求記錄。

操作控制類:需要通過屬性配置來開啟操作

  1. /shutdown: 該端點用來實現關閉應用的遠程操作,需要配置屬性endpoints.shutdown.enable=true才能實現。該操作非常危險,不建議使用。

到此關於spring-boot-starter-actuator模塊的監控與管理的相關端點信息就介紹完了。

代碼見:github.com/JavaCodeMood

參考資料: 《Spring Cloud微服務實戰》 翟永超 著

作者:霜花似雪

鏈接:imooc.com/article/28593

來源:慕課網

本文原創發佈於慕課網 ,轉載請註明出處,謝謝合作


推薦閱讀:

最讓程序員自豪的事情是什麼?

接手別人的代碼,死的心有嗎?

如何學習 SQL 語言?

30行Javascript代碼實現圖片懶載入

PWA,現代前端必會的黑科技

暴露真實IP真的沒關係嗎?

月薪3萬的程序員都避開了哪些坑?

和程序猿談戀愛是一種怎樣的體驗?


推薦閱讀:
相关文章