我们知道Spring Boot 提供了Actuator组件,方便我们对应用程序进行监控和维护。接下来,就来介绍Actuator到底是什么?如何在Spring Boot项目中快速集成Actuator?

一、Actuator简介

1.Actuator是什么?

Actuator是Spring Boot提供的应用系统监控的开源框架,它是Spring Boot体系中非常重要的组件。它可以轻松实现应用程序的监控治理。支持通过众多 REST接口、远程Shell和JMX收集应用的运行情况。

2.端点(Endpoint)

Actuator的核心是端点(Endpoint),它用来监视、提供应用程序的信息,Spring Boot提供的spring-boot-actuator组件中已经内置了非常多的 Endpoint(health、info、beans、metrics、httptrace、shutdown等),每个端点都可以启用和禁用。Actuator也允许我们扩展自己的端点。通过JMX或HTTP的形式暴露自定义端点。

Actuator会将自定义端点的ID默认映射到一个带/actuator前缀的URL。比如,health端点默认映射到/actuator/health。这样就可以通过HTTP的形式获取自定义端点的数据。

Actuator同时还可以与外部应用监控系统整合,比如 Prometheus, Graphite, DataDog, Influx, Wavefront, New Relic等。这些系统提供了非常好的仪表盘、图标、分析和告警等功能,使得你可以通过统一的接口轻松的监控和管理你的应用系统。这对于实施微服务的中小团队来说,无疑快速高效的解决方案。

二、Spring Boot集成Actuator

在Spring Boot项目中集成Actuator非常简单,只需要在项目中添加spring-boot-starter-actuator组件,就能自动启动应用监控的功能。

首先,创建一个Spring Boot项目来添加spring-boot-starter-actuator依赖:

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

上面的示例所示,我们添加了actuator和web两个组件。spring-boot-starter-actuator除了可以监控Web系统外,还可以监控后台服务等Spring Boot应用。

然后,修改配置文件,配置Actuator端点

# 打开所有的监控点
management.endpoints.web.exposure.include=*
management.endpoint.health.show-details=always

最后,启动项目并在浏览器中输入http://localhost:8080/actuator,我们可以看到返回的是Actuator提供的各种数据接口信息。

Actuator提供了丰富的数据接口,包括/health、/env、/metrics等。下面我们请求其中的一个地址/actuator/health,查看接口返回的详细信息。

如图上图所示,/health接口返回了系统详细的健康状态信息,包括系统的状态(UP为正常)、磁盘使用情况等信息。

三、自定义端点

Spring Boot支持自定义端点,只需要在我们定义的类中使用@Endpoint、@JmxEndpoint、@WebEndpoint等注解,实现对应的方法即可定义一个Actuator中的自定义端点。从Spring Boot 2.x版本开始,Actuator支持CRUD(增删改查)模型,而不是旧的RW(读/写)模型。我们可以按照3种策略来自定义:

  • 使用@Endpoint注解,同时支持JMX和HTTP方式。
  • 使用@JmxEndpoint 注解,只支持JMX技术。
  • 使用@WebEndpoint注解,只支持HTTP。

编写自定义端点类很简单,首先需要在类前面使用@Endpoint注解,然后在类的方法上使用@ReadOperation、@WriteOperation或@DeleteOperation(分别对应HTTP中的GET、POST、DELETE)等注解获取、设置端点信息。

下面我们创建一个获取系统当前时间的自定义端点。

首先,创建自定义端点类SystemTimeEndpoint,使用@Endpoint注解声明端点ID,同时需要使用@Component注解,将此类交给Spring Boot管理。示例代码如下:

/*
 * 自定义端点类
 * @Endpoint //表示这是一个自定义事件端点类
 * Endpoint 中有一个id //它是设置端点的URL路径
 * */
@Endpoint(id="systemtime") //端点路径不要与系统自带的重合
@Component
public class SystemTimeEndpoint {
    //一般端点都是对象,或者一个json返回的格式,所以通常我们会将端点定义一个MAP的返回形式
    //通过ReadOperation
    //访问地址是根据前缀+ endpoint 的ID
    ///actuator/systemtime
    private String format = "yyyy-MM-dd HH:mm:ss";
    @ReadOperation //显示监控指标
    public Map<String,Object> info(){
        Map<String,Object> info  = new HashMap<>();
        info.put("system","数据管理服务");
        info.put("memo","系统当前时间端点");
        info.put("datetime",new SimpleDateFormat(format).format(new Date()));
        return info;
    }
    //动态修改指标
    @WriteOperation //动态修改指标,是以post方式修改
    public void setFormat(String format){
        this.format = format;
    }
}

上面的示例中,我们通过@Endpoint注解定义一个自定义端点,参数id为自定义端点的唯一标识和访问路径,必须唯一不重复。

做好这些配置后,就能访问http://127.0.0.1:8080/actuator/systemtime端点了,如图下图所示。

最后

以上,Actuator到底是什么,如何在Spring Boot项目中快速集成Actuator介绍完了。Actuator是Spring Boot 提供的非常重要的应用监控组件,希望大家能熟悉掌握。

原文链接:https://developer.51cto.com/article/700961.html