问题描述
我在我的应用程序中使用了 SpringBoot v 1.5.3 ,并且添加了执行器 health ,信息和指标,并且使用可用的端点可以正常工作。 我需要获取应用程序的全局运行状况。
这是我的代码:
package com.test.health.indicator;
import java.util.Collections;
import org.springframework.boot.actuate.endpoint.HealthEndpoint;
import org.springframework.boot.actuate.health.Health;
import org.springframework.boot.actuate.health.Healthindicator;
import org.springframework.boot.actuate.info.Info;
import org.springframework.boot.actuate.info.InfoContributor;
import org.springframework.stereotype.Component;
import org.springframework.boot.actuate.health.Status;
@Component
public class healthContributorToInfo implements InfoContributor {
private HealthEndpoint healthEndpoint;
@Override
public void contribute(Info.Builder builder) {
Health health = ((Healthindicator) this.healthEndpoint).health();
Status status = health.getStatus();
builder.withDetail("Health Indicator",Collections.singletonMap("Global Satatus",Health.status(status.getCode())));
}
}
不幸的是,我的代码返回了null异常,因为我没有得到健康状态对象。 有人知道如何做到这一点。 谢谢!
解决方法
private HealthEndpoint healthEndpoint;
可能为空?如何在组件中设置/注入?我看不到任何会为其设置值的构造函数。
我认为,由于您尚未注入HealthEndPoint的依赖项,因此以下一行给出了NPE。
Health health = ((HealthIndicator) this.healthEndpoint).health();
您可以尝试自动装配还是为HealthEndPoint进行构造函数注入,看看是否可行。