zoukankan      html  css  js  c++  java
  • Spring boot——Actuator 健康检查

    一、使用方法

    在Spring boot应用中,要实现可监控的功能,依赖的是 spring-boot-starter-actuator 这个组件。
    它提供了很多监控和管理你的spring boot应用的HTTP或者JMX端点,并且你可以有选择地开启和关闭部分功能。
    当你的spring boot应用中引入下面的依赖之后,将自动的拥有审计健康检查Metrics监控功能。
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>
    </dependency>

    具体的使用方法:

    1. 引入上述的依赖jar;
    2. 通过下面的配置启用所有的监控端点,默认情况下,这些端点是禁用的;
    management.endpoints.web.exposure.include=*
    management.endpoints.jmx.exposure.include=*

    “*”号代表启用所有的监控端点,可以单独启用,例如,healthinfometrics等。

    1. 通过actuator/+端点名就可以获取相应的信息。
    一般的监控管理端点的配置信息,如下:
    management.server.port=10111
    management.server.ssl.enabled=false
    management.server.servlet.context-path=/
    management.endpoint.health.show-details=always

    management.endpoint.health.show-details的值除了always之外还有when-authorizednever,默认值是never

    二、健康检查

    当我们开启health的健康端点时,我们能够查到应用健康信息是一个汇总的信息,访问http://127.0.0.1:10111/actuator/health时,我们获取到的信息是{"status":"UP"},status的值还有可能是 DOWN
    要想查看详细的应用健康信息需要配置management.endpoint.health.show-details 的值为always,配置之后我们再次访问http://127.0.0.1:10111/actuator/health,获取的信息如下:
    {
        "status": "UP",
        "details": {
            "diskSpace": {
                "status": "UP",
                "details": {
                    "total": 250685575168,
                    "free": 172252426240,
                    "threshold": 10485760
                }
            },
            "redis": {
                "status": "UP",
                "details": {
                    "version": "3.2.11"
                }
            },
            "db": {
                "status": "UP",
                "details": {
                    "database": "Oracle",
                    "hello": "Hello"
                }
            }
        }
    }
    从上面的应用的详细健康信息发现,健康信息包含磁盘空间redisDB,启用监控的这个spring boot应用确实是连接了redis和oracle DB,actuator就自动给监控起来了,确实是很方便、很有用。
    经过测试发现,details中所有的监控项中的任何一个健康状态是DOWN,整体应用的健康状态也是DOWN

    三、健康检查原理

    Spring boot的健康信息都是从ApplicationContext中的各种HealthIndicator Beans中收集到的,Spring boot框架中包含了大量的HealthIndicators的实现类,当然你也可以实现自己认为的健康状态。
    默认情况下,最终的spring boot应用的状态是由HealthAggregator汇总而成的,汇总的算法是:
    • 设置状态码顺序:setStatusOrder(Status.DOWN, Status.OUT_OF_SERVICE, Status.UP, Status.UNKNOWN)
    • 过滤掉不能识别的状态码。
    • 如果无任何状态码,整个spring boot应用的状态是 UNKNOWN
    • 将所有收集到的状态码按照 1 中的顺序排序。
    • 返回有序状态码序列中的第一个状态码,作为整个spring boot应用的状态。

    源代码请参见:org.springframework.boot.actuate.health.OrderedHealthAggregator

    Spring boot框架自带的 HealthIndicators 目前包括:
    NameDescription
    CassandraHealthIndicator Checks that a Cassandra database is up.
    DiskSpaceHealthIndicator Checks for low disk space.
    DataSourceHealthIndicator Checks that a connection to DataSource can be obtained.
    ElasticsearchHealthIndicator Checks that an Elasticsearch cluster is up.
    InfluxDbHealthIndicator Checks that an InfluxDB server is up.
    JmsHealthIndicator Checks that a JMS broker is up.
    MailHealthIndicator Checks that a mail server is up.
    MongoHealthIndicator Checks that a Mongo database is up.
    Neo4jHealthIndicator Checks that a Neo4j server is up.
    RabbitHealthIndicator Checks that a Rabbit server is up.
    RedisHealthIndicator Checks that a Redis server is up.
    SolrHealthIndicator Checks that a Solr server is up.
    你可以通过management.health.defaults.enabled这个配置项将它们全部禁用掉,也可以通过management.health.xxxx.enabled将其中任意一个禁用掉。
     

    四、自定义 HealthIndicator 健康检查

    有时候需要提供自定义的健康状态检查信息,你可以通过实现HealthIndicator的接口来实现,并将该实现类注册为spring bean。
    你需要实现其中的health()方法,并返回自定义的健康状态响应信息,该响应信息应该包括一个状态码和要展示详细信息。
    例如,下面就是一个接口HealthIndicator的实现类:
    import org.springframework.boot.actuate.health.Health;
    import org.springframework.boot.actuate.health.HealthIndicator;
    import org.springframework.stereotype.Component;
    
    @Component
    public class MyHealthIndicator 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();
        }
    
    }
    另外,除了Spring boot定义的几个状态类型,我们也可以自定义状态类型,用来表示一个新的系统状态。
    在这种情况下,你还需要实现接口 HealthAggregator ,或者通过配置 management.health.status.order 来继续使用HealthAggregator的默认实现。
     
    例如,在你自定义的健康检查HealthIndicator的实现类中,使用了自定义的状态类型FATAL,为了配置该状态类型的严重程度,你需要在application的配置文件中添加如下配置:
    management.health.status.order=FATAL, DOWN, OUT_OF_SERVICE, UNKNOWN, UP
    在做健康检查时,响应中的HTTP状态码反应了整体的健康状态,(例如,UP 对应 200, 而 OUT_OF_SERVICE DOWN 对应 503)。
    同样,你也需要为自定义的状态类型设置对应的HTTP状态码,例如,下面的配置可以将 FATAL 映射为 503(服务不可用):
    management.health.status.http-mapping.FATAL=503

    如果你需要更多的控制,你可以定义自己的 HealthStatusHttpMapper bean。

    下面是内置健康状态类型对应的HTTP状态码列表:

    StatusMapping
    DOWN SERVICE_UNAVAILABLE (503)
    OUT_OF_SERVICE SERVICE_UNAVAILABLE (503)
    UP No mapping by default, so http status is 200
    UNKNOWN No mapping by default, so http status is 200


    引用:
    • https://www.jianshu.com/p/1aadc4c85f51
  • 相关阅读:
    go语言入门(三)
    go语言入门(二)
    Nand Flash 基础
    哈希技术
    NorFlash基础
    二阶构造模式
    C++基础知识汇总
    Arm寄存器介绍及汇编基础
    Linux Makefile详解
    Linux链接器脚本详解
  • 原文地址:https://www.cnblogs.com/caoweixiong/p/15325382.html
Copyright © 2011-2022 走看看