Unverified Commit 34befdf1 by Spencer Gibb

Merge branch 'stephlag-zuul_hystrix_threadpool'

parents bc7b8367 21df6e43
...@@ -1060,6 +1060,8 @@ Zuul's rule engine allows rules and filters to be written in essentially any JVM ...@@ -1060,6 +1060,8 @@ 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 name.equalsIgnoreCase("Content-Length") || name.equalsIgnoreCase("Transfer-Encoding"); return "Content-Length".equalsIgnoreCase(name) || "Transfer-Encoding".equalsIgnoreCase(name);
} }
} }
...@@ -86,20 +86,24 @@ public class ZuulProxyConfiguration extends ZuulConfiguration { ...@@ -86,20 +86,24 @@ 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(SpringClientFactory clientFactory) { public RibbonCommandFactory<?> ribbonCommandFactory(
return new HttpClientRibbonCommandFactory(clientFactory); SpringClientFactory clientFactory, ZuulProperties zuulProperties) {
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(SpringClientFactory clientFactory) { public RibbonCommandFactory<?> ribbonCommandFactory(
return new RestClientRibbonCommandFactory(clientFactory); SpringClientFactory clientFactory, ZuulProperties zuulProperties) {
return new RestClientRibbonCommandFactory(clientFactory, zuulProperties);
} }
} }
...@@ -107,10 +111,12 @@ public class ZuulProxyConfiguration extends ZuulConfiguration { ...@@ -107,10 +111,12 @@ 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(SpringClientFactory clientFactory) { public RibbonCommandFactory<?> ribbonCommandFactory(
return new OkHttpRibbonCommandFactory(clientFactory); SpringClientFactory clientFactory, ZuulProperties zuulProperties) {
return new OkHttpRibbonCommandFactory(clientFactory, zuulProperties);
} }
} }
...@@ -118,18 +124,16 @@ public class ZuulProxyConfiguration extends ZuulConfiguration { ...@@ -118,18 +124,16 @@ 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, return new PreDecorationFilter(routeLocator, this.server.getServletPrefix(),
this.server.getServletPrefix(), this.zuulProperties, proxyRequestHelper);
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, RibbonRoutingFilter filter = new RibbonRoutingFilter(helper, ribbonCommandFactory,
ribbonCommandFactory, this.requestCustomizers); this.requestCustomizers);
return filter; return filter;
} }
......
...@@ -31,10 +31,14 @@ import org.springframework.boot.context.properties.ConfigurationProperties; ...@@ -31,10 +31,14 @@ 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
...@@ -131,6 +135,10 @@ public class ZuulProperties { ...@@ -131,6 +135,10 @@ 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(
...@@ -299,6 +307,17 @@ public class ZuulProperties { ...@@ -299,6 +307,17 @@ 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;
if (!path.startsWith("/")) { if (!path.startsWith("/")) {
......
...@@ -23,6 +23,7 @@ import java.io.InputStream; ...@@ -23,6 +23,7 @@ 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;
...@@ -33,23 +34,13 @@ import com.netflix.niws.client.http.RestClient; ...@@ -33,23 +34,13 @@ import com.netflix.niws.client.http.RestClient;
/** /**
* Hystrix wrapper around Eureka Ribbon command * Hystrix wrapper around Eureka Ribbon command
* *
* see original * 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>
* 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> {
@SuppressWarnings("unused") public RestClientRibbonCommand(String commandKey, RestClient client, RibbonCommandContext context, ZuulProperties zuulProperties) {
@Deprecated super(commandKey, client, context, zuulProperties);
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,28 +17,32 @@ ...@@ -17,28 +17,32 @@
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
*/ */
public class RestClientRibbonCommandFactory implements RibbonCommandFactory<RestClientRibbonCommand> { @RequiredArgsConstructor
public class RestClientRibbonCommandFactory
implements RibbonCommandFactory<RestClientRibbonCommand> {
private final SpringClientFactory clientFactory; private final SpringClientFactory clientFactory;
public RestClientRibbonCommandFactory(SpringClientFactory clientFactory) { private final ZuulProperties zuulProperties;
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,7 +19,6 @@ package org.springframework.cloud.netflix.zuul.filters.route; ...@@ -19,7 +19,6 @@ 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;
...@@ -54,17 +53,11 @@ public class RibbonCommandContext { ...@@ -54,17 +53,11 @@ 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;
......
...@@ -120,12 +120,13 @@ public class RibbonRoutingFilter extends ZuulFilter { ...@@ -120,12 +120,13 @@ 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); requestEntity, this.requestCustomizers, request.getContentLengthLong());
} }
protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception { protected ClientHttpResponse forward(RibbonCommandContext context) throws Exception {
Map<String, Object> info = this.helper.debug(context.getMethod(), context.getUri(), Map<String, Object> info = this.helper.debug(context.getMethod(),
context.getHeaders(), context.getParams(), context.getRequestEntity()); context.getUri(), context.getHeaders(), context.getParams(),
context.getRequestEntity());
RibbonCommand command = this.ribbonCommandFactory.create(context); RibbonCommand command = this.ribbonCommandFactory.create(context);
try { try {
......
...@@ -20,6 +20,7 @@ package org.springframework.cloud.netflix.zuul.filters.route.apache; ...@@ -20,6 +20,7 @@ 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;
...@@ -29,8 +30,10 @@ import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibb ...@@ -29,8 +30,10 @@ 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, RibbonCommandContext context) { final RibbonLoadBalancingHttpClient client,
super(commandKey, client, context); final RibbonCommandContext context,
final ZuulProperties zuulProperties) {
super(commandKey, client, context, zuulProperties);
} }
@Override @Override
......
...@@ -18,6 +18,7 @@ package org.springframework.cloud.netflix.zuul.filters.route.apache; ...@@ -18,6 +18,7 @@ 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,6 +33,8 @@ public class HttpClientRibbonCommandFactory implements ...@@ -32,6 +33,8 @@ public class HttpClientRibbonCommandFactory implements
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) {
final String serviceId = context.getServiceId(); final String serviceId = context.getServiceId();
...@@ -39,7 +42,7 @@ public class HttpClientRibbonCommandFactory implements ...@@ -39,7 +42,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); return new HttpClientRibbonCommand(serviceId, client, context, zuulProperties);
} }
} }
...@@ -20,6 +20,7 @@ package org.springframework.cloud.netflix.zuul.filters.route.okhttp; ...@@ -20,6 +20,7 @@ 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;
...@@ -29,8 +30,10 @@ import org.springframework.cloud.netflix.zuul.filters.route.support.AbstractRibb ...@@ -29,8 +30,10 @@ 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, RibbonCommandContext context) { final OkHttpLoadBalancingClient client,
super(commandKey, client, context); final RibbonCommandContext context,
final ZuulProperties zuulProperties) {
super(commandKey, client, context, zuulProperties);
} }
@Override @Override
......
...@@ -18,6 +18,7 @@ package org.springframework.cloud.netflix.zuul.filters.route.okhttp; ...@@ -18,6 +18,7 @@ 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,6 +33,8 @@ public class OkHttpRibbonCommandFactory implements ...@@ -32,6 +33,8 @@ public class OkHttpRibbonCommandFactory implements
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) {
final String serviceId = context.getServiceId(); final String serviceId = context.getServiceId();
...@@ -39,7 +42,7 @@ public class OkHttpRibbonCommandFactory implements ...@@ -39,7 +42,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); return new OkHttpRibbonCommand(serviceId, client, context, zuulProperties);
} }
} }
...@@ -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,39 +32,48 @@ import com.netflix.hystrix.HystrixCommand; ...@@ -32,39 +32,48 @@ 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> extends HystrixCommand<ClientHttpResponse> implements public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwareClient<RQ, RS>, RQ extends ClientRequest, RS extends HttpResponse>
RibbonCommand { extends HystrixCommand<ClientHttpResponse> implements 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,
this("default", client, context); ZuulProperties zuulProperties) {
this("default", client, context, zuulProperties);
} }
public AbstractRibbonCommand(String commandKey, LBC client, RibbonCommandContext context) { public AbstractRibbonCommand(String commandKey, LBC client,
super(getSetter(commandKey)); RibbonCommandContext context, ZuulProperties zuulProperties) {
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()
.withExecutionIsolationStrategy(zuulProperties.getRibbonIsolationStrategy());
if (zuulProperties.getRibbonIsolationStrategy() == ExecutionIsolationStrategy.SEMAPHORE){
final String name = ZuulConstants.ZUUL_EUREKA + commandKey + ".semaphore.maxSemaphores"; final String name = ZuulConstants.ZUUL_EUREKA + commandKey + ".semaphore.maxSemaphores";
// we want to default to semaphore-isolation since this wraps
// 2 others commands that are already thread isolated
final DynamicIntProperty value = DynamicPropertyFactory.getInstance() final DynamicIntProperty value = DynamicPropertyFactory.getInstance()
.getIntProperty(name, 100); .getIntProperty(name, 100);
final HystrixCommandProperties.Setter setter = HystrixCommandProperties .Setter() setter.withExecutionIsolationSemaphoreMaxConcurrentRequests(value.get());
.withExecutionIsolationStrategy(HystrixCommandProperties.ExecutionIsolationStrategy.SEMAPHORE) } else {
.withExecutionIsolationSemaphoreMaxConcurrentRequests(value.get()); // 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);
...@@ -74,10 +83,6 @@ public abstract class AbstractRibbonCommand<LBC extends AbstractLoadBalancerAwar ...@@ -74,10 +83,6 @@ 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,6 +29,7 @@ import java.io.ByteArrayInputStream; ...@@ -29,6 +29,7 @@ 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;
...@@ -37,6 +38,7 @@ import org.apache.http.client.config.RequestConfig; ...@@ -37,6 +38,7 @@ 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;
...@@ -52,10 +54,12 @@ public class RibbonApacheHttpRequestTests { ...@@ -52,10 +54,12 @@ 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 = new RibbonApacheHttpRequest(new RibbonCommandContext("example", "GET", uri, false, RibbonApacheHttpRequest httpRequest =
headers, params, null)); new RibbonApacheHttpRequest(
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());
...@@ -63,7 +67,9 @@ public class RibbonApacheHttpRequestTests { ...@@ -63,7 +67,9 @@ 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,9 +26,11 @@ import static org.junit.Assert.assertThat; ...@@ -26,9 +26,11 @@ 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;
...@@ -47,33 +49,41 @@ public class OkHttpRibbonRequestTests { ...@@ -47,33 +49,41 @@ 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, false, headers, params, null); RibbonCommandContext context = new RibbonCommandContext("example", "GET", uri,
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"), is(equalTo("my-value"))); assertThat("my-header is wrong", request.header("my-header"),
assertThat("myparam is missing", request.url().queryParameter("myparam"), is(equalTo("myparamval"))); is(equalTo("my-value")));
assertThat("myparam is missing", request.url().queryParameter("myparam"),
is(equalTo("myparamval")));
} }
@Test @Test
// this situation happens, see https://github.com/spring-cloud/spring-cloud-netflix/issues/1042#issuecomment-227723877 // this situation happens, see
// 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, "GET"); testEntity(entityValue, new ByteArrayInputStream(entityValue.getBytes()), false,
"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, "POST"); testEntity(entityValue, new ByteArrayInputStream(entityValue.getBytes()), true,
"POST");
} }
void testEntity(String entityValue, ByteArrayInputStream requestEntity, boolean addContentLengthHeader, String method) throws IOException { void testEntity(String entityValue, ByteArrayInputStream requestEntity,
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";
...@@ -94,8 +104,9 @@ public class OkHttpRibbonRequestTests { ...@@ -94,8 +104,9 @@ public class OkHttpRibbonRequestTests {
builder.addHeader("from-customizer", "foo"); builder.addHeader("from-customizer", "foo");
} }
}; };
RibbonCommandContext context = new RibbonCommandContext("example", method, uri, false, RibbonCommandContext context = new RibbonCommandContext("example", method, uri,
headers, new LinkedMultiValueMap<String, String>(), requestEntity, Collections.singletonList(requestCustomizer)); false, headers, new LinkedMultiValueMap<String, String>(), requestEntity,
Collections.singletonList(requestCustomizer));
context.setContentLength(length); context.setContentLength(length);
OkHttpRibbonRequest httpRequest = new OkHttpRibbonRequest(context); OkHttpRibbonRequest httpRequest = new OkHttpRibbonRequest(context);
...@@ -112,7 +123,8 @@ public class OkHttpRibbonRequestTests { ...@@ -112,7 +123,8 @@ 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(), is(equalTo((long) entityValue.length()))); assertThat("contentLength is wrong", body.contentLength(),
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();
...@@ -120,4 +132,3 @@ public class OkHttpRibbonRequestTests { ...@@ -120,4 +132,3 @@ public class OkHttpRibbonRequestTests {
} }
} }
} }
...@@ -27,10 +27,13 @@ import java.io.ByteArrayInputStream; ...@@ -27,10 +27,13 @@ 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;
...@@ -41,6 +44,13 @@ import com.netflix.client.http.HttpRequest; ...@@ -41,6 +44,13 @@ 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";
...@@ -48,8 +58,10 @@ public class RestClientRibbonCommandTests { ...@@ -48,8 +58,10 @@ 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 = new RestClientRibbonCommand("cmd", null, new RibbonCommandContext("example", "GET", uri, false, RestClientRibbonCommand command =
headers, params, null)); new RestClientRibbonCommand("cmd", null,
new RibbonCommandContext("example", "GET", uri, false, headers, params, null,new ArrayList<RibbonRequestCustomizer>()),
zuulProperties);
HttpRequest request = command.createRequest(); HttpRequest request = command.createRequest();
...@@ -96,7 +108,7 @@ public class RestClientRibbonCommandTests { ...@@ -96,7 +108,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); RestClientRibbonCommand command = new RestClientRibbonCommand("cmd", null, context, zuulProperties);
HttpRequest request = command.createRequest(); HttpRequest request = command.createRequest();
......
...@@ -41,6 +41,7 @@ import org.springframework.cloud.netflix.ribbon.SpringClientFactory; ...@@ -41,6 +41,7 @@ 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;
...@@ -179,7 +180,7 @@ public class HttpClientRibbonCommandIntegrationTests extends ZuulProxyTestBase { ...@@ -179,7 +180,7 @@ public class HttpClientRibbonCommandIntegrationTests extends ZuulProxyTestBase {
@Bean @Bean
public RibbonCommandFactory<?> ribbonCommandFactory( public RibbonCommandFactory<?> ribbonCommandFactory(
final SpringClientFactory clientFactory) { final SpringClientFactory clientFactory) {
return new HttpClientRibbonCommandFactory(clientFactory); return new HttpClientRibbonCommandFactory(clientFactory, new ZuulProperties());
} }
@Bean @Bean
......
...@@ -32,6 +32,7 @@ import org.springframework.cloud.netflix.ribbon.RibbonClient; ...@@ -32,6 +32,7 @@ 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;
...@@ -107,7 +108,7 @@ public class OkHttpRibbonCommandIntegrationTests extends ZuulProxyTestBase { ...@@ -107,7 +108,7 @@ public class OkHttpRibbonCommandIntegrationTests extends ZuulProxyTestBase {
@Bean @Bean
public RibbonCommandFactory<?> ribbonCommandFactory( public RibbonCommandFactory<?> ribbonCommandFactory(
final SpringClientFactory clientFactory) { final SpringClientFactory clientFactory) {
return new OkHttpRibbonCommandFactory(clientFactory); return new OkHttpRibbonCommandFactory(clientFactory, new ZuulProperties());
} }
@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); super(clientFactory, new ZuulProperties());
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); super(commandKey, restClient, context, new ZuulProperties());
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