From f1f239d7742aa657a262b240055b6609a9d77ca1 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Tue, 29 Sep 2015 09:14:50 +0100 Subject: [PATCH] Change hystrix command key to be the service ID in Zuul proxy When using Ribbon the hystrix config and metrics are namespaced as "RibbonCommand" (via the group key). the command key can then be serviceId, making it easier to remember how to configure Ribbon and Hystrix. Fixes gh-329 --- .../route/RestClientRibbonCommand.java | 32 +++++++++---------- .../route/RestClientRibbonCommandFactory.java | 6 ++-- 2 files changed, 20 insertions(+), 18 deletions(-) diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/RestClientRibbonCommand.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/RestClientRibbonCommand.java index 5959a5fa..8cb95861 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/RestClientRibbonCommand.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/RestClientRibbonCommand.java @@ -55,7 +55,7 @@ public class RestClientRibbonCommand extends HystrixCommand private Verb verb; private URI uri; - + private Boolean retryable; private MultiValueMap headers; @@ -97,7 +97,7 @@ public class RestClientRibbonCommand extends HystrixCommand .withExecutionIsolationStrategy(ExecutionIsolationStrategy.SEMAPHORE) .withExecutionIsolationSemaphoreMaxConcurrentRequests(value.get()); return Setter.withGroupKey(HystrixCommandGroupKey.Factory.asKey("RibbonCommand")) - .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey + "RibbonCommand")) + .andCommandKey(HystrixCommandKey.Factory.asKey(commandKey)) .andCommandPropertiesDefaults(setter); } @@ -110,11 +110,11 @@ public class RestClientRibbonCommand extends HystrixCommand RequestContext context = RequestContext.getCurrentContext(); Builder builder = HttpRequest.newBuilder().verb(this.verb).uri(this.uri) .entity(this.requestEntity); - - if(retryable != null) { - builder.setRetriable(retryable); + + if(this.retryable != null) { + builder.setRetriable(this.retryable); } - + for (String name : this.headers.keySet()) { List values = this.headers.get(name); for (String value : values) { @@ -134,11 +134,11 @@ public class RestClientRibbonCommand extends HystrixCommand HttpResponse response = this.restClient .executeWithLoadBalancer(httpClientRequest); context.set("ribbonResponse", response); - + // Explicitly close the HttpResponse if the Hystrix command timed out to // release the underlying HTTP connection held by the response. - // - if( this.isResponseTimedOut() ) { + // + if( this.isResponseTimedOut() ) { if( response!= null ) { response.close(); } @@ -154,30 +154,30 @@ public class RestClientRibbonCommand extends HystrixCommand } protected MultiValueMap getHeaders() { - return headers; + return this.headers; } protected MultiValueMap getParams() { - return params; + return this.params; } protected InputStream getRequestEntity() { - return requestEntity; + return this.requestEntity; } protected RestClient getRestClient() { - return restClient; + return this.restClient; } protected Boolean getRetryable() { - return retryable; + return this.retryable; } protected URI getUri() { - return uri; + return this.uri; } protected Verb getVerb() { - return verb; + return this.verb; } } diff --git a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/RestClientRibbonCommandFactory.java b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/RestClientRibbonCommandFactory.java index da011c68..30cd9dbf 100644 --- a/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/RestClientRibbonCommandFactory.java +++ b/spring-cloud-netflix-core/src/main/java/org/springframework/cloud/netflix/zuul/filters/route/RestClientRibbonCommandFactory.java @@ -16,10 +16,12 @@ package org.springframework.cloud.netflix.zuul.filters.route; +import org.springframework.cloud.netflix.ribbon.SpringClientFactory; + import com.netflix.client.http.HttpRequest; import com.netflix.niws.client.http.RestClient; + import lombok.SneakyThrows; -import org.springframework.cloud.netflix.ribbon.SpringClientFactory; /** * @author Spencer Gibb @@ -45,7 +47,7 @@ public class RestClientRibbonCommandFactory implements RibbonCommandFactory