Commit ef67fa32 by Spencer Gibb

Add local service to zuul ignoredServices by default.

fixes gh-447
parent 8058ced9
......@@ -27,6 +27,7 @@ import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.extern.apachecommons.CommonsLog;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
import org.springframework.util.AntPathMatcher;
......@@ -62,7 +63,17 @@ public class ProxyRouteLocator implements RouteLocator {
//set Zuul servlet path
this.servletPath = properties.getServletPath() != null? properties.getServletPath() : "";
}
if (properties.isIgnoreLocalService()) {
ServiceInstance instance = discovery.getLocalServiceInstance();
if (instance != null) {
String localServiceId = instance.getServiceId();
if (!properties.getIgnoredServices().contains(localServiceId)) {
properties.getIgnoredServices().add(localServiceId);
}
}
}
this.discovery = discovery;
this.properties = properties;
}
......
......@@ -55,6 +55,8 @@ public class ZuulProperties {
private String servletPath = "/zuul";
private boolean ignoreLocalService = true;
@PostConstruct
public void init() {
for (Entry<String, ZuulRoute> entry : this.routes.entrySet()) {
......
......@@ -16,6 +16,14 @@
package org.springframework.cloud.netflix.zuul.filters;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.MockitoAnnotations.initMocks;
import java.util.Collections;
import java.util.LinkedHashMap;
import java.util.Map;
......@@ -23,19 +31,12 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.springframework.cloud.client.DefaultServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.zuul.filters.ProxyRouteLocator.ProxyRouteSpec;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
import org.springframework.core.env.ConfigurableEnvironment;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.BDDMockito.given;
import static org.mockito.MockitoAnnotations.initMocks;
/**
* @author Spencer Gibb
* @author Dave Syer
......@@ -485,6 +486,39 @@ public class ProxyRouteLocatorTests {
assertMapping(routesMap, "http://example.com/" + MYSERVICE, MYSERVICE);
}
@Test
public void testIgnoredLocalServiceByDefault() {
given(this.discovery.getServices()).willReturn(Collections.singletonList(MYSERVICE));
given(this.discovery.getLocalServiceInstance()).willReturn(new DefaultServiceInstance(MYSERVICE, "localhost", 80, false));
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
this.properties);
LinkedHashMap<String, ZuulRoute> routes = routeLocator.locateRoutes();
ZuulRoute actual = routes.get("/**");
assertNull("routes didn't ignore "+MYSERVICE, actual);
Map<String, String> routesMap = routeLocator.getRoutes();
assertNotNull("routesMap was null", routesMap);
assertTrue("routesMap was empty", routesMap.isEmpty());
}
@Test
public void testIgnoredLocalServiceFalse() {
this.properties.setIgnoreLocalService(false);
given(this.discovery.getServices()).willReturn(Collections.singletonList(MYSERVICE));
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
this.properties);
Map<String, String> routesMap = routeLocator.getRoutes();
assertNotNull("routesMap was null", routesMap);
assertFalse("routesMap was empty", routesMap.isEmpty());
assertMapping(routesMap, MYSERVICE);
}
protected void assertMapping(Map<String, String> routesMap, String serviceId) {
assertMapping(routesMap, serviceId, serviceId);
}
......
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