天天看点

「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

继续阅读