Add listener for HeartbeatEvent to refresh zuul routes

Fixes gh-1084
This commit is contained in:
Dave Syer
2016-06-09 11:18:55 +01:00
parent 2566707256
commit 4507d1d46d
2 changed files with 22 additions and 1 deletions

View File

@@ -1121,7 +1121,7 @@ specified as an ant-style pattern, so "/myusers/{asterisk}" only matches one
level, but "/myusers/{all}" matches hierarchically.
The location of the backend can be specified as either a "serviceId"
(for a Eureka service) or a "url" (for a physical location), e.g.
(for a service from discovery) or a "url" (for a physical location), e.g.
.application.yml
[source,yaml]
@@ -1286,6 +1286,18 @@ classpath, and otherwise they are initialized to a set of well-known
Security. The assumption in this case is that the downstream services
might add these headers too, and we want the values from the proxy.
=== The Routes Endpoint
If you are using `@EnableZuulProxy` with tha Spring Boot Actuator you
will enable (by default) an additional endpoint, available via HTTP as
`/routes`. A GET to this endpoint will return a list of the mapped
routes. A POST will force a refresh of the existing routes (e.g. in
case there have been changes in the service catalog).
NOTE: the routes should respond automatically to changes in the
service catalog, but the POST to /routes is a way to force the change
to happen immediately.
=== Strangulation Patterns and Local Forwards
A common pattern when migrating an existing application or API is to

View File

@@ -27,6 +27,8 @@ import org.springframework.boot.autoconfigure.web.ServerPropertiesAutoConfigurat
import org.springframework.boot.context.embedded.ServletRegistrationBean;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.actuator.HasFeatures;
import org.springframework.cloud.client.discovery.event.HeartbeatEvent;
import org.springframework.cloud.client.discovery.event.HeartbeatMonitor;
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.cloud.netflix.zuul.filters.SimpleRouteLocator;
@@ -168,6 +170,8 @@ public class ZuulConfiguration {
@Autowired
private ZuulHandlerMapping zuulHandlerMapping;
private HeartbeatMonitor heartbeatMonitor = new HeartbeatMonitor();
@Override
public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ContextRefreshedEvent
@@ -175,6 +179,11 @@ public class ZuulConfiguration {
|| event instanceof RoutesRefreshedEvent) {
this.zuulHandlerMapping.setDirty(true);
}
else if (event instanceof HeartbeatEvent) {
if (this.heartbeatMonitor.update(((HeartbeatEvent) event).getValue())) {
this.zuulHandlerMapping.setDirty(true);
}
}
}
}