SpringBoot集成Actuator端点配置

2020-09-29  本文已影响0人  木木与呆呆

1.说明

Actuator端点可以监控应用程序并与之交互。
Spring Boot包括许多内置的端点,
比如health端点提供基本的应用程序运行状况信息,
并允许添加自定义端点。

可以控制每个单独的端点启用或禁用,
也可以通过include和exclude属性通配,
这会影响端点的Bean创建。
要远程访问端点,还必须通过JMX或HTTP公开。
大多数应用程序选择HTTP方式对外开放,
其中/actuator前缀和端点的ID组成了对外暴露的URL。
比如默认情况下,health端点映射到/actuator/health。

本文主要介绍通过HTTP进行监控和管理。

2.支持的端点

以下是与技术无关的端点:

ID 描述
auditevents 公开当前应用程序的审核事件信息。
beans 显示应用程序中所有Spring bean的完整列表。
caches 公开可用的缓存。
conditions 显示在配置和自动配置类上评估的条件,以及它们匹配或不匹配的原因。
configprops 显示所有@ConfigurationProperties的整理列表。
env 从Spring的ConfigurableEnvironment中公开属性。
flyway 显示已应用的所有Flyway数据库迁移。
health 显示应用程序运行状况信息。
httptrace 显示HTTP跟踪信息(默认情况下,记录最后100个HTTP请求-响应)。
info 显示任意应用程序信息。
integrationgraph 显示Spring集成图。
loggers 显示和修改应用程序中日志记录器的配置。
liquibase 显示已应用的任何Liquibase数据库迁移。
metrics 显示当前应用程序的"度量"信息。
mappings 显示所有@RequestMapping路径的整理列表。
scheduledtasks 显示应用程序中的计划任务。
sessions 允许从支持Spring Session的会话存储中检索和删除用户会话。在使用Spring Session对反应式web应用程序的支持时不可用。
shutdown 允许优雅地关闭应用程序。
threaddump 执行线程转储。

如果你的应用程序是 web 应用程序(Spring MVC、Spring WebFlux或Jersey),则可以使用以下附加端点:

ID 描述
heapdump 返回hprof堆转储文件。
jolokia 通过HTTP公开JMX bean(当Jolokia在类路径上时,WebFlux不可用)。
logfile 返回日志文件的内容(如果已设置logging.file或logging.path属性)。支持使用 HTTP Range 头来检索日志文件的部分内容。 Yes
prometheus 以Prometheus服务器可以抓取的格式公开度量。

3.默认公开端点

由于端点可能包含敏感信息,
因此要仔细考虑何时公开它们。
下表显示了内置端点对于JMX或HTTP,
是否默认对外公开的情况:

ID JMX HTTP
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 Yes
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
scheduledtasks Yes No
sessions Yes No
shutdown Yes No
threaddump Yes No

从上表可以看到HTTP默认对外公开2个端点,
只有health和info。

实际上公开的端点受到include和exclude属性控制,
下表显示了这两个属性的默认配置,
和上表能够一一对应起来,
其中包含web的属性对应的是HTTP的配置。

属性 默认
management.endpoints.jmx.exposure.exclude
management.endpoints.jmx.exposure.include *
management.endpoints.web.exposure.exclude
management.endpoints.web.exposure.include info, health

4.修改公开端点

只公开loggers端点:

management:
  endpoints:
    web:
      exposure:
        include: loggers

公开所有端点,但是要排除loggers和env端点:

management:
  endpoints:
    web:
      exposure:
        include: "*"
        exclude:
        - loggers
        - env

注意*在YAML中具有特殊含义,
如果要包括(或排除)所有端点,
请务必添加引号。
另外如果想在公开端点时实现自定义策略,
可以注册EndpointFilter bean。

5.查看所有公开端点

通过修改配置,公开所有端点后,
浏览器访问Actuator提供的管理URL:

http://localhost:8011/actuator

返回结果:

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

发现对外开放了14个端点,
没有对外开放的端点还有9个:
auditevents, flyway, integrationgraph, jolokia, logfile,
liquibase, prometheus, sessions, shutdown。

6.启用端点

上面介绍了公开端点的配置,
但是即使公开了所有端点,
也不一定能看到对应的端点。
上面还有9个端点没有对外开放,
原因是有2个,
一个端点是没有启用,
另一个是端点因为没有相关的功能实现,
所以暴露出去也没有用。
而且除shutdown以外,
所有端点是默认启用的。

下面演示启用shutdown端点:

management:
  endpoint:
    shutdown:
      enabled: true

7.禁用端点(通配)

禁用端点可以通过设置对应ID为faslse,
下面演示关闭logger端点:

management:
  endpoint:
    loggers:
      enabled: false

也可以通过设置所有端点的开启默认值为fasle,
从而禁用所有端点,
然后再启用特定端点,
下面演示禁用所有端点,
只启用shutdown端点:

management:
  endpoints:
    enabled-by-default: false
  endpoint:
    shutdown:
      enabled: true

同样的启用所有端点,
只禁用shutdown端点:

management:
  endpoints:
    enabled-by-default: true
  endpoint:
    shutdown:
      enabled: false

8.公开端点和启用端点的区别

只有一个端点同时公开和启用了才能访问到;
一个端点启用后,
可以选择在JMX公开,
而不在HTTP公开;
一个端点禁用后,
不管在JMX或者HTTP是否公开,
都无法被访问到。
从而实现更细致的控制管理。

9.自定义管理端点的路径

可以为管理端点自定义前缀,
特别是已经将/actuator用于其他目的,
下面修改管理端点的前缀为/manage:

management:
  endpoints:
    web:
      base-path: /manage

重启服务后,原来的管理URL不能再访问:

http://localhost:8011/actuator

应该访问新的管理URL:

http://localhost:8011/manage

而且其下的health等其他端点也变成新的URL:

http://localhost:8011/manage/health

如果把管理端点路径设置为/或者空时,
将禁用管理URL,
以防止与其他映射发生冲突。

10.自定义普通端点的路径

可以将端点映射到不同的路径,
下面以健康检查为例,
演示将/actuator/health映射到/actuator/healthcheck:

management:
  endpoints:
    web:
      path-mapping:
        health: healthcheck

重启服务后,原来的健康URL不能再访问:

http://localhost:8011/actuator/health

应该访问新的健康URL:

http://localhost:8011/actuator/healthcheck

进一步修改管理端点路径设置为/:

management:
  endpoints:
    web:
      path-mapping:
        health: healthcheck
      base-path: /

虽然不能访问管理URL,
但是可以访问更短的健康URL:

http://localhost:8011/healthcheck
上一篇下一篇

猜你喜欢

热点阅读