actuator的health节点及自定义health模块

/actuator/health

此节点可以检查各个组件的健康状态,例如调用接口时,DB组件会返回OK。

HealthMvcEndpoint && HealthEndpoint

  • /actuator/health接口是由HealthMvcEndpoint的invoke方法来处理的。
  • HealthMvcEndpoint的底层是通过HealthEndpoint来实现的。
  • HealthMvcEndpoint初始化代码
    @Bean
    @ConditionalOnBean(HealthEndpoint.class)
    @ConditionalOnMissingBean
    @ConditionalOnEnabledEndpoint("health")
    public HealthMvcEndpoint healthMvcEndpoint(HealthEndpoint delegate,
            ManagementServerProperties managementServerProperties) {
        HealthMvcEndpoint healthMvcEndpoint = new HealthMvcEndpoint(delegate,
                this.managementServerProperties.getSecurity().isEnabled(),
                managementServerProperties.getSecurity().getRoles());
        if (this.healthMvcEndpointProperties.getMapping() != null) {
            healthMvcEndpoint
                    .addStatusMapping(this.healthMvcEndpointProperties.getMapping());
        }
        return healthMvcEndpoint;
    }
  • HealthEndpoint初始化代码
    @Bean
    @ConditionalOnMissingBean
    public HealthEndpoint healthEndpoint() {
        return new HealthEndpoint(
                this.healthAggregator == null ? new OrderedHealthAggregator()
                        : this.healthAggregator,
                this.healthIndicators == null
                        ? Collections.<String, HealthIndicator>emptyMap()
                        : this.healthIndicators);
    }

自定义组件的健康状态检查

某些情况下,当我们需要对我们新建的一个模块进行健康监控的话,可以把这些监控信息统一归纳到health节点下,只需要实现HealthIndicator接口即可。

/**
 * Strategy interface used to provide an indication of application health.
 *
 * @author Dave Syer
 * @see ApplicationHealthIndicator
 */
public interface HealthIndicator {

    /**
     * Return an indication of health.
     * @return the health for
     */
    Health health();

}

一般都是通过继承AbstractHealthIndicator来实现的,添加@Component即可。

/**
 * Base {@link HealthIndicator} implementations that encapsulates creation of
 * {@link Health} instance and error handling.
 * <p>
 * This implementation is only suitable if an {@link Exception} raised from
 * {@link #doHealthCheck(org.springframework.boot.actuate.health.Health.Builder)} should
 * create a {@link Status#DOWN} health status.
 *
 * @author Christian Dupuis
 * @since 1.1.0
 */
public abstract class AbstractHealthIndicator implements HealthIndicator {

    private final Log logger = LogFactory.getLog(getClass());

    @Override
    public final Health health() {
        Health.Builder builder = new Health.Builder();
        try {
            doHealthCheck(builder);
        }
        catch (Exception ex) {
            this.logger.warn("Health check failed", ex);
            builder.down(ex);
        }
        return builder.build();
    }

    /**
     * Actual health check logic.
     * @param builder the {@link Builder} to report health status and details
     * @throws Exception any {@link Exception} that should create a {@link Status#DOWN}
     * system status.
     */
    protected abstract void doHealthCheck(Health.Builder builder) throws Exception;

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,665评论 19 139
  • 关于Mongodb的全面总结 MongoDB的内部构造《MongoDB The Definitive Guide》...
    中v中阅读 32,314评论 2 89
  • 0. 写在前面 这部分内容将会简要介绍如何定制自己的starter,以及starter的一些基本原理 1. sta...
    ImushroomT阅读 7,382评论 2 9
  • 出去玩一次回来,被记得牢牢的总不是自然风光人文历史,而是那些鸡零狗碎的小事和来自不同国家的外国人,有的可爱有趣,有...
    妖婆阅读 366评论 0 0
  • 原文链接 翻译自官方文档 开始在内存中使用 创建一个workbook 在刚开始使用openpyxl的时候,不需要直...
    TryEnough阅读 33,323评论 1 44

友情链接更多精彩内容