Commit 9bc8a503 by Spencer Gibb

Fire InstanceRegisterEvent after eureka registration, then components can listen…

Fire InstanceRegisterEvent after eureka registration, then components can listen for that event and safely access DiscoveryClient.
parent a3ce593b
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;
}
}
...@@ -27,6 +27,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean ...@@ -27,6 +27,8 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent; import org.springframework.boot.context.embedded.EmbeddedServletContainerInitializedEvent;
import org.springframework.boot.context.properties.EnableConfigurationProperties; import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.discovery.DiscoveryClient; 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.ApplicationListener;
import org.springframework.context.SmartLifecycle; import org.springframework.context.SmartLifecycle;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -66,6 +68,9 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered { ...@@ -66,6 +68,9 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered {
@Autowired(required = false) @Autowired(required = false)
private HealthCheckHandler healthCheckHandler; private HealthCheckHandler healthCheckHandler;
@Autowired
private ApplicationContext context;
@PreDestroy @PreDestroy
public void close() { public void close() {
logger.info("Removing application {} from eureka", instanceConfig.getAppname()); logger.info("Removing application {} from eureka", instanceConfig.getAppname());
...@@ -90,6 +95,7 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered { ...@@ -90,6 +95,7 @@ public class EurekaClientConfiguration implements SmartLifecycle, Ordered {
if (healthCheckHandler != null) { if (healthCheckHandler != null) {
DiscoveryManager.getInstance().getDiscoveryClient().registerHealthCheck(healthCheckHandler); DiscoveryManager.getInstance().getDiscoveryClient().registerHealthCheck(healthCheckHandler);
} }
context.publishEvent(new InstanceRegisteredEvent(this, instanceConfig));
running = true; running = true;
} }
} }
......
...@@ -2,8 +2,8 @@ package org.springframework.cloud.netflix.zuul; ...@@ -2,8 +2,8 @@ package org.springframework.cloud.netflix.zuul;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.InstanceRegisteredEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.event.ContextRefreshedEvent;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping; import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
...@@ -14,7 +14,7 @@ import java.util.Map; ...@@ -14,7 +14,7 @@ import java.util.Map;
* @author Spencer Gibb * @author Spencer Gibb
*/ */
@Slf4j @Slf4j
public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements ApplicationListener<ContextRefreshedEvent> { public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements ApplicationListener<InstanceRegisteredEvent> {
@Autowired @Autowired
protected RouteLocator routeLocator; protected RouteLocator routeLocator;
...@@ -34,7 +34,7 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements App ...@@ -34,7 +34,7 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements App
} }
@Override @Override
public void onApplicationEvent(ContextRefreshedEvent event) { public void onApplicationEvent(InstanceRegisteredEvent event) {
registerHandlers(routeLocator.getRoutes()); registerHandlers(routeLocator.getRoutes());
} }
......
package org.springframework.cloud.netflix.zuul.sample; package org.springframework.cloud.netflix.zuul.sample;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
...@@ -14,8 +13,7 @@ import org.springframework.test.context.web.WebAppConfiguration; ...@@ -14,8 +13,7 @@ import org.springframework.test.context.web.WebAppConfiguration;
@IntegrationTest("server.port=0") @IntegrationTest("server.port=0")
public class ZuulProxyApplicationTests { public class ZuulProxyApplicationTests {
@Ignore @Test
@Test
public void contextLoads() { public void contextLoads() {
} }
......
package org.springframework.cloud.netflix.sidecar; package org.springframework.cloud.netflix.sidecar;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
@RunWith(SpringJUnit4ClassRunner.class) @RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = SidecarApplication.class) @SpringApplicationConfiguration(classes = SidecarApplication.class)
@WebAppConfiguration
@IntegrationTest("server.port=0") @IntegrationTest("server.port=0")
@Ignore
public class SidecarApplicationTests { public class SidecarApplicationTests {
@Test() @Test
public void contextLoads() { public void contextLoads() {
} }
......
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