Browse Source

Merge remote-tracking branch 'origin/master' into 2.0.x

pull/6/head
Ryan Baxter 7 years ago
parent
commit
3b4ed6c0a4
  1. 86
      docs/src/main/asciidoc/spring-cloud-netflix.adoc
  2. 2
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignAutoConfiguration.java
  3. 76
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientFactoryBean.java
  4. 174
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientProperties.java
  5. 8
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConstants.java
  6. 8
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/Route.java
  7. 3
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/SimpleRouteLocator.java
  8. 38
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ZuulProperties.java
  9. 160
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/post/LocationRewriteFilter.java
  10. 13
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/support/AbstractRibbonCommand.java
  11. 77
      spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/support/FilterConstants.java
  12. 163
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignClientUsingPropertiesTests.java
  13. 6
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/ZuulPropertiesTests.java
  14. 109
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/post/LocationRewriteFilterIntegrationTests.java
  15. 178
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/post/LocationRewriteFilterTests.java
  16. 107
      spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/route/support/RibbonCommandHystrixThreadPoolKeyTests.java
  17. 19
      spring-cloud-netflix-core/src/test/resources/feign-properties.properties
  18. 4
      spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientConfigBean.java
  19. 8
      spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaConstants.java
  20. 3
      spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerConfigBean.java

86
docs/src/main/asciidoc/spring-cloud-netflix.adoc

@ -886,6 +886,28 @@ ribbon: @@ -886,6 +886,28 @@ ribbon:
clients: client1, client2, client3
----
[[how-to-configure-hystrix-thread-pools]]
=== How to Configure Hystrix thread pools
If you change `zuul.ribbonIsolationStrategy` to THREAD, the thread isolation strategy for Hystrix will be used for all routes. In this case, the HystrixThreadPoolKey is set to "RibbonCommand" as default. It means that HystrixCommands for all routes will be executed in the same Hystrix thread pool. This behavior can be changed using the following configuration and it will result in HystrixCommands being executed in the Hystrix thread pool for each route.
.application.yml
----
zuul:
threadPool:
useSeparateThreadPools: true
----
The default HystrixThreadPoolKey in this case is same with service ID for each route. To add a prefix to HystrixThreadPoolKey, set `zuul.threadPool.threadPoolKeyPrefix` to a value that you want to add. For example:
.application.yml
----
zuul:
threadPool:
useSeparateThreadPools: true
threadPoolKeyPrefix: zuulgw
----
[[spring-cloud-feign]]
== Declarative REST Client: Feign
@ -1020,8 +1042,46 @@ public class FooConfiguration { @@ -1020,8 +1042,46 @@ public class FooConfiguration {
This replaces the `SpringMvcContract` with `feign.Contract.Default` and adds a `RequestInterceptor` to the collection of `RequestInterceptor`.
`@FeignClient` also can be configured using configuration properties.
application.yml
[source,yaml]
----
feign:
client:
config:
feignName:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: full
errorDecoder: com.example.SimpleErrorDecoder
retryer: com.example.SimpleRetryer
requestInterceptors:
- com.example.FooRequestInterceptor
- com.example.BarRequestInterceptor
decode404: false
----
Default configurations can be specified in the `@EnableFeignClients` attribute `defaultConfiguration` in a similar manner as described above. The difference is that this configuration will apply to _all_ feign clients.
If you prefer using configuration properties to configured all `@FeignClient`, you can create configuration properties with `default` feign name.
application.yml
[source,yaml]
----
feign:
client:
config:
default:
connectTimeout: 5000
readTimeout: 5000
loggerLevel: basic
----
If we create both `@Configuration` bean and configuration properties, configuration properties will win.
It will override `@Configuration` values. But if you want to change the priority to `@Configuration`,
you can change `feign.client.default-to-properties` to `false`.
NOTE: If you need to use `ThreadLocal` bound variables in your `RequestInterceptor`s you will need to either set the
thread isolation strategy for Hystrix to `SEMAPHORE` or disable Hystrix in Feign.
@ -1842,6 +1902,32 @@ class MyFallbackProvider implements ZuulFallbackProvider { @@ -1842,6 +1902,32 @@ class MyFallbackProvider implements ZuulFallbackProvider {
}
----
[[zuul-redirect-location-rewrite]]
=== Rewriting `Location` header
If Zuul is fronting a web application then there may be a need to re-write the `Location` header when the web application redirects through a http status code of 3XX, otherwise the browser will end up redirecting to the web application's url instead of the Zuul url.
A `LocationRewriteFilter` Zuul filter can be configured to re-write the Location header to the Zuul's url, it also adds back the stripped global and route specific prefixes. The filter can be added the following way via a Spring Configuration file:
[source,java]
----
import org.springframework.cloud.netflix.zuul.filters.post.LocationRewriteFilter;
...
@Configuration
@EnableZuulProxy
public class ZuulConfig {
@Bean
public LocationRewriteFilter locationRewriteFilter() {
return new LocationRewriteFilter();
}
}
----
[WARNING]
====
Use this filter with caution though, the filter acts on the `Location` header of ALL 3XX response codes which may not be appropriate in all scenarios, say if the user is redirecting to an external URL.
====
[[zuul-developer-guide]]
=== Zuul Developer Guide

2
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignAutoConfiguration.java

@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; @@ -25,6 +25,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.cloud.client.actuator.HasFeatures;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
@ -40,6 +41,7 @@ import feign.okhttp.OkHttpClient; @@ -40,6 +41,7 @@ import feign.okhttp.OkHttpClient;
*/
@Configuration
@ConditionalOnClass(Feign.class)
@EnableConfigurationProperties({FeignClientProperties.class})
public class FeignAutoConfiguration {
@Autowired(required = false)

76
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientFactoryBean.java

@ -1,5 +1,5 @@ @@ -1,5 +1,5 @@
/*
* Copyright 2013-2016 the original author or authors.
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@ -18,9 +18,11 @@ package org.springframework.cloud.netflix.feign; @@ -18,9 +18,11 @@ package org.springframework.cloud.netflix.feign;
import java.util.Map;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.FactoryBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.cloud.netflix.feign.ribbon.LoadBalancerFeignClient;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
@ -44,6 +46,7 @@ import lombok.EqualsAndHashCode; @@ -44,6 +46,7 @@ import lombok.EqualsAndHashCode;
/**
* @author Spencer Gibb
* @author Venil Noronha
* @author Eko Kurniawan Khannedy
*/
@Data
@EqualsAndHashCode(callSuper = false)
@ -74,7 +77,6 @@ class FeignClientFactoryBean implements FactoryBean<Object>, InitializingBean, @@ -74,7 +77,6 @@ class FeignClientFactoryBean implements FactoryBean<Object>, InitializingBean,
Assert.hasText(this.name, "Name must be set");
}
@Override
public void setApplicationContext(ApplicationContext context) throws BeansException {
this.applicationContext = context;
@ -93,7 +95,29 @@ class FeignClientFactoryBean implements FactoryBean<Object>, InitializingBean, @@ -93,7 +95,29 @@ class FeignClientFactoryBean implements FactoryBean<Object>, InitializingBean,
.contract(get(context, Contract.class));
// @formatter:on
// optional values
configureFeign(context, builder);
return builder;
}
protected void configureFeign(FeignContext context, Feign.Builder builder) {
FeignClientProperties properties = applicationContext.getBean(FeignClientProperties.class);
if (properties != null) {
if (properties.isDefaultToProperties()) {
configureUsingConfiguration(context, builder);
configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
configureUsingProperties(properties.getConfig().get(this.name), builder);
} else {
configureUsingProperties(properties.getConfig().get(properties.getDefaultConfig()), builder);
configureUsingProperties(properties.getConfig().get(this.name), builder);
configureUsingConfiguration(context, builder);
}
} else {
configureUsingConfiguration(context, builder);
}
}
protected void configureUsingConfiguration(FeignContext context, Feign.Builder builder) {
Logger.Level level = getOptional(context, Logger.Level.class);
if (level != null) {
builder.logLevel(level);
@ -119,8 +143,52 @@ class FeignClientFactoryBean implements FactoryBean<Object>, InitializingBean, @@ -119,8 +143,52 @@ class FeignClientFactoryBean implements FactoryBean<Object>, InitializingBean,
if (decode404) {
builder.decode404();
}
}
return builder;
protected void configureUsingProperties(FeignClientProperties.FeignClientConfiguration config, Feign.Builder builder) {
if (config == null) {
return;
}
if (config.getLoggerLevel() != null) {
builder.logLevel(config.getLoggerLevel());
}
if (config.getConnectTimeout() != null && config.getReadTimeout() != null) {
builder.options(new Request.Options(config.getConnectTimeout(), config.getReadTimeout()));
}
if (config.getRetryer() != null) {
Retryer retryer = getOrInstantiate(config.getRetryer());
builder.retryer(retryer);
}
if (config.getErrorDecoder() != null) {
ErrorDecoder errorDecoder = getOrInstantiate(config.getErrorDecoder());
builder.errorDecoder(errorDecoder);
}
if (config.getRequestInterceptors() != null && !config.getRequestInterceptors().isEmpty()) {
// this will add request interceptor to builder, not replace existing
for (Class<RequestInterceptor> bean : config.getRequestInterceptors()) {
RequestInterceptor interceptor = getOrInstantiate(bean);
builder.requestInterceptor(interceptor);
}
}
if (config.getDecode404() != null) {
if (config.getDecode404()) {
builder.decode404();
}
}
}
private <T> T getOrInstantiate(Class<T> tClass) {
try {
return applicationContext.getBean(tClass);
} catch (NoSuchBeanDefinitionException e) {
return BeanUtils.instantiateClass(tClass);
}
}
protected <T> T get(FeignContext context, Class<T> type) {

174
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/feign/FeignClientProperties.java

@ -0,0 +1,174 @@ @@ -0,0 +1,174 @@
/*
* Copyright 2013-2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.netflix.feign;
import feign.Logger;
import feign.RequestInterceptor;
import feign.Retryer;
import feign.codec.ErrorDecoder;
import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
/**
* @author Eko Kurniawan Khannedy
*/
@ConfigurationProperties("feign.client")
public class FeignClientProperties {
private boolean defaultToProperties = true;
private String defaultConfig = "default";
private Map<String, FeignClientConfiguration> config = new HashMap<>();
public boolean isDefaultToProperties() {
return defaultToProperties;
}
public void setDefaultToProperties(boolean defaultToProperties) {
this.defaultToProperties = defaultToProperties;
}
public String getDefaultConfig() {
return defaultConfig;
}
public void setDefaultConfig(String defaultConfig) {
this.defaultConfig = defaultConfig;
}
public Map<String, FeignClientConfiguration> getConfig() {
return config;
}
public void setConfig(Map<String, FeignClientConfiguration> config) {
this.config = config;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FeignClientProperties that = (FeignClientProperties) o;
return defaultToProperties == that.defaultToProperties &&
Objects.equals(defaultConfig, that.defaultConfig) &&
Objects.equals(config, that.config);
}
@Override
public int hashCode() {
return Objects.hash(defaultToProperties, defaultConfig, config);
}
public static class FeignClientConfiguration {
private Logger.Level loggerLevel;
private Integer connectTimeout;
private Integer readTimeout;
private Class<Retryer> retryer;
private Class<ErrorDecoder> errorDecoder;
private List<Class<RequestInterceptor>> requestInterceptors;
private Boolean decode404;
public Logger.Level getLoggerLevel() {
return loggerLevel;
}
public void setLoggerLevel(Logger.Level loggerLevel) {
this.loggerLevel = loggerLevel;
}
public Integer getConnectTimeout() {
return connectTimeout;
}
public void setConnectTimeout(Integer connectTimeout) {
this.connectTimeout = connectTimeout;
}
public Integer getReadTimeout() {
return readTimeout;
}
public void setReadTimeout(Integer readTimeout) {
this.readTimeout = readTimeout;
}
public Class<Retryer> getRetryer() {
return retryer;
}
public void setRetryer(Class<Retryer> retryer) {
this.retryer = retryer;
}
public Class<ErrorDecoder> getErrorDecoder() {
return errorDecoder;
}
public void setErrorDecoder(Class<ErrorDecoder> errorDecoder) {
this.errorDecoder = errorDecoder;
}
public List<Class<RequestInterceptor>> getRequestInterceptors() {
return requestInterceptors;
}
public void setRequestInterceptors(List<Class<RequestInterceptor>> requestInterceptors) {
this.requestInterceptors = requestInterceptors;
}
public Boolean getDecode404() {
return decode404;
}
public void setDecode404(Boolean decode404) {
this.decode404 = decode404;
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
FeignClientConfiguration that = (FeignClientConfiguration) o;
return loggerLevel == that.loggerLevel &&
Objects.equals(connectTimeout, that.connectTimeout) &&
Objects.equals(readTimeout, that.readTimeout) &&
Objects.equals(retryer, that.retryer) &&
Objects.equals(errorDecoder, that.errorDecoder) &&
Objects.equals(requestInterceptors, that.requestInterceptors) &&
Objects.equals(decode404, that.decode404);
}
@Override
public int hashCode() {
return Objects.hash(loggerLevel, connectTimeout, readTimeout, retryer,
errorDecoder, requestInterceptors, decode404);
}
}
}

8
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/hystrix/HystrixConstants.java

@ -19,8 +19,12 @@ package org.springframework.cloud.netflix.hystrix; @@ -19,8 +19,12 @@ package org.springframework.cloud.netflix.hystrix;
/**
* @author Spencer Gibb
*/
public interface HystrixConstants {
public class HystrixConstants {
String HYSTRIX_STREAM_DESTINATION = "springCloudHystrixStream";
public static final String HYSTRIX_STREAM_DESTINATION = "springCloudHystrixStream";
private HystrixConstants() {
throw new AssertionError("Must not instantiate constant utility class");
}
}

8
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/Route.java

@ -42,6 +42,12 @@ public class Route { @@ -42,6 +42,12 @@ public class Route {
}
}
}
public Route(String id, String path, String location, String prefix,
Boolean retryable, Set<String> ignoredHeaders, boolean prefixStripped) {
this(id, path, location, prefix, retryable, ignoredHeaders);
this.prefixStripped = prefixStripped;
}
private String id;
@ -58,6 +64,8 @@ public class Route { @@ -58,6 +64,8 @@ public class Route {
private Set<String> sensitiveHeaders = new LinkedHashSet<>();
private boolean customSensitiveHeaders;
private boolean prefixStripped = true;
public boolean isCustomSensitiveHeaders() {
return this.customSensitiveHeaders;

3
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/SimpleRouteLocator.java

@ -151,7 +151,8 @@ public class SimpleRouteLocator implements RouteLocator, Ordered { @@ -151,7 +151,8 @@ public class SimpleRouteLocator implements RouteLocator, Ordered {
}
return new Route(route.getId(), targetPath, route.getLocation(), prefix,
retryable,
route.isCustomSensitiveHeaders() ? route.getSensitiveHeaders() : null);
route.isCustomSensitiveHeaders() ? route.getSensitiveHeaders() : null,
route.isStripPrefix());
}
/**

38
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/ZuulProperties.java

@ -160,6 +160,8 @@ public class ZuulProperties { @@ -160,6 +160,8 @@ public class ZuulProperties {
private ExecutionIsolationStrategy ribbonIsolationStrategy = SEMAPHORE;
private HystrixSemaphore semaphore = new HystrixSemaphore();
private HystrixThreadPool threadPool = new HystrixThreadPool();
public Set<String> getIgnoredHeaders() {
Set<String> ignoredHeaders = new LinkedHashSet<>(this.ignoredHeaders);
@ -302,7 +304,8 @@ public class ZuulProperties { @@ -302,7 +304,8 @@ public class ZuulProperties {
public Route getRoute(String prefix) {
return new Route(this.id, this.path, getLocation(), prefix, this.retryable,
isCustomSensitiveHeaders() ? this.sensitiveHeaders : null);
isCustomSensitiveHeaders() ? this.sensitiveHeaders : null,
this.stripPrefix);
}
public void setSensitiveHeaders(Set<String> headers) {
@ -357,6 +360,39 @@ public class ZuulProperties { @@ -357,6 +360,39 @@ public class ZuulProperties {
}
public static class HystrixThreadPool {
/**
* Flag to determine whether RibbonCommands should use separate thread pools for hystrix.
* By setting to true, RibbonCommands will be executed in a hystrix's thread pool that it is associated with.
* Each RibbonCommand will be associated with a thread pool according to its commandKey (serviceId).
* As default, all commands will be executed in a single thread pool whose threadPoolKey is "RibbonCommand".
* This property is only applicable when using THREAD as ribbonIsolationStrategy
*/
private boolean useSeparateThreadPools = false;
/**
* A prefix for HystrixThreadPoolKey of hystrix's thread pool that is allocated to each service Id.
* This property is only applicable when using THREAD as ribbonIsolationStrategy and useSeparateThreadPools = true
*/
private String threadPoolKeyPrefix = "";
public boolean isUseSeparateThreadPools() {
return useSeparateThreadPools;
}
public void setUseSeparateThreadPools(boolean useSeparateThreadPools) {
this.useSeparateThreadPools = useSeparateThreadPools;
}
public String getThreadPoolKeyPrefix() {
return threadPoolKeyPrefix;
}
public void setThreadPoolKeyPrefix(String threadPoolKeyPrefix) {
this.threadPoolKeyPrefix = threadPoolKeyPrefix;
}
}
public String getServletPattern() {
String path = this.servletPath;
if (!path.startsWith("/")) {

160
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/post/LocationRewriteFilter.java

@ -0,0 +1,160 @@ @@ -0,0 +1,160 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.springframework.cloud.netflix.zuul.filters.post;
import com.netflix.util.Pair;
import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.springframework.beans.factory.annotation.Autowired;
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.http.HttpStatus;
import org.springframework.http.server.ServletServerHttpRequest;
import org.springframework.util.StringUtils;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
import org.springframework.web.util.UrlPathHelper;
import java.net.URI;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.POST_TYPE;
import static org.springframework.cloud.netflix.zuul.filters.support.FilterConstants.SEND_RESPONSE_FILTER_ORDER;
/**
* {@link ZuulFilter} Responsible for rewriting the Location header to be the Zuul URL
*
* @author Biju Kunjummen
*/
public class LocationRewriteFilter extends ZuulFilter {
private final UrlPathHelper urlPathHelper = new UrlPathHelper();
@Autowired
private ZuulProperties zuulProperties;
@Autowired
private RouteLocator routeLocator;
private static final String LOCATION_HEADER = "Location";
public LocationRewriteFilter() {
}
public LocationRewriteFilter(ZuulProperties zuulProperties,
RouteLocator routeLocator) {
this.routeLocator = routeLocator;
this.zuulProperties = zuulProperties;
}
@Override
public String filterType() {
return POST_TYPE;
}
@Override
public int filterOrder() {
return SEND_RESPONSE_FILTER_ORDER - 100;
}
@Override
public boolean shouldFilter() {
RequestContext ctx = RequestContext.getCurrentContext();
int statusCode = ctx.getResponseStatusCode();
return HttpStatus.valueOf(statusCode).is3xxRedirection();
}
@Override
public Object run() {
RequestContext ctx = RequestContext.getCurrentContext();
Route route = routeLocator.getMatchingRoute(
urlPathHelper.getPathWithinApplication(ctx.getRequest()));
if (route != null) {
Pair<String, String> lh = locationHeader(ctx);
if (lh != null) {
String location = lh.second();
URI originalRequestUri = UriComponentsBuilder
.fromHttpRequest(new ServletServerHttpRequest(ctx.getRequest()))
.build().toUri();
UriComponentsBuilder redirectedUriBuilder = UriComponentsBuilder
.fromUriString(location);
UriComponents redirectedUriComps = redirectedUriBuilder.build();
String newPath = getRestoredPath(this.zuulProperties, route,
redirectedUriComps);
String modifiedLocation = redirectedUriBuilder
.scheme(originalRequestUri.getScheme())
.host(originalRequestUri.getHost())
.port(originalRequestUri.getPort()).replacePath(newPath).build()
.toUriString();
lh.setSecond(modifiedLocation);
}
}
return null;
}
private String getRestoredPath(ZuulProperties zuulProperties, Route route,
UriComponents redirectedUriComps) {
StringBuilder path = new StringBuilder();
String redirectedPathWithoutGlobal = downstreamHasGlobalPrefix(zuulProperties)
? redirectedUriComps.getPath()
.substring(("/" + zuulProperties.getPrefix()).length())
: redirectedUriComps.getPath();
if (downstreamHasGlobalPrefix(zuulProperties)) {
path.append("/" + zuulProperties.getPrefix());
}
else {
path.append(zuulHasGlobalPrefix(zuulProperties)
? "/" + zuulProperties.getPrefix() : "");
}
path.append(downstreamHasRoutePrefix(route) ? "" : "/" + route.getPrefix())
.append(redirectedPathWithoutGlobal);
return path.toString();
}
private boolean downstreamHasGlobalPrefix(ZuulProperties zuulProperties) {
return (!zuulProperties.isStripPrefix()
&& StringUtils.hasText(zuulProperties.getPrefix()));
}
private boolean zuulHasGlobalPrefix(ZuulProperties zuulProperties) {
return StringUtils.hasText(zuulProperties.getPrefix());
}
private boolean downstreamHasRoutePrefix(Route route) {
return (!route.isPrefixStripped() && StringUtils.hasText(route.getPrefix()));
}
private Pair<String, String> locationHeader(RequestContext ctx) {
if (ctx.getZuulResponseHeaders() != null) {
for (Pair<String, String> pair : ctx.getZuulResponseHeaders()) {
if (pair.first().equals(LOCATION_HEADER)) {
return pair;
}
}
}
return null;
}
}

13
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/support/AbstractRibbonCommand.java

@ -34,6 +34,7 @@ import com.netflix.hystrix.HystrixCommandGroupKey; @@ -34,6 +34,7 @@ import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy;
import com.netflix.hystrix.HystrixThreadPoolKey;
import com.netflix.zuul.constants.ZuulConstants;
import com.netflix.zuul.context.RequestContext;
@ -78,6 +79,9 @@ public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwar @@ -78,6 +79,9 @@ public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwar
ZuulProperties zuulProperties) {
// @formatter:off
Setter commandSetter = Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RibbonCommand"))
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey));
final HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter()
.withExecutionIsolationStrategy(zuulProperties.getRibbonIsolationStrategy());
if (zuulProperties.getRibbonIsolationStrategy() == ExecutionIsolationStrategy.SEMAPHORE){
@ -87,13 +91,12 @@ public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwar @@ -87,13 +91,12 @@ public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwar
final DynamicIntProperty value = DynamicPropertyFactory.getInstance()
.getIntProperty(name, zuulProperties.getSemaphore().getMaxSemaphores());
setter.withExecutionIsolationSemaphoreMaxConcurrentRequests(value.get());
} else {
// TODO Find out is some parameters can be set here
} else if (zuulProperties.getThreadPool().isUseSeparateThreadPools()) {
final String threadPoolKey = zuulProperties.getThreadPool().getThreadPoolKeyPrefix() + commandKey;
commandSetter.andThreadPoolKey(HystrixThreadPoolKey.Factory.asKey(threadPoolKey));
}
return Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RibbonCommand"))
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey))
.andCommandPropertiesDefaults(setter);
return commandSetter.andCommandPropertiesDefaults(setter);
// @formatter:on
}

77
spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/support/FilterConstants.java

@ -26,190 +26,195 @@ import com.netflix.zuul.ZuulFilter; @@ -26,190 +26,195 @@ import com.netflix.zuul.ZuulFilter;
/**
* @author Spencer Gibb
*/
public interface FilterConstants {
public class FilterConstants {
// KEY constants -----------------------------------
/**
* Zuul {@link com.netflix.zuul.context.RequestContext} key for use in {@link org.springframework.cloud.netflix.zuul.filters.pre.ServletDetectionFilter}
*/
String IS_DISPATCHER_SERVLET_REQUEST_KEY = "isDispatcherServletRequest";
public static final String IS_DISPATCHER_SERVLET_REQUEST_KEY = "isDispatcherServletRequest";
/**
* Zuul {@link com.netflix.zuul.context.RequestContext} key for use in {@link org.springframework.cloud.netflix.zuul.filters.route.SendForwardFilter}
*/
String FORWARD_TO_KEY = "forward.to";
public static final String FORWARD_TO_KEY = "forward.to";
/**
* Zuul {@link com.netflix.zuul.context.RequestContext} key for use in TODO: determine use
*/
String PROXY_KEY = "proxy";
public static final String PROXY_KEY = "proxy";
/**
* Zuul {@link com.netflix.zuul.context.RequestContext} key for use in {@link org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter}
*/
String REQUEST_ENTITY_KEY = "requestEntity";
public static final String REQUEST_ENTITY_KEY = "requestEntity";
/**
* Zuul {@link com.netflix.zuul.context.RequestContext} key for use in to override the path of the request.
*/
String REQUEST_URI_KEY = "requestURI";
public static final String REQUEST_URI_KEY = "requestURI";
/**
* Zuul {@link com.netflix.zuul.context.RequestContext} key for use in {@link org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter}
*/
String RETRYABLE_KEY = "retryable";
public static final String RETRYABLE_KEY = "retryable";
/**
* Zuul {@link com.netflix.zuul.context.RequestContext} key for use in {@link org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter}
*/
String ROUTING_DEBUG_KEY = "routingDebug";
public static final String ROUTING_DEBUG_KEY = "routingDebug";
/**
* Zuul {@link com.netflix.zuul.context.RequestContext} key for use in {@link org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter}
*/
String SERVICE_ID_KEY = "serviceId";
public static final String SERVICE_ID_KEY = "serviceId";
// ORDER constants -----------------------------------
/**
* Filter Order for {@link DebugFilter#filterOrder()}
*/
int DEBUG_FILTER_ORDER = 1;
public static final int DEBUG_FILTER_ORDER = 1;
/**
* Filter Order for {@link org.springframework.cloud.netflix.zuul.filters.pre.FormBodyWrapperFilter#filterOrder()}
*/
int FORM_BODY_WRAPPER_FILTER_ORDER = -1;
public static final int FORM_BODY_WRAPPER_FILTER_ORDER = -1;
/**
* Filter Order for {@link org.springframework.cloud.netflix.zuul.filters.pre.PreDecorationFilter}
*/
int PRE_DECORATION_FILTER_ORDER = 5;
public static final int PRE_DECORATION_FILTER_ORDER = 5;
/**
* Filter Order for {@link org.springframework.cloud.netflix.zuul.filters.route.RibbonRoutingFilter#filterOrder()}
*/
int RIBBON_ROUTING_FILTER_ORDER = 10;
public static final int RIBBON_ROUTING_FILTER_ORDER = 10;
/**
* Filter Order for {@link org.springframework.cloud.netflix.zuul.filters.post.SendErrorFilter#filterOrder()}
*/
int SEND_ERROR_FILTER_ORDER = 0;
public static final int SEND_ERROR_FILTER_ORDER = 0;
/**
* Filter Order for {@link SendForwardFilter#filterOrder()}
*/
int SEND_FORWARD_FILTER_ORDER = 500;
public static final int SEND_FORWARD_FILTER_ORDER = 500;
/**
* Filter Order for {@link org.springframework.cloud.netflix.zuul.filters.post.SendResponseFilter#filterOrder()}
*/
int SEND_RESPONSE_FILTER_ORDER = 1000;
public static final int SEND_RESPONSE_FILTER_ORDER = 1000;
/**
* Filter Order for {@link org.springframework.cloud.netflix.zuul.filters.route.SimpleHostRoutingFilter#filterOrder()}
*/
int SIMPLE_HOST_ROUTING_FILTER_ORDER = 100;
public static final int SIMPLE_HOST_ROUTING_FILTER_ORDER = 100;
/**
* filter order for {@link Servlet30WrapperFilter#filterOrder()}
*/
int SERVLET_30_WRAPPER_FILTER_ORDER = -2;
public static final int SERVLET_30_WRAPPER_FILTER_ORDER = -2;
/**
* filter order for {@link org.springframework.cloud.netflix.zuul.filters.pre.ServletDetectionFilter#filterOrder()}
*/
int SERVLET_DETECTION_FILTER_ORDER = -3;
public static final int SERVLET_DETECTION_FILTER_ORDER = -3;
// Zuul Filter TYPE constants -----------------------------------
/**
* {@link ZuulFilter#filterType()} error type.
*/
String ERROR_TYPE = "error";
public static final String ERROR_TYPE = "error";
/**
* {@link ZuulFilter#filterType()} post type.
*/
String POST_TYPE = "post";
public static final String POST_TYPE = "post";
/**
* {@link ZuulFilter#filterType()} pre type.
*/
String PRE_TYPE = "pre";
public static final String PRE_TYPE = "pre";
/**
* {@link ZuulFilter#filterType()} route type.
*/
String ROUTE_TYPE = "route";
public static final String ROUTE_TYPE = "route";
// OTHER constants -----------------------------------
/**
* Zuul {@link com.netflix.zuul.context.RequestContext} key for use in {@link org.springframework.cloud.netflix.zuul.filters.route.SendForwardFilter}
*/
String FORWARD_LOCATION_PREFIX = "forward:";
public static final String FORWARD_LOCATION_PREFIX = "forward:";
/**
* default http port
*/
int HTTP_PORT = 80;
public static final int HTTP_PORT = 80;
/**
* default https port
*/
int HTTPS_PORT = 443;
public static final int HTTPS_PORT = 443;
/**
* http url scheme
*/
String HTTP_SCHEME = "http";
public static final String HTTP_SCHEME = "http";
/**
* https url scheme
*/
String HTTPS_SCHEME = "https";
public static final String HTTPS_SCHEME = "https";
// HEADER constants -----------------------------------
/**
* X-* Header for the matching url. Used when routes use a url rather than serviceId
*/
String SERVICE_HEADER = "X-Zuul-Service";
public static final String SERVICE_HEADER = "X-Zuul-Service";
/**
* X-* Header for the matching serviceId
*/
String SERVICE_ID_HEADER = "X-Zuul-ServiceId";
public static final String SERVICE_ID_HEADER = "X-Zuul-ServiceId";
/**
* X-Forwarded-For Header
*/
String X_FORWARDED_FOR_HEADER = "X-Forwarded-For";
public static final String X_FORWARDED_FOR_HEADER = "X-Forwarded-For";
/**
* X-Forwarded-Host Header
*/
String X_FORWARDED_HOST_HEADER = "X-Forwarded-Host";
public static final String X_FORWARDED_HOST_HEADER = "X-Forwarded-Host";
/**
* X-Forwarded-Prefix Header
*/
String X_FORWARDED_PREFIX_HEADER = "X-Forwarded-Prefix";
public static final String X_FORWARDED_PREFIX_HEADER = "X-Forwarded-Prefix";
/**
* X-Forwarded-Port Header
*/
String X_FORWARDED_PORT_HEADER = "X-Forwarded-Port";
public static final String X_FORWARDED_PORT_HEADER = "X-Forwarded-Port";
/**
* X-Forwarded-Proto Header
*/
String X_FORWARDED_PROTO_HEADER = "X-Forwarded-Proto";
public static final String X_FORWARDED_PROTO_HEADER = "X-Forwarded-Proto";
/**
* X-Zuul-Debug Header
*/
String X_ZUUL_DEBUG_HEADER = "X-Zuul-Debug-Header";
public static final String X_ZUUL_DEBUG_HEADER = "X-Zuul-Debug-Header";
private FilterConstants() {
throw new AssertionError("Must not instantiate constant utility class");
}
}

163
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/feign/FeignClientUsingPropertiesTests.java

@ -0,0 +1,163 @@ @@ -0,0 +1,163 @@
/*
* Copyright 2013-2015 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.netflix.feign;
import feign.RequestInterceptor;
import feign.RequestTemplate;
import feign.RetryableException;
import feign.Retryer;
import feign.codec.ErrorDecoder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest;
import static org.junit.Assert.*;
/**
* @author Eko Kurniawan Khannedy
*/
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest(classes = FeignClientUsingPropertiesTests.Application.class, webEnvironment = WebEnvironment.RANDOM_PORT)
@TestPropertySource("classpath:feign-properties.properties")
@DirtiesContext
public class FeignClientUsingPropertiesTests {
@Autowired
FeignContext context;
@Autowired
private ApplicationContext applicationContext;
@Value("${local.server.port}")
private int port = 0;
private FeignClientFactoryBean fooFactoryBean;
private FeignClientFactoryBean barFactoryBean;
public FeignClientUsingPropertiesTests() {
fooFactoryBean = new FeignClientFactoryBean();
fooFactoryBean.setName("foo");
fooFactoryBean.setType(FeignClientFactoryBean.class);
barFactoryBean = new FeignClientFactoryBean();
barFactoryBean.setName("bar");
barFactoryBean.setType(FeignClientFactoryBean.class);
}
public FooClient fooClient() {
fooFactoryBean.setApplicationContext(applicationContext);
return fooFactoryBean.feign(context).target(FooClient.class, "http://localhost:" + this.port);
}
public BarClient barClient() {
barFactoryBean.setApplicationContext(applicationContext);
return barFactoryBean.feign(context).target(BarClient.class, "http://localhost:" + this.port);
}
@Test
public void testFoo() {
String response = fooClient().foo();
assertNotNull("OK", response);
}
@Test(expected = RetryableException.class)
public void testBar() {
barClient().bar();
fail("it should timeout");
}
protected interface FooClient {
@RequestMapping(method = RequestMethod.GET, value = "/foo")
String foo();
}
protected interface BarClient {
@RequestMapping(method = RequestMethod.GET, value = "/bar")
String bar();
}
@Configuration
@EnableAutoConfiguration
@RestController
protected static class Application {
@RequestMapping(method = RequestMethod.GET, value = "/foo")
public String foo(HttpServletRequest request) throws IllegalAccessException {
if ("Foo".equals(request.getHeader("Foo")) &&
"Bar".equals(request.getHeader("Bar"))) {
return "OK";
} else {
throw new IllegalAccessException("It should has Foo and Bar header");
}
}
@RequestMapping(method = RequestMethod.GET, value = "/bar")
public String bar() throws InterruptedException {
Thread.sleep(2000L);
return "OK";
}
}
public static class FooRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
template.header("Foo", "Foo");
}
}
public static class BarRequestInterceptor implements RequestInterceptor {
@Override
public void apply(RequestTemplate template) {
template.header("Bar", "Bar");
}
}
public static class NoRetryer implements Retryer {
@Override
public void continueOrPropagate(RetryableException e) {
throw e;
}
@Override
public Retryer clone() {
return this;
}
}
public static class DefaultErrorDecoder extends ErrorDecoder.Default {
}
}

6
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/ZuulPropertiesTests.java

@ -24,6 +24,7 @@ import org.junit.Before; @@ -24,6 +24,7 @@ import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties.ZuulRoute;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
@ -103,4 +104,9 @@ public class ZuulPropertiesTests { @@ -103,4 +104,9 @@ public class ZuulPropertiesTests {
assertFalse(this.zuul.getSensitiveHeaders().contains("Cookie"));
}
@Test
public void defaultHystrixThreadPool() {
assertFalse(this.zuul.getThreadPool().isUseSeparateThreadPools());
assertEquals("", this.zuul.getThreadPool().getThreadPoolKeyPrefix());
}
}

109
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/post/LocationRewriteFilterIntegrationTests.java

@ -0,0 +1,109 @@ @@ -0,0 +1,109 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.springframework.cloud.netflix.zuul.filters.post;
import com.netflix.loadbalancer.Server;
import com.netflix.loadbalancer.ServerList;
import com.netflix.zuul.context.RequestContext;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.context.embedded.LocalServerPort;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.StaticServerList;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.context.annotation.Bean;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.test.annotation.DirtiesContext;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.web.bind.annotation.RequestMapping;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Biju Kunjummen
*/
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, properties = {
"zuul.routes.aservice.path:/service/**", "zuul.routes.aservice.strip-prefix:true",
"eureka.client.enabled:false" })
@DirtiesContext
public class LocationRewriteFilterIntegrationTests {
@LocalServerPort
private int port;
@Before
public void before() {
RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context);
}
@Test
public void testWithRedirectPrefixStripped() {
String url = "http://localhost:" + port + "/service/redirectingUri";
ResponseEntity<String> response = new TestRestTemplate().getForEntity(url,
String.class);
List<String> locationHeaders = response.getHeaders().get("Location");
assertThat(locationHeaders).hasSize(1);
String locationHeader = locationHeaders.get(0);
assertThat(locationHeader).withFailMessage("Location should have prefix")
.isEqualTo(
String.format("http://localhost:%d/service/redirectedUri", port));
}
@SpringBootConfiguration
@EnableAutoConfiguration
@EnableZuulProxy
@Controller
@RibbonClient(name = "aservice", configuration = RibbonConfig.class)
protected static class Config {
@RequestMapping("/redirectingUri")
public String redirect1() {
return "redirect:/redirectedUri";
}
@Bean
public LocationRewriteFilter locationRewriteFilter() {
return new LocationRewriteFilter();
}
}
public static class RibbonConfig {
@LocalServerPort
private int port;
@Bean
public ServerList<Server> ribbonServerList() {
return new StaticServerList<>(new Server("localhost", this.port));
}
}
}

178
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/post/LocationRewriteFilterTests.java

@ -0,0 +1,178 @@ @@ -0,0 +1,178 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package org.springframework.cloud.netflix.zuul.filters.post;
import com.netflix.util.Pair;
import com.netflix.zuul.context.RequestContext;
import org.junit.Before;
import org.junit.Test;
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.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import java.util.Collections;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* @author Biju Kunjummen
*/
public class LocationRewriteFilterTests {
private final String ZUUL_HOST = "myzuul.com";
private final String ZUUL_SCHEME = "https";
private final int ZUUL_PORT = 8443;
private final String ZUUL_BASE_URL = String.format("%s://%s:%d", ZUUL_SCHEME,
ZUUL_HOST, ZUUL_PORT);
private final String SERVER_HOST = "someserver.com";
private final String SERVER_SCHEME = "http";
private final int SERVER_PORT = 8564;
private final String SERVER_BASE_URL = String.format("%s://%s:%d", SERVER_SCHEME,
SERVER_HOST, SERVER_PORT);
@Before
public void before() {
RequestContext context = new RequestContext();
RequestContext.testSetCurrentContext(context);
}
@Test
public void shouldRewriteLocationHeadersWithRoutePrefix() {
RequestContext context = RequestContext.getCurrentContext();
ZuulProperties zuulProperties = new ZuulProperties();
LocationRewriteFilter filter = setFilterUpWith(context, zuulProperties,
new Route("service1", "/redirectingUri", "service1", "prefix", false,
Collections.EMPTY_SET, true),
"/prefix/redirectingUri", "/redirectedUri;someparam?param1=abc");
filter.run();
assertThat(getLocationHeader(context).second()).isEqualTo(String
.format("%s/prefix/redirectedUri;someparam?param1=abc", ZUUL_BASE_URL));
}
@Test
public void shouldBeUntouchedIfNoRoutesFound() {
RequestContext context = RequestContext.getCurrentContext();
ZuulProperties zuulProperties = new ZuulProperties();
LocationRewriteFilter filter = setFilterUpWith(context, zuulProperties, null,
"/prefix/redirectingUri", "/redirectedUri;someparam?param1=abc");
filter.run();
assertThat(getLocationHeader(context).second()).isEqualTo(
String.format("%s/redirectedUri;someparam?param1=abc", SERVER_BASE_URL));
}
@Test
public void shouldRewriteLocationHeadersIfPrefixIsNotStripped() {
RequestContext context = RequestContext.getCurrentContext();
ZuulProperties zuulProperties = new ZuulProperties();
LocationRewriteFilter filter = setFilterUpWith(context, zuulProperties,
new Route("service1", "/something/redirectingUri", "service1", "prefix",
false, Collections.EMPTY_SET, false),
"/prefix/redirectingUri",
"/something/redirectedUri;someparam?param1=abc");
filter.run();
assertThat(getLocationHeader(context).second()).isEqualTo(String.format(
"%s/something/redirectedUri;someparam?param1=abc", ZUUL_BASE_URL));
}
@Test
public void shouldRewriteLocationHeadersIfPrefixIsEmpty() {
RequestContext context = RequestContext.getCurrentContext();
ZuulProperties zuulProperties = new ZuulProperties();
LocationRewriteFilter filter = setFilterUpWith(context, zuulProperties,
new Route("service1", "/something/redirectingUri", "service1", "", false,
Collections.EMPTY_SET, true),
"/redirectingUri", "/something/redirectedUri;someparam?param1=abc");
filter.run();
assertThat(getLocationHeader(context).second()).isEqualTo(String.format(
"%s/something/redirectedUri;someparam?param1=abc", ZUUL_BASE_URL));
}
@Test
public void shouldAddBackGlobalPrefixIfPresent() {
RequestContext context = RequestContext.getCurrentContext();
ZuulProperties zuulProperties = new ZuulProperties();
zuulProperties.setPrefix("global");
zuulProperties.setStripPrefix(true);
LocationRewriteFilter filter = setFilterUpWith(context, zuulProperties,
new Route("service1", "/something/redirectingUri", "service1", "prefix",
false, Collections.EMPTY_SET, true),
"/global/prefix/redirectingUri",
"/something/redirectedUri;someparam?param1=abc");
filter.run();
assertThat(getLocationHeader(context).second()).isEqualTo(String.format(
"%s/global/prefix/something/redirectedUri;someparam?param1=abc",
ZUUL_BASE_URL));
}
@Test
public void shouldNotAddBackGlobalPrefixIfNotStripped() {
RequestContext context = RequestContext.getCurrentContext();
ZuulProperties zuulProperties = new ZuulProperties();
zuulProperties.setPrefix("global");
zuulProperties.setStripPrefix(false);
LocationRewriteFilter filter = setFilterUpWith(context, zuulProperties,
new Route("service1", "/something/redirectingUri", "service1", "prefix",
false, Collections.EMPTY_SET, true),
"/global/prefix/redirectingUri",
"/global/something/redirectedUri;someparam?param1=abc");
filter.run();
assertThat(getLocationHeader(context).second()).isEqualTo(String.format(
"%s/global/prefix/something/redirectedUri;someparam?param1=abc",
ZUUL_BASE_URL));
}
private LocationRewriteFilter setFilterUpWith(RequestContext context,
ZuulProperties zuulProperties, Route route, String toZuulRequestUri,
String redirectedUri) {
MockHttpServletRequest httpServletRequest = new MockHttpServletRequest();
httpServletRequest.setRequestURI(toZuulRequestUri);
httpServletRequest.setServerName(ZUUL_HOST);
httpServletRequest.setScheme(ZUUL_SCHEME);
httpServletRequest.setServerPort(ZUUL_PORT);
context.setRequest(httpServletRequest);
MockHttpServletResponse httpServletResponse = new MockHttpServletResponse();
context.getZuulResponseHeaders().add(new Pair<>("Location",
String.format("%s%s", SERVER_BASE_URL, redirectedUri)));
context.setResponse(httpServletResponse);
RouteLocator routeLocator = mock(RouteLocator.class);
when(routeLocator.getMatchingRoute(toZuulRequestUri)).thenReturn(route);
LocationRewriteFilter filter = new LocationRewriteFilter(zuulProperties,
routeLocator);
return filter;
}
private Pair<String, String> getLocationHeader(RequestContext ctx) {
if (ctx.getZuulResponseHeaders() != null) {
for (Pair<String, String> pair : ctx.getZuulResponseHeaders()) {
if (pair.first().equals("Location")) {
return pair;
}
}
}
return null;
}
}

107
spring-cloud-netflix-core/src/test/java/org/springframework/cloud/netflix/zuul/filters/route/support/RibbonCommandHystrixThreadPoolKeyTests.java

@ -0,0 +1,107 @@ @@ -0,0 +1,107 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.springframework.cloud.netflix.zuul.filters.route.support;
import com.netflix.client.AbstractLoadBalancerAwareClient;
import com.netflix.client.ClientRequest;
import com.netflix.client.http.HttpResponse;
import com.netflix.hystrix.HystrixCommandProperties;
import org.junit.Before;
import org.junit.Test;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Yongsung Yoon
*/
public class RibbonCommandHystrixThreadPoolKeyTests {
private ZuulProperties zuulProperties;
@Before
public void setUp() throws Exception {
zuulProperties = new ZuulProperties();
}
@Test
public void testDefaultHystrixThreadPoolKey() throws Exception {
zuulProperties.setRibbonIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD);
TestRibbonCommand ribbonCommand1 = new TestRibbonCommand("testCommand1", zuulProperties);
TestRibbonCommand ribbonCommand2 = new TestRibbonCommand("testCommand2", zuulProperties);
// CommandGroupKey should be used as ThreadPoolKey as default.
assertThat(ribbonCommand1.getThreadPoolKey().name()).isEqualTo(ribbonCommand1.getCommandGroup().name());
assertThat(ribbonCommand2.getThreadPoolKey().name()).isEqualTo(ribbonCommand2.getCommandGroup().name());
}
@Test
public void testUseSeparateThreadPools() throws Exception {
zuulProperties.setRibbonIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD);
zuulProperties.getThreadPool().setUseSeparateThreadPools(true);
TestRibbonCommand ribbonCommand1 = new TestRibbonCommand("testCommand1", zuulProperties);
TestRibbonCommand ribbonCommand2 = new TestRibbonCommand("testCommand2", zuulProperties);
assertThat(ribbonCommand1.getThreadPoolKey().name()).isEqualTo("testCommand1");
assertThat(ribbonCommand2.getThreadPoolKey().name()).isEqualTo("testCommand2");
}
@Test
public void testThreadPoolKeyPrefix() throws Exception {
final String prefix = "zuulgw-";
zuulProperties.setRibbonIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.THREAD);
zuulProperties.getThreadPool().setUseSeparateThreadPools(true);
zuulProperties.getThreadPool().setThreadPoolKeyPrefix(prefix);
TestRibbonCommand ribbonCommand1 = new TestRibbonCommand("testCommand1", zuulProperties);
TestRibbonCommand ribbonCommand2 = new TestRibbonCommand("testCommand2", zuulProperties);
assertThat(ribbonCommand1.getThreadPoolKey().name()).isEqualTo(prefix + "testCommand1");
assertThat(ribbonCommand2.getThreadPoolKey().name()).isEqualTo(prefix + "testCommand2");
}
@Test
public void testNoSideEffectOnSemaphoreIsolation() throws Exception {
final String prefix = "zuulgw-";
zuulProperties.setRibbonIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE);
zuulProperties.getThreadPool().setUseSeparateThreadPools(true);
zuulProperties.getThreadPool().setThreadPoolKeyPrefix(prefix);
TestRibbonCommand ribbonCommand1 = new TestRibbonCommand("testCommand1", zuulProperties);
TestRibbonCommand ribbonCommand2 = new TestRibbonCommand("testCommand2", zuulProperties);
// There should be no side effect on semaphore isolation
assertThat(ribbonCommand1.getThreadPoolKey().name()).isEqualTo(ribbonCommand1.getCommandGroup().name());
assertThat(ribbonCommand2.getThreadPoolKey().name()).isEqualTo(ribbonCommand2.getCommandGroup().name());
}
public static class TestRibbonCommand
extends AbstractRibbonCommand<AbstractLoadBalancerAwareClient<ClientRequest, HttpResponse>, ClientRequest, HttpResponse> {
public TestRibbonCommand(String commandKey, ZuulProperties zuulProperties) {
super(commandKey, null, null, zuulProperties);
}
@Override
protected ClientRequest createRequest() throws Exception {
return null;
}
}
}

19
spring-cloud-netflix-core/src/test/resources/feign-properties.properties

@ -0,0 +1,19 @@ @@ -0,0 +1,19 @@
# This configuration used by test class org.springframework.cloud.netflix.feign.FeignClientUsingPropertiesTests
logging.level.org.springframework.cloud.netflix.feign=debug
feign.client.default-to-properties=true
feign.client.default-config=default
feign.client.config.default.connectTimeout=5000
feign.client.config.default.readTimeout=5000
feign.client.config.default.loggerLevel=full
feign.client.config.default.errorDecoder=org.springframework.cloud.netflix.feign.FeignClientUsingPropertiesTests.DefaultErrorDecoder
feign.client.config.default.retryer=org.springframework.cloud.netflix.feign.FeignClientUsingPropertiesTests.NoRetryer
feign.client.config.default.decode404=true
feign.client.config.foo.requestInterceptors[0]=org.springframework.cloud.netflix.feign.FeignClientUsingPropertiesTests.FooRequestInterceptor
feign.client.config.foo.requestInterceptors[1]=org.springframework.cloud.netflix.feign.FeignClientUsingPropertiesTests.BarRequestInterceptor
feign.client.config.bar.connectTimeout=1000
feign.client.config.bar.readTimeout=1000

4
spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaClientConfigBean.java

@ -33,12 +33,14 @@ import com.netflix.discovery.shared.transport.EurekaTransportConfig; @@ -33,12 +33,14 @@ import com.netflix.discovery.shared.transport.EurekaTransportConfig;
import lombok.Data;
import static org.springframework.cloud.netflix.eureka.EurekaConstants.DEFAULT_PREFIX;
/**
* @author Dave Syer
*/
@Data
@ConfigurationProperties(EurekaClientConfigBean.PREFIX)
public class EurekaClientConfigBean implements EurekaClientConfig, EurekaConstants {
public class EurekaClientConfigBean implements EurekaClientConfig {
public static final String PREFIX = "eureka.client";

8
spring-cloud-netflix-eureka-client/src/main/java/org/springframework/cloud/netflix/eureka/EurekaConstants.java

@ -19,8 +19,12 @@ package org.springframework.cloud.netflix.eureka; @@ -19,8 +19,12 @@ package org.springframework.cloud.netflix.eureka;
/**
* @author Spencer Gibb
*/
public interface EurekaConstants {
public class EurekaConstants {
String DEFAULT_PREFIX = "/eureka";
public static final String DEFAULT_PREFIX = "/eureka";
private EurekaConstants() {
throw new AssertionError("Must not instantiate constant utility class");
}
}

3
spring-cloud-netflix-eureka-server/src/main/java/org/springframework/cloud/netflix/eureka/server/EurekaServerConfigBean.java

@ -23,7 +23,6 @@ import java.util.Set; @@ -23,7 +23,6 @@ import java.util.Set;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.netflix.eureka.EurekaConstants;
import org.springframework.core.env.PropertyResolver;
import com.netflix.eureka.EurekaServerConfig;
@ -36,7 +35,7 @@ import lombok.Data; @@ -36,7 +35,7 @@ import lombok.Data;
*/
@Data
@ConfigurationProperties(EurekaServerConfigBean.PREFIX)
public class EurekaServerConfigBean implements EurekaServerConfig, EurekaConstants {
public class EurekaServerConfigBean implements EurekaServerConfig {
public static final String PREFIX = "eureka.server";

Loading…
Cancel
Save