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 | 整理并展示应用中所有 | 是 |
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 | 否 |
可修改配置修改端点的公开方式。
属性 | 默认值 |
| |
|
|
| |
|
|
加固HTTP端点访问
对于包含敏感信息的端点,可以通过Spring Security实现授权访问。
自定义端点
添加一个@Bean
并且附上@Endpoint
注解,bean里任何方法,只要带@ReadOperation
、@WriteOperation
或@DeleteOperation
注解的都会自动地通过JMX公开,或者通过HTTP公开(前提它是Web应用)。还可以通过@JmxEndpoint
或@WebEndpoint
直接指明公开方式。如果是特定Web框架下的应用,还可以直接实现Servlet或Spring @Controller
或@RestController
,这种做法的代价就是不能通过JMX公开以及在别的Web框架下无法使用。