Last active
September 2, 2015 18:05
-
-
Save jmnarloch/308dcaca18d3c2518663 to your computer and use it in GitHub Desktop.
Propagating the application health status to Eureka
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class EurekaHealthCheckHandler implements HealthCheckHandler, ApplicationContextAware, InitializingBean { | |
private static final Map<Status, InstanceInfo.InstanceStatus> healthStatuses = new HashMap<Status, InstanceInfo.InstanceStatus>() {{ | |
put(Status.UNKNOWN, InstanceInfo.InstanceStatus.UNKNOWN); | |
put(Status.OUT_OF_SERVICE, InstanceInfo.InstanceStatus.OUT_OF_SERVICE); | |
put(Status.DOWN, InstanceInfo.InstanceStatus.DOWN); | |
put(Status.UP, InstanceInfo.InstanceStatus.UP); | |
}}; | |
private final CompositeHealthIndicator healthIndicator; | |
private ApplicationContext applicationContext; | |
public EurekaHealthCheckHandler(HealthAggregator healthAggregator) { | |
Assert.notNull(healthAggregator, "HealthAggregator must not be null"); | |
this.healthIndicator = new CompositeHealthIndicator(healthAggregator); | |
} | |
@Override | |
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException { | |
this.applicationContext = applicationContext; | |
} | |
@Override | |
public void afterPropertiesSet() throws Exception { | |
final Map<String, HealthIndicator> healthIndicators = applicationContext.getBeansOfType(HealthIndicator.class); | |
for (Map.Entry<String, HealthIndicator> entry : healthIndicators.entrySet()) { | |
healthIndicator.addHealthIndicator(entry.getKey(), entry.getValue()); | |
} | |
} | |
@Override | |
public InstanceInfo.InstanceStatus getStatus(InstanceInfo.InstanceStatus instanceStatus) { | |
return getHealthStatus(); | |
} | |
protected InstanceInfo.InstanceStatus getHealthStatus() { | |
final Status status = healthIndicator.health().getStatus(); | |
return mapToInstanceStatus(status); | |
} | |
protected InstanceInfo.InstanceStatus mapToInstanceStatus(Status status) { | |
if(!healthStatuses.containsKey(status)) { | |
return InstanceInfo.InstanceStatus.UNKNOWN; | |
} | |
return healthStatuses.get(status); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@Configuration | |
public class EurekaHealthCheckHandlerConfiguration { | |
@Autowired(required = false) | |
private HealthAggregator healthAggregator = new OrderedHealthAggregator(); | |
@Bean | |
@ConditionalOnMissingBean | |
public EurekaHealthCheckHandler eurekaHealthCheckHandler() { | |
return new EurekaHealthCheckHandler(healthAggregator); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment