springboot的actuator内置了/health的endpoint,很方便地规范了每个服务的健康状况的api,而且HealthIndicator可以自己去扩展,增加相关依赖服务的健康状态,非常灵活方便而且可扩展。

/health实例

{
"status": "UP",
"custom": {
"status": "UNKNOWN",
"custom": {
"status": "UNKNOWN",
"msg": "mock down to test aggregator"
}
},
"diskSpace": {
"status": "UP",
"total": 249779191808,
"free": 57925111808,
"threshold": 10485760
}
}

health的Status枚举

org.springframework.boot.actuate.health.Status.java

@JsonInclude(Include.NON_EMPTY)
public final class Status { /**
* {@link Status} indicating that the component or subsystem is in an unknown state.
*/
public static final Status UNKNOWN = new Status("UNKNOWN"); /**
* {@link Status} indicating that the component or subsystem is functioning as
* expected.
*/
public static final Status UP = new Status("UP"); /**
* {@link Status} indicating that the component or subsystem has suffered an
* unexpected failure.
*/
public static final Status DOWN = new Status("DOWN"); /**
* {@link Status} indicating that the component or subsystem has been taken out of
* service and should not be used.
*/
public static final Status OUT_OF_SERVICE = new Status("OUT_OF_SERVICE");

对多个Status的聚合

对于多个HealthIndicator的status,spring boot默认对其进行aggregrate,然后计算最顶层的status字段的值,而且对于status是DOWN或者是OUT_OF_SERVICE的,返回的http的状态码是503,这对于应用监控系统来说真是大大的贴心啊,再总结一下:

  • 自动聚合多个HealthIndicator的status
  • 对于status是DOWN或者是OUT_OF_SERVICE的,返回503

这样应用监控系统一来就无需去解析返回结果,直接根据http的状态码就可以判断了,非常方便,太省心了有没有。

看看AbstractHealthAggregator.java的抽象类,其中对状态的聚合是abstract的。

public abstract class AbstractHealthAggregator implements HealthAggregator {

    @Override
public final Health aggregate(Map<String, Health> healths) {
List<Status> statusCandidates = new ArrayList<Status>();
for (Map.Entry<String, Health> entry : healths.entrySet()) {
statusCandidates.add(entry.getValue().getStatus());
}
Status status = aggregateStatus(statusCandidates);
Map<String, Object> details = aggregateDetails(healths);
return new Health.Builder(status, details).build();
} /**
* Return the single 'aggregate' status that should be used from the specified
* candidates.
* @param candidates the candidates
* @return a single status
*/
protected abstract Status aggregateStatus(List<Status> candidates); /**
* Return the map of 'aggregate' details that should be used from the specified
* healths.
* @param healths the health instances to aggregate
* @return a map of details
* @since 1.3.1
*/
protected Map<String, Object> aggregateDetails(Map<String, Health> healths) {
return new LinkedHashMap<String, Object>(healths);
} }

自定义Status,以及对Status聚合逻辑的调整

重写参考了OrderedHealthAggregator类,增加了 GIT_WARNING

public class OrderedHealthAggregator extends AbstractHealthAggregator {

    private List<String> statusOrder;

    public static final Status GIT_WARNING = new Status("GIT_WARNING", "警告:config-server连不上gitlab,请及时处理.");

    /**
* Create a new {@link OrderedHealthAggregator} instance.
*/
public OrderedHealthAggregator() {
setStatusOrder(Status.DOWN, Status.OUT_OF_SERVICE, GIT_WARNING, Status.UP, Status.UNKNOWN);
} /**
* Set the ordering of the status.
* @param statusOrder an ordered list of the status
*/
public void setStatusOrder(Status... statusOrder) {
String[] order = new String[statusOrder.length];
for (int i = 0; i < statusOrder.length; i++) {
order[i] = statusOrder[i].getCode();
}
setStatusOrder(Arrays.asList(order));
} /**
* Set the ordering of the status.
* @param statusOrder an ordered list of the status codes
*/
public void setStatusOrder(List<String> statusOrder) {
Assert.notNull(statusOrder, "StatusOrder must not be null");
this.statusOrder = statusOrder;
} @Override
protected Status aggregateStatus(List<Status> candidates) {
// Only sort those status instances that we know about
List<Status> filteredCandidates = new ArrayList<Status>();
for (Status candidate : candidates) {
//只识别Status.DOWN, Status.OUT_OF_SERVICE, GIT_WARNING, Status.UP, Status.UNKNOWN几种状态
if (this.statusOrder.contains(candidate.getCode())) {
filteredCandidates.add(candidate);
}
}
// If no status is given return UNKNOWN
if (filteredCandidates.isEmpty()) {
return Status.UNKNOWN;
}
//对多个结果状态的排序,按照各个状态在statusOrder集合中的位置排序
Collections.sort(filteredCandidates, new StatusComparator(this.statusOrder)); //返回排在最上面的那个状态值
return filteredCandidates.get(0);
} /**
* {@link Comparator} used to order {@link Status}.
*/
private class StatusComparator implements Comparator<Status> { private final List<String> statusOrder; StatusComparator(List<String> statusOrder) {
this.statusOrder = statusOrder;
} @Override
public int compare(Status s1, Status s2) {
//对多个结果状态的排序,按照各个状态在statusOrder集合中的位置排序
int i1 = this.statusOrder.indexOf(s1.getCode());
int i2 = this.statusOrder.indexOf(s2.getCode());
return (i1 < i2 ? -1 : (i1 == i2 ? s1.getCode().compareTo(s2.getCode()) : 1));
} } }

即Status.DOWN, Status.OUT_OF_SERVICE, GIT_WARNING, Status.UP, Status.UNKNOWN优先级依次递减。status中一旦有出现DOWN的情况,整体的status就是DOWN,依次类推。

Springboot Actuator之五:Springboot中的HealthAggregator、新增自定义Status的更多相关文章

  1. Winform中实现ZedGraph新增自定义Y轴上下限、颜色、标题功能

    场景 Winform中实现ZedGraph的多条Y轴(附源码下载): https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1001322 ...

  2. 5. SpringBoot —— Actuator简介

    Actuator是SpringBoot提供的用来帮助我们在将应用程序推向生产环境时对其进行监视和管理的工具集.使用Actuator最简单的方式,就是在pom文件中添加如下依赖: <depende ...

  3. SpringBoot actuator 应用监控。

    前言 : 今天在阅读 <SpringCloud微服务实战>一书时看到了SpringBoot actuator相关知识,并且自己也本地调试实践.觉得SpringBoot这一套监控还是挺有意思 ...

  4. springboot Actuator健康检查

    通过情况下,如我们想在系统中添加一个健康检查的接口,我们怎么做呢? 我们会新建一个类,或在已存在类的基础上添加检测接口. package com.crhms.medicareopinion; impo ...

  5. SpringBoot Actuator & SpringBoot Admin

    SpringBoot Actuator提供了很多监控和管理你的spring boot应用的HTTP或者JMX端点,并且你可以有选择地开启和关闭部分功能. 当你的spring boot应用中引入依赖之后 ...

  6. Springboot Actuator之八:actuator的执行原理

    本文接着<Springboot Actuator之七:actuator 中原生endpoint源码解析1>,前面主要分析了原生endpoint的作用. 现在着重了解actuator的执行原 ...

  7. SpringBoot Actuator监控【转】

    springboot actuator 监控 springboot1.5和springboot2.0 的actuator在启动日志上的差异就很大了. springboot1.5在启动时会打印很多/XX ...

  8. springboot actuator 配置安全

    springboot actuator监控是什么?类似php的phpinfor()函数,不过actuator更强大,可以查看的数据.状态更多.Actuator是Spring Boot提供的对应用系统的 ...

  9. SpringBoot Actuator — 埋点和监控

    项目中看到了有埋点监控.报表.日志分析,有点兴趣想慢慢捣鼓一下 1. 数据埋点 监控机器环境的性能和业务流程或逻辑等各项数据,并根据这些数据生成对应的指标,那么我们就称为数据埋点.比如我们想知道某个接 ...

随机推荐

  1. 藏红花StigmaCroci西红花StigmaCroci番红花

    伊朗藏红花(StigmaCroci)是一种耐旱植物,适于生长在冬季最低气温不低于零下20度,夏季最高气温不高于零上35度且气候干燥的地区. 因其水土和气候条件的限制,除了伊朗能大量种植外,希腊.印度. ...

  2. vue+element 获取验证码

    我们在做一个项目,登录注册页面是少不了的,为了人机校验,验证码也是必须的 我的这个项目获取验证码,前端发送一个随机四位数给后端,后端返回一张图片,前端渲染就可以 template代码: <el- ...

  3. 互联网企业级监控系统 OpenFalcon

    Open-Falcon 人性化的互联网企业级监控系统,Open-Falcon 整体可以分为两部分,即绘图组件.告警组件.其中: 安装绘图组件 负责数据的采集.收集.存储.归档.采样.查询.展示(Das ...

  4. CDA数据分析【数据处理工具SPSS】

    一.概述 SPSS[Statistical Package for the Social Science]社会科学统计软件包.SPSS统计软件在社会学.经济学.心理学.教育学等多个学科的研究工作和通信 ...

  5. MySQL——基本概念

    1.数据库:是一个长期存储在计算机内的.有组织的.有共享的.统一管理的数据集合.它是一个按数据结构来存储的和管理数据的计算机软件系统,即数据库包含两层含义:保管数据的“仓库”,以及数据管理的方法和技术 ...

  6. (原)在anaconda3+ubuntu16.04中编译Pose flow中的deepmatching动态库

    转载请注明出处: https://www.cnblogs.com/darkknightzh/p/11285239.html 参考网址: https://github.com/YuliangXiu/Po ...

  7. 葫芦娃团队对火鸡堂、基于云的胜利冲锋队团队的Beta产品测试报告

    Beta项目互测 课程名称:软件工程1916|W(福州大学) 作业要求:Beta阶段团队项目互评 团队名称:葫芦娃队 作业目标:Beta项目互测 一.火鸡堂团队产品测试 1.截图(推荐用动态gif图录 ...

  8. 树莓派搭建基于flask的web服务器-通过移动端控制LED

    1.概述 在局域网内,基于flask搭建web服务,从而可以使用移动客户端访问该web服务.由于是flask新手,所以本次实现的web服务功能较为简单,即控制LED灯的开/关及闪烁. 2.准备工作 2 ...

  9. windows 下批量删除git 分支

    删除筛选出来的分支,比如fixed git branch -D @(git branch | select-string  "fixed" | Foreach {$_.Line.T ...

  10. UEditor在开发环境中正常运作,但是部署到Tomcat中却无法使用

    背景 ​ SpringBoot项目,在 JSP 中使用 UEditor 问题 ​ UEditor 在开发环境中正常运作,但是导致部署到 Tomcat 中却无法使用 原因 在开发环境中,路径不够严谨,多 ...