Commit 9c6474fb by Spencer Gibb

Make ZuulHandlerMapping @RefreshScope.

Handle InstanceRegisteredEvent and RefreshScopeRefreshedEvent in ZuulHandlerMapping rather than ProxyRouteLocator. Move /routes endpoint out of ZuulHandlerMapping into RoutesEndpoint (because of a problem when I added @RefreshScope to ZuulHandlerMapping)
parent 5246b932
...@@ -10,11 +10,8 @@ import java.util.concurrent.atomic.AtomicReference; ...@@ -10,11 +10,8 @@ import java.util.concurrent.atomic.AtomicReference;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.client.discovery.DiscoveryClient; import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.context.environment.EnvironmentChangeEvent;
import org.springframework.cloud.netflix.zuul.ZuulProperties.ZuulRoute; import org.springframework.cloud.netflix.zuul.ZuulProperties.ZuulRoute;
import org.springframework.context.ApplicationListener;
import org.springframework.util.AntPathMatcher; import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher; import org.springframework.util.PathMatcher;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
...@@ -23,7 +20,7 @@ import org.springframework.util.StringUtils; ...@@ -23,7 +20,7 @@ import org.springframework.util.StringUtils;
* @author Spencer Gibb * @author Spencer Gibb
*/ */
@Slf4j @Slf4j
public class ProxyRouteLocator implements ApplicationListener<EnvironmentChangeEvent> { public class ProxyRouteLocator {
public static final String DEFAULT_ROUTE = "/"; public static final String DEFAULT_ROUTE = "/";
...@@ -42,16 +39,6 @@ public class ProxyRouteLocator implements ApplicationListener<EnvironmentChangeE ...@@ -42,16 +39,6 @@ public class ProxyRouteLocator implements ApplicationListener<EnvironmentChangeE
this.properties = properties; this.properties = properties;
} }
@Override
public void onApplicationEvent(EnvironmentChangeEvent event) {
for (String key : event.getKeys()) {
if (key.startsWith("zuul.routes")) {
resetRoutes();
return;
}
}
}
public void addRoute(String path, String location) { public void addRoute(String path, String location) {
staticRoutes.put(path, location); staticRoutes.put(path, location);
resetRoutes(); resetRoutes();
......
package org.springframework.cloud.netflix.zuul;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.Map;
/**
* Endpoint to display and reset the zuul proxy routes
*
* @author Spencer Gibb
* @author Dave Syer
*/
public class RoutesEndpoint implements MvcEndpoint {
private ZuulHandlerMapping handlerMapping;
@Autowired
public RoutesEndpoint(ZuulHandlerMapping handlerMapping) {
this.handlerMapping = handlerMapping;
}
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
public Map<String, String> reset() {
return handlerMapping.reset();
}
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
public Map<String, String> getRoutes() {
return handlerMapping.getRoutes();
}
@Override
public String getPath() {
return "/routes";
}
@Override
public boolean isSensitive() {
return true;
}
@Override
public Class<? extends Endpoint<?>> getEndpointType() {
return null;
}
}
...@@ -8,6 +8,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; ...@@ -8,6 +8,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression; import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
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.context.config.annotation.RefreshScope;
import org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter; import org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter;
import org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter; import org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter;
import org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter; import org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter;
...@@ -50,6 +51,7 @@ public class ZuulConfiguration { ...@@ -50,6 +51,7 @@ public class ZuulConfiguration {
} }
@Bean @Bean
@RefreshScope
public ZuulHandlerMapping zuulHandlerMapping() { public ZuulHandlerMapping zuulHandlerMapping() {
return new ZuulHandlerMapping(routes(), zuulController()); return new ZuulHandlerMapping(routes(), zuulController());
} }
...@@ -67,6 +69,11 @@ public class ZuulConfiguration { ...@@ -67,6 +69,11 @@ public class ZuulConfiguration {
} }
@Bean
public RoutesEndpoint zuulEndpoint() {
return new RoutesEndpoint(zuulHandlerMapping());
}
// pre filters // pre filters
@Bean @Bean
public DebugFilter debugFilter() { public DebugFilter debugFilter() {
......
package org.springframework.cloud.netflix.zuul; package org.springframework.cloud.netflix.zuul;
import java.util.Collection;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.actuate.endpoint.Endpoint;
import org.springframework.boot.actuate.endpoint.mvc.MvcEndpoint;
import org.springframework.cloud.client.discovery.InstanceRegisteredEvent; import org.springframework.cloud.client.discovery.InstanceRegisteredEvent;
import org.springframework.cloud.context.scope.refresh.RefreshScopeRefreshedEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.jmx.export.annotation.ManagedAttribute; import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation; import org.springframework.jmx.export.annotation.ManagedOperation;
import org.springframework.jmx.export.annotation.ManagedResource; import org.springframework.jmx.export.annotation.ManagedResource;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping; import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
import java.util.Collection;
import java.util.Map;
/** /**
* MVC HandlerMapping that maps incoming request paths to remote services. * MVC HandlerMapping that maps incoming request paths to remote services.
* *
...@@ -24,7 +21,7 @@ import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping; ...@@ -24,7 +21,7 @@ import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
*/ */
@ManagedResource(description = "Can be used to list and reset the reverse proxy routes") @ManagedResource(description = "Can be used to list and reset the reverse proxy routes")
public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements
ApplicationListener<InstanceRegisteredEvent>, MvcEndpoint { ApplicationListener<ApplicationEvent> {
private ProxyRouteLocator routeLocator; private ProxyRouteLocator routeLocator;
...@@ -38,8 +35,11 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements ...@@ -38,8 +35,11 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements
} }
@Override @Override
public void onApplicationEvent(InstanceRegisteredEvent event) { public void onApplicationEvent(ApplicationEvent event) {
registerHandlers(routeLocator.getRoutePaths()); if (event instanceof InstanceRegisteredEvent
|| event instanceof RefreshScopeRefreshedEvent) {
reset();
}
} }
protected void registerHandlers(Collection<String> routes) { protected void registerHandlers(Collection<String> routes) {
...@@ -53,8 +53,6 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements ...@@ -53,8 +53,6 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements
} }
} }
@RequestMapping(method = RequestMethod.POST)
@ResponseBody
@ManagedOperation @ManagedOperation
public Map<String, String> reset() { public Map<String, String> reset() {
routeLocator.resetRoutes(); routeLocator.resetRoutes();
...@@ -62,26 +60,9 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements ...@@ -62,26 +60,9 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements
return getRoutes(); return getRoutes();
} }
@RequestMapping(method = RequestMethod.GET)
@ResponseBody
@ManagedAttribute @ManagedAttribute
public Map<String, String> getRoutes() { public Map<String, String> getRoutes() {
return routeLocator.getRoutes(); return routeLocator.getRoutes();
} }
@Override
public String getPath() {
return "/routes";
}
@Override
public boolean isSensitive() {
return true;
}
@Override
public Class<? extends Endpoint<?>> getEndpointType() {
return null;
}
} }
...@@ -11,6 +11,9 @@ sidecar: ...@@ -11,6 +11,9 @@ sidecar:
eureka: eureka:
instance: instance:
app-group-name: mysidecargroup app-group-name: mysidecargroup
client:
serviceUrl:
defaultZone: http://user:password@localhost:8761/eureka/
endpoints: endpoints:
refresh: refresh:
......
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