0
点赞
收藏
分享

微信扫一扫

SpringBoot 2.x 监控——actuator健康检查

路西法阁下 2021-09-21 阅读 55

依赖

当你的spring boot应用中引入下面的依赖之后,将自动的拥有审计、健康检查、Metrics监控功能。

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

启用监控端点

application.yml通过下面的配置启用所有的监控端点。如果没有这个配置,默认情况下只有info和health是启用的,其他端点是禁用的。

############  actuator监控  ############
management:
  endpoints:
    web:
      exposure:
        include: "*"

application.yml中完整的配置信息


############  actuator监控  ############
management:
  server:
    port: 9080
    servlet:
      context-path: /
  health:
    diskSpace:
      enabled: true
    db:
      enabled: true
    redis:
      enabled: false
    elasticsearch:
      enabled: false
    mail:
      enabled: false
  endpoint:
    health:
      show-details: always
  endpoints:
    web:
      exposure:
        include: "*"

要想查看详细的应用健康信息需要配置management.endpoint.health.show-details 的值为always(除了always之外还有when-authorized、never,默认值是never),配置之后我们再次访问http://localhost:9080/actuator/health,获取的信息如下:

{
    "status": "UP",
    "components": {
        "db": {
            "status": "UP",
            "details": {
                "database": "MySQL",
                "result": 1,
                "validationQuery": "/* ping */ SELECT 1"
            }
        },
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 353341796352,
                "free": 325037277184,
                "threshold": 10485760
            }
        },
        "ping": {
            "status": "UP"
        },
        "refreshScope": {
            "status": "UP"
        }
    }
}

Spring Boot 内置了一些 HealthIndicator,启用监控的这个spring boot应用连接了RedisMysqlactuator就自动给监控起来了。components中的监控项,任何一个健康状态是DOWN,整体应用的健康状态也是DOWN

其他信息获取

http://localhost:9080/actuator/可以查看所有的端点url,通过actuator/+端点名就可以获取相应的信息,如:

http://localhost:9080/actuator/health
http://localhost:9080/actuator/info
http://localhost:9080/actuator/beans
......

健康检查的原理

Spring boot的健康信息都是从ApplicationContext中的各种HealthIndicator点击查看此类
实现类中收集到的,Spring boot框架中包含了大量的HealthIndicators的实现类。

Name Description
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.
JmsHealthIndicator Checks that a JMS broker is up.
MailHealthIndicator Checks that a mail server is up.
MongoHealthIndicator Checks that a Mongo database 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.

可见,Spring Boot 帮忙我们集成了许多比较常见的健康监控,例如:MySQL、 MongoDB、 Redis、 ElasticSearch、 Solr、 RabbitMQ等。

自定义 HealthIndicator 健康检查

通过实现HealthIndicator的接口来实现,并将该实现类注册为spring bean(添加注释@Component)。你需要实现其中的health()方法,并返回自定义的健康状态响应信息,该响应信息应该包括一个状态码和要展示详细信息。例如,下面就是一个接口HealthIndicator的实现类:

package com.pay.payee.actuator;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.HealthIndicator;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.DependsOn;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

/**
 * @ClassName: RedisCustomHealth
 * @Description: 自定义Redis健康检查actuator
 * @author: 郭秀志 jbcode@126.com
 * @date: 2020/5/8 0:00
 * @Copyright:
 */
@Component
@DependsOn(value = {"redisTemplate"})
@ConditionalOnProperty(value = "bank_router.actuator.check.redis", havingValue = "true")
public class RedisCustomHealth implements HealthIndicator {

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Health health() {
        try {
            redisTemplate.opsForValue().set("heartBeat", "guoxiuzhi");
            redisTemplate.opsForValue().get("heartBeat");
            return Health.up().withDetail("Redis正常", "设置数据key=heartBeat,value=guoxiuzhi").build();
        } catch (Exception e) {
            return Health.down().withDetail("down的原因:", e.getMessage()).build();
        }
    }

}

@ConditionalOnProperty(value = "bank_router.actuator.check.redis", havingValue = "true")意思是:属性文件如下配置的值为true才执行。

bank_router:
  actuator:
    check:
      mysql: true
      redis: true

测试自定义Redis健康检查

application.yml启用Redis检查:

    redis:
      enabled: true

启动Redis。
启动被检查的项目。
再次访问http://localhost:9080/actuator/health,输出信息:

{
    "status": "UP",
    "components": {
        "db": {
            "status": "UP",
            "details": {
                "database": "MySQL",
                "result": 1,
                "validationQuery": "/* ping */ SELECT 1"
            }
        },
        "diskSpace": {
            "status": "UP",
            "details": {
                "total": 353341796352,
                "free": 325039079424,
                "threshold": 10485760
            }
        },
        "ping": {
            "status": "UP"
        },
        "redis": {
            "status": "UP",
            "details": {
                "version": "3.2.100"
            }
        },
        "redisCustomHealth": {
            "status": "UP",
            "details": {
                "Redis正常": "设置数据key=heartBeat,value=guoxiuzhi"
            }
        },
        "refreshScope": {
            "status": "UP"
        }
    }
}

可以看到自带和自定义Redis的健康检查都出现了。

总结

Actuator监控管理中的健康检查功能,随时能掌握线上应用的健康状况,尤其是现在流行的容器云(K8s)下的应用,它们的自动恢复和扩容都依赖健康检查功能。

举报

相关推荐

0 条评论