info:
health端点实现:
- 获取所有的健康检查结果Health放到map里
@Override
public Health health() {
Map<String, Health> healths = new LinkedHashMap<String, Health>();
for (Map.Entry<String, HealthIndicator> entry : this.indicators.entrySet()) {
healths.put(entry.getKey(), entry.getValue().health());
}
return this.healthAggregator.aggregate(healths);
}
- 合并Health里的status状态,最后拿列表第一个build返回
@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();
}
- 对Status进行排序归并,获得最终值
就是将所有status按照{“DOWN”,”OUT_OF_SERVICE”,”UP”,”UNKNOWN”}从优先级进行排序,最后取第一个:
- 如果有检测down了则总体是down
- 如果没有down,有检测out_of_service了则总体是out_of_service
- 如果检测只有up或者unknown则总体是up
- 如果全部检测是unknown了则总体是unknown
@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) {
if (this.statusOrder.contains(candidate.getCode())) {
filteredCandidates.add(candidate);
}
}
// If no status is given return UNKNOWN
if (filteredCandidates.isEmpty()) {
return Status.UNKNOWN;
}
// Sort given Status instances by configured order
Collections.sort(filteredCandidates, new StatusComparator(this.statusOrder));
return filteredCandidates.get(0);
}