Fire InstanceRegisterEvent after eureka registration, then components can listen for that event and safely access DiscoveryClient.

This commit is contained in:
Spencer Gibb
2014-11-25 17:19:02 -07:00
parent a3ce593bf6
commit 9bc8a5039d
5 changed files with 35 additions and 11 deletions

View File

@@ -0,0 +1,24 @@
package org.springframework.cloud.client.discovery;
import org.springframework.context.ApplicationEvent;
/**
* @author Spencer Gibb
*/
public class InstanceRegisteredEvent extends ApplicationEvent {
private Object config;
/**
* Create a new ApplicationEvent.
*
* @param source the component that published the event (never {@code null})
*/
public InstanceRegisteredEvent(Object source, Object config) {
super(source);
this.config = config;
}
public Object getConfig() {
return config;
}
}

View File

@@ -27,6 +27,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.InstanceRegisteredEvent;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.Bean;
@@ -66,6 +68,9 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered {
@Autowired(required = false)
private HealthCheckHandler healthCheckHandler;
@Autowired
private ApplicationContext context;
@PreDestroy
public void close() {
logger.info("Removing application {} from eureka", instanceConfig.getAppname());
@@ -90,6 +95,7 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered {
if (healthCheckHandler != null) {
DiscoveryManager.getInstance().getDiscoveryClient().registerHealthCheck(healthCheckHandler);
}
context.publishEvent(new InstanceRegisteredEvent(this, instanceConfig));
running = true;
}
}

View File

@@ -2,8 +2,8 @@ package org.springframework.cloud.netflix.zuul;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.InstanceRegisteredEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
@@ -14,7 +14,7 @@ import java.util.Map;
* @author Spencer Gibb
*/
@Slf4j
public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements ApplicationListener<ContextRefreshedEvent> {
public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements ApplicationListener<InstanceRegisteredEvent> {
@Autowired
protected RouteLocator routeLocator;
@@ -34,7 +34,7 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements App
}
@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
public void onApplicationEvent(InstanceRegisteredEvent event) {
registerHandlers(routeLocator.getRoutes());
}

View File

@@ -1,6 +1,5 @@
package org.springframework.cloud.netflix.zuul.sample;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest;
@@ -14,8 +13,7 @@ import org.springframework.test.context.web.WebAppConfiguration;
@IntegrationTest("server.port=0")
public class ZuulProxyApplicationTests {
@Ignore
@Test
@Test
public void contextLoads() {
}

View File

@@ -1,21 +1,17 @@
package org.springframework.cloud.netflix.sidecar;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SidecarApplication.class)
@WebAppConfiguration
@IntegrationTest("server.port=0")
@Ignore
public class SidecarApplicationTests {
@Test()
@Test
public void contextLoads() {
}