Move discovery related route locator stuff into a new package
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
:github-repo: spring-cloud/spring-cloud-netflix
|
||||
:github-raw: http://raw.github.com/{github-repo}/{github-tag}
|
||||
:github-code: http://github.com/{github-repo}/tree/{github-tag}
|
||||
:all: {asterisk}{asterisk}
|
||||
= Spring Cloud Netflix
|
||||
|
||||
include::intro.adoc[]
|
||||
@@ -1055,7 +1056,7 @@ and the serviceId independently:
|
||||
This means that http calls to "/myusers" get forwarded to the
|
||||
"users_service" service. The route has to have a "path" which can be
|
||||
specified as an ant-style pattern, so "/myusers/\*" only matches one
|
||||
level, but "/myusers/**" matches hierarchically.
|
||||
level, but "/myusers/{all}" matches hierarchically.
|
||||
|
||||
The location of the backend can be specified as either a "serviceId"
|
||||
(for a Eureka service) or a "url" (for a physical location), e.g.
|
||||
@@ -1093,25 +1094,28 @@ users:
|
||||
listOfServers: example.com,google.com
|
||||
----
|
||||
|
||||
You can provide convention between serviceId and routes using regexmapper.
|
||||
It uses regular expression named group to extract variables from serviceId and inject them
|
||||
into a route pattern.
|
||||
You can provide convention between serviceId and routes using
|
||||
regexmapper. It uses regular expression named group to extract
|
||||
variables from serviceId and inject them into a route pattern.
|
||||
|
||||
.application.yml
|
||||
[source,yaml]
|
||||
.ApplicationConfiguration.java
|
||||
[source,java]
|
||||
----
|
||||
zuul:
|
||||
regexMapper:
|
||||
enabled: true
|
||||
servicePattern: "(?<name>^.+)-(?<version>v.+$)"
|
||||
routePattern: "${version}/${name}"
|
||||
@Bean
|
||||
public PatternServiceRouteMapper serviceRouteMapper() {
|
||||
retuen new PatternServiceRouteMapper(
|
||||
"(?<name>^.+)-(?<version>v.+$)",
|
||||
"${version}/${name}");
|
||||
}
|
||||
----
|
||||
|
||||
This means that a serviceId "myusers-v1" will be mapped to route "/v1/myusers/**".
|
||||
Any regular expression is accepted but all named group must be present in both servicePattern and routePattern.
|
||||
If servicePattern do not match a serviceId, the default behavior is used. In exemple above,
|
||||
a serviceId "myusers" will be mapped to route "/myusers/**" (no version detected)
|
||||
These feature is disable by default and is only applied to discovered services.
|
||||
This means that a serviceId "myusers-v1" will be mapped to route
|
||||
"/v1/myusers/{all}". Any regular expression is accepted but all named
|
||||
group must be present in both servicePattern and routePattern. If
|
||||
servicePattern do not match a serviceId, the default behavior is
|
||||
used. In exemple above, a serviceId "myusers" will be mapped to route
|
||||
"/myusers/{all}" (no version detected) These feature is disable by
|
||||
default and is only applied to discovered services.
|
||||
|
||||
To add a prefix to all mappings, set `zuul.prefix` to a value, such as
|
||||
`/api`. The proxy prefix is stripped from the request before the
|
||||
@@ -1145,7 +1149,7 @@ above).
|
||||
|
||||
An application with the `@EnableZuulProxy` could act as a standalone
|
||||
server if you set a default route ("/"), for example `zuul.route.home:
|
||||
/` would route all traffic (i.e. "/**") to the "home" service.
|
||||
/` would route all traffic (i.e. "/{all}") to the "home" service.
|
||||
|
||||
If more fine-grained ignoring is needed, you can specify specific patterns to ignore.
|
||||
These patterns are being evaluated at the start of the route location process, which
|
||||
@@ -1195,10 +1199,10 @@ Example configuration:
|
||||
|
||||
In this example we are strangling the "legacy" app which is mapped to
|
||||
all requests that do not match one of the other patterns. Paths in
|
||||
`/first/**` have been extracted into a new service with an external
|
||||
URL. And paths in `/second/**` are forwared so they can be handled
|
||||
`/first/{all}` have been extracted into a new service with an external
|
||||
URL. And paths in `/second/{all}` are forwared so they can be handled
|
||||
locally, e.g. with a normal Spring `@RequestMapping`. Paths in
|
||||
`/third/**` are also forwarded, but with a different prefix
|
||||
`/third/{all}` are also forwarded, but with a different prefix
|
||||
(i.e. `/third/foo` is forwarded to `/3rd/foo`).
|
||||
|
||||
NOTE: The ignored pattterns aren't completely ignored, they just
|
||||
@@ -1211,8 +1215,8 @@ If you `@EnableZuulProxy` you can use the proxy paths to
|
||||
upload files and it should just work as long as the files
|
||||
are small. For large files there is an alternative path
|
||||
which bypasses the Spring `DispatcherServlet` (to
|
||||
avoid multipart processing) in "/zuul/*". I.e. if
|
||||
`zuul.routes.customers=/customers/**` then you can
|
||||
avoid multipart processing) in "/zuul/{asterisk}". I.e. if
|
||||
`zuul.routes.customers=/customers/{all}` then you can
|
||||
POST large files to "/zuul/customers/*". The servlet
|
||||
path is externalized via `zuul.servletPath`. Extremely
|
||||
large files will also require elevated timeout settings
|
||||
@@ -1243,9 +1247,10 @@ use `@EnableZuulServer` (instead of `@EnableZuulProxy`). Any beans that you add
|
||||
will be installed automatically, as they are with `@EnableZuulProxy`, but without any of the proxy filters being added
|
||||
automatically.
|
||||
|
||||
In this case the routes into the Zuul server are
|
||||
still specified by configuring "zuul.routes.*", but there is no service discovery and no proxying, so the
|
||||
"serviceId" and "url" settings are ignored. For example:
|
||||
In this case the routes into the Zuul server are still specified by
|
||||
configuring "zuul.routes.{asterisk}", but there is no service
|
||||
discovery and no proxying, so the "serviceId" and "url" settings are
|
||||
ignored. For example:
|
||||
|
||||
.application.yml
|
||||
[source,yaml]
|
||||
@@ -1255,7 +1260,7 @@ still specified by configuring "zuul.routes.*", but there is no service discover
|
||||
api: /api/**
|
||||
----
|
||||
|
||||
maps all paths in "/api/**" to the Zuul filter chain.
|
||||
maps all paths in "/api/{all}" to the Zuul filter chain.
|
||||
|
||||
=== Disable Zuul Filters
|
||||
|
||||
|
||||
@@ -21,7 +21,6 @@ import org.springframework.boot.actuate.endpoint.Endpoint;
|
||||
import org.springframework.boot.actuate.trace.TraceRepository;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
|
||||
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
|
||||
import org.springframework.boot.autoconfigure.web.ServerProperties;
|
||||
import org.springframework.cloud.client.actuator.HasFeatures;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
@@ -31,13 +30,12 @@ import org.springframework.cloud.client.discovery.event.InstanceRegisteredEvent;
|
||||
import org.springframework.cloud.client.discovery.event.ParentHeartbeatEvent;
|
||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRequestHelper;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ServiceRouteMapper;
|
||||
import org.springframework.cloud.netflix.zuul.filters.SimpleServiceRouteMapper;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
|
||||
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.discovery.ServiceRouteMapper;
|
||||
import org.springframework.cloud.netflix.zuul.filters.discovery.SimpleServiceRouteMapper;
|
||||
import org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter;
|
||||
import org.springframework.cloud.netflix.zuul.filters.regex.RegExServiceRouteMapper;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.RestClientRibbonCommandFactory;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter;
|
||||
@@ -81,9 +79,9 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
|
||||
@Bean
|
||||
@Override
|
||||
@ConditionalOnMissingBean(RouteLocator.class)
|
||||
public ProxyRouteLocator routeLocator() {
|
||||
return new ProxyRouteLocator(this.server.getServletPrefix(), this.discovery,
|
||||
this.zuulProperties, this.serviceRouteMapper);
|
||||
public DiscoveryClientRouteLocator routeLocator() {
|
||||
return new DiscoveryClientRouteLocator(this.server.getServletPrefix(),
|
||||
this.discovery, this.zuulProperties, this.serviceRouteMapper);
|
||||
}
|
||||
|
||||
@Bean
|
||||
@@ -126,24 +124,10 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
|
||||
return new ZuulDiscoveryRefreshListener();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@ConditionalOnProperty(name = "zuul.regexMapper.enabled", matchIfMissing = false)
|
||||
protected static class RegexServiceRouteMapperConfiguration {
|
||||
|
||||
@Bean
|
||||
public ServiceRouteMapper serviceRouteMapper(ZuulProperties props) {
|
||||
return new RegExServiceRouteMapper(props.getRegexMapper().getServicePattern(),
|
||||
props.getRegexMapper().getRoutePattern());
|
||||
}
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Bean
|
||||
@ConditionalOnMissingBean(ServiceRouteMapper.class)
|
||||
protected static class SimpleServiceRouteMapperConfiguration {
|
||||
@Bean
|
||||
public ServiceRouteMapper serviceRouteMapper() {
|
||||
return new SimpleServiceRouteMapper();
|
||||
}
|
||||
public ServiceRouteMapper serviceRouteMapper() {
|
||||
return new SimpleServiceRouteMapper();
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@@ -151,7 +135,7 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
|
||||
protected static class RoutesEndpointConfiguration {
|
||||
|
||||
@Bean
|
||||
public RoutesEndpoint zuulEndpoint(ProxyRouteLocator routeLocator) {
|
||||
public RoutesEndpoint zuulEndpoint(DiscoveryClientRouteLocator routeLocator) {
|
||||
return new RoutesEndpoint(routeLocator);
|
||||
}
|
||||
|
||||
|
||||
@@ -14,9 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.netflix.zuul;
|
||||
|
||||
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
|
||||
package org.springframework.cloud.netflix.zuul.filters;
|
||||
|
||||
/**
|
||||
* Interface for a route locator that can be refreshed if routes change.
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.netflix.zuul.filters;
|
||||
package org.springframework.cloud.netflix.zuul.filters.discovery;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -25,7 +25,10 @@ import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.springframework.cloud.client.ServiceInstance;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.netflix.zuul.RefreshableRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.Route;
|
||||
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.util.PathMatcher;
|
||||
@@ -35,10 +38,14 @@ import org.springframework.util.StringUtils;
|
||||
import lombok.extern.apachecommons.CommonsLog;
|
||||
|
||||
/**
|
||||
* A {@link RouteLocator} that combines static, configured routes with those from a
|
||||
* {@link DiscoveryClient}. The discovery client takes precedence.
|
||||
*
|
||||
* @author Spencer Gibb
|
||||
* @author Dave Syer
|
||||
*/
|
||||
@CommonsLog
|
||||
public class ProxyRouteLocator implements RefreshableRouteLocator {
|
||||
public class DiscoveryClientRouteLocator implements RefreshableRouteLocator {
|
||||
|
||||
public static final String DEFAULT_ROUTE = "/**";
|
||||
|
||||
@@ -56,7 +63,7 @@ public class ProxyRouteLocator implements RefreshableRouteLocator {
|
||||
|
||||
private ServiceRouteMapper serviceRouteMapper;
|
||||
|
||||
public ProxyRouteLocator(String servletPath, DiscoveryClient discovery,
|
||||
public DiscoveryClientRouteLocator(String servletPath, DiscoveryClient discovery,
|
||||
ZuulProperties properties) {
|
||||
if (StringUtils.hasText(servletPath)) { // a servletPath is passed explicitly
|
||||
this.servletPath = servletPath;
|
||||
@@ -81,7 +88,7 @@ public class ProxyRouteLocator implements RefreshableRouteLocator {
|
||||
this.properties = properties;
|
||||
}
|
||||
|
||||
public ProxyRouteLocator(String servletPath, DiscoveryClient discovery,
|
||||
public DiscoveryClientRouteLocator(String servletPath, DiscoveryClient discovery,
|
||||
ZuulProperties properties, ServiceRouteMapper serviceRouteMapper) {
|
||||
this(servletPath, discovery, properties);
|
||||
this.serviceRouteMapper = serviceRouteMapper;
|
||||
@@ -1,9 +1,8 @@
|
||||
package org.springframework.cloud.netflix.zuul.filters.regex;
|
||||
package org.springframework.cloud.netflix.zuul.filters.discovery;
|
||||
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import org.springframework.cloud.netflix.zuul.filters.ServiceRouteMapper;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
/**
|
||||
@@ -12,12 +11,15 @@ import org.springframework.util.StringUtils;
|
||||
* This service route mapper use Java 7 RegEx named group feature to rewrite a discovered
|
||||
* service Id into a route.
|
||||
*
|
||||
* Ex : If we want to map service Id [rest-service-v1] to /v1/rest-service/** route
|
||||
* service pattern : "(?<name>.*)-(?<version>v.*$)" route pattern : "${version}/${name}"
|
||||
* Ex : If we want to map service Id <code>[rest-service-v1]</code> to
|
||||
* <code>/v1/rest-service/**</code> route service pattern :
|
||||
* <code>"(?<name>.*)-(?<version>v.*$)"</code> route pattern :
|
||||
* <code>"${version}/${name}"</code>
|
||||
*
|
||||
* /!\ This implementation use Matcher.replaceFirst so only one match will be replace.
|
||||
* This implementation uses <code>Matcher.replaceFirst</code> so only one match will be
|
||||
* replaced.
|
||||
*/
|
||||
public class RegExServiceRouteMapper implements ServiceRouteMapper {
|
||||
public class PatternServiceRouteMapper implements ServiceRouteMapper {
|
||||
|
||||
/**
|
||||
* A RegExp Pattern that extract needed information from a service ID. Ex :
|
||||
@@ -30,7 +32,7 @@ public class RegExServiceRouteMapper implements ServiceRouteMapper {
|
||||
*/
|
||||
private String routePattern;
|
||||
|
||||
public RegExServiceRouteMapper(String servicePattern, String routePattern) {
|
||||
public PatternServiceRouteMapper(String servicePattern, String routePattern) {
|
||||
this.servicePattern = Pattern.compile(servicePattern);
|
||||
this.routePattern = routePattern;
|
||||
}
|
||||
@@ -45,8 +47,8 @@ public class RegExServiceRouteMapper implements ServiceRouteMapper {
|
||||
*/
|
||||
@Override
|
||||
public String apply(String serviceId) {
|
||||
Matcher matcher = servicePattern.matcher(serviceId);
|
||||
String route = matcher.replaceFirst(routePattern);
|
||||
Matcher matcher = this.servicePattern.matcher(serviceId);
|
||||
String route = matcher.replaceFirst(this.routePattern);
|
||||
route = cleanRoute(route);
|
||||
return (StringUtils.hasText(route) ? route : serviceId);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.cloud.netflix.zuul.filters;
|
||||
package org.springframework.cloud.netflix.zuul.filters.discovery;
|
||||
|
||||
/**
|
||||
* Provide a way to apply convention between routes and discovered services name.
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.cloud.netflix.zuul.filters;
|
||||
package org.springframework.cloud.netflix.zuul.filters.discovery;
|
||||
|
||||
/**
|
||||
* @author Stéphane Leroy
|
||||
@@ -21,7 +21,7 @@ import java.util.Collection;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import org.springframework.boot.autoconfigure.web.ErrorController;
|
||||
import org.springframework.cloud.netflix.zuul.RefreshableRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.RefreshableRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.RouteLocator;
|
||||
import org.springframework.util.PatternMatchUtils;
|
||||
import org.springframework.web.servlet.handler.AbstractUrlHandlerMapping;
|
||||
|
||||
@@ -25,8 +25,8 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.IntegrationTest;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.TestRestTemplate;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
|
||||
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -53,7 +53,7 @@ public class ContextPathZuulProxyApplicationTests {
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private ProxyRouteLocator routes;
|
||||
private DiscoveryClientRouteLocator routes;
|
||||
|
||||
@Autowired
|
||||
private RoutesEndpoint endpoint;
|
||||
|
||||
@@ -13,7 +13,7 @@ import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.TestRestTemplate;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClient;
|
||||
import org.springframework.cloud.netflix.ribbon.StaticServerList;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpEntity;
|
||||
@@ -51,7 +51,7 @@ public class RetryableZuulProxyApplicationTests {
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private ProxyRouteLocator routes;
|
||||
private DiscoveryClientRouteLocator routes;
|
||||
|
||||
@Autowired
|
||||
private RoutesEndpoint endpoint;
|
||||
|
||||
@@ -33,8 +33,8 @@ import org.springframework.boot.test.TestRestTemplate;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClient;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClients;
|
||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRouteLocator;
|
||||
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.RibbonCommandFactory;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.apache.HttpClientRibbonCommandFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -73,7 +73,7 @@ public class SampleZuulProxyAppTestsWithHttpClient {
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private ProxyRouteLocator routes;
|
||||
private DiscoveryClientRouteLocator routes;
|
||||
|
||||
@Autowired
|
||||
private RoutesEndpoint endpoint;
|
||||
|
||||
@@ -35,8 +35,8 @@ import org.springframework.cloud.netflix.ribbon.RibbonClient;
|
||||
import org.springframework.cloud.netflix.ribbon.RibbonClients;
|
||||
import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
|
||||
import org.springframework.cloud.netflix.ribbon.StaticServerList;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
|
||||
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.RestClientRibbonCommandFactory;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -77,7 +77,7 @@ public class SampleZuulProxyApplicationTests {
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private ProxyRouteLocator routes;
|
||||
private DiscoveryClientRouteLocator routes;
|
||||
|
||||
@Autowired
|
||||
private RoutesEndpoint endpoint;
|
||||
|
||||
@@ -25,8 +25,8 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.test.IntegrationTest;
|
||||
import org.springframework.boot.test.SpringApplicationConfiguration;
|
||||
import org.springframework.boot.test.TestRestTemplate;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
|
||||
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpMethod;
|
||||
@@ -53,7 +53,7 @@ public class ServletPathZuulProxyApplicationTests {
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private ProxyRouteLocator routes;
|
||||
private DiscoveryClientRouteLocator routes;
|
||||
|
||||
@Autowired
|
||||
private RoutesEndpoint endpoint;
|
||||
|
||||
@@ -17,6 +17,7 @@ import org.springframework.boot.test.TestRestTemplate;
|
||||
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
|
||||
import org.springframework.cloud.netflix.zuul.RoutesEndpoint;
|
||||
import org.springframework.cloud.netflix.zuul.ZuulProxyConfiguration;
|
||||
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
@@ -54,7 +55,7 @@ public class CustomHostRoutingFilterTests {
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private ProxyRouteLocator routes;
|
||||
private DiscoveryClientRouteLocator routes;
|
||||
|
||||
@Autowired
|
||||
private RoutesEndpoint endpoint;
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package org.springframework.cloud.netflix.zuul.filters;
|
||||
package org.springframework.cloud.netflix.zuul.filters.discovery;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedHashMap;
|
||||
@@ -25,8 +25,11 @@ 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.Route;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
|
||||
import org.springframework.cloud.netflix.zuul.filters.regex.RegExServiceRouteMapper;
|
||||
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapper;
|
||||
import org.springframework.core.env.ConfigurableEnvironment;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
@@ -41,7 +44,7 @@ import static org.mockito.MockitoAnnotations.initMocks;
|
||||
* @author Spencer Gibb
|
||||
* @author Dave Syer
|
||||
*/
|
||||
public class ProxyRouteLocatorTests {
|
||||
public class DiscoveryClientRouteLocatorTests {
|
||||
|
||||
public static final String IGNOREDSERVICE = "ignoredservice";
|
||||
|
||||
@@ -66,7 +69,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPath() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**"));
|
||||
this.properties.init();
|
||||
@@ -78,7 +81,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithPrefix() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**"));
|
||||
this.properties.setPrefix("/proxy");
|
||||
@@ -91,7 +94,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithServletPath() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/app", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/app", this.discovery,
|
||||
this.properties);
|
||||
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**"));
|
||||
this.properties.init();
|
||||
@@ -103,7 +106,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithNoPrefixStripping() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.getRoutes().put("foo",
|
||||
new ZuulRoute("foo", "/foo/**", "foo", null, false, null));
|
||||
@@ -117,7 +120,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithLocalPrefixStripping() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**", "foo"));
|
||||
this.properties.setStripPrefix(false);
|
||||
@@ -130,7 +133,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithGlobalPrefixStripping() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.getRoutes().put("foo",
|
||||
new ZuulRoute("foo", "/foo/**", "foo", null, false, null));
|
||||
@@ -143,7 +146,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithRoutePrefixStripping() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
ZuulRoute zuulRoute = new ZuulRoute("/foo/**");
|
||||
zuulRoute.setStripPrefix(true);
|
||||
@@ -157,7 +160,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithoutMatchingIgnoredPattern() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredPatterns(Collections.singletonList(IGNOREDPATTERN));
|
||||
this.properties.getRoutes().put("bar", new ZuulRoute("/bar/**"));
|
||||
@@ -170,7 +173,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithMatchingIgnoredPattern() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredPatterns(Collections.singletonList(IGNOREDPATTERN));
|
||||
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**"));
|
||||
@@ -182,7 +185,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithMatchingIgnoredPatternWithPrefix() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredPatterns(Collections.singletonList(IGNOREDPATTERN));
|
||||
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**"));
|
||||
@@ -196,7 +199,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithMatchingIgnoredPatternWithServletPath() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/app", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/app", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredPatterns(Collections.singletonList(IGNOREDPATTERN));
|
||||
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**"));
|
||||
@@ -208,7 +211,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithoutMatchingIgnoredPatternWithNoPrefixStripping() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredPatterns(Collections.singletonList(IGNOREDPATTERN));
|
||||
this.properties.getRoutes().put("foo",
|
||||
@@ -223,7 +226,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithMatchingIgnoredPatternWithNoPrefixStripping() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredPatterns(Collections.singletonList("/proxy" + IGNOREDPATTERN));
|
||||
this.properties.getRoutes().put("foo",
|
||||
@@ -237,7 +240,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithoutMatchingIgnoredPatternWithLocalPrefixStripping() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredPatterns(Collections.singletonList(IGNOREDPATTERN));
|
||||
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**", "foo"));
|
||||
@@ -251,7 +254,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithMatchingIgnoredPatternWithLocalPrefixStripping() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredPatterns(Collections.singletonList("/proxy" + IGNOREDPATTERN));
|
||||
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**", "foo"));
|
||||
@@ -264,7 +267,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithoutMatchingIgnoredPatternWithGlobalPrefixStripping() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredPatterns(Collections.singletonList(IGNOREDPATTERN));
|
||||
this.properties.getRoutes().put("foo",
|
||||
@@ -278,7 +281,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithMatchingIgnoredPatternWithGlobalPrefixStripping() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredPatterns(Collections.singletonList("/proxy" + IGNOREDPATTERN));
|
||||
this.properties.getRoutes().put("foo",
|
||||
@@ -291,7 +294,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetMatchingPathWithMatchingIgnoredPatternWithRoutePrefixStripping() throws Exception {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
ZuulRoute zuulRoute = new ZuulRoute("/foo/**");
|
||||
zuulRoute.setStripPrefix(true);
|
||||
@@ -305,7 +308,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetRoutes() {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.getRoutes().put(ASERVICE, new ZuulRoute("/" + ASERVICE + "/**"));
|
||||
this.properties.init();
|
||||
@@ -317,7 +320,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetRoutesWithMapping() {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.getRoutes().put(ASERVICE,
|
||||
new ZuulRoute("/" + ASERVICE + "/**", ASERVICE));
|
||||
@@ -329,7 +332,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetPhysicalRoutes() {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.getRoutes().put(ASERVICE,
|
||||
new ZuulRoute("/" + ASERVICE + "/**", "http://" + ASERVICE));
|
||||
@@ -341,7 +344,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetDefaultRoute() {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.getRoutes().put(ASERVICE, new ZuulRoute("/**", ASERVICE));
|
||||
Map<String, String> routesMap = routeLocator.getRoutes();
|
||||
@@ -352,7 +355,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testGetDefaultPhysicalRoute() {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.getRoutes().put(ASERVICE,
|
||||
new ZuulRoute("/**", "http://" + ASERVICE));
|
||||
@@ -364,7 +367,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testIgnoreRoutes() {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredServices(Collections.singletonList(IGNOREDSERVICE));
|
||||
given(this.discovery.getServices()).willReturn(
|
||||
@@ -376,7 +379,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testIgnoreRoutesWithPattern() {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredServices(Collections.singletonList("ignore*"));
|
||||
given(this.discovery.getServices()).willReturn(
|
||||
@@ -388,7 +391,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testIgnoreAllRoutes() {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredServices(Collections.singletonList("*"));
|
||||
given(this.discovery.getServices()).willReturn(
|
||||
@@ -401,7 +404,7 @@ public class ProxyRouteLocatorTests {
|
||||
@Test
|
||||
public void testIgnoredRouteIncludedIfConfiguredAndDiscovered() {
|
||||
this.properties.getRoutes().put("foo", new ZuulRoute("/foo/**"));
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredServices(Collections.singletonList("*"));
|
||||
given(this.discovery.getServices()).willReturn(Collections.singletonList("foo"));
|
||||
@@ -416,7 +419,7 @@ public class ProxyRouteLocatorTests {
|
||||
route.setStripPrefix(true);
|
||||
route.setRetryable(Boolean.TRUE);
|
||||
this.properties.getRoutes().put("foo", route);
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredServices(Collections.singletonList("*"));
|
||||
given(this.discovery.getServices()).willReturn(Collections.singletonList("foo"));
|
||||
@@ -434,7 +437,7 @@ public class ProxyRouteLocatorTests {
|
||||
route.setId("foo");
|
||||
|
||||
this.properties.getRoutes().put("foo", route);
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredServices(Collections.singletonList("*"));
|
||||
given(this.discovery.getServices()).willReturn(Collections.singletonList("foo"));
|
||||
@@ -450,7 +453,7 @@ public class ProxyRouteLocatorTests {
|
||||
public void testIgnoredRouteIncludedIfConfiguredAndNotDiscovered() {
|
||||
this.properties.getRoutes()
|
||||
.put("foo", new ZuulRoute("/foo/**", "http://foo.com"));
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.properties.setIgnoredServices(Collections.singletonList("*"));
|
||||
given(this.discovery.getServices()).willReturn(Collections.singletonList("bar"));
|
||||
@@ -461,7 +464,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
@Test
|
||||
public void testAutoRoutes() {
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
given(this.discovery.getServices()).willReturn(
|
||||
Collections.singletonList(MYSERVICE));
|
||||
@@ -476,7 +479,7 @@ public class ProxyRouteLocatorTests {
|
||||
ZuulRoute route = new ZuulRoute("/" + MYSERVICE + "/**", "http://example.com/"
|
||||
+ MYSERVICE);
|
||||
this.properties.getRoutes().put(MYSERVICE, route);
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
given(this.discovery.getServices()).willReturn(
|
||||
Collections.singletonList(MYSERVICE));
|
||||
@@ -491,7 +494,7 @@ public class ProxyRouteLocatorTests {
|
||||
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,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
|
||||
LinkedHashMap<String, ZuulRoute> routes = routeLocator.locateRoutes();
|
||||
@@ -509,7 +512,7 @@ public class ProxyRouteLocatorTests {
|
||||
|
||||
given(this.discovery.getServices()).willReturn(Collections.singletonList(MYSERVICE));
|
||||
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
|
||||
Map<String, String> routesMap = routeLocator.getRoutes();
|
||||
@@ -522,9 +525,9 @@ public class ProxyRouteLocatorTests {
|
||||
public void testRegExServiceRouteMapperNoServiceIdMatches() {
|
||||
given(this.discovery.getServices()).willReturn(Collections.singletonList(MYSERVICE));
|
||||
|
||||
RegExServiceRouteMapper regExServiceRouteMapper = new RegExServiceRouteMapper(properties.getRegexMapper().getServicePattern(),
|
||||
PatternServiceRouteMapper regExServiceRouteMapper = new PatternServiceRouteMapper(properties.getRegexMapper().getServicePattern(),
|
||||
properties.getRegexMapper().getRoutePattern());
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties, regExServiceRouteMapper);
|
||||
Map<String, String> routesMap = routeLocator.getRoutes();
|
||||
assertNotNull("routesMap was null", routesMap);
|
||||
@@ -536,9 +539,9 @@ public class ProxyRouteLocatorTests {
|
||||
public void testRegExServiceRouteMapperServiceIdMatches() {
|
||||
given(this.discovery.getServices()).willReturn(Collections.singletonList("rest-service-v1"));
|
||||
|
||||
RegExServiceRouteMapper regExServiceRouteMapper = new RegExServiceRouteMapper(properties.getRegexMapper().getServicePattern(),
|
||||
PatternServiceRouteMapper regExServiceRouteMapper = new PatternServiceRouteMapper(properties.getRegexMapper().getServicePattern(),
|
||||
properties.getRegexMapper().getRoutePattern());
|
||||
ProxyRouteLocator routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
DiscoveryClientRouteLocator routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties, regExServiceRouteMapper);
|
||||
Map<String, String> routesMap = routeLocator.getRoutes();
|
||||
assertNotNull("routesMap was null", routesMap);
|
||||
@@ -1,4 +1,4 @@
|
||||
package org.springframework.cloud.netflix.zuul.filters.regex;
|
||||
package org.springframework.cloud.netflix.zuul.filters.discovery;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
@@ -17,7 +17,6 @@ import org.springframework.cloud.netflix.ribbon.RibbonClient;
|
||||
import org.springframework.cloud.netflix.ribbon.StaticServerList;
|
||||
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
|
||||
import org.springframework.cloud.netflix.zuul.RoutesEndpoint;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRouteLocator;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.http.HttpEntity;
|
||||
@@ -38,7 +37,7 @@ import com.netflix.loadbalancer.ServerList;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.regex.RegExServiceRouteMapperIntegrationTests.SERVICE_ID;
|
||||
import static org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapperIntegrationTests.SERVICE_ID;
|
||||
|
||||
/**
|
||||
* @author Stéphane Leroy
|
||||
@@ -47,12 +46,9 @@ import static org.springframework.cloud.netflix.zuul.filters.regex.RegExServiceR
|
||||
@SpringApplicationConfiguration(classes = SampleCustomZuulProxyApplication.class)
|
||||
@WebIntegrationTest(value = { "spring.application.name=regex-test-application",
|
||||
"spring.jmx.enabled=true" }, randomPort = true)
|
||||
@TestPropertySource(properties = { "eureka.client.enabled=false",
|
||||
"zuul.regexMapper.enabled=true",
|
||||
"zuul.regexMapper.servicePattern=(?<domain>^.+)-(?<name>.+)-(?<version>v.+$)",
|
||||
"zuul.regexMapper.routePattern=${version}/${domain}/${name}" })
|
||||
@TestPropertySource(properties = "eureka.client.enabled=false")
|
||||
@DirtiesContext
|
||||
public class RegExServiceRouteMapperIntegrationTests {
|
||||
public class PatternServiceRouteMapperIntegrationTests {
|
||||
|
||||
protected static final String SERVICE_ID = "domain-service-v1";
|
||||
|
||||
@@ -60,14 +56,14 @@ public class RegExServiceRouteMapperIntegrationTests {
|
||||
private int port;
|
||||
|
||||
@Autowired
|
||||
private ProxyRouteLocator routes;
|
||||
private DiscoveryClientRouteLocator routes;
|
||||
|
||||
@Autowired
|
||||
private RoutesEndpoint endpoint;
|
||||
|
||||
@Test
|
||||
public void getRegexMappedService() {
|
||||
endpoint.reset();
|
||||
this.endpoint.reset();
|
||||
ResponseEntity<String> result = new TestRestTemplate().exchange(
|
||||
"http://localhost:" + this.port + "/v1/domain/service/get/1",
|
||||
HttpMethod.GET, new HttpEntity<>((Void) null), String.class);
|
||||
@@ -78,7 +74,7 @@ public class RegExServiceRouteMapperIntegrationTests {
|
||||
@Test
|
||||
public void getStaticRoute() {
|
||||
this.routes.addRoute("/self/**", "http://localhost:" + this.port);
|
||||
endpoint.reset();
|
||||
this.endpoint.reset();
|
||||
ResponseEntity<String> result = new TestRestTemplate().exchange(
|
||||
"http://localhost:" + this.port + "/self/get/1", HttpMethod.GET,
|
||||
new HttpEntity<>((Void) null), String.class);
|
||||
@@ -109,6 +105,13 @@ class SampleCustomZuulProxyApplication {
|
||||
return "Get " + id;
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PatternServiceRouteMapper serviceRouteMapper() {
|
||||
return new PatternServiceRouteMapper(
|
||||
"(?<domain>^.+)-(?<name>.+)-(?<version>v.+$)",
|
||||
"${version}/${domain}/${name}");
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(SampleCustomZuulProxyApplication.class, args);
|
||||
}
|
||||
@@ -1,13 +1,14 @@
|
||||
package org.springframework.cloud.netflix.zuul.filters.regex;
|
||||
package org.springframework.cloud.netflix.zuul.filters.discovery;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.cloud.netflix.zuul.filters.discovery.PatternServiceRouteMapper;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
|
||||
/**
|
||||
* @author Stéphane Leroy
|
||||
*/
|
||||
public class RegExServiceRouteMapperTests {
|
||||
public class PatternServiceRouteMapperTests {
|
||||
|
||||
/**
|
||||
* Service pattern that follow convention {domain}-{name}-{version}. The name is
|
||||
@@ -18,7 +19,7 @@ public class RegExServiceRouteMapperTests {
|
||||
|
||||
@Test
|
||||
public void test_return_mapped_route_if_serviceid_matches() {
|
||||
RegExServiceRouteMapper toTest = new RegExServiceRouteMapper(SERVICE_PATTERN,
|
||||
PatternServiceRouteMapper toTest = new PatternServiceRouteMapper(SERVICE_PATTERN,
|
||||
ROUTE_PATTERN);
|
||||
|
||||
assertEquals("service version convention", "v1/rest/service",
|
||||
@@ -27,7 +28,7 @@ public class RegExServiceRouteMapperTests {
|
||||
|
||||
@Test
|
||||
public void test_return_serviceid_if_no_matches() {
|
||||
RegExServiceRouteMapper toTest = new RegExServiceRouteMapper(SERVICE_PATTERN,
|
||||
PatternServiceRouteMapper toTest = new PatternServiceRouteMapper(SERVICE_PATTERN,
|
||||
ROUTE_PATTERN);
|
||||
|
||||
// No version here
|
||||
@@ -38,7 +39,7 @@ public class RegExServiceRouteMapperTests {
|
||||
@Test
|
||||
public void test_route_should_be_cleaned_before_returned() {
|
||||
// Messy patterns
|
||||
RegExServiceRouteMapper toTest = new RegExServiceRouteMapper(SERVICE_PATTERN
|
||||
PatternServiceRouteMapper toTest = new PatternServiceRouteMapper(SERVICE_PATTERN
|
||||
+ "(?<nevermatch>.)?", "/${version}/${nevermatch}/${domain}/${name}/");
|
||||
assertEquals("No matches for this service id", "v1/domain/service",
|
||||
toTest.apply("domain-service-v1"));
|
||||
@@ -25,9 +25,9 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.springframework.cloud.client.discovery.DiscoveryClient;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ProxyRouteLocator;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
|
||||
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
|
||||
import org.springframework.cloud.netflix.zuul.filters.discovery.DiscoveryClientRouteLocator;
|
||||
import org.springframework.mock.web.MockHttpServletRequest;
|
||||
|
||||
import com.netflix.util.Pair;
|
||||
@@ -45,14 +45,14 @@ public class PreDecorationFilterTests {
|
||||
|
||||
private ZuulProperties properties = new ZuulProperties();
|
||||
|
||||
private ProxyRouteLocator routeLocator;
|
||||
private DiscoveryClientRouteLocator routeLocator;
|
||||
|
||||
private MockHttpServletRequest request = new MockHttpServletRequest();
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
initMocks(this);
|
||||
this.routeLocator = new ProxyRouteLocator("/", this.discovery,
|
||||
this.routeLocator = new DiscoveryClientRouteLocator("/", this.discovery,
|
||||
this.properties);
|
||||
this.filter = new PreDecorationFilter(this.routeLocator, true);
|
||||
RequestContext ctx = RequestContext.getCurrentContext();
|
||||
|
||||
Reference in New Issue
Block a user