天天看點

「Spring Boot」 Actuator Endpoint

作者:嘟null

Actuator

官網位址:https://docs.spring.io/spring-boot/docs/2.5.6/reference/html/actuator.html

⽬的

  • 監控并管理應⽤程式

通路⽅式

  • HTTP
  • JMX Java Management Extensions(Java管理擴充)的縮寫

依賴

  • spring-boot-starter-actuator

⼀些常⽤ Endpoint

「Spring Boot」 Actuator Endpoint
「Spring Boot」 Actuator Endpoint

如何通路 Actuator Endpoint

HTTP 通路

  • /actuator/

端⼝與路徑

  • management.server.address=
  • management.server.port=
  • management.endpoints.web.base-path=/actuator
  • management.endpoints.web.path-mapping.=路徑

開啟 Endpoint

  • management.endpoint..enabled=true
  • management.endpoints.enabled-by-default=false

暴露 Endpoint

  • management.endpoints.jmx.exposure.exclude=
  • management.endpoints.jmx.exposure.include=*
  • management.endpoints.web.exposure.exclude=
  • management.endpoints.web.exposure.include=info, health

完整版Endpoint

官網位址:https://docs.spring.io/spring-boot/docs/2.5.6/reference/html/actuator.html#actuator.endpoints

Actuator endpoints允許您監視應用程式并與之互動。Spring Boot包括許多内置endpoints,并允許您添加自己的endpoints。例如,health endpoint提供基本應用程式health資訊。

可以通過HTTP或JMX啟用或禁用每個endpoint并公開(使其遠端通路)。當一個endpoint同時啟用和公開時,它就被認為是可用的。内置endpoint隻有在可用時才會自動配置。大多數應用程式選擇通過HTTP進行公開,其中endpoint的ID加上/actuator字首被映射到一個URL。例如,預設情況下,health endpoint映射到/actuator/health。

要了解更多關于Actuator’s endpoints及其請求和響應格式的資訊,可參考單獨的API文檔(HTML or PDF)。

ID Description
auditevents 為目前應用程式公開審計事件資訊。需要一個AuditEventRepository bean。
beans 顯示應用程式中所有Spring bean的完整清單。
caches 公開可用的緩存。
conditions 顯示在配置和自動配置類上評估的條件,以及它們比對或不比對的原因。
configprops 顯示所有@ConfigurationProperties的排序清單。
env 暴露Spring中ConfigurableEnvironment的屬性。
flyway 顯示已應用的任何Flyway資料庫遷移。需要一個或多個Flyway bean。
health 顯示應用程式health資訊。
httptrace 顯示HTTP跟蹤資訊(預設情況下,最近100次HTTP請求-響應交換)。需要一個HttpTraceRepository bean。
info 顯示任意的應用程式資訊。
integrationgraph 顯示Spring Integration graph。需要依賴了spring-integration-core。
loggers 顯示和修改應用程式中loggers的配置。
liquibase 顯示已應用的任何Liquibase資料庫遷移。需要一個或多個Liquibase bean。
metrics 顯示目前應用程式的“metrics”資訊。
mappings 顯示所有@RequestMapping路徑的排序清單。
quartz 顯示有關Quartz Scheduler jobs的資訊。
scheduledtasks 顯示應用程式中的scheduled任務。
sessions 允許從Spring session支援的會話存儲中檢索和删除使用者會話。需要使用Spring Session的基于servlet的web應用程式。
shutdown 讓應用程式優雅地關閉。預設禁用。
startup 顯示由ApplicationStartup收集的啟動步驟資料。要求SpringApplication配置一個BufferingApplicationStartup。
threaddump 執行線程轉儲。

如果你的應用程式是一個web應用程式(Spring MVC, Spring WebFlux,或Jersey),你可以使用以下附加endPoints:

ID Description
heapdump 傳回一個hprof堆轉儲檔案。需要一個HotSpot JVM。
jolokia 通過HTTP公開JMX bean(當Jolokia位于類路徑上,對WebFlux不可用時)。需要存在依賴jolokia-core。
logfile 傳回日志檔案的内容(如果已經設定了logging.file.name或logging.file.path屬性)。支援使用HTTP Range頭來檢索部分日志檔案的内容。
prometheus 以Prometheus伺服器可以抓取的格式公開名額。需要依賴于micrometer-registry-prometheus。

啟用 EndPoints

預設情況下,除shutdown外的所有endpoints都是啟用的。通過配置可啟用endpoint,使用它的management.endpoint.<id>.enabled屬性。下面的例子啟用了shutdown endpoint:

management.endpoint.shutdown.enabled=true

如果您更喜歡選擇加入endpoint而不是選擇退出endpoint,可将management.endpoints.enabled-by-default屬性設定為false,再将單個endpoint啟用的屬性設定為true。下面的示例啟用info endpoint并禁用所有其他endpoints:

management.endpoints.enabled-by-default=false
management.endpoint.info.enabled=true           

這會從應用程式上下文中完全删除禁用的endpoint。如果您隻想更改暴露endpoint的技術,請使用include和exclude屬性。

暴露 EndPoints

由于endpoints可能包含敏感資訊,應仔細考慮何時公開它們。下表顯示了内置endpoints的預設公開。

ID JMX Web
auditevents Yes No
beans Yes No
caches Yes No
conditions Yes No
configprops Yes No
env Yes No
flyway Yes No
health Yes Yes
heapdump N/A No
httptrace Yes No
info Yes No
integrationgraph Yes No
jolokia N/A No
logfile N/A No
loggers Yes No
liquibase Yes No
metrics Yes No
mappings Yes No
prometheus N/A No
quartz Yes No
scheduledtasks Yes No
sessions Yes No
shutdown Yes No
startup Yes No
threaddump Yes No

要更改公開的endpoints,請使用以下特定于技術的include和exclude屬性:

Property Default
management.endpoints.jmx.exposure.exclude
management.endpoints.jmx.exposure.include *
management.endpoints.web.exposure.exclude
management.endpoints.web.exposure.include health

include屬性列出了公開的endpoints的id。exclude屬性列出不應公開的endpoints的id。exclude屬性優先于include屬性。包含和排除屬性都可以使用endpoint id清單進行配置。

例如,要停止通過JMX公開所有endpoints,而隻公開health and info endpoints,可使用以下屬性:

management.endpoints.jmx.exposure.include=health,info

*可以用來選擇所有endpoints。例如,要通過HTTP公開除env和beans endpoints外的所有内容,請使用以下屬性:

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans           

*在YAML中有特殊含義,是以如果您想包含(或排除)所有endpoints,請務必添加引号。

如果您的應用程式是公開的,我們強烈建議您也保護您的endpoints。

如果您想在endpoints暴露時實作自己的政策,可以注冊一個EndpointFilter bean

繼續閱讀