Commit 29383de8 by Dave Syer

Allow zuul.ignoredServices to be a pattern

and also allow explicitly configured services to be unignored. I.e. zuul: ignoredServices: * routes: foo: /foo/** Will expose only the foo service. Fixes gh-198
parent b8cf89fb
...@@ -708,7 +708,22 @@ failures will show up in Hystrix metrics, and once the circuit is open ...@@ -708,7 +708,22 @@ failures will show up in Hystrix metrics, and once the circuit is open
the proxy will not try to contact the service. the proxy will not try to contact the service.
To skip having a service automatically added, set To skip having a service automatically added, set
`zuul.ignored-services` to a list of service ids. To augment or change `zuul.ignored-services` to a list of service id patterns. If a service
matches a pattern that is ignored, but also included in the explicitly
configured routes map, then it will be unignored. Example:
.application.yml
[source,yaml]
----
zuul:
ignoredServices: *
routes:
users: /myusers/**
----
In this example, all services are ignored *except* "users".
To augment or change
the proxy routes, you can add external configuration like the the proxy routes, you can add external configuration like the
following: following:
......
...@@ -31,6 +31,7 @@ import org.springframework.cloud.client.discovery.DiscoveryClient; ...@@ -31,6 +31,7 @@ import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute; import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
import org.springframework.util.AntPathMatcher; import org.springframework.util.AntPathMatcher;
import org.springframework.util.PathMatcher; import org.springframework.util.PathMatcher;
import org.springframework.util.PatternMatchUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
...@@ -107,7 +108,7 @@ public class ProxyRouteLocator implements RouteLocator { ...@@ -107,7 +108,7 @@ public class ProxyRouteLocator implements RouteLocator {
prefix = prefix + routePrefix; prefix = prefix + routePrefix;
} }
} }
if(route.getRetryable() != null) { if (route.getRetryable() != null) {
retryable = route.getRetryable(); retryable = route.getRetryable();
} }
break; break;
...@@ -122,19 +123,38 @@ public class ProxyRouteLocator implements RouteLocator { ...@@ -122,19 +123,38 @@ public class ProxyRouteLocator implements RouteLocator {
} }
protected LinkedHashMap<String, ZuulRoute> locateRoutes() { protected LinkedHashMap<String, ZuulRoute> locateRoutes() {
LinkedHashMap<String, ZuulRoute> routesMap = new LinkedHashMap<>(); LinkedHashMap<String, ZuulRoute> routesMap = new LinkedHashMap<String, ZuulRoute>();
addConfiguredRoutes(routesMap); addConfiguredRoutes(routesMap);
routesMap.putAll(this.staticRoutes); routesMap.putAll(this.staticRoutes);
if (this.discovery != null) { if (this.discovery != null) {
Map<String, ZuulRoute> staticServices = new LinkedHashMap<String, ZuulRoute>();
for (ZuulRoute route : routesMap.values()) {
String serviceId = route.getServiceId();
if (serviceId == null) {
serviceId = route.getId();
}
if (serviceId != null) {
staticServices.put(serviceId, route);
}
}
// Add routes for discovery services by default // Add routes for discovery services by default
List<String> services = this.discovery.getServices(); List<String> services = this.discovery.getServices();
String[] ignored = this.properties.getIgnoredServices()
.toArray(new String[0]);
for (String serviceId : services) { for (String serviceId : services) {
// Ignore specifically ignored services and those that were manually // Ignore specifically ignored services and those that were manually
// configured // configured
String key = "/" + serviceId + "/**"; String key = "/" + serviceId + "/**";
if (!this.properties.getIgnoredServices().contains(serviceId) ZuulRoute route = new ZuulRoute(key, serviceId);
if (staticServices.containsKey(serviceId)
&& staticServices.get(serviceId).getUrl() == null) {
// Explicitly configured with no URL, cannot be ignored
routesMap.put(key, route);
}
if (!PatternMatchUtils.simpleMatch(ignored, serviceId)
&& !routesMap.containsKey(key)) { && !routesMap.containsKey(key)) {
routesMap.put(key, new ZuulRoute(key, serviceId)); // Not ignored
routesMap.put(key, route);
} }
} }
} }
...@@ -167,8 +187,8 @@ public class ProxyRouteLocator implements RouteLocator { ...@@ -167,8 +187,8 @@ public class ProxyRouteLocator implements RouteLocator {
for (ZuulRoute entry : routeEntries.values()) { for (ZuulRoute entry : routeEntries.values()) {
String route = entry.getPath(); String route = entry.getPath();
if (routes.containsKey(route)) { if (routes.containsKey(route)) {
log.warn("Overwriting route "+route+": already defined by " + log.warn("Overwriting route " + route + ": already defined by "
routes.get(route)); + routes.get(route));
} }
routes.put(route, entry); routes.put(route, entry);
} }
...@@ -191,7 +211,7 @@ public class ProxyRouteLocator implements RouteLocator { ...@@ -191,7 +211,7 @@ public class ProxyRouteLocator implements RouteLocator {
private String location; private String location;
private String prefix; private String prefix;
private Boolean retryable; private Boolean retryable;
} }
......
...@@ -42,7 +42,7 @@ public class ZuulProperties { ...@@ -42,7 +42,7 @@ public class ZuulProperties {
private String prefix = ""; private String prefix = "";
private boolean stripPrefix = true; private boolean stripPrefix = true;
private Boolean retryable; private Boolean retryable;
private Map<String, ZuulRoute> routes = new LinkedHashMap<String, ZuulRoute>(); private Map<String, ZuulRoute> routes = new LinkedHashMap<String, ZuulRoute>();
......
...@@ -210,6 +210,55 @@ public class ProxyRouteLocatorTests { ...@@ -210,6 +210,55 @@ public class ProxyRouteLocatorTests {
} }
@Test @Test
public void testIgnoreRoutesWithPattern() {
ProxyRouteLocator routeLocator = new ProxyRouteLocator(this.discovery,
this.properties);
this.properties.setIgnoredServices(Collections.singletonList("ignore*"));
given(this.discovery.getServices()).willReturn(
Collections.singletonList(IGNOREDSERVICE));
Map<String, String> routesMap = routeLocator.getRoutes();
String serviceId = routesMap.get(getMapping(IGNOREDSERVICE));
assertNull("routes did not ignore " + IGNOREDSERVICE, serviceId);
}
@Test
public void testIgnoreAllRoutes() {
ProxyRouteLocator routeLocator = new ProxyRouteLocator(this.discovery,
this.properties);
this.properties.setIgnoredServices(Collections.singletonList("*"));
given(this.discovery.getServices()).willReturn(
Collections.singletonList(IGNOREDSERVICE));
Map<String, String> routesMap = routeLocator.getRoutes();
String serviceId = routesMap.get(getMapping(IGNOREDSERVICE));
assertNull("routes did not ignore " + IGNOREDSERVICE, serviceId);
}
@Test
public void testIgnoredRouteIncludedIfConfiguredAndDiscovered() {
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**"));
ProxyRouteLocator routeLocator = new ProxyRouteLocator(this.discovery,
this.properties);
this.properties.setIgnoredServices(Collections.singletonList("*"));
given(this.discovery.getServices()).willReturn(Collections.singletonList("foo"));
Map<String, String> routesMap = routeLocator.getRoutes();
String serviceId = routesMap.get(getMapping("foo"));
assertNotNull("routes ignored foo", serviceId);
}
@Test
public void testIgnoredRouteIncludedIfConfiguredAndNotDiscovered() {
this.properties.getRoutes()
.put("foo", new ZuulRoute("/foo/**", "http://foo.com"));
ProxyRouteLocator routeLocator = new ProxyRouteLocator(this.discovery,
this.properties);
this.properties.setIgnoredServices(Collections.singletonList("*"));
given(this.discovery.getServices()).willReturn(Collections.singletonList("bar"));
Map<String, String> routesMap = routeLocator.getRoutes();
String id = routesMap.get(getMapping("foo"));
assertNotNull("routes ignored foo", id);
}
@Test
public void testAutoRoutes() { public void testAutoRoutes() {
ProxyRouteLocator routeLocator = new ProxyRouteLocator(this.discovery, ProxyRouteLocator routeLocator = new ProxyRouteLocator(this.discovery,
this.properties); this.properties);
......
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