2018.11.14


文章目录

  • 前言
  • 介绍
  • 技术透明( technology-agnostic)的端点
  • 端点使用
  • 端点开启
  • 端点公开方式
  • 加固HTTP端点访问
  • 自定义端点


前言

翻译自Spring Boot官方文档——Spring Boot Actuator Endpoint1

介绍

Spring Boot的Actuator执行器endpoints端点,可以用于应用程序的监控和交互。Spring Boot内建了许多端点,用户也可以自定义端点。比如health端点可以提供应用健康信息。

每个端点都可以单独地开启和关闭。若要远程访问端点,才需要开通JMX或HTTP访问。HTTP的访问方式,就是访问由/actuator/<endpoint-id>拼接成的URL。如health端点的URL为/actuator/health

技术透明( technology-agnostic)的端点

ID

说明

默认是否开启

beans

整理并展示应用中所有的Spring beans列表


mappings

整理并展示应用中所有@RequestMappings路径


scheduledtasks

展示应用中所有调度任务


sessions

支持用户会话的回收和删除,但不支持Spring reactive web应用


shutdown

支持应用优雅退出


如果是Web应用(Spring MVC、Spring WebFlux等),则还有如下端点:

ID

说明

是否默认开启

logfile

返回logfile文件内容(需要配置logging.file或logging.path)


端点使用

端点开启

默认配置下,除了shutdown外所有端点都是开启的。开启一个端点,使用端点的management.endpoint.<id>.enabled属性。
如果希望端点的开启是“选择进入”(opt-in)而非“选择退出”(opt-out),那么可以设置management.endpoints.enabled-by-default属性为false,所有的端点就会默认关闭。

端点公开方式

端点可能包含敏感信息,应谨慎选择端点公开(Exposure)方式。

ID

JMX

Web

beans



mappings



scheduledtasks



sessions



shutdown



logfile

N/A


可修改配置修改端点的公开方式。

属性

默认值

management.endpoints.jmx.exposure.exclude

management.endpoints.jmx.exposure.include

*

management.endpoints.web.exposure.exclude

management.endpoints.web.exposure.include

info,health

加固HTTP端点访问

对于包含敏感信息的端点,可以通过Spring Security实现授权访问。

自定义端点

添加一个@Bean并且附上@Endpoint注解,bean里任何方法,只要带@ReadOperation@WriteOperation@DeleteOperation注解的都会自动地通过JMX公开,或者通过HTTP公开(前提它是Web应用)。还可以通过@JmxEndpoint@WebEndpoint直接指明公开方式。如果是特定Web框架下的应用,还可以直接实现Servlet或Spring @Controller@RestController,这种做法的代价就是不能通过JMX公开以及在别的Web框架下无法使用。


  1. Spring Boot Actuator Endpoint ↩︎