Spring Boot Actuator分析,自定义端点
2018-02-07 16:36
423 查看
每个Actuator端点都是有一个特定的ID用来决定端点的路径。/beans端点的默认ID就是 beans。端点的路径是由ID决定的,那么可以通过修改ID来改变端点的路径。要做的就是设置一个属性, 属性名是 endpoints.endpoint-id.id
修改端点的ID:
如把/beans改为/beansome:
endpoints.beans.id=beansome
这时要是想查看bean的信息时,路径就由原来的/beans变为/beansome;
开启和禁用端点:
默认情况下,所有端点(除了/shutdown)都是启用的。 禁用端点所有的端点: endpoints.enabled=false
禁用某个特定的端点:
endpoints.endpoint-id.enabled=false
禁用后,再次访问该端点的URL时,会出现404错误。
默认端点信息:
自定义端点
首先,我们需要继承 AbstractEndpoint 抽象类。因为它是 Endpoint 接口的抽象实现,此外,我们还需要重写 invoke 方法。
上面就是自定义的端点,简单解释下:构造方法PersonEndpoint(),两个参数分别表示端点 ID 和是否端点默认是敏感的。我这边设置端点 ID 是 servertime,它默认不是敏感的。
我们需要通过重写 invoke 方法,返回我们要监控的内容。这里我定义了一个 Map,它将返回两个参数,一个是标准的包含时区的当前时间格式,一个是当前时间的时间戳格式
创建配置类,实例化bean
启动Spring Boot工程在浏览器中访问http://localhost:8080/person
修改端点的ID:
如把/beans改为/beansome:
endpoints.beans.id=beansome
这时要是想查看bean的信息时,路径就由原来的/beans变为/beansome;
开启和禁用端点:
默认情况下,所有端点(除了/shutdown)都是启用的。 禁用端点所有的端点: endpoints.enabled=false
禁用某个特定的端点:
endpoints.endpoint-id.enabled=false
禁用后,再次访问该端点的URL时,会出现404错误。
默认端点信息:
HTTP方法 | 路径 | 描述 | 鉴权 |
GET | /autoconfig | 查看自动配置的使用情况 | true |
GET | /configprops | 查看配置属性,包括默认配置 | true |
GET | /beans | 查看bean及其关系列表 | true |
GET | /dump | 打印线程栈 | true |
GET | /env | 查看所有环境变量 | true |
GET | /env/{name} | 查看具体变量值 | true |
GET | /health | 查看应用健康指标 | false |
GET | /info | 查看应用信息 | false |
GET | /mappings | 查看所有url映射 | true |
GET | /metrics | 查看应用基本指标 | true |
GET | /metrics/{name} | 查看具体指标 | true |
POST | /shutdown | 关闭应用 | true |
GET | /trace | 查看基本追踪信息 | true |
首先,我们需要继承 AbstractEndpoint 抽象类。因为它是 Endpoint 接口的抽象实现,此外,我们还需要重写 invoke 方法。
@ConfigurationProperties(prefix = "endpoints.person") public class PersonEndpoint extends AbstractEndpoint<Map<String, Object>> { public PersonEndpoint() { super("person", false); } @Override public Map<String, Object> invoke() { Map<String, Object> result = new HashMap<String, Object>(); DateTime dateTime = DateTime.now(); result.put("当前时间", dateTime.toString()); result.put("当前时间戳", dateTime.getMillis()); return result; } }通过设置 @ConfigurationProperties(prefix = "endpoints.person"),我们就可以在 application.properties 中通过 endpoints.person 配置我们的端点。
上面就是自定义的端点,简单解释下:构造方法PersonEndpoint(),两个参数分别表示端点 ID 和是否端点默认是敏感的。我这边设置端点 ID 是 servertime,它默认不是敏感的。
我们需要通过重写 invoke 方法,返回我们要监控的内容。这里我定义了一个 Map,它将返回两个参数,一个是标准的包含时区的当前时间格式,一个是当前时间的时间戳格式
创建配置类,实例化bean
@Configuration public class EndpointConfig { @Bean public static Endpoint<Map<String, Object>> servertime() { return new PersonEndpoint(); } }
启动Spring Boot工程在浏览器中访问http://localhost:8080/person
相关文章推荐
- Spring Boot Actuator分析,自定义端点
- Spring Boot Actuator分析,自定义端点
- Spring Boot Actuator分析,自定义端点
- Spring Boot Actuator分析,自定义端点
- Spring Boot Actuator分析,自定义端点
- Spring Boot Actuator分析,自定义端点
- Spring Boot Actuator分析,自定义端点
- Spring Boot Actuator分析,自定义端点
- Spring Boot Actuator分析,自定义端点
- Spring Boot Actuator分析,自定义端点
- Spring Boot Actuator分析,自定义端点
- Spring Boot Actuator分析,自定义端点
- Spring Boot 2.0 执行器端点(Actuator Endpoint)精简模式
- Spring Boot Actuator监控端点小结
- Spring Boot 2.0新增的Actuator端点的特性
- Spring Boot中使用Actuator的/info端点输出Git版本信息
- Spring Boot 2.0新增的Actuator端点的特性
- 对actuator的管理端点进行ip白名单限制(springBoot添加filter)
- Spring Boot actuator访问不了端点
- Spring Boot教程(十二)Spring Boot Actuator监控端点小结(2)