ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Spring boot——Actuator 健康检查

2021-09-24 11:35:17  阅读:345  来源: 互联网

标签:状态 management Spring boot up health Actuator Checks


 

一、使用方法

在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-authorized、never,默认值是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"
            }
        }
    }
}
从上面的应用的详细健康信息发现,健康信息包含磁盘空间、redis、DB,启用监控的这个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_SERVICEDOWN 对应 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

标签:状态,management,Spring,boot,up,health,Actuator,Checks
来源: https://www.cnblogs.com/caoweixiong/p/15325382.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有