6.SpringBoot-Actuator-健康指标Health

基础配置

您可以使用运行状况信息来检查正在运行的应用程序的状态。
他经常用于监控软件系统,使其可以在系统出现问题时通知一些人。
运行状况端点公开的信息取决于management.endpoint.health.show-details属性
该属性可以使用以下值之一进行配置:

NameDescription
never永远不现实细节.
when-authorized详细信息仅向授权用户显示.可以使用management.endpoint.health.roles配置授权角色.
always向所有用户显示详细信息.

指标介绍

在合适的时候,Spring Boot会自动配置以下健康指标:

NameDescription
CassandraHealthIndicatorChecks that a Cassandra database is up.
CouchbaseHealthIndicatorChecks that a Couchbase cluster is up.
DiskSpaceHealthIndicatorChecks for low disk space.
DataSourceHealthIndicatorChecks that a connection to DataSource can be obtained.
ElasticsearchHealthIndicatorChecks that an Elasticsearch cluster is up.
InfluxDbHealthIndicatorChecks that an InfluxDB server is up.
JmsHealthIndicatorChecks that a JMS broker is up.
MailHealthIndicatorChecks that a mail server is up.
MongoHealthIndicatorChecks that a Mongo database is up.
Neo4jHealthIndicatorChecks that a Neo4j server is up.
RabbitHealthIndicatorChecks that a Rabbit server is up.
RedisHealthIndicatorChecks that a Redis server is up.
SolrHealthIndicatorChecks that a Solr server is up.

自定义自己的指标

简单实现

    @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();
        }
        public int check(){
            return 2;
        }
    }

只要定义一个组件并实现HealthIndicator接口即可
现在访问health接口你就会发现多了一个指标

{
    "status": "DOWN",
    "details": {
        "my": {
            "status": "DOWN",
            "details": {
                "Error Code": 2
            }
        },
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 500106784768,
                "free": 385645244416,
                "threshold": 10485760
            }
        }
    }
}

返回自定义内容

我们观察返回内容会返现需要返回一个status
该状态有以下几种,每种都对应一个http状态码

status对应http状态码描述
DOWN503表示组件或子系统出现意外故障。
OUT_OF_SERVICE503表示组件或子系统已停止服务且不应使用
UNKNOWN200表示组件或子系统处于未知状态
UP200表示组件或子系统正在按预期运行

返回值必须是一个Health,Health本身也是一个构建器

Health.status(Status.UNKNOWN).withDetail("Error Code", errorCode).build()

通过status方法来设置状态,通过withDetail来设置返回内容详情,最后通过build方法来构建一个Health对象

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值