Spring Boot Actuator:自定义端点,其顶部具有MVC层
Spring Boot Actuator端点允许您监视应用程序并与之交互。 Spring Boot包含许多内置端点,您也可以添加自己的端点。
添加自定义端点就像创建一个从org.springframework.boot.actuate.endpoint.AbstractEndpoint
扩展的类一样容易。 但是Spring Boot Actuator也提供了用MVC层装饰端点的可能性。
端点端点
有许多内置端点,但是缺少一个端点是公开所有端点的端点。 默认情况下,终结点通过HTTP公开,其中终结点的ID映射到URL。 在下面的示例中,创建了具有ID endpoints
的新端点,并且其invoke
方法返回所有可用端点:
@Component
public class EndpointsEndpoint extends AbstractEndpoint<List<Endpoint>> {private List<Endpoint> endpoints;@Autowiredpublic EndpointsEndpoint(List<Endpoint> endpoints) {super("endpoints");this.endpoints = endpoints;}@Overridepublic List<Endpoint> invoke() {return endpoints;}
}
@Component
注释将端点添加到现有端点列表中。 /endpoints
URL现在将公开所有具有id
, enabled
和sensitive
属性的端点:
[{"id": "trace","sensitive": true,"enabled": true},{"id": "configprops","sensitive": true,"enabled": true}
]
新端点还将作为MBean在JMX服务器上注册: [org.springframework.boot:type=Endpoint,name=endpointsEndpoint]
MVC端点
Spring Boot Actuator提供了一项附加功能,该功能是通过org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint
接口在终结点之上的MVC层的策略 。 MvcEndpoint
可以使用@RequestMapping
和其他Spring MVC功能。
请注意, EndpointsEndpoint
返回所有可用的端点。 但是,如果用户可以通过其enabled
sensitive
属性过滤端点,那就太好了。
MvcEndpoint
必须使用有效的@RequestMapping
方法创建一个新的MvcEndpoint
。 请注意,不允许在类级别使用@Controller
和@RequestMapping
,因此使用@Component
来使端点可用:
@Component
public class EndpointsMvcEndpoint extends EndpointMvcAdapter {private final EndpointsEndpoint delegate;@Autowiredpublic EndpointsMvcEndpoint(EndpointsEndpoint delegate) {super(delegate);this.delegate = delegate;}@RequestMapping(value = "/filter", method = RequestMethod.GET)@ResponseBodypublic Set<Endpoint> filter(@RequestParam(required = false) Boolean enabled,@RequestParam(required = false) Boolean sensitive) {}
}
新方法将在/endpoints/filter
URL下可用。 该方法的实现很简单:它获取可选的enabled
sensitive
参数,并过滤委托的invoke
方法结果:
@RequestMapping(value = "/filter", method = RequestMethod.GET)
@ResponseBody
public Set<Endpoint> filter(@RequestParam(required = false) Boolean enabled,@RequestParam(required = false) Boolean sensitive) { Predicate<Endpoint> isEnabled =endpoint -> matches(endpoint::isEnabled, ofNullable(enabled));Predicate<Endpoint> isSensitive =endpoint -> matches(endpoint::isSensitive, ofNullable(sensitive));return this.delegate.invoke().stream().filter(isEnabled.and(isSensitive)).collect(toSet());
}private <T> boolean matches(Supplier<T> supplier, Optional<T> value) {return !value.isPresent() || supplier.get().equals(value.get());
}
用法示例:
- 所有启用的端点:
/endpoints/filter?enabled=true
- 所有敏感端点:
/endpoints/filter?sensitive=true
- 所有已启用和敏感的端点:
/endpoints/filter?enabled=true&sensitive=true
使端点可发现
EndpointsMvcEndpoint
使用MVC功能,但仍返回纯终结点对象。 如果Spring HATEOAS在类路径中,则可以扩展filter
方法以返回org.springframework.hateoas.Resource
以及指向端点的链接:
class EndpointResource extends ResourceSupport {private final String managementContextPath;private final Endpoint endpoint;EndpointResource(String managementContextPath, Endpoint endpoint) {this.managementContextPath = managementContextPath;this.endpoint = endpoint;if (endpoint.isEnabled()) {UriComponentsBuilder path = fromCurrentServletMapping().path(this.managementContextPath).pathSegment(endpoint.getId());this.add(new Link(path.build().toUriString(), endpoint.getId())); }}public Endpoint getEndpoint() {return endpoint;}
}
EndpointResource
将包含指向每个已启用端点的链接。 请注意,构造函数采用managamentContextPath
变量。 该变量包含一个Spring Boot Actuator management.contextPath
属性值。 用于设置管理端点的前缀。
EndpointsMvcEndpoint
类中所需的更改:
@Component
public class EndpointsMvcEndpoint extends EndpointMvcAdapter {@Value("${management.context-path:/}") // default to '/'private String managementContextPath;@RequestMapping(value = "/filter", method = RequestMethod.GET)@ResponseBodypublic Set<Endpoint> filter(@RequestParam(required = false) Boolean enabled,@RequestParam(required = false) Boolean sensitive) {// predicates declarationsreturn this.delegate.invoke().stream().filter(isEnabled.and(isSensitive)).map(e -> new EndpointResource(managementContextPath, e)).collect(toSet());}
}
我的Chrome浏览器中安装了JSON Formatter的结果:
但是,为什么不直接从EndpointsEnpoint
返回资源呢? 在EndpointResource
了从HttpServletRequest
中提取信息的UriComponentsBuilder
,它将在调用MBean的getData
操作时引发异常(除非不需要JMX)。
管理端点状态
端点不仅可以用于监视,还可以用于管理。 已经有内置的ShutdownEndpoint
(默认情况下禁用),可以关闭ApplicationContext
。 在以下(假设的)示例中,用户可以更改所选端点的状态:
@RequestMapping(value = "/{endpointId}/state")
@ResponseBody
public EndpointResource enable(@PathVariable String endpointId) {Optional<Endpoint> endpointOptional = this.delegate.invoke().stream().filter(e -> e.getId().equals(endpointId)).findFirst();if (!endpointOptional.isPresent()) {throw new RuntimeException("Endpoint not found: " + endpointId);}Endpoint endpoint = endpointOptional.get(); ((AbstractEndpoint) endpoint).setEnabled(!endpoint.isEnabled());return new EndpointResource(managementContextPath, endpoint);
}
呼叫disabled
端点用户时,应收到以下响应:
{"message": "This endpoint is disabled"
}
更进一步
下一步可能是为自定义(或现有)端点添加用户界面,但这不在本文讨论范围之内。 如果您有兴趣,可以看看Spring Boot Admin ,它是Spring Boot应用程序的简单管理界面。
摘要
Spring Boot Actuator 提供了Spring Boot的所有生产就绪功能以及许多内置端点。 只需花费很少的精力,即可添加自定义端点,以扩展应用程序的监视和管理功能。
参考文献
- http://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#production-ready
翻译自: https://www.javacodegeeks.com/2014/10/spring-boot-actuator-custom-endpoint-with-mvc-layer-on-top-of-it.html
Spring Boot Actuator:自定义端点,其顶部具有MVC层相关推荐
- Spring Boot Actuator:在其顶部具有MVC层的自定义端点
Spring Boot Actuator端点允许您监视应用程序并与之交互. Spring Boot包含许多内置端点,您也可以添加自己的端点. 添加自定义端点就像创建一个从org.springframe ...
- 如何将 Spring Boot Actuator 的指标信息输出到 InfluxDB 和 Prometheus
来源:SpringForAll社区 Spring Boot Actuator是Spring Boot 2发布后修改最多的项目之一.它经过了主要的改进,旨在简化定制,并包括一些新功能,如支持其他Web技 ...
- SpringBoot2.x系列教程(七十)Spring Boot Actuator集成及自定义Endpoint详解
前言 曾经看到Spring Boot Actuator这个框架时,一直在想,它到底有什么作用呢?虽然知道它提供了很多端点,有助于应用程序的监控和管理,但如果没有直接的实践案例,还是很难有说服力的. 直 ...
- Spring Boot Actuator 端点启用和暴露
Spring Boot Actuator 端点启用和暴露 # 从技术上更改端点的暴露 -- 通过HTTP公开所有的端点,可通过 /actuator/{ID} 去查看,如 /actuator/beans ...
- spring boot Actuator之自定义Endpoint
本文基于spring boot 2.2.0 release版本. 在上一篇文章<spring boot Actuator原理详解之启动>详细介绍了在web环境下,Actuator是如何启动 ...
- 警惕 Spring Boot Actuator 引发的安全问题
前言 一年一度的 HW 行动开始了,最近也是被各种安全漏洞搞的特别闹心,一周能收到几十封安全团队扫描出来的漏洞邮件,这其中有一类漏洞很容易被人忽视,但影响面却极广,危害也极大,我说出它的名字你应该也不 ...
- 朱晔和你聊Spring系列S1E7:简单好用的Spring Boot Actuator
本文会来看一下Spring Boot Actuator提供给我们的监控端点Endpoint.健康检查Health和打点指标Metrics等所谓的Production-ready(生产环境必要的一些)功 ...
- actuator的原理_使用Spring Boot Actuator监视Java应用程序
actuator的原理 朋友不允许朋友写用户身份验证. 厌倦了管理自己的用户? 立即尝试Okta的API和Java SDK. 数分钟之内即可在任何应用程序中对用户进行身份验证,管理和保护. 您是否曾与 ...
- 使用Spring Boot Actuator、Jolokia和Grafana实现准实时监控--转
原文地址:http://mp.weixin.qq.com/s?__biz=MzAxODcyNjEzNQ==&mid=2247483789&idx=1&sn=ae11f04780 ...
最新文章
- 怎么样给下拉框加载背景色
- php看什么教程,PHP初学者适合看什么
- 【揭秘】12306是如何抗住几亿日活、百万级高并发的?
- html冷门标签,html 冷门
- Oracle 10中修改字符集(character set)
- php开发领域,PHP-MySQL相关领域
- APNS提供了两项基本的服务:消息推送和反馈服务
- fortran安装_如何在 CentOS 8 上安装 GCC
- MySQL主从失败:slave_IO_Running为No
- Ping记录时间的方法
- 花书+吴恩达深度学习(二六)近似推断(EM, 变分推断)
- 转载 网络上的8051 free IP core资源
- ubuntu需要多大的固态硬盘_Ubuntu16.10 迁移到 SSD
- Vue引入百度地图增加导航功能
- Costech A17M23SWB MTo
- 面试题汇总-大牛的Java170
- 阿里云进入Iot Studio
- 硬件工程师的你也不想一辈子画图、调板子吧!!!
- 专访 | 刘嘉松:开源,互惠且共赢
- 为什么电脑计算机里没有桌面,为什么电脑开机后桌面上什么都没有?
热门文章
- 多线程的线程通信(生产消费)
- StringBuilder的使用
- vue的基本项目结构
- php 错误提示开启,php开启与关闭错误提示,php开启错误提示_PHP教程
- 二级MYSQL的语法整理_MySQL语法整理
- camel apache_短款Apache Camel K
- java 枚举内嵌枚举_高度有用的Java ChronoUnit枚举
- 运动基元_开发人员的新分布式基元
- java oauth2.0_教程:如何实现Java OAuth 2.0以使用GitHub和Google登录
- hazelcast入门教程_Hazelcast入门指南第7部分