SpringBoot 健康检查

757 阅读2分钟

Spring Boot-Actuator 为Spring Boot 项目中提供健康检查与监控的服务。

添加依赖

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-actuator</artifactId>
  <version>2.6.6</version>
</dependency>

添加配置

management:
  endpoint:
    health:
      probes:
        enabled: true
      livenessState:
        enabled: true
      readinessState:
        enabled: true
      show-details: always
  health:
    db:
      enabled: true
    elasticsearch:
      enabled: true

请求及响应

curl -X GET http://127.0.0.1:8080/actuator/health
{
    "status": "UP",
    "components": {
        "db": {
            "status": "UP",
            "details": {
                "database": "MySQL",
                "validationQuery": "SELECT 1",
                "result": 1
            }
        },
        "diskSpace": {         // 磁盘检查
            "status": "UP",
            "details": {
                "total": 340653293568,
                "free": 126678343680,
                "threshold": 10485760,
                "exists": true
            }
        },
        "elasticsearch": {
            "status": "UP",
            "details": {
                "cluster_name": "docker-cluster",
                "status": "yellow",
                "timed_out": false,
                "number_of_nodes": 1,
                "number_of_data_nodes": 1,
                "active_primary_shards": 92,
                "active_shards": 92,
                "relocating_shards": 0,
                "initializing_shards": 0,
                "unassigned_shards": 61,
                "delayed_unassigned_shards": 0,
                "number_of_pending_tasks": 0,
                "number_of_in_flight_fetch": 0,
                "task_max_waiting_in_queue_millis": 0,
                "active_shards_percent_as_number": 60.130718954248366
            }
        },
        "livenessState": {
            "status": "UP"
        },
        "ping": {
            "status": "UP"
        },
        "readinessState": {
            "status": "UP"
        }
    },
    "groups": [
        "liveness",
        "readiness"
    ]
}

扩展

常用Endpoint

Spring Boot-actuator,提供了许多有用的EndPoint,对Spring Boot应用提供各种监控,下面说一下我常用的EndPoint:

/health 应用的健康状态
/configprops 获取应用的配置信息,因为Spring Boot 可能发布时是单独的Jar包,配置文件可能包含其中, 当我们需要检查配置文件时可以使用 ConfigpropsEndPoint 进行查看一些配置是否正确。
/trace 最近几次的http请求信息

当我们访问 http://localhost:8080/health 时,可以看到 HealthEndPoint 给我们提供默认的监控结果,包含 磁盘检测和数据库检测。

其实看 Spring Boot-actuator 源码,你会发现 HealthEndPoint 提供的信息不仅限于此,org.springframework.boot.actuate.health 包下 你会发现 ElasticsearchHealthIndicator、RedisHealthIndicator、RabbitHealthIndicator 等
也就是 HealthEndPoint 也提供 ES, Redis 等组件的健康信息。

自定义Indicator 扩展 HealthEndPoint

看源码 其实 磁盘和数据库健康信息就是 DiskSpaceHealthIndicator、DataSourceHealthIndicator 来实现的,当我们对一些我们自定义的组件进行监控时, 我们也可以实现个Indicator :

@Component
public class User implements HealthIndicator {
    /**
     * user监控 访问: http://localhost:8080/health
     *
     * @return 自定义Health监控
     */
    @Override
    public Health health() {

        return new Health.Builder().withDetail("usercount", 10) //自定义监控内容
                .withDetail("userstatus", "up").up().build();
    }
}

这时我们再次访问: http://localhost:8080/health 这时返回的结果如下,包含了我们自定义的 User 健康信息。

{
    "status": "UP",
    "user": {
        "status": "UP",
        "usercount": 10,
        "userstatus": "up"
    },
    "diskSpace": {
        "status": "UP",
        "total": 398458875904,
        "free": 315097989120,
        "threshold": 10485760
    },
    "db": {
        "status": "UP",
        "database": "MySQL",
        "hello": 1
    }
}