Commit 8b2254a3 by Spencer Gibb

Register a HealthCheckHandler on DiscoveryClient if there is a HealthCheckHandler bean registered.

fixes gh-50
parent c752cc31
...@@ -17,6 +17,7 @@ package org.springframework.cloud.netflix.eureka; ...@@ -17,6 +17,7 @@ package org.springframework.cloud.netflix.eureka;
import javax.annotation.PreDestroy; import javax.annotation.PreDestroy;
import com.netflix.appinfo.HealthCheckHandler;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
...@@ -62,6 +63,9 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered { ...@@ -62,6 +63,9 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered {
@Autowired @Autowired
private EurekaInstanceConfigBean instanceConfig; private EurekaInstanceConfigBean instanceConfig;
@Autowired(required = false)
private HealthCheckHandler healthCheckHandler;
@PreDestroy @PreDestroy
public void close() { public void close() {
logger.info("Removing application {} from eureka", instanceConfig.getAppname()); logger.info("Removing application {} from eureka", instanceConfig.getAppname());
...@@ -78,9 +82,14 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered { ...@@ -78,9 +82,14 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered {
//because of containerPortInitializer below //because of containerPortInitializer below
if (!running && instanceConfig.getNonSecurePort() > 0) { if (!running && instanceConfig.getNonSecurePort() > 0) {
discoveryManagerIntitializer().init(); discoveryManagerIntitializer().init();
logger.info("Registering application {} with eureka with status {}", logger.info("Registering application {} with eureka with status {}",
instanceConfig.getAppname(), instanceConfig.getInitialStatus()); instanceConfig.getAppname(), instanceConfig.getInitialStatus());
ApplicationInfoManager.getInstance().setInstanceStatus(instanceConfig.getInitialStatus()); ApplicationInfoManager.getInstance().setInstanceStatus(instanceConfig.getInitialStatus());
if (healthCheckHandler != null) {
DiscoveryManager.getInstance().getDiscoveryClient().registerHealthCheck(healthCheckHandler);
}
running = true; running = true;
} }
} }
......
package org.springframework.cloud.netflix.eureka.sample; package org.springframework.cloud.netflix.eureka.sample;
import com.netflix.appinfo.HealthCheckHandler;
import com.netflix.appinfo.InstanceInfo;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.cloud.netflix.eureka.EnableEurekaClient; import org.springframework.cloud.netflix.eureka.EnableEurekaClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
...@@ -15,6 +18,16 @@ import org.springframework.web.bind.annotation.RestController; ...@@ -15,6 +18,16 @@ import org.springframework.web.bind.annotation.RestController;
@EnableEurekaClient @EnableEurekaClient
public class Application { public class Application {
@Bean
public HealthCheckHandler healthCheckHandler() {
return new HealthCheckHandler() {
@Override
public InstanceInfo.InstanceStatus getStatus(InstanceInfo.InstanceStatus currentStatus) {
return InstanceInfo.InstanceStatus.UP;
}
};
}
@RequestMapping("/") @RequestMapping("/")
public String home() { public String home() {
return "Hello world"; return "Hello world";
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment