Unverified Commit b2d27a1d by Bertrand Renuart Committed by Spencer Gibb

Refactor HandlerMapping refresh into a single listener

fixes gh-2807
parent 406e112a
...@@ -31,10 +31,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean ...@@ -31,10 +31,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass; import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.cloud.client.actuator.HasFeatures; import org.springframework.cloud.client.actuator.HasFeatures;
import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.cloud.client.discovery.event.HeartbeatMonitor;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent;
import org.springframework.cloud.client.serviceregistry.Registration; import org.springframework.cloud.client.serviceregistry.Registration;
import org.springframework.cloud.commons.httpclient.ApacheHttpClientConnectionManagerFactory; import org.springframework.cloud.commons.httpclient.ApacheHttpClientConnectionManagerFactory;
import org.springframework.cloud.commons.httpclient.ApacheHttpClientFactory; import org.springframework.cloud.commons.httpclient.ApacheHttpClientFactory;
...@@ -51,9 +47,6 @@ import org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter; ...@@ -51,9 +47,6 @@ import org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter; import org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter;
import org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter; import org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter;
import org.springframework.cloud.netflix.zuul.web.ZuulHandlerMapping;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import; import org.springframework.context.annotation.Import;
...@@ -135,11 +128,6 @@ public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration { ...@@ -135,11 +128,6 @@ public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration {
} }
@Bean @Bean
public ApplicationListener<ApplicationEvent> zuulDiscoveryRefreshRoutesListener() {
return new ZuulDiscoveryRefreshListener();
}
@Bean
@ConditionalOnMissingBean(ServiceRouteMapper.class) @ConditionalOnMissingBean(ServiceRouteMapper.class)
public ServiceRouteMapper serviceRouteMapper() { public ServiceRouteMapper serviceRouteMapper() {
return new SimpleServiceRouteMapper(); return new SimpleServiceRouteMapper();
...@@ -190,41 +178,4 @@ public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration { ...@@ -190,41 +178,4 @@ public class ZuulProxyAutoConfiguration extends ZuulServerAutoConfiguration {
return helper; return helper;
} }
} }
private static class ZuulDiscoveryRefreshListener
implements ApplicationListener<ApplicationEvent> {
private HeartbeatMonitor monitor = new HeartbeatMonitor();
@Autowired
private ZuulHandlerMapping zuulHandlerMapping;
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof InstanceRegisteredEvent) {
reset();
}
else if (event instanceof ParentHeartbeatEvent) {
ParentHeartbeatEvent e = (ParentHeartbeatEvent) event;
resetIfNeeded(e.getValue());
}
else if (event instanceof HeartbeatEvent) {
HeartbeatEvent e = (HeartbeatEvent) event;
resetIfNeeded(e.getValue());
}
}
private void resetIfNeeded(Object value) {
if (this.monitor.update(value)) {
reset();
}
}
private void reset() {
this.zuulHandlerMapping.setDirty(true);
}
}
} }
...@@ -32,6 +32,8 @@ import org.springframework.boot.web.servlet.error.ErrorController; ...@@ -32,6 +32,8 @@ import org.springframework.boot.web.servlet.error.ErrorController;
import org.springframework.cloud.client.actuator.HasFeatures; import org.springframework.cloud.client.actuator.HasFeatures;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent; import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.cloud.client.discovery.event.HeartbeatMonitor; import org.springframework.cloud.client.discovery.event.HeartbeatMonitor;
import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent;
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent; import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.zuul.filters.CompositeRouteLocator; import org.springframework.cloud.netflix.zuul.filters.CompositeRouteLocator;
...@@ -238,16 +240,28 @@ public class ZuulServerAutoConfiguration { ...@@ -238,16 +240,28 @@ public class ZuulServerAutoConfiguration {
public void onApplicationEvent(ApplicationEvent event) { public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent if (event instanceof ContextRefreshedEvent
|| event instanceof RefreshScopeRefreshedEvent || event instanceof RefreshScopeRefreshedEvent
|| event instanceof RoutesRefreshedEvent) { || event instanceof RoutesRefreshedEvent
this.zuulHandlerMapping.setDirty(true); || event instanceof InstanceRegisteredEvent) {
reset();
} }
else if (event instanceof HeartbeatEvent) { else if (event instanceof ParentHeartbeatEvent) {
if (this.heartbeatMonitor.update(((HeartbeatEvent) event).getValue())) { ParentHeartbeatEvent e = (ParentHeartbeatEvent) event;
this.zuulHandlerMapping.setDirty(true); resetIfNeeded(e.getValue());
} }
else if (event instanceof HeartbeatEvent) {
HeartbeatEvent e = (HeartbeatEvent) event;
resetIfNeeded(e.getValue());
} }
} }
private void resetIfNeeded(Object value) {
if (this.heartbeatMonitor.update(value)) {
reset();
}
} }
private void reset() {
this.zuulHandlerMapping.setDirty(true);
}
}
} }
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