Springboot(一)监控与管理Actuator Spring Boot 2.0官方文档之 Actuator

前言:

     Actuator是一个暴露自身信息的模块,主要实现对应用系统的监控与管理。主要有三大功能:

  • 应用配置类:获取应用程序中加载的应用配置、环境变量、自动化配置报告等与Spring Boot应用密切相关的配置类信息。
  • 度量指标类:获取应用程序运行过程中用于监控的度量指标,比如:内存信息、线程池信息、HTTP请求统计等。
  • 操作控制类:提供了对应用的关闭等操作类功能。


加入maven包:
 
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-actuator</artifactId>
        </dependency>

在springboot配置文件中配置访问根路径,springboot2.0默认: /actuator,老版本为/

management:
  endpoints:
    web:
      base-path: /actuator
端点(Endpoints)
    端点可用于监控应用或者与应用进行交互,Spring Boot包含很多内置的端点,每个断电都可以禁用或者启用,要访问远程端点必须通过JMX或者http进行暴露,下图为启动应用程序的端点。


端点列表:




ID
描述 默认启用
auditevents 显示当前应用程序的审计事件信息 Yes
beans 显示一个应用中所有Spring Beans的完整列表 Yes
conditions 显示配置类和自动配置类(configuration and auto-configuration classes)的状态及它们被应用或未被应用的原因 Yes
configprops 显示一个所有@ConfigurationProperties的集合列表 Yes
env 显示来自Spring的 ConfigurableEnvironment的属性 Yes
flyway 显示数据库迁移路径,如果有的话 Yes
health 显示应用的健康信息(当使用一个未认证连接访问时显示一个简单的’status’,使用认证连接访问则显示全部信息详情) Yes
info 显示任意的应用信息 Yes
liquibase 展示任何Liquibase数据库迁移路径,如果有的话 Yes
metrics 展示当前应用的metrics信息 Yes
mappings 显示一个所有@RequestMapping路径的集合列表 Yes
scheduledtasks 显示应用程序中的计划任务 Yes
sessions 允许从Spring会话支持的会话存储中检索和删除(retrieval and deletion)用户会话。使用Spring Session对反应性Web应用程序的支持时不可用。 Yes
shutdown 允许应用以优雅的方式关闭(默认情况下不启用) No
threaddump 执行一个线程dump Yes

如果使用web应用(Spring MVC, Spring WebFlux, 或者 Jersey),你还可以使用以下端点:

ID 描述 默认启用
heapdump 返回一个GZip压缩的hprof堆dump文件 Yes
jolokia 通过HTTP暴露JMX beans(当Jolokia在类路径上时,WebFlux不可用) Yes
logfile 返回日志文件内容(如果设置了logging.file或logging.path属性的话),支持使用HTTP Range头接收日志文件内容的部分信息 Yes
prometheus 以可以被Prometheus服务器抓取的格式显示metrics信息 Yes

端点启动不代表可以直接用,必须将其暴露出来。

启用端点:

   默认除了shutdown以外,所有端点均已启用,要配置单个端点的启用,需要按照下面方式:

management.endpoint.shutdown.enabled=true

另外可以通过management.endpoints.enabled-by-default来修改全局端口默认配置,以下示例启用info端点并禁用所有其他端点:

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

暴露端点

由于端点可能包含敏感信息,因此应仔细考虑何时公开它们。下表显示了内置端点的默认曝光:

ID JMX Web
auditevents Yes No
beans 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
jolokia Yes No
logfile Yes 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

要更改公开哪些端点,请使用以下技术特定的includeexclude属性:

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

include属性列出了公开的端点的ID,exclude属性列出了不应该公开的端点的ID
exclude属性优先于include属性。包含和排除属性都可以使用端点ID列表进行配置。

例如,要停止通过JMX公开所有端点并仅公开healthinfo端点,请使用以下属性:

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

可以用来选择所有端点。例如,要通过HTTP公开除env和beans端点之外的所有内容,请使用以下属性:

management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env,beans
*在YAML中有特殊的含义,所以如果你想包含(或排除)所有的端点,一定要加引号,如下例所示:
management:
  endpoints:
    web:
      exposure:
        include: '*'
跨域支持

跨源资源共享(Cross-origin resource sharing,CORS)是W3C规范,允许您以灵活的方式指定授权哪种跨域请求。如果您使用Spring MVC或Spring WebFlux,则可以配置Actuator的Web端点来支持这些场景。

默认情况下,CORS支持处于禁用状态,只有在设置了management.endpoints.web.cors.allowed-origins属性后才能启用。以下配置允许来自example.com域的GET和POST调用:

management.endpoints.web.cors.allowed-origins=http://example.com
management.endpoints.web.cors.allowed-methods=GET,POST
自定义管理服务器地址

您可以通过设置management.server.address属性来自定义管理端点可用的地址。如果您只想在内部网络或面向操作系统的网络上收听,或只收听本地主机的连接,那么这样做会很有用。

只有当端口与主服务器端口不同时,您才可以监听其他地址。

以下示例application.properties不允许远程管理连接:

management.server.port=8081
management.server.address=127.0.0.1
  • 2禁用HTP端点

如果您不想通过HTTP公开端点,则可以将管理端口设置为-1,如以下示例所示:

management.server.port=-1



我的spring配置文件:

management:
  endpoints:
    web:
      base-path: /actuator
      exposure:
        include: '*'

程序启动时检测到的端点:




我的github地址


参考:

猜你喜欢

转载自blog.csdn.net/u012326462/article/details/80596051