【Spring Boot】 Actuator Endpoint

Actuator

官網(wǎng)地址:https://docs.spring.io/spring-boot/docs/2.5.6/reference/html/actuator.html

?的

  • 監(jiān)控并管理應?程序

訪問?式

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

依賴

  • spring-boot-starter-actuator

?些常? Endpoint

image
image

如何訪問 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

官網(wǎng)地址:https://docs.spring.io/spring-boot/docs/2.5.6/reference/html/actuator.html#actuator.endpoints

Actuator endpoints允許您監(jiān)視應用程序并與之交互。Spring Boot包括許多內(nèi)置endpoints,并允許您添加自己的endpoints。例如,health endpoint提供基本應用程序health信息。

可以通過HTTP或JMX啟用或禁用每個endpoint并公開(使其遠程訪問)。當一個endpoint同時啟用和公開時,它就被認為是可用的。內(nèi)置endpoint只有在可用時才會自動配置。大多數(shù)應用程序選擇通過HTTP進行公開,其中endpoint的ID加上/actuator前綴被映射到一個URL。例如,默認情況下,health endpoint映射到/actuator/health

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

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

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

ID Description
heapdump 返回一個hprof堆轉(zhuǎn)儲文件。需要一個HotSpot JVM。
jolokia 通過HTTP公開JMX bean(當Jolokia位于類路徑上,對WebFlux不可用時)。需要存在依賴jolokia-core。
logfile 返回日志文件的內(nèi)容(如果已經(jīng)設(shè)置了logging.file.name或logging.file.path屬性)。支持使用HTTP Range頭來檢索部分日志文件的內(nèi)容。
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屬性設(shè)置為false,再將單個endpoint啟用的屬性設(shè)置為true。下面的示例啟用info endpoint并禁用所有其他endpoints:

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

這會從應用程序上下文中完全刪除禁用的endpoint。如果您只想更改暴露endpoint的技術(shù),請使用include和exclude屬性。

暴露 EndPoints

由于endpoints可能包含敏感信息,應仔細考慮何時公開它們。下表顯示了內(nèi)置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,請使用以下特定于技術(shù)的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屬性優(yōu)先于include屬性。包含和排除屬性都可以使用endpoint id列表進行配置。

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

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

*可以用來選擇所有endpoints。例如,要通過HTTP公開除envbeans endpoints外的所有內(nèi)容,請使用以下屬性:

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

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

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

如果您想在endpoints暴露時實現(xiàn)自己的策略,可以注冊一個EndpointFilter bean

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容