Spring学习干货,Spring Boot 2.x 中的 Actuator你知道吗?

前言

Spring Boot 提供了很多开箱即用的starter,其中有一款非常特别的starter——actuator 。它是用来对Spring Boot 应用进行监控、指标采集、管理,并提供一些很有用的端点(endpoint)来实现上述功能。这有助于我们对Spring Boot 应用进行监视和管理。我们本文将探讨Spring Boot 2.x下的actuator的一些知识点。

依赖引入

即像其它starter一样,通过非常简单的依赖集成即可开箱即用。我们通过在项目中引入(以maven为例):

<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> 

端点

Actuator的核心是端点(endpoint),我们通过端点来获取应用的一些监控信息或者通过端点来改变系统的一些状态。Actuator中内置了非常多的端点,详情可查看 Endpoints 列表 。

端点启用关闭

默认情况下除了`shutdown`是关闭的其它端点都是启用的。个别端点的启用依赖其他组件,比如我们上篇文章介绍的Prometheus。我们可以在Spring Boot 配置文件中通过参数(id参见3 提供入口中的端点列表)management.endpoint.<id>.enabled来配置端点是否启用。

另外我们可以通过`management.endpoints.enabled-by-default`来修改全局端口默认配置,以下示例启用info端点并禁用所有其他端点:

# 先设置所有的端点默认为关闭 management.endpoints.enabled-by-default=false # 然后开启info 端点 management.endpoint.info.enabled=true 

端点开放封闭

特别需要我们注意的是,以上只是我们开启的端点功能,并不意味我们把这些端点暴露给应用之外的环境中去。暴露的配置规则是:

management.endpoints.<web|jmx>.exposure.<include|exclude> 

我们以web为例,通过以下方式来暴露web端点:

# * 为暴露所有web端点,如果暴露的是一个列表请使用端点 id 并以逗号隔开  management.endpoints.web.exposure.include='*' 

同样的如果我们要排除一些web端点请使用配置

management.endpoints.web.exposure.exclude 

我们可以通过`/actuator`来列举暴露的端点。当然这个路径取决于我们的配置:

management.endpoints.web.basePath 

端点的安全性

对于生产环境来说端点都是敏感的。我们当然不希望非法的访问端点,特别如`/shutdown`这种端点。我们可以通过上面3.1、3.2进行关闭、封闭操作。也可直接配置management.server.port=-1 来关闭管理端点。或者确保在外面的安全框架诸如Spring security的控制之下。即保证 EndpointRequest.toAnyEndpoint()的安全性。

自定义端点

Spring Boot 2.x 开始,Actuator支持CRUD模型,而不是旧的RW(读/写)模型。我们可以按照两种策略来自定义:

  • @Endpoint 同时支持JMX和http
  • @JmxEndpoint 只支持JMX技术
  • @WebEndpoint 只支持http

通过在一个端点类(必须是Spring Bean)上添加上面其中一个来表明该类是一个端点类。

在类的方法使用@ReadOperation,@WriteOperation或@DeleteOperation,这分别会映射到Http中的 GET、POST、DELETE(对http来说)。 以下是我们自定义的一个端点:

@Component @Endpoint(id = "features") public class FeaturesEndpoint { private Map<String, Feature> features = new ConcurrentHashMap<>(); @ReadOperation public Map<String, Feature> features() { return features; } @ReadOperation public Feature feature(@Selector String name) { return features.get(name); } @WriteOperation public void configureFeature(@Selector String name, Feature feature) { features.put(name, feature); } @DeleteOperation public void deleteFeature(@Selector String name) { features.remove(name); } public static class Feature { private Boolean enabled; // [...] getters and setters } } 

注意请务必保证端点的id在该应用中唯一。

端点扩展

假设我们想要确保我们的应用程序的生产实例永远不是SNAPSHOT版本。我们决定通过更改返回此信息的Actuator端点(/info)来完成此操作。如果我们的应用程序恰好是SNAPSHOT。我们将获得不同的HTTP状态代码。

我们可以使用`@EndpointExtension`或其更具体的`@EndpointWebExtension`、`@EndpointJmxExtension`轻松扩展预定义端点的行为:

@Component @EndpointWebExtension(endpoint = InfoEndpoint.class) public class InfoWebEndpointExtension { private InfoEndpoint delegate; // standard constructor @ReadOperation public WebEndpointResponse<Map> info() { Map<String, Object> info = this.delegate.info(); Integer status = getStatus(info); return new WebEndpointResponse<>(info, status); } private Integer getStatus(Map<String, Object> info) { // return 5xx if this is a snapshot return 200; } } 

metrics

Spring Boot运维应用监控依赖于对应用度量(metric)指标的抽取。Spring Boot 2.x中的Actuator 对 Micrometer 的自动配置。甚至我们可以通过一个叫`MeterRegistry`的Spring Bean来注册一个自定义的metric指标。我们可以通过`/actuator/metrics`端点获取所有的metric指标。也可以通过`/actuator/metrics/{metricName}` 来获取具体度量的元数据。

Micrometer 是一个应用度量门面类库,类似SLF4。后面我会专门写一篇文章来介绍这个知识点。请关注我的微信公众号:Felordcn 以保证第一时间获取相关的知识。

IDE的支持

如果你使用的IDE是Jetbrains Intellij Idea 旗舰版,添加Actuator依赖启动后console会有以下的面板提示方便我们直接查看端点:

总结

本文我们介绍了Spring Boot 2.x中Actuator 组件。该组件可以帮助我们来获取系统的一些元信息和一些监控度量指标。对于Spring Boot 应用十分重要。该组件是一个生产级别的工具,我们不应该忽略它。

发布了61 篇原创文章 · 获赞 17 · 访问量 1394

猜你喜欢

转载自blog.csdn.net/cxytony/article/details/103734983