From 69bccac870c355720e887c64f72489ffbcef6ab8 Mon Sep 17 00:00:00 2001 From: "Fitzgerald, Andrew" Date: Mon, 30 Apr 2018 02:54:33 -0400 Subject: [PATCH] Add additional Hystrix documentation. Covers part of #288, fixes #291 --- docs/src/main/asciidoc/spring-cloud-gateway.adoc | 12 ++++++++++++ .../filter/factory/HystrixGatewayFilterFactory.java | 1 + .../gateway/route/builder/GatewayFilterSpec.java | 3 +++ 3 files changed, 16 insertions(+) diff --git a/docs/src/main/asciidoc/spring-cloud-gateway.adoc b/docs/src/main/asciidoc/spring-cloud-gateway.adoc index b80b7ce40..2623af33b 100644 --- a/docs/src/main/asciidoc/spring-cloud-gateway.adoc +++ b/docs/src/main/asciidoc/spring-cloud-gateway.adoc @@ -355,6 +355,11 @@ spring: This will add `X-Response-Foo:Bar` header to the downstream response's headers for all matching requests. === Hystrix GatewayFilter Factory +https://github.com/Netflix/Hystrix[Hystrix] is a library from Netflix that implements the https://martinfowler.com/bliki/CircuitBreaker.html[circuit breaker pattern]. +The Hystrix GatewayFilter allows you to introduce circuit breakers to your gateway routes, protecting your services from cascading failures and allowing you to provide fallback responses in the event of downstream failures. + +To enable Hystrix GatewayFilters in your project, add a dependency on `spring-cloud-starter-netflix-hystrix` from http://cloud.spring.io/spring-cloud-netflix/[Spring Cloud Netflix]. + The Hystrix GatewayFilter Factory requires a single `name` parameter, which is the name of the `HystrixCommand`. .application.yml @@ -395,6 +400,13 @@ spring: ---- This will forward to the `/incaseoffailureusethis` URI when the Hystrix fallback is called. Note that this example also demonstrates (optional) Spring Cloud Netflix Ribbon load-balancing via the `lb` prefix on the destination URI. +Hystrix settings (such as timeouts) can be configured with global defaults or on a route by route basis using application properties as explained on the https://github.com/Netflix/Hystrix/wiki/Configuration[Hystrix wiki]. + +To set a 5 second timeout for the example route above, the following configuration would be used: + +.application.yml +[source,yaml] +hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds: 5000 === PrefixPath GatewayFilter Factory The PrefixPath GatewayFilter Factory takes a single `prefix` parameter. diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactory.java index 9b4602969..4de837b45 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactory.java @@ -50,6 +50,7 @@ import rx.RxReactiveStreams; import rx.Subscription; /** + * Depends on `spring-cloud-starter-netflix-hystrix`, {@see http://cloud.spring.io/spring-cloud-netflix/} * @author Spencer Gibb */ public class HystrixGatewayFilterFactory extends AbstractGatewayFilterFactory { diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java index 383402570..b85f93e48 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/builder/GatewayFilterSpec.java @@ -113,6 +113,9 @@ public class GatewayFilterSpec extends UriSpec { .apply(c -> c.setName(headerName).setValue(headerValue))); } + /** + * Depends on `spring-cloud-starter-netflix-hystrix`, {@see http://cloud.spring.io/spring-cloud-netflix/} + */ public GatewayFilterSpec hystrix(Consumer configConsumer) { HystrixGatewayFilterFactory factory; try {