






















@
SpringBoot官方提供了spring-boot-starter-actuator场景启动器用于系统的监控管理,可以通过HTTP,JMX,SSH协议来进行操作,自动得到审计、健康及指标信息等
环境准备:
创建一个SpringBoot Initialize项目,详情可以参考我之前博客:SpringBoot系列之快速创建项目教程

要将执行器添加到基于Maven的项目中,请检查添加以下“ Starter”依赖项:
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies>
项目启动成功后,如果没设置context-path,项目会自动加入/actuator作为前缀,大部分端点是默认启动的,不过要通过web浏览器方式访问的只有health、info端点
可以通过配置修改默认前缀
management.endpoints.web.base-path=/actuator
通用的端点(http、Jms、ssh方式都能访问):
| ID | 描述 | 默认启用 |
|---|---|---|
GET方式调用health端点,返回json信息

Web 应用程序(Spring MVC、Spring WebFlux 或 Jersey),则可以使用以下附加端点,这个应该是2.x版本才加上的
| ID | 描述 | 默认启用 |
|---|---|---|
启用端点,修改配置,语法management.endpoint.[端点名称].enabled=true
management.endpoint.shutdown.enabled=true
下表显示了内置端点和默认暴露情况,以JMX、WEB(Http)做对比:
| ID | JMX | Web |
|---|---|---|
要更改暴露的端点,请使用以下特定的 include 和 exclude 属性:
| 属性 | 默认 |
|---|---|
include 属性列出了暴露的端点的 ID。exclude 属性列出了不应暴露的端点的 ID。exclude 属性优先于 include 属性。
例子:
关闭jmx访问所有端点的权限,只让其能访问health、info
management.endpoints.jmx.exposure.include=health,info
启用web访问所有端点,除env之外的权限
management.endpoints.web.exposure.include=*
management.endpoints.web.exposure.exclude=env
注意
* 在 YAML 中具有特殊含义,因此如果要包含(或排除)所有端点,请务必添加引号,如下所示:
management:
endpoints:
web:
exposure:
include: "*"
自定义InfoContributor
package com.example.springboot.actuator.actuate.health;
import java.util.Collections;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
@Component
public class ExampleInfoContributor implements InfoContributor {
@Override
public void contribute(Info.Builder builder) {
builder.withDetail("example",
Collections.singletonMap("key", "value"));
}
}
可以在浏览器或者postman调用:

跨域支持配置
management.endpoints.web.cors.allowed-origins=http://localhost
management.endpoints.web.cors.allowed-methods=GET,POST
定置端点:
management.endpoint.info.enabled=true
management.endpoint.info.cache.time-to-live=10s
ok,actuator的知识点比较多,详情请参考官方文档,本博客参考官方文档,做了简单记录,仅仅作为入门参考手册
代码例子下载:code download
此内容由惯性聚合(RSS阅读器)自动聚合整理,仅供阅读参考。 原文来自 — 版权归原作者所有。