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)
This commit is contained in:
@@ -10,11 +10,8 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
|
||||
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.context.ApplicationListener;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.PathMatcher;
|
||||
import org.springframework.util.StringUtils;
|
||||
@@ -23,7 +20,7 @@ import org.springframework.util.StringUtils;
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
@Slf4j
|
||||
public class ProxyRouteLocator implements ApplicationListener<EnvironmentChangeEvent> {
|
||||
public class ProxyRouteLocator {
|
||||
|
||||
public static final String DEFAULT_ROUTE = "/";
|
||||
|
||||
@@ -42,16 +39,6 @@ public class ProxyRouteLocator implements ApplicationListener<EnvironmentChangeE
|
||||
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) {
|
||||
staticRoutes.put(path, location);
|
||||
resetRoutes();
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
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;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnExpression;
|
||||
import org.springframework.boot.context.properties.EnableConfigurationProperties;
|
||||
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.SendResponseFilter;
|
||||
import org.springframework.cloud.netflix.zuul.filters.pre.DebugFilter;
|
||||
@@ -50,6 +51,7 @@ public class ZuulConfiguration {
|
||||
}
|
||||
|
||||
@Bean
|
||||
@RefreshScope
|
||||
public ZuulHandlerMapping zuulHandlerMapping() {
|
||||
return new ZuulHandlerMapping(routes(), zuulController());
|
||||
}
|
||||
@@ -67,6 +69,11 @@ public class ZuulConfiguration {
|
||||
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RoutesEndpoint zuulEndpoint() {
|
||||
return new RoutesEndpoint(zuulHandlerMapping());
|
||||
}
|
||||
|
||||
// pre filters
|
||||
@Bean
|
||||
public DebugFilter debugFilter() {
|
||||
|
||||
@@ -1,21 +1,18 @@
|
||||
package org.springframework.cloud.netflix.zuul;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
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.context.scope.refresh.RefreshScopeRefreshedEvent;
|
||||
import org.springframework.context.ApplicationEvent;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.jmx.export.annotation.ManagedAttribute;
|
||||
import org.springframework.jmx.export.annotation.ManagedOperation;
|
||||
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 java.util.Collection;
|
||||
import java.util.Map;
|
||||
|
||||
/**
|
||||
* MVC HandlerMapping that maps incoming request paths to remote services.
|
||||
*
|
||||
@@ -24,7 +21,7 @@ import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
|
||||
*/
|
||||
@ManagedResource(description = "Can be used to list and reset the reverse proxy routes")
|
||||
public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements
|
||||
ApplicationListener<InstanceRegisteredEvent>, MvcEndpoint {
|
||||
ApplicationListener<ApplicationEvent> {
|
||||
|
||||
private ProxyRouteLocator routeLocator;
|
||||
|
||||
@@ -38,8 +35,11 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onApplicationEvent(InstanceRegisteredEvent event) {
|
||||
registerHandlers(routeLocator.getRoutePaths());
|
||||
public void onApplicationEvent(ApplicationEvent event) {
|
||||
if (event instanceof InstanceRegisteredEvent
|
||||
|| event instanceof RefreshScopeRefreshedEvent) {
|
||||
reset();
|
||||
}
|
||||
}
|
||||
|
||||
protected void registerHandlers(Collection<String> routes) {
|
||||
@@ -53,8 +53,6 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements
|
||||
}
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.POST)
|
||||
@ResponseBody
|
||||
@ManagedOperation
|
||||
public Map<String, String> reset() {
|
||||
routeLocator.resetRoutes();
|
||||
@@ -62,26 +60,9 @@ public class ZuulHandlerMapping extends AbstractUrlHandlerMapping implements
|
||||
return getRoutes();
|
||||
}
|
||||
|
||||
@RequestMapping(method = RequestMethod.GET)
|
||||
@ResponseBody
|
||||
@ManagedAttribute
|
||||
public Map<String, String> 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:
|
||||
eureka:
|
||||
instance:
|
||||
app-group-name: mysidecargroup
|
||||
client:
|
||||
serviceUrl:
|
||||
defaultZone: http://user:password@localhost:8761/eureka/
|
||||
|
||||
endpoints:
|
||||
refresh:
|
||||
|
||||
Reference in New Issue
Block a user