在没有Boot和@EnableAutoConfiguration的情况下添加Spring Actuator的健康检查

时间:2016-10-26 09:45:55

标签: java spring spring-mvc spring-boot-actuator

我想将Actuator添加到我的Spring应用程序(而不是Spring Boot)。 确切地说,我需要使用执行器HealthCheck,因此我将能够创建具有不同健康指标的bean,并且他们将在/ health上创建完整的健康检查。

起初我尝试将@EndpointAutoConfiguration添加到我的@Configuration课程,但是有豆子冲突,因为我有我的海关豆,我真的需要,所以没有必要除HealthIndicatorAutoConfiguration

之外的其他配置

1 个答案:

答案 0 :(得分:0)

可能的解决方案是使用关键字段创建自定义HealthIndicator和HealthIndicatorCollector。还添加了一个servlet:

contextHandler.addServlet(new ServletHolder(platformHealthCheckServlet), "/healthcheck");

这将从上下文中收集所有自定义HealthIndicators并在/ healthcheck端点处创建良好的状态json:

@Autowired
public CustomHealthCheckServlet(Map<String, CustomHealthIndicator> healthIndicatorMap, ObjectMapper objectMapper) {
    this.healthIndicatorMap = Collections.unmodifiableMap(healthIndicatorMap);
    this.mapper = objectMapper;
}

CustomHealthIndicator ,它添加了 setCritical 方法:

public interface CustomHealthIndicator extends HealthIndicator {
    void setCritical(boolean var1);
}

CustomAbstractHealthIndicator 覆盖了 AbstractHealthIndicator 中的 doHealthCheck 方法,向其构建器添加了关键细节并创建了自己的抽象方法 doCheck >:

public abstract class CustomAbstractHealthIndicator extends AbstractHealthIndicator 
implements CustomHealthIndicator {
    private boolean critical = false;

    public CustomAbstractHealthIndicator() {
     }

    public void setCritical(boolean critical) {
        this.critical = critical;
    }

    @Override
    protected void doHealthCheck(Builder builder) throws Exception {
        builder.withDetail("critical", this.critical);
        this.doCheck(builder);
    }

    protected abstract Builder doCheck(Builder var1) throws Exception;
}

还有另一个Custom类,它根据每个运行状况的严重状态来聚合运行状况bean,并根据整个运行状况的关键状态来计算整个应用程序的状态,而忽略不重要但可用于监视的指标:

public class CustomHealthAggregator implements HealthAggregator {

    private List<String> statusOrder;

    public CustomHealthAggregator() {
        setStatusOrder(Status.DOWN, Status.OUT_OF_SERVICE, Status.UP, 
Status.UNKNOWN);
    }

    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));
    }

    public void setStatusOrder(List<String> statusOrder) {
        Assert.notNull(statusOrder, "StatusOrder must not be null");
        this.statusOrder = statusOrder;
    }

    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 UP
        if (filteredCandidates.isEmpty()) {
            return Status.UP;
        }
        // Sort given Status instances by configured order
        Collections.sort(filteredCandidates, 
        new StatusComparator(this.statusOrder));
        return filteredCandidates.get(0);
    }

    @Override
    public Health aggregate(Map<String, Health> healths) {
        List<Status> statusCandidates = healths.entrySet()
                .stream()
                .filter(healthEntry -> 
              healthEntry.getValue().getDetails().get("critical").equals(true))
                .map(e -> e.getValue().getStatus())
                .collect(Collectors.toList());

        Status status = aggregateStatus(statusCandidates);
        Map<String, Object> details = new LinkedMap();
        details.put("version", AppVersionUtil.getAppVersion());
        details.putAll(aggregateDetails(healths));
        return new Health.Builder(status, details).build();
    }

    protected Map<String, Object> aggregateDetails(Map<String, Health> healths) 
    {
        return new LinkedHashMap<String, Object>(healths);
    }

    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) {
            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));
        }

    }
}