《Spring Boot开发:从0到1》第11章 Spring Boot应用监控第11章 Spring Boot Actuator与应用监控

时间:2022-06-07
本文章向大家介绍《Spring Boot开发:从0到1》第11章 Spring Boot应用监控第11章 Spring Boot Actuator与应用监控,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

第11章 Spring Boot Actuator与应用监控

Spring Boot的Actuator 将应用的很多信息暴露出来,如容器中的 bean,自动配置时的决策,运行时健康状态, metrics等等。Actuator提供了三中方式获取这些信息:

HTTP Endpoints Remote Shell JMX (MBeans) 当然通过继承指定的类来自定义一些 actuator信息,暴露自己想要的信息。

11.1 使用Spring Boot Actuator监控应用

1.Spring boot Actuator Endpoints介绍

Actuator是Spring Boot提供的附加特性,来帮我们监控和管理生产环境下运行时的应用程序。我们可以通过HTTP endpoints、JMX或者SSH来监控和管理应用的健康状况、系统指标、参数信息、内存状况等等。

Spring Boot Actuator所提供的HTTP监控服务如下表:

上面的这些HTTP服务,我们就叫Endpoint。Endpoint允许对应用进行上述健康状况、系统指标、参数信息、内存状况等指标的监控和交互。Spring Boot提供了很多内置的Endpoint,同时支持定制Endpoint。

Endpoint被暴露的方式取决于采用的技术(HTTP、JMX、SSH等),大部分应用采用HTTP的方式, 暴露的方式即通过Endpoint的ID映射成一个URL,例如 id=health 的内置Endpoint映射到URL=/health, 提供应用基础健康检查信息, 为了安全起见,一般不暴露在应用服务端口上,而是暴露在专门的管理端口上。

其中,重点挑两个讲一下。

/health 提供应用程序的健康状态,或者是某个核心模块的健康状态。例如, 数据库连接,磁盘使用情况等指标。

/metrics,这个endpoint显示Metrics 子系统管理的信息。主要是一些度量的值,比如系统的吞吐量,堆栈信息,耗时(timer),接口被触发的次数(meter,count),对象大小(gauge)等。metrics的监控主要分以下几种类型:

2.开启Actuator 很简单,只需要引入官方提供的starter:

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

3.定制Endpoint

Endpoint可以通过application.properties配置文件中的配置项进行定制, 格式如下:

endpoints.[name].[property]

有三个通用的property:

id: id enable: 开关 sensitive: 是否需要权限控制才可以看到 以health为例,/health暴露的监控信息是所有实现了HealthIndicator接口的Bean。

通过自定义HealthIndicator实现定制health endpoint

@Component
public class HealthCheck implements HealthIndicator {
    @Override
    public Health health() {
        int errorCode = check(); // perform some specific health check
        if (errorCode != 0) {
            return Health.down().withDetail("Error Code", errorCode).build();
        }
        return Health.up().build();
    }

    public int check() {
        // Your logic to check health
        return 0;
    }
}

输出类似如下格式:

{
    "status" : "DOWN",
    "myHealthCheck" : {
        "status" : "DOWN",
        "Error Code" : 1,
        "Description" : "You custom MyHealthCheck endpoint is down"
     },
     "diskSpace" : {
         "status" : "UP",
         "free" : 209047318528,
         "threshold" : 10485760
     }
}

4.创建新的Endpoint

一般通过继承AbstractEndpoint<T>抽象类创建一个新的Endpoint, 或者直接实现Endpoint<T>接口,AbstractEndpoint<T>抽象类也实现了Endpoint<T>接口。

@Component
public class CustomEndpoint implements Endpoint<List<String>> {

    public String getId() {
        return "customEndpoint";
    }

    public boolean isEnabled() {
        return true;
    }

    public boolean isSensitive() {
        return true;
    }

    public List<String> invoke() {
        // Custom logic to build the output
        List<String> messages = new ArrayList<String>();
        messages.add("This is message 1");
        messages.add("This is message 2");
        return messages;
    }
}

id=customEndpoint, 对应的URL为/customEndpoint

输出信息格式如下:

[ "This is message 1", "This is message 2" ]

11.2 Spring Boot远程Shell