Revert "Adds zuul property to customize Hystrix ExecutionIsolationStrategy"

This reverts commit 21df6e43.
parent 58cf5284
...@@ -1060,8 +1060,6 @@ Zuul's rule engine allows rules and filters to be written in essentially any JVM ...@@ -1060,8 +1060,6 @@ Zuul's rule engine allows rules and filters to be written in essentially any JVM
NOTE: The configuration property `zuul.max.host.connections` has been replaced by two new properties, `zuul.host.maxTotalConnections` and `zuul.host.maxPerRouteConnections` which default to 200 and 20 respectively. NOTE: The configuration property `zuul.max.host.connections` has been replaced by two new properties, `zuul.host.maxTotalConnections` and `zuul.host.maxPerRouteConnections` which default to 200 and 20 respectively.
NOTE: Default Hystrix isolation pattern (ExecutionIsolationStrategy) for all routes is SEMAPHORE. `zuul.ribbonIsolationStrategy` can be changed to THREAD if this isolation pattern is preferred.
[[netflix-zuul-reverse-proxy]] [[netflix-zuul-reverse-proxy]]
=== Embedded Zuul Reverse Proxy === Embedded Zuul Reverse Proxy
......
...@@ -108,6 +108,6 @@ public class RibbonHttpRequest extends AbstractClientHttpRequest { ...@@ -108,6 +108,6 @@ public class RibbonHttpRequest extends AbstractClientHttpRequest {
} }
private boolean isDynamic(String name) { private boolean isDynamic(String name) {
return "Content-Length".equalsIgnoreCase(name) || "Transfer-Encoding".equalsIgnoreCase(name); return name.equalsIgnoreCase("Content-Length") || name.equalsIgnoreCase("Transfer-Encoding");
} }
} }
...@@ -86,24 +86,20 @@ public class ZuulProxyConfiguration extends ZuulConfiguration { ...@@ -86,24 +86,20 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
@Configuration @Configuration
@ConditionalOnProperty(name = "zuul.ribbon.httpclient.enabled", matchIfMissing = true) @ConditionalOnProperty(name = "zuul.ribbon.httpclient.enabled", matchIfMissing = true)
protected static class HttpClientRibbonConfiguration { protected static class HttpClientRibbonConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public RibbonCommandFactory<?> ribbonCommandFactory( public RibbonCommandFactory<?> ribbonCommandFactory(SpringClientFactory clientFactory) {
SpringClientFactory clientFactory, ZuulProperties zuulProperties) { return new HttpClientRibbonCommandFactory(clientFactory);
return new HttpClientRibbonCommandFactory(clientFactory, zuulProperties);
} }
} }
@Configuration @Configuration
@ConditionalOnProperty("zuul.ribbon.restclient.enabled") @ConditionalOnProperty("zuul.ribbon.restclient.enabled")
protected static class RestClientRibbonConfiguration { protected static class RestClientRibbonConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public RibbonCommandFactory<?> ribbonCommandFactory( public RibbonCommandFactory<?> ribbonCommandFactory(SpringClientFactory clientFactory) {
SpringClientFactory clientFactory, ZuulProperties zuulProperties) { return new RestClientRibbonCommandFactory(clientFactory);
return new RestClientRibbonCommandFactory(clientFactory, zuulProperties);
} }
} }
...@@ -111,12 +107,10 @@ public class ZuulProxyConfiguration extends ZuulConfiguration { ...@@ -111,12 +107,10 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
@ConditionalOnProperty("zuul.ribbon.okhttp.enabled") @ConditionalOnProperty("zuul.ribbon.okhttp.enabled")
@ConditionalOnClass(name = "okhttp3.OkHttpClient") @ConditionalOnClass(name = "okhttp3.OkHttpClient")
protected static class OkHttpRibbonConfiguration { protected static class OkHttpRibbonConfiguration {
@Bean @Bean
@ConditionalOnMissingBean @ConditionalOnMissingBean
public RibbonCommandFactory<?> ribbonCommandFactory( public RibbonCommandFactory<?> ribbonCommandFactory(SpringClientFactory clientFactory) {
SpringClientFactory clientFactory, ZuulProperties zuulProperties) { return new OkHttpRibbonCommandFactory(clientFactory);
return new OkHttpRibbonCommandFactory(clientFactory, zuulProperties);
} }
} }
...@@ -124,16 +118,18 @@ public class ZuulProxyConfiguration extends ZuulConfiguration { ...@@ -124,16 +118,18 @@ public class ZuulProxyConfiguration extends ZuulConfiguration {
@Bean @Bean
public PreDecorationFilter preDecorationFilter(RouteLocator routeLocator, public PreDecorationFilter preDecorationFilter(RouteLocator routeLocator,
ProxyRequestHelper proxyRequestHelper) { ProxyRequestHelper proxyRequestHelper) {
return new PreDecorationFilter(routeLocator, this.server.getServletPrefix(), return new PreDecorationFilter(routeLocator,
this.zuulProperties, proxyRequestHelper); this.server.getServletPrefix(),
this.zuulProperties,
proxyRequestHelper);
} }
// route filters // route filters
@Bean @Bean
public RibbonRoutingFilter ribbonRoutingFilter(ProxyRequestHelper helper, public RibbonRoutingFilter ribbonRoutingFilter(ProxyRequestHelper helper,
RibbonCommandFactory<?> ribbonCommandFactory) { RibbonCommandFactory<?> ribbonCommandFactory) {
RibbonRoutingFilter filter = new RibbonRoutingFilter(helper, ribbonCommandFactory, RibbonRoutingFilter filter = new RibbonRoutingFilter(helper,
this.requestCustomizers); ribbonCommandFactory, this.requestCustomizers);
return filter; return filter;
} }
......
...@@ -31,14 +31,10 @@ import org.springframework.boot.context.properties.ConfigurationProperties; ...@@ -31,14 +31,10 @@ import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy;
import lombok.AllArgsConstructor; import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.NoArgsConstructor; import lombok.NoArgsConstructor;
import static com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
* @author Dave Syer * @author Dave Syer
...@@ -140,10 +136,6 @@ public class ZuulProperties { ...@@ -140,10 +136,6 @@ public class ZuulProperties {
*/ */
private boolean sslHostnameValidationEnabled =true; private boolean sslHostnameValidationEnabled =true;
private ExecutionIsolationStrategy ribbonIsolationStrategy = SEMAPHORE;
private HystrixSemaphore semaphore = new HystrixSemaphore();
public Set<String> getIgnoredHeaders() { public Set<String> getIgnoredHeaders() {
Set<String> ignoredHeaders = new LinkedHashSet<>(this.ignoredHeaders); Set<String> ignoredHeaders = new LinkedHashSet<>(this.ignoredHeaders);
if (ClassUtils.isPresent( if (ClassUtils.isPresent(
...@@ -311,17 +303,6 @@ public class ZuulProperties { ...@@ -311,17 +303,6 @@ public class ZuulProperties {
*/ */
private int maxPerRouteConnections = 20; private int maxPerRouteConnections = 20;
} }
@Data
@AllArgsConstructor
@NoArgsConstructor
public static class HystrixSemaphore {
/**
* The maximum number of total semaphores for Hystrix.
*/
private int maxSemaphores = 100;
}
public String getServletPattern() { public String getServletPattern() {
String path = this.servletPath; String path = this.servletPath;
......
...@@ -23,7 +23,6 @@ import java.io.InputStream; ...@@ -23,7 +23,6 @@ import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.util.List; import java.util.List;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommand; import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommand;
import org.springframework.util.MultiValueMap; import org.springframework.util.MultiValueMap;
...@@ -34,13 +33,23 @@ import com.netflix.niws.client.http.RestClient; ...@@ -34,13 +33,23 @@ import com.netflix.niws.client.http.RestClient;
/** /**
* Hystrix wrapper around Eureka Ribbon command * Hystrix wrapper around Eureka Ribbon command
* *
* see <a href="https://github.com/Netflix/zuul/blob/master/zuul-netflix/src/main/java/com/netflix/zuul/dependency/ribbon/hystrix/RibbonCommand.java">original</a> * see original
* https://github.com/Netflix/zuul/blob/master/zuul-netflix/src/main/java/com/
* netflix/zuul/dependency/ribbon/hystrix/RibbonCommand.java
*/ */
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public class RestClientRibbonCommand extends AbstractRibbonCommand<RestClient, HttpRequest, HttpResponse> { public class RestClientRibbonCommand extends AbstractRibbonCommand<RestClient, HttpRequest, HttpResponse> {
public RestClientRibbonCommand(String commandKey, RestClient client, RibbonCommandContext context, ZuulProperties zuulProperties) { @SuppressWarnings("unused")
super(commandKey, client, context, zuulProperties); @Deprecated
public RestClientRibbonCommand(String commandKey, RestClient restClient, HttpRequest.Verb verb, String uri,
Boolean retryable, MultiValueMap<String, String> headers,
MultiValueMap<String, String> params, InputStream requestEntity) {
this(commandKey, restClient, new RibbonCommandContext(commandKey, verb.verb(), uri, retryable, headers, params, requestEntity));
}
public RestClientRibbonCommand(String commandKey, RestClient client, RibbonCommandContext context) {
super(commandKey, client, context);
} }
@Override @Override
......
...@@ -17,32 +17,28 @@ ...@@ -17,32 +17,28 @@
package org.springframework.cloud.netflix.zuul.filters.route; package org.springframework.cloud.netflix.zuul.filters.route;
import com.netflix.client.http.HttpRequest;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import com.netflix.client.http.HttpRequest;
import com.netflix.niws.client.http.RestClient; import com.netflix.niws.client.http.RestClient;
import lombok.RequiredArgsConstructor;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
*/ */
@RequiredArgsConstructor public class RestClientRibbonCommandFactory implements RibbonCommandFactory<RestClientRibbonCommand> {
public class RestClientRibbonCommandFactory
implements RibbonCommandFactory<RestClientRibbonCommand> {
private final SpringClientFactory clientFactory; private final SpringClientFactory clientFactory;
private final ZuulProperties zuulProperties; public RestClientRibbonCommandFactory(SpringClientFactory clientFactory) {
this.clientFactory = clientFactory;
}
@Override @Override
@SuppressWarnings("deprecation") @SuppressWarnings("deprecation")
public RestClientRibbonCommand create(RibbonCommandContext context) { public RestClientRibbonCommand create(RibbonCommandContext context) {
RestClient restClient = this.clientFactory.getClient(context.getServiceId(), RestClient restClient = this.clientFactory.getClient(context.getServiceId(),
RestClient.class); RestClient.class);
return new RestClientRibbonCommand(context.getServiceId(), restClient, context, return new RestClientRibbonCommand(context.getServiceId(), restClient, context);
this.zuulProperties);
} }
public SpringClientFactory getClientFactory() { public SpringClientFactory getClientFactory() {
......
...@@ -19,6 +19,7 @@ package org.springframework.cloud.netflix.zuul.filters.route; ...@@ -19,6 +19,7 @@ package org.springframework.cloud.netflix.zuul.filters.route;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer; import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer;
...@@ -53,11 +54,17 @@ public class RibbonCommandContext { ...@@ -53,11 +54,17 @@ public class RibbonCommandContext {
private final List<RibbonRequestCustomizer> requestCustomizers; private final List<RibbonRequestCustomizer> requestCustomizers;
private Long contentLength; private Long contentLength;
public RibbonCommandContext(String serviceId, String method, String uri, Boolean retryable,
MultiValueMap<String, String> headers, MultiValueMap<String, String> params,
InputStream requestEntity) {
this(serviceId, method, uri, retryable, headers, params, requestEntity,
new ArrayList<RibbonRequestCustomizer>(), null);
}
public URI uri() { public URI uri() {
try { try {
return new URI(this.uri); return new URI(this.uri);
} } catch (URISyntaxException e) {
catch (URISyntaxException e) {
ReflectionUtils.rethrowRuntimeException(e); ReflectionUtils.rethrowRuntimeException(e);
} }
return null; return null;
......
...@@ -47,8 +47,8 @@ public class RibbonRoutingFilter extends ZuulFilter { ...@@ -47,8 +47,8 @@ public class RibbonRoutingFilter extends ZuulFilter {
protected List<RibbonRequestCustomizer> requestCustomizers; protected List<RibbonRequestCustomizer> requestCustomizers;
public RibbonRoutingFilter(ProxyRequestHelper helper, public RibbonRoutingFilter(ProxyRequestHelper helper,
RibbonCommandFactory<?> ribbonCommandFactory, RibbonCommandFactory<?> ribbonCommandFactory,
List<RibbonRequestCustomizer> requestCustomizers) { List<RibbonRequestCustomizer> requestCustomizers) {
this.helper = helper; this.helper = helper;
this.ribbonCommandFactory = ribbonCommandFactory; this.ribbonCommandFactory = ribbonCommandFactory;
this.requestCustomizers = requestCustomizers; this.requestCustomizers = requestCustomizers;
...@@ -120,13 +120,12 @@ public class RibbonRoutingFilter extends ZuulFilter { ...@@ -120,13 +120,12 @@ public class RibbonRoutingFilter extends ZuulFilter {
uri = uri.replace("//", "/"); uri = uri.replace("//", "/");
return new RibbonCommandContext(serviceId, verb, uri, retryable, headers, params, return new RibbonCommandContext(serviceId, verb, uri, retryable, headers, params,
requestEntity, this.requestCustomizers, request.getContentLengthLong()); requestEntity, this.requestCustomizers);
} }
protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception { protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception {
Map<String, Object> info = this.helper.debug(context.getMethod(), Map<String, Object> info = this.helper.debug(context.getMethod(), context.getUri(),
context.getUri(), context.getHeaders(), context.getParams(), context.getHeaders(), context.getParams(), context.getRequestEntity());
context.getRequestEntity());
RibbonCommand command = this.ribbonCommandFactory.create(context); RibbonCommand command = this.ribbonCommandFactory.create(context);
try { try {
......
...@@ -20,7 +20,6 @@ package org.springframework.cloud.netflix.zuul.filters.route.apache; ...@@ -20,7 +20,6 @@ package org.springframework.cloud.netflix.zuul.filters.route.apache;
import org.springframework.cloud.netflix.ribbon.apache.RibbonApacheHttpRequest; import org.springframework.cloud.netflix.ribbon.apache.RibbonApacheHttpRequest;
import org.springframework.cloud.netflix.ribbon.apache.RibbonApacheHttpResponse; import org.springframework.cloud.netflix.ribbon.apache.RibbonApacheHttpResponse;
import org.springframework.cloud.netflix.ribbon.apache.RibbonLoadBalancingHttpClient; import org.springframework.cloud.netflix.ribbon.apache.RibbonLoadBalancingHttpClient;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext;
import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommand; import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommand;
...@@ -30,10 +29,8 @@ import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibb ...@@ -30,10 +29,8 @@ import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibb
public class HttpClientRibbonCommand extends AbstractRibbonCommand<RibbonLoadBalancingHttpClient, RibbonApacheHttpRequest, RibbonApacheHttpResponse> { public class HttpClientRibbonCommand extends AbstractRibbonCommand<RibbonLoadBalancingHttpClient, RibbonApacheHttpRequest, RibbonApacheHttpResponse> {
public HttpClientRibbonCommand(final String commandKey, public HttpClientRibbonCommand(final String commandKey,
final RibbonLoadBalancingHttpClient client, final RibbonLoadBalancingHttpClient client, RibbonCommandContext context) {
final RibbonCommandContext context, super(commandKey, client, context);
final ZuulProperties zuulProperties) {
super(commandKey, client, context, zuulProperties);
} }
@Override @Override
......
...@@ -18,7 +18,6 @@ package org.springframework.cloud.netflix.zuul.filters.route.apache; ...@@ -18,7 +18,6 @@ package org.springframework.cloud.netflix.zuul.filters.route.apache;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.ribbon.apache.RibbonLoadBalancingHttpClient; import org.springframework.cloud.netflix.ribbon.apache.RibbonLoadBalancingHttpClient;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory;
...@@ -32,8 +31,6 @@ public class HttpClientRibbonCommandFactory implements ...@@ -32,8 +31,6 @@ public class HttpClientRibbonCommandFactory implements
RibbonCommandFactory<HttpClientRibbonCommand> { RibbonCommandFactory<HttpClientRibbonCommand> {
private final SpringClientFactory clientFactory; private final SpringClientFactory clientFactory;
private final ZuulProperties zuulProperties;
@Override @Override
public HttpClientRibbonCommand create(final RibbonCommandContext context) { public HttpClientRibbonCommand create(final RibbonCommandContext context) {
...@@ -42,7 +39,7 @@ public class HttpClientRibbonCommandFactory implements ...@@ -42,7 +39,7 @@ public class HttpClientRibbonCommandFactory implements
serviceId, RibbonLoadBalancingHttpClient.class); serviceId, RibbonLoadBalancingHttpClient.class);
client.setLoadBalancer(this.clientFactory.getLoadBalancer(serviceId)); client.setLoadBalancer(this.clientFactory.getLoadBalancer(serviceId));
return new HttpClientRibbonCommand(serviceId, client, context, zuulProperties); return new HttpClientRibbonCommand(serviceId, client, context);
} }
} }
...@@ -20,7 +20,6 @@ package org.springframework.cloud.netflix.zuul.filters.route.okhttp; ...@@ -20,7 +20,6 @@ package org.springframework.cloud.netflix.zuul.filters.route.okhttp;
import org.springframework.cloud.netflix.ribbon.okhttp.OkHttpLoadBalancingClient; import org.springframework.cloud.netflix.ribbon.okhttp.OkHttpLoadBalancingClient;
import org.springframework.cloud.netflix.ribbon.okhttp.OkHttpRibbonRequest; import org.springframework.cloud.netflix.ribbon.okhttp.OkHttpRibbonRequest;
import org.springframework.cloud.netflix.ribbon.okhttp.OkHttpRibbonResponse; import org.springframework.cloud.netflix.ribbon.okhttp.OkHttpRibbonResponse;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext;
import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommand; import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibbonCommand;
...@@ -30,10 +29,8 @@ import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibb ...@@ -30,10 +29,8 @@ import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibb
public class OkHttpRibbonCommand extends AbstractRibbonCommand<OkHttpLoadBalancingClient, OkHttpRibbonRequest, OkHttpRibbonResponse> { public class OkHttpRibbonCommand extends AbstractRibbonCommand<OkHttpLoadBalancingClient, OkHttpRibbonRequest, OkHttpRibbonResponse> {
public OkHttpRibbonCommand(final String commandKey, public OkHttpRibbonCommand(final String commandKey,
final OkHttpLoadBalancingClient client, final OkHttpLoadBalancingClient client, RibbonCommandContext context) {
final RibbonCommandContext context, super(commandKey, client, context);
final ZuulProperties zuulProperties) {
super(commandKey, client, context, zuulProperties);
} }
@Override @Override
......
...@@ -18,7 +18,6 @@ package org.springframework.cloud.netflix.zuul.filters.route.okhttp; ...@@ -18,7 +18,6 @@ package org.springframework.cloud.netflix.zuul.filters.route.okhttp;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.ribbon.okhttp.OkHttpLoadBalancingClient; import org.springframework.cloud.netflix.ribbon.okhttp.OkHttpLoadBalancingClient;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory;
...@@ -32,8 +31,6 @@ public class OkHttpRibbonCommandFactory implements ...@@ -32,8 +31,6 @@ public class OkHttpRibbonCommandFactory implements
RibbonCommandFactory<OkHttpRibbonCommand> { RibbonCommandFactory<OkHttpRibbonCommand> {
private final SpringClientFactory clientFactory; private final SpringClientFactory clientFactory;
private final ZuulProperties zuulProperties;
@Override @Override
public OkHttpRibbonCommand create(final RibbonCommandContext context) { public OkHttpRibbonCommand create(final RibbonCommandContext context) {
...@@ -42,7 +39,7 @@ public class OkHttpRibbonCommandFactory implements ...@@ -42,7 +39,7 @@ public class OkHttpRibbonCommandFactory implements
serviceId, OkHttpLoadBalancingClient.class); serviceId, OkHttpLoadBalancingClient.class);
client.setLoadBalancer(this.clientFactory.getLoadBalancer(serviceId)); client.setLoadBalancer(this.clientFactory.getLoadBalancer(serviceId));
return new OkHttpRibbonCommand(serviceId, client, context, zuulProperties); return new OkHttpRibbonCommand(serviceId, client, context);
} }
} }
...@@ -18,10 +18,10 @@ ...@@ -18,10 +18,10 @@
package org.springframework.cloud.netflix.zuul.filters.route.support; package org.springframework.cloud.netflix.zuul.filters.route.support;
import org.springframework.cloud.netflix.ribbon.RibbonHttpResponse; import org.springframework.cloud.netflix.ribbon.RibbonHttpResponse;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommand; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommand;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext;
import org.springframework.http.client.ClientHttpResponse; import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.StringUtils;
import com.netflix.client.AbstractLoadBalancerAwareClient; import com.netflix.client.AbstractLoadBalancerAwareClient;
import com.netflix.client.ClientRequest; import com.netflix.client.ClientRequest;
...@@ -32,48 +32,39 @@ import com.netflix.hystrix.HystrixCommand; ...@@ -32,48 +32,39 @@ import com.netflix.hystrix.HystrixCommand;
import com.netflix.hystrix.HystrixCommandGroupKey; import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixCommandKey; import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandProperties; import com.netflix.hystrix.HystrixCommandProperties;
import com.netflix.hystrix.HystrixCommandProperties.ExecutionIsolationStrategy;
import com.netflix.zuul.constants.ZuulConstants; import com.netflix.zuul.constants.ZuulConstants;
import com.netflix.zuul.context.RequestContext; import com.netflix.zuul.context.RequestContext;
/** /**
* @author Spencer Gibb * @author Spencer Gibb
*/ */
public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwareClient<RQ, RS>, RQ extends ClientRequest, RS extends HttpResponse> public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwareClient<RQ, RS>, RQ extends ClientRequest, RS extends HttpResponse> extends HystrixCommand<ClientHttpResponse> implements
extends HystrixCommand<ClientHttpResponse> implements RibbonCommand { RibbonCommand {
protected final LBC client; protected final LBC client;
protected RibbonCommandContext context; protected RibbonCommandContext context;
public AbstractRibbonCommand(LBC client, RibbonCommandContext context, public AbstractRibbonCommand(LBC client, RibbonCommandContext context) {
ZuulProperties zuulProperties) { this("default", client, context);
this("default", client, context, zuulProperties);
} }
public AbstractRibbonCommand(String commandKey, LBC client, public AbstractRibbonCommand(String commandKey, LBC client, RibbonCommandContext context) {
RibbonCommandContext context, ZuulProperties zuulProperties) { super(getSetter(commandKey));
super(getSetter(commandKey, zuulProperties));
this.client = client; this.client = client;
this.context = context; this.context = context;
} }
protected static Setter getSetter(final String commandKey, protected static Setter getSetter(final String commandKey) {
ZuulProperties zuulProperties) {
// we want to default to semaphore-isolation since this wraps
// 2 others commands that are already thread isolated
// @formatter:off // @formatter:off
final HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter() final String name = ZuulConstants.ZUUL_EUREKA + commandKey + ".semaphore.maxSemaphores";
.withExecutionIsolationStrategy(zuulProperties.getRibbonIsolationStrategy()); final DynamicIntProperty value = DynamicPropertyFactory.getInstance()
if (zuulProperties.getRibbonIsolationStrategy() == ExecutionIsolationStrategy.SEMAPHORE){ .getIntProperty(name, 100);
final String name = ZuulConstants.ZUUL_EUREKA + commandKey + ".semaphore.maxSemaphores"; final HystrixCommandProperties.Setter setter = HystrixCommandProperties .Setter()
// we want to default to semaphore-isolation since this wraps .withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE)
// 2 others commands that are already thread isolated .withExecutionIsolationSemaphoreMaxConcurrentRequests(value.get());
final DynamicIntProperty value = DynamicPropertyFactory.getInstance()
.getIntProperty(name, 100);
setter.withExecutionIsolationSemaphoreMaxConcurrentRequests(value.get());
} else {
// TODO Find out is some parameters can be set here
}
return Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RibbonCommand")) return Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RibbonCommand"))
.andCommandKey(HystrixCommandKey.Factory.asKey(commandKey + "RibbonCommand")) .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey + "RibbonCommand"))
.andCommandPropertiesDefaults(setter); .andCommandPropertiesDefaults(setter);
...@@ -83,6 +74,10 @@ public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwar ...@@ -83,6 +74,10 @@ public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwar
@Override @Override
protected ClientHttpResponse run() throws Exception { protected ClientHttpResponse run() throws Exception {
final RequestContext context = RequestContext.getCurrentContext(); final RequestContext context = RequestContext.getCurrentContext();
long contentLength = context.getRequest().getContentLengthLong();
if (contentLength != -1) {
this.context.setContentLength(contentLength);
}
RQ request = createRequest(); RQ request = createRequest();
RS response = this.client.executeWithLoadBalancer(request); RS response = this.client.executeWithLoadBalancer(request);
......
...@@ -29,7 +29,6 @@ import java.io.ByteArrayInputStream; ...@@ -29,7 +29,6 @@ import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.net.URI; import java.net.URI;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
...@@ -38,7 +37,6 @@ import org.apache.http.client.config.RequestConfig; ...@@ -38,7 +37,6 @@ import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.HttpUriRequest; import org.apache.http.client.methods.HttpUriRequest;
import org.apache.http.client.methods.RequestBuilder; import org.apache.http.client.methods.RequestBuilder;
import org.junit.Test; import org.junit.Test;
import org.springframework.cloud.netflix.feign.encoding.HttpEncoding;
import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer; import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
...@@ -54,12 +52,10 @@ public class RibbonApacheHttpRequestTests { ...@@ -54,12 +52,10 @@ public class RibbonApacheHttpRequestTests {
String uri = "http://example.com"; String uri = "http://example.com";
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("my-header", "my-value"); headers.add("my-header", "my-value");
headers.add(HttpEncoding.CONTENT_LENGTH, "5192");
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>(); LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("myparam", "myparamval"); params.add("myparam", "myparamval");
RibbonApacheHttpRequest httpRequest = RibbonApacheHttpRequest httpRequest = new RibbonApacheHttpRequest(new RibbonCommandContext("example", "GET", uri, false,
new RibbonApacheHttpRequest( headers, params, null));
new RibbonCommandContext("example", "GET", uri, false, headers, params, null, new ArrayList<RibbonRequestCustomizer>()));
HttpUriRequest request = httpRequest.toRequest(RequestConfig.custom().build()); HttpUriRequest request = httpRequest.toRequest(RequestConfig.custom().build());
...@@ -67,9 +63,7 @@ public class RibbonApacheHttpRequestTests { ...@@ -67,9 +63,7 @@ public class RibbonApacheHttpRequestTests {
assertThat("uri is wrong", request.getURI().toString(), startsWith(uri)); assertThat("uri is wrong", request.getURI().toString(), startsWith(uri));
assertThat("my-header is missing", request.getFirstHeader("my-header"), is(notNullValue())); assertThat("my-header is missing", request.getFirstHeader("my-header"), is(notNullValue()));
assertThat("my-header is wrong", request.getFirstHeader("my-header").getValue(), is(equalTo("my-value"))); assertThat("my-header is wrong", request.getFirstHeader("my-header").getValue(), is(equalTo("my-value")));
assertThat("Content-Length is wrong", request.getFirstHeader(HttpEncoding.CONTENT_LENGTH).getValue(), is(equalTo("5192")));
assertThat("myparam is missing", request.getURI().getQuery(), is(equalTo("myparam=myparamval"))); assertThat("myparam is missing", request.getURI().getQuery(), is(equalTo("myparam=myparamval")));
} }
@Test @Test
......
...@@ -26,11 +26,9 @@ import static org.junit.Assert.assertThat; ...@@ -26,11 +26,9 @@ import static org.junit.Assert.assertThat;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import org.junit.Test; import org.junit.Test;
import org.springframework.cloud.netflix.feign.encoding.HttpEncoding;
import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer; import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandContext;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
...@@ -49,41 +47,33 @@ public class OkHttpRibbonRequestTests { ...@@ -49,41 +47,33 @@ public class OkHttpRibbonRequestTests {
String uri = "http://example.com"; String uri = "http://example.com";
LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>(); LinkedMultiValueMap<String, String> headers = new LinkedMultiValueMap<>();
headers.add("my-header", "my-value"); headers.add("my-header", "my-value");
// headers.add(HttpEncoding.CONTENT_LENGTH, "5192");
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>(); LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("myparam", "myparamval"); params.add("myparam", "myparamval");
RibbonCommandContext context = new RibbonCommandContext("example", "GET", uri, RibbonCommandContext context = new RibbonCommandContext("example", "GET", uri, false, headers, params, null);
false, headers, params, null, new ArrayList<RibbonRequestCustomizer>());
OkHttpRibbonRequest httpRequest = new OkHttpRibbonRequest(context); OkHttpRibbonRequest httpRequest = new OkHttpRibbonRequest(context);
Request request = httpRequest.toRequest(); Request request = httpRequest.toRequest();
assertThat("body is not null", request.body(), is(nullValue())); assertThat("body is not null", request.body(), is(nullValue()));
assertThat("uri is wrong", request.url().toString(), startsWith(uri)); assertThat("uri is wrong", request.url().toString(), startsWith(uri));
assertThat("my-header is wrong", request.header("my-header"), assertThat("my-header is wrong", request.header("my-header"), is(equalTo("my-value")));
is(equalTo("my-value"))); assertThat("myparam is missing", request.url().queryParameter("myparam"), is(equalTo("myparamval")));
assertThat("myparam is missing", request.url().queryParameter("myparam"),
is(equalTo("myparamval")));
} }
@Test @Test
// this situation happens, see // this situation happens, see https://github.com/spring-cloud/spring-cloud-netflix/issues/1042#issuecomment-227723877
// https://github.com/spring-cloud/spring-cloud-netflix/issues/1042#issuecomment-227723877
public void testEmptyEntityGet() throws Exception { public void testEmptyEntityGet() throws Exception {
String entityValue = ""; String entityValue = "";
testEntity(entityValue, new ByteArrayInputStream(entityValue.getBytes()), false, testEntity(entityValue, new ByteArrayInputStream(entityValue.getBytes()), false, "GET");
"GET");
} }
@Test @Test
public void testNonEmptyEntityPost() throws Exception { public void testNonEmptyEntityPost() throws Exception {
String entityValue = "abcd"; String entityValue = "abcd";
testEntity(entityValue, new ByteArrayInputStream(entityValue.getBytes()), true, testEntity(entityValue, new ByteArrayInputStream(entityValue.getBytes()), true, "POST");
"POST");
} }
void testEntity(String entityValue, ByteArrayInputStream requestEntity, void testEntity(String entityValue, ByteArrayInputStream requestEntity, boolean addContentLengthHeader, String method) throws IOException {
boolean addContentLengthHeader, String method) throws IOException {
String lengthString = String.valueOf(entityValue.length()); String lengthString = String.valueOf(entityValue.length());
Long length = null; Long length = null;
String uri = "http://example.com"; String uri = "http://example.com";
...@@ -104,9 +94,8 @@ public class OkHttpRibbonRequestTests { ...@@ -104,9 +94,8 @@ public class OkHttpRibbonRequestTests {
builder.addHeader("from-customizer", "foo"); builder.addHeader("from-customizer", "foo");
} }
}; };
RibbonCommandContext context = new RibbonCommandContext("example", method, uri, RibbonCommandContext context = new RibbonCommandContext("example", method, uri, false,
false, headers, new LinkedMultiValueMap<String, String>(), requestEntity, headers, new LinkedMultiValueMap<String, String>(), requestEntity, Collections.singletonList(requestCustomizer));
Collections.singletonList(requestCustomizer));
context.setContentLength(length); context.setContentLength(length);
OkHttpRibbonRequest httpRequest = new OkHttpRibbonRequest(context); OkHttpRibbonRequest httpRequest = new OkHttpRibbonRequest(context);
...@@ -123,8 +112,7 @@ public class OkHttpRibbonRequestTests { ...@@ -123,8 +112,7 @@ public class OkHttpRibbonRequestTests {
if (!method.equalsIgnoreCase("get")) { if (!method.equalsIgnoreCase("get")) {
assertThat("body is null", request.body(), is(notNullValue())); assertThat("body is null", request.body(), is(notNullValue()));
RequestBody body = request.body(); RequestBody body = request.body();
assertThat("contentLength is wrong", body.contentLength(), assertThat("contentLength is wrong", body.contentLength(), is(equalTo((long) entityValue.length())));
is(equalTo((long) entityValue.length())));
Buffer content = new Buffer(); Buffer content = new Buffer();
body.writeTo(content); body.writeTo(content);
String string = content.readByteString().utf8(); String string = content.readByteString().utf8();
...@@ -132,3 +120,4 @@ public class OkHttpRibbonRequestTests { ...@@ -132,3 +120,4 @@ public class OkHttpRibbonRequestTests {
} }
} }
} }
...@@ -27,13 +27,10 @@ import java.io.ByteArrayInputStream; ...@@ -27,13 +27,10 @@ import java.io.ByteArrayInputStream;
import java.io.InputStream; import java.io.InputStream;
import java.net.URI; import java.net.URI;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer; import org.springframework.cloud.netflix.ribbon.support.RibbonRequestCustomizer;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.StreamUtils; import org.springframework.util.StreamUtils;
...@@ -44,13 +41,6 @@ import com.netflix.client.http.HttpRequest; ...@@ -44,13 +41,6 @@ import com.netflix.client.http.HttpRequest;
*/ */
public class RestClientRibbonCommandTests { public class RestClientRibbonCommandTests {
private ZuulProperties zuulProperties;
@Before
public void setUp() {
zuulProperties = new ZuulProperties();
}
@Test @Test
public void testNullEntity() throws Exception { public void testNullEntity() throws Exception {
String uri = "http://example.com"; String uri = "http://example.com";
...@@ -58,10 +48,8 @@ public class RestClientRibbonCommandTests { ...@@ -58,10 +48,8 @@ public class RestClientRibbonCommandTests {
headers.add("my-header", "my-value"); headers.add("my-header", "my-value");
LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>(); LinkedMultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("myparam", "myparamval"); params.add("myparam", "myparamval");
RestClientRibbonCommand command = RestClientRibbonCommand command = new RestClientRibbonCommand("cmd", null, new RibbonCommandContext("example", "GET", uri, false,
new RestClientRibbonCommand("cmd", null, headers, params, null));
new RibbonCommandContext("example", "GET", uri, false, headers, params, null,new ArrayList<RibbonRequestCustomizer>()),
zuulProperties);
HttpRequest request = command.createRequest(); HttpRequest request = command.createRequest();
...@@ -108,7 +96,7 @@ public class RestClientRibbonCommandTests { ...@@ -108,7 +96,7 @@ public class RestClientRibbonCommandTests {
uri.toString(), false, headers, new LinkedMultiValueMap<String, String>(), uri.toString(), false, headers, new LinkedMultiValueMap<String, String>(),
requestEntity, Collections.singletonList(requestCustomizer)); requestEntity, Collections.singletonList(requestCustomizer));
context.setContentLength(length); context.setContentLength(length);
RestClientRibbonCommand command = new RestClientRibbonCommand("cmd", null, context, zuulProperties); RestClientRibbonCommand command = new RestClientRibbonCommand("cmd", null, context);
HttpRequest request = command.createRequest(); HttpRequest request = command.createRequest();
......
...@@ -41,7 +41,6 @@ import org.springframework.cloud.netflix.ribbon.SpringClientFactory; ...@@ -41,7 +41,6 @@ import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.ribbon.StaticServerList; import org.springframework.cloud.netflix.ribbon.StaticServerList;
import org.springframework.cloud.netflix.ribbon.apache.RibbonLoadBalancingHttpClient; import org.springframework.cloud.netflix.ribbon.apache.RibbonLoadBalancingHttpClient;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory;
import org.springframework.cloud.netflix.zuul.filters.route.support.ZuulProxyTestBase; import org.springframework.cloud.netflix.zuul.filters.route.support.ZuulProxyTestBase;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -180,7 +179,7 @@ public class HttpClientRibbonCommandIntegrationTests extends ZuulProxyTestBase { ...@@ -180,7 +179,7 @@ public class HttpClientRibbonCommandIntegrationTests extends ZuulProxyTestBase {
@Bean @Bean
public RibbonCommandFactory<?> ribbonCommandFactory( public RibbonCommandFactory<?> ribbonCommandFactory(
final SpringClientFactory clientFactory) { final SpringClientFactory clientFactory) {
return new HttpClientRibbonCommandFactory(clientFactory, new ZuulProperties()); return new HttpClientRibbonCommandFactory(clientFactory);
} }
@Bean @Bean
......
...@@ -32,7 +32,6 @@ import org.springframework.cloud.netflix.ribbon.RibbonClient; ...@@ -32,7 +32,6 @@ import org.springframework.cloud.netflix.ribbon.RibbonClient;
import org.springframework.cloud.netflix.ribbon.RibbonClients; import org.springframework.cloud.netflix.ribbon.RibbonClients;
import org.springframework.cloud.netflix.ribbon.SpringClientFactory; import org.springframework.cloud.netflix.ribbon.SpringClientFactory;
import org.springframework.cloud.netflix.zuul.EnableZuulProxy; import org.springframework.cloud.netflix.zuul.EnableZuulProxy;
import org.springframework.cloud.netflix.zuul.filters.ZuulProperties;
import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory; import org.springframework.cloud.netflix.zuul.filters.route.RibbonCommandFactory;
import org.springframework.cloud.netflix.zuul.filters.route.support.ZuulProxyTestBase; import org.springframework.cloud.netflix.zuul.filters.route.support.ZuulProxyTestBase;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
...@@ -108,7 +107,7 @@ public class OkHttpRibbonCommandIntegrationTests extends ZuulProxyTestBase { ...@@ -108,7 +107,7 @@ public class OkHttpRibbonCommandIntegrationTests extends ZuulProxyTestBase {
@Bean @Bean
public RibbonCommandFactory<?> ribbonCommandFactory( public RibbonCommandFactory<?> ribbonCommandFactory(
final SpringClientFactory clientFactory) { final SpringClientFactory clientFactory) {
return new OkHttpRibbonCommandFactory(clientFactory, new ZuulProperties()); return new OkHttpRibbonCommandFactory(clientFactory);
} }
@Bean @Bean
......
...@@ -330,7 +330,7 @@ public class RestClientRibbonCommandIntegrationTests extends ZuulProxyTestBase { ...@@ -330,7 +330,7 @@ public class RestClientRibbonCommandIntegrationTests extends ZuulProxyTestBase {
private SpringClientFactory clientFactory; private SpringClientFactory clientFactory;
public MyRibbonCommandFactory(SpringClientFactory clientFactory) { public MyRibbonCommandFactory(SpringClientFactory clientFactory) {
super(clientFactory, new ZuulProperties()); super(clientFactory);
this.clientFactory = clientFactory; this.clientFactory = clientFactory;
} }
...@@ -356,7 +356,7 @@ public class RestClientRibbonCommandIntegrationTests extends ZuulProxyTestBase { ...@@ -356,7 +356,7 @@ public class RestClientRibbonCommandIntegrationTests extends ZuulProxyTestBase {
public MyCommand(int errorCode, String commandKey, RestClient restClient, public MyCommand(int errorCode, String commandKey, RestClient restClient,
RibbonCommandContext context) { RibbonCommandContext context) {
super(commandKey, restClient, context, new ZuulProperties()); super(commandKey, restClient, context);
this.errorCode = errorCode; this.errorCode = errorCode;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment