Use RouteLocator interface.

fixes gh-1030
parent c3f12106
...@@ -76,7 +76,7 @@ public class ZuulConfiguration { ...@@ -76,7 +76,7 @@ public class ZuulConfiguration {
} }
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean(RouteLocator.class)
public RouteLocator routeLocator() { public RouteLocator routeLocator() {
return new SimpleRouteLocator(this.server.getServletPrefix(), return new SimpleRouteLocator(this.server.getServletPrefix(),
this.zuulProperties); this.zuulProperties);
......
...@@ -135,7 +135,7 @@ public class ZuulProxyConfiguration extends ZuulConfiguration { ...@@ -135,7 +135,7 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
protected static class RoutesEndpointConfiguration { protected static class RoutesEndpointConfiguration {
@Bean @Bean
public RoutesEndpoint zuulEndpoint(DiscoveryClientRouteLocator routeLocator) { public RoutesEndpoint zuulEndpoint(RouteLocator routeLocator) {
return new RoutesEndpoint(routeLocator); return new RoutesEndpoint(routeLocator);
} }
......
...@@ -24,15 +24,20 @@ import javax.servlet.http.HttpServletRequest; ...@@ -24,15 +24,20 @@ import javax.servlet.http.HttpServletRequest;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication; import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.IntegrationTest; import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration; import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate; import org.springframework.boot.test.TestRestTemplate;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.netflix.ribbon.RibbonClient; import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients; import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.ribbon.StaticServerList; import org.springframework.cloud.netflix.ribbon.StaticServerList;
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
import org.springframework.cloud.netflix.zuul.filters.route.RestClientRibbonCommand; import org.springframework.cloud.netflix.zuul.filters.route.RestClientRibbonCommand;
import org.springframework.cloud.netflix.zuul.filters.route.RestClientRibbonCommandFactory; import org.springframework.cloud.netflix.zuul.filters.route.RestClientRibbonCommandFactory;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext;
...@@ -82,6 +87,9 @@ import lombok.SneakyThrows; ...@@ -82,6 +87,9 @@ import lombok.SneakyThrows;
@DirtiesContext @DirtiesContext
public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase { public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase {
@Autowired
RouteLocator routeLocator;
@Test @Test
public void simpleHostRouteWithTrailingSlash() { public void simpleHostRouteWithTrailingSlash() {
this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/"); this.routes.addRoute("/self/**", "http://localhost:" + this.port + "/");
...@@ -191,6 +199,12 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase { ...@@ -191,6 +199,12 @@ public class SampleZuulProxyApplicationTests extends ZuulProxyTestBase {
this.ribbonCommandFactory instanceof SampleZuulProxyApplication.MyRibbonCommandFactory); this.ribbonCommandFactory instanceof SampleZuulProxyApplication.MyRibbonCommandFactory);
} }
@Test
public void routeLocatorOverridden() {
assertTrue("routeLocator not a MyRouteLocator",
this.routeLocator instanceof SampleZuulProxyApplication.MyRouteLocator);
}
} }
// Don't use @SpringBootApplication because we don't want to component scan // Don't use @SpringBootApplication because we don't want to component scan
...@@ -238,6 +252,11 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl ...@@ -238,6 +252,11 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl
return new MyRibbonCommandFactory(clientFactory); return new MyRibbonCommandFactory(clientFactory);
} }
@Bean
public RouteLocator routeLocator(DiscoveryClient discoveryClient, ZuulProperties zuulProperties) {
return new MyRouteLocator("/", discoveryClient, zuulProperties);
}
public static void main(String[] args) { public static void main(String[] args) {
SpringApplication.run(SampleZuulProxyApplication.class, args); SpringApplication.run(SampleZuulProxyApplication.class, args);
} }
...@@ -299,4 +318,11 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl ...@@ -299,4 +318,11 @@ class SampleZuulProxyApplication extends ZuulProxyTestBase.AbstractZuulProxyAppl
} }
} }
static class MyRouteLocator extends DiscoveryClientRouteLocator {
public MyRouteLocator(String servletPath, DiscoveryClient discovery, ZuulProperties properties) {
super(servletPath, discovery, 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