Spring boot actuator Health information

官方文档: Spring Boot Reference Documentation

1.用途

我们可以通过使用health 信息来检查我们应用运行的状态.这些状态通常被用来监控软件当生产系统要宕机的时候提供一些预警. 这些信息通过  health的终端进行暴露.

2.配置

通过配置

  • management.health.defaults.enabled=true|false 禁用所有的配置
  • management.health.<id>.enabled=true   启用id的配置
  • management.endpoint.health.show-details
  • management.endpoint.health.show-components

下面是配置的值,我们只需要配置其中之一

Name

Description

never

Details are never shown.

when-authorized

Details are shown only to authorized users. Authorized roles can be configured by using management.endpoint.health.roles.

always

Details are shown to all users.

never是默认的.当一个用户在一个或者多个endpoint的角色时考虑被授权.如果endpoint没有配置角色(默认的),所有的授权用户都可以被授权.

通过management.endp

oint.health.roles  配置

3.核心类

  • HealthContributorRegistry  收集所有的health信息(默认的实例都定义在ApplicationContext)
  • HealthContributor 可以是HealthIndicator或者CompositeHealthContributor.
    • HealthIndicator  提供实际的health信息,包括 Status
    • CompositeHealthContributor 提供一个HealthContributor组合.

contributors通过一个树形结构来代表整个系统的health.

Status

Status

Mapping

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

StatusAggregator: 通过一个有序的状态列表来对每个 HealthIndicator进行排序.排序中的第一个状态用来做总的health状态.如果没有HealthIndicator 返回status 则会使用 UNKNOWN状态

下面是health的web端展示

    1

    2

    3

    4

    5

    6

    7

    8

    9

   10

   11

   12

   13

   14

   15

   16

   17

   18

   19

   20

   21

   22

   23

   24

   25

   26

   27

   28

   29

   30

   31

{

  "status" : "UP",

  "components" : {

    "coffeeIndicator" : {

      "status" : "UP",

      "details" : {

        "count" : 5,

        "message" : "We have enough coffee."

      }

    },

    "db" : {

      "status" : "UP",

      "details" : {

        "database" : "H2",

        "validationQuery" : "isValid()"

      }

    },

    "diskSpace" : {

      "status" : "UP",

      "details" : {

        "total" : 204359069696,

        "free" : 68756983808,

        "threshold" : 10485760,

        "exists" : true

      }

    },

    "ping" : {

      "status" : "UP"

    }

  }

}

自动配置的HealthIndicators

通过 management.health.key.enabled来启用 key如下

Key

Name

Description

cassandra

CassandraDriverHealthIndicator

Checks that a Cassandra database is up.

couchbase

CouchbaseHealthIndicator

Checks that a Couchbase cluster is up.

db

DataSourceHealthIndicator

Checks that a connection to DataSource can be obtained.

diskspace

DiskSpaceHealthIndicator

Checks for low disk space.

elasticsearch

ElasticsearchRestHealthIndicator

Checks that an Elasticsearch cluster is up.

hazelcast

HazelcastHealthIndicator

Checks that a Hazelcast server is up.

influxdb

InfluxDbHealthIndicator

Checks that an InfluxDB server is up.

jms

JmsHealthIndicator

Checks that a JMS broker is up.

ldap

LdapHealthIndicator

Checks that an LDAP server is up.

mail

MailHealthIndicator

Checks that a mail server is up.

mongo

MongoHealthIndicator

Checks that a Mongo database is up.

neo4j

Neo4jHealthIndicator

Checks that a Neo4j database is up.

ping

PingHealthIndicator

Always responds with UP.

rabbit

RabbitHealthIndicator

Checks that a Rabbit server is up.

redis

RedisHealthIndicator

Checks that a Redis server is up.

solr

SolrHealthIndicator

Checks that a Solr server is up.

自定义Health Indicator

定义自己的指标

  • 注册实现HealthIndicator接口的bean到Spring中
  • 实现health()方法并且返回Health.
    • Health包含了额外的需要展示的状态和明细.

    1

    2

    3

    4

    5

    6

    7

    8

    9

   10

   11

   12

   13

   14

   15

   16

   17

   18

   19

   20

   21

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();

        if (errorCode != 0) {

            return Health.down().withDetail("Error Code", errorCode).build();

        }

        return Health.up().build();

    }

    private int check() {

        // perform some specific health check

        return ...

    }

}

自定义状态

Spring提前定义了Status的类型,Health可以返回自定义的Status来代表一个新系统的状态.

  • 提供一个实现 StatusAggregator接口的bean
  • 通过 management.endpoint.health.status.order来配置顺序

    1

    2

    3

    4

    5

management:

  endpoint:

    health:

      status:

        order: "fatal,down,out-of-service,unknown,up"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FantasyBaby

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值