当前,我们正在使用Spring Boot执行器 vr 1.5.10 ,并且可以成功使用 / health 端点访问我们的运行状况页面。但是,在访问健康端点时,我们还需要将该健康状态记录在应用程序的日志文件中,以便稍后我们可以根据状态创建Kibana观察者警报。
我看到我们可以使用“ @EndpointWebExtension ”扩展运行状况终结点,并按照here中的建议在其中记录状态。但是似乎1.5.10版不支持该功能。
任何方向将不胜感激!
答案 0 :(得分:1)
您可以编写自己的HealthAggregator并在其中设置Default Spring Aggregator并使用它。
public class OwnHealthAggregator implements HealthAggregator {
private final HealthAggregator defaultHealth;
public OwnHealthAggregator(HealthAggregator defaultHealth) {
this.defaultHealth = defaultHealth;
}
@Override
public Health aggregate(Map<String, Health> healths) {
Health result = this.defaultHealth.aggregate(healths);
// LOG Result here
return result;
}
}
定义一个bean并使用默认的bean:
@Bean
public HealthAggregator health() {
return new OwnHealthAggregator(new OrderedHealthAggregator());
}
在此版本中使用执行器时,请注意,因为默认情况下启用了许多端点(例如/ beans,/ env,/ dump)。
答案 1 :(得分:1)
Spring将注入实现特定接口的所有组件的列表。
例如:
private List<HealthIndicator> healthIndicatorList
将包含实现HealthIndicator
类的每个类。
您可以在自己的组件中实现HealthIndicator
类,
然后在healthIndicatorList的每个成员上调用health()
方法并记录结果。
当然,
请勿递归调用日志记录运行状况指示器的health()
方法。
这是一些代码:
@Component
public class LoggingHealthIndicator implements HealthIndicator
{
@Autowired
private List<HealthIndicator> healthIndicatorList
@Override
public Health health()
{
for (final HealthIndicator current : healthIndicatorList)
{
if (current != this)
{
// assumes SLF4j
logger.log("blam message; {}", current.health());
}
}
return Health.up().build();
}
}
该代码可能无法正常工作,因为您需要全班学习 HealthIndictor实现,并且本身就是一个HealthIndicator。
您可能需要添加@PostConstruct
方法并自己检索HealthIndictor组件的列表。