官方文档: 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 | Checks that a Cassandra database is up. | |
couchbase | Checks that a Couchbase cluster is up. | |
db | Checks that a connection to DataSource can be obtained. | |
diskspace | Checks for low disk space. | |
elasticsearch | Checks that an Elasticsearch cluster is up. | |
hazelcast | Checks that a Hazelcast server is up. | |
influxdb | Checks that an InfluxDB server is up. | |
jms | Checks that a JMS broker is up. | |
ldap | Checks that an LDAP server is up. | |
| Checks that a mail server is up. | |
mongo | Checks that a Mongo database is up. | |
neo4j | Checks that a Neo4j database is up. | |
ping | Always responds with UP. | |
rabbit | Checks that a Rabbit server is up. | |
redis | Checks that a Redis server is up. | |
solr | 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" |