From 922eb8eedf3011bfaac0028555f4c61357123193 Mon Sep 17 00:00:00 2001 From: buildmaster Date: Thu, 28 May 2020 18:15:16 +0000 Subject: [PATCH 1/6] Update SNAPSHOT to 2.2.3.RELEASE --- README.adoc | 5 +++++ docs/pom.xml | 2 +- pom.xml | 10 +++++----- spring-cloud-gateway-core/pom.xml | 2 +- spring-cloud-gateway-dependencies/pom.xml | 4 ++-- spring-cloud-gateway-mvc/pom.xml | 2 +- spring-cloud-gateway-sample/pom.xml | 2 +- spring-cloud-gateway-webflux/pom.xml | 2 +- spring-cloud-starter-gateway/pom.xml | 2 +- 9 files changed, 18 insertions(+), 13 deletions(-) diff --git a/README.adoc b/README.adoc index 7092e7a8..4d32bc39 100644 --- a/README.adoc +++ b/README.adoc @@ -89,6 +89,11 @@ https://eclipse.org[Eclipse] when working with the code. We use the https://eclipse.org/m2e/[m2eclipse] eclipse plugin for maven support. Other IDEs and tools should also work without issue as long as they use Maven 3.3.3 or better. +==== Activate the Spring Maven profile +Spring Cloud projects require the 'spring' Maven profile to be activated to resolve +the spring milestone and snapshot repositories. Use your preferred IDE to set this +profile to be active, or you may experience build errors. + ==== Importing into eclipse with m2eclipse We recommend the https://eclipse.org/m2e/[m2eclipse] eclipse plugin when working with eclipse. If you don't already have m2eclipse installed it is available from the "eclipse diff --git a/docs/pom.xml b/docs/pom.xml index 3216fc7e..ed5ac6a3 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.3.RELEASE spring-cloud-gateway-docs pom diff --git a/pom.xml b/pom.xml index c50709ea..fe9cea5f 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.3.RELEASE pom Spring Cloud Gateway @@ -15,7 +15,7 @@ org.springframework.cloud spring-cloud-build - 2.3.0.BUILD-SNAPSHOT + 2.3.0.RELEASE @@ -52,9 +52,9 @@ UTF-8 UTF-8 1.8 - 2.2.3.BUILD-SNAPSHOT - 2.2.3.BUILD-SNAPSHOT - 1.0.3.BUILD-SNAPSHOT + 2.2.3.RELEASE + 2.2.3.RELEASE + 1.0.3.RELEASE 0.6 1.0.3.RELEASE diff --git a/spring-cloud-gateway-core/pom.xml b/spring-cloud-gateway-core/pom.xml index 44b37665..d08c5b07 100644 --- a/spring-cloud-gateway-core/pom.xml +++ b/spring-cloud-gateway-core/pom.xml @@ -7,7 +7,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.3.RELEASE .. spring-cloud-gateway-core diff --git a/spring-cloud-gateway-dependencies/pom.xml b/spring-cloud-gateway-dependencies/pom.xml index fe5bb95c..a1a70b85 100644 --- a/spring-cloud-gateway-dependencies/pom.xml +++ b/spring-cloud-gateway-dependencies/pom.xml @@ -6,12 +6,12 @@ spring-cloud-dependencies-parent org.springframework.cloud - 2.3.0.BUILD-SNAPSHOT + 2.3.0.RELEASE spring-cloud-gateway-dependencies - 2.2.3.BUILD-SNAPSHOT + 2.2.3.RELEASE pom spring-cloud-gateway-dependencies diff --git a/spring-cloud-gateway-mvc/pom.xml b/spring-cloud-gateway-mvc/pom.xml index d980011f..39f3c8ca 100644 --- a/spring-cloud-gateway-mvc/pom.xml +++ b/spring-cloud-gateway-mvc/pom.xml @@ -11,7 +11,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.3.RELEASE .. diff --git a/spring-cloud-gateway-sample/pom.xml b/spring-cloud-gateway-sample/pom.xml index df828a87..2a93539c 100644 --- a/spring-cloud-gateway-sample/pom.xml +++ b/spring-cloud-gateway-sample/pom.xml @@ -16,7 +16,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.3.RELEASE .. diff --git a/spring-cloud-gateway-webflux/pom.xml b/spring-cloud-gateway-webflux/pom.xml index 0929246b..646ab622 100644 --- a/spring-cloud-gateway-webflux/pom.xml +++ b/spring-cloud-gateway-webflux/pom.xml @@ -11,7 +11,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.3.RELEASE .. diff --git a/spring-cloud-starter-gateway/pom.xml b/spring-cloud-starter-gateway/pom.xml index 69c6835f..839b2d3b 100644 --- a/spring-cloud-starter-gateway/pom.xml +++ b/spring-cloud-starter-gateway/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.3.RELEASE .. spring-cloud-starter-gateway From e8364edac6de4584490e4610459a5df199b88c7b Mon Sep 17 00:00:00 2001 From: buildmaster Date: Thu, 28 May 2020 18:17:06 +0000 Subject: [PATCH 2/6] Going back to snapshots --- README.adoc | 5 ----- docs/pom.xml | 2 +- pom.xml | 10 +++++----- spring-cloud-gateway-core/pom.xml | 2 +- spring-cloud-gateway-dependencies/pom.xml | 4 ++-- spring-cloud-gateway-mvc/pom.xml | 2 +- spring-cloud-gateway-sample/pom.xml | 2 +- spring-cloud-gateway-webflux/pom.xml | 2 +- spring-cloud-starter-gateway/pom.xml | 2 +- 9 files changed, 13 insertions(+), 18 deletions(-) diff --git a/README.adoc b/README.adoc index 4d32bc39..7092e7a8 100644 --- a/README.adoc +++ b/README.adoc @@ -89,11 +89,6 @@ https://eclipse.org[Eclipse] when working with the code. We use the https://eclipse.org/m2e/[m2eclipse] eclipse plugin for maven support. Other IDEs and tools should also work without issue as long as they use Maven 3.3.3 or better. -==== Activate the Spring Maven profile -Spring Cloud projects require the 'spring' Maven profile to be activated to resolve -the spring milestone and snapshot repositories. Use your preferred IDE to set this -profile to be active, or you may experience build errors. - ==== Importing into eclipse with m2eclipse We recommend the https://eclipse.org/m2e/[m2eclipse] eclipse plugin when working with eclipse. If you don't already have m2eclipse installed it is available from the "eclipse diff --git a/docs/pom.xml b/docs/pom.xml index ed5ac6a3..3216fc7e 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.RELEASE + 2.2.3.BUILD-SNAPSHOT spring-cloud-gateway-docs pom diff --git a/pom.xml b/pom.xml index fe9cea5f..c50709ea 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.RELEASE + 2.2.3.BUILD-SNAPSHOT pom Spring Cloud Gateway @@ -15,7 +15,7 @@ org.springframework.cloud spring-cloud-build - 2.3.0.RELEASE + 2.3.0.BUILD-SNAPSHOT @@ -52,9 +52,9 @@ UTF-8 UTF-8 1.8 - 2.2.3.RELEASE - 2.2.3.RELEASE - 1.0.3.RELEASE + 2.2.3.BUILD-SNAPSHOT + 2.2.3.BUILD-SNAPSHOT + 1.0.3.BUILD-SNAPSHOT 0.6 1.0.3.RELEASE diff --git a/spring-cloud-gateway-core/pom.xml b/spring-cloud-gateway-core/pom.xml index d08c5b07..44b37665 100644 --- a/spring-cloud-gateway-core/pom.xml +++ b/spring-cloud-gateway-core/pom.xml @@ -7,7 +7,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.RELEASE + 2.2.3.BUILD-SNAPSHOT .. spring-cloud-gateway-core diff --git a/spring-cloud-gateway-dependencies/pom.xml b/spring-cloud-gateway-dependencies/pom.xml index a1a70b85..fe5bb95c 100644 --- a/spring-cloud-gateway-dependencies/pom.xml +++ b/spring-cloud-gateway-dependencies/pom.xml @@ -6,12 +6,12 @@ spring-cloud-dependencies-parent org.springframework.cloud - 2.3.0.RELEASE + 2.3.0.BUILD-SNAPSHOT spring-cloud-gateway-dependencies - 2.2.3.RELEASE + 2.2.3.BUILD-SNAPSHOT pom spring-cloud-gateway-dependencies diff --git a/spring-cloud-gateway-mvc/pom.xml b/spring-cloud-gateway-mvc/pom.xml index 39f3c8ca..d980011f 100644 --- a/spring-cloud-gateway-mvc/pom.xml +++ b/spring-cloud-gateway-mvc/pom.xml @@ -11,7 +11,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.RELEASE + 2.2.3.BUILD-SNAPSHOT .. diff --git a/spring-cloud-gateway-sample/pom.xml b/spring-cloud-gateway-sample/pom.xml index 2a93539c..df828a87 100644 --- a/spring-cloud-gateway-sample/pom.xml +++ b/spring-cloud-gateway-sample/pom.xml @@ -16,7 +16,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.RELEASE + 2.2.3.BUILD-SNAPSHOT .. diff --git a/spring-cloud-gateway-webflux/pom.xml b/spring-cloud-gateway-webflux/pom.xml index 646ab622..0929246b 100644 --- a/spring-cloud-gateway-webflux/pom.xml +++ b/spring-cloud-gateway-webflux/pom.xml @@ -11,7 +11,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.RELEASE + 2.2.3.BUILD-SNAPSHOT .. diff --git a/spring-cloud-starter-gateway/pom.xml b/spring-cloud-starter-gateway/pom.xml index 839b2d3b..69c6835f 100644 --- a/spring-cloud-starter-gateway/pom.xml +++ b/spring-cloud-starter-gateway/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.RELEASE + 2.2.3.BUILD-SNAPSHOT .. spring-cloud-starter-gateway From 2df5dde632f2f190da5f2f01954ff6d841f5598a Mon Sep 17 00:00:00 2001 From: buildmaster Date: Thu, 28 May 2020 18:17:06 +0000 Subject: [PATCH 3/6] Bumping versions to 2.2.4.BUILD-SNAPSHOT after release --- docs/pom.xml | 2 +- pom.xml | 10 +++++----- spring-cloud-gateway-core/pom.xml | 2 +- spring-cloud-gateway-dependencies/pom.xml | 4 ++-- spring-cloud-gateway-mvc/pom.xml | 2 +- spring-cloud-gateway-sample/pom.xml | 2 +- spring-cloud-gateway-webflux/pom.xml | 2 +- spring-cloud-starter-gateway/pom.xml | 2 +- 8 files changed, 13 insertions(+), 13 deletions(-) diff --git a/docs/pom.xml b/docs/pom.xml index 3216fc7e..d56ae60e 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.4.BUILD-SNAPSHOT spring-cloud-gateway-docs pom diff --git a/pom.xml b/pom.xml index c50709ea..50c330ee 100644 --- a/pom.xml +++ b/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.4.BUILD-SNAPSHOT pom Spring Cloud Gateway @@ -15,7 +15,7 @@ org.springframework.cloud spring-cloud-build - 2.3.0.BUILD-SNAPSHOT + 2.3.0.RELEASE @@ -52,9 +52,9 @@ UTF-8 UTF-8 1.8 - 2.2.3.BUILD-SNAPSHOT - 2.2.3.BUILD-SNAPSHOT - 1.0.3.BUILD-SNAPSHOT + 2.2.4.BUILD-SNAPSHOT + 2.2.4.BUILD-SNAPSHOT + 1.0.4.BUILD-SNAPSHOT 0.6 1.0.3.RELEASE diff --git a/spring-cloud-gateway-core/pom.xml b/spring-cloud-gateway-core/pom.xml index 44b37665..9d69811a 100644 --- a/spring-cloud-gateway-core/pom.xml +++ b/spring-cloud-gateway-core/pom.xml @@ -7,7 +7,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.4.BUILD-SNAPSHOT .. spring-cloud-gateway-core diff --git a/spring-cloud-gateway-dependencies/pom.xml b/spring-cloud-gateway-dependencies/pom.xml index fe5bb95c..b8a0576c 100644 --- a/spring-cloud-gateway-dependencies/pom.xml +++ b/spring-cloud-gateway-dependencies/pom.xml @@ -6,12 +6,12 @@ spring-cloud-dependencies-parent org.springframework.cloud - 2.3.0.BUILD-SNAPSHOT + 2.3.1.BUILD-SNAPSHOT spring-cloud-gateway-dependencies - 2.2.3.BUILD-SNAPSHOT + 2.2.4.BUILD-SNAPSHOT pom spring-cloud-gateway-dependencies diff --git a/spring-cloud-gateway-mvc/pom.xml b/spring-cloud-gateway-mvc/pom.xml index d980011f..34e7fcc9 100644 --- a/spring-cloud-gateway-mvc/pom.xml +++ b/spring-cloud-gateway-mvc/pom.xml @@ -11,7 +11,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.4.BUILD-SNAPSHOT .. diff --git a/spring-cloud-gateway-sample/pom.xml b/spring-cloud-gateway-sample/pom.xml index df828a87..c1dddd58 100644 --- a/spring-cloud-gateway-sample/pom.xml +++ b/spring-cloud-gateway-sample/pom.xml @@ -16,7 +16,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.4.BUILD-SNAPSHOT .. diff --git a/spring-cloud-gateway-webflux/pom.xml b/spring-cloud-gateway-webflux/pom.xml index 0929246b..6d261ab2 100644 --- a/spring-cloud-gateway-webflux/pom.xml +++ b/spring-cloud-gateway-webflux/pom.xml @@ -11,7 +11,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.4.BUILD-SNAPSHOT .. diff --git a/spring-cloud-starter-gateway/pom.xml b/spring-cloud-starter-gateway/pom.xml index 69c6835f..f5b1a842 100644 --- a/spring-cloud-starter-gateway/pom.xml +++ b/spring-cloud-starter-gateway/pom.xml @@ -6,7 +6,7 @@ org.springframework.cloud spring-cloud-gateway - 2.2.3.BUILD-SNAPSHOT + 2.2.4.BUILD-SNAPSHOT .. spring-cloud-starter-gateway From d165617d92e3b1335572d01f090b2a07ab5854b7 Mon Sep 17 00:00:00 2001 From: Marcin Grzejszczak Date: Thu, 4 Jun 2020 13:51:55 +0200 Subject: [PATCH 4/6] Migrated to docs.spring.io & updated sc-build --- README.adoc | 8 +++++++- docs/pom.xml | 15 ++++----------- docs/src/main/asciidoc/_configprops.adoc | 2 +- pom.xml | 2 +- 4 files changed, 13 insertions(+), 14 deletions(-) diff --git a/README.adoc b/README.adoc index 7092e7a8..3e56e049 100644 --- a/README.adoc +++ b/README.adoc @@ -4,6 +4,7 @@ Manual changes to this file will be lost when it is generated again. Edit the files in the src/main/asciidoc/ directory instead. //// + image::https://circleci.com/gh/spring-cloud/spring-cloud-gateway/tree/master.svg?style=svg["CircleCI", link="https://circleci.com/gh/spring-cloud/spring-cloud-gateway/tree/master"] image::https://codecov.io/gh/spring-cloud/spring-cloud-gateway/branch/master/graph/badge.svg["Codecov", link="https://codecov.io/gh/spring-cloud/spring-cloud-gateway/branch/master"] image::https://api.codacy.com/project/badge/Grade/a6885a06921e4f72a0df0b7aabd6d118["Codacy code quality", link="https://www.codacy.com/app/spring-cloud/spring-cloud-gateway?utm_source=github.com&utm_medium=referral&utm_content=spring-cloud/spring-cloud-gateway&utm_campaign=Badge_Grade"] @@ -89,6 +90,11 @@ https://eclipse.org[Eclipse] when working with the code. We use the https://eclipse.org/m2e/[m2eclipse] eclipse plugin for maven support. Other IDEs and tools should also work without issue as long as they use Maven 3.3.3 or better. +==== Activate the Spring Maven profile +Spring Cloud projects require the 'spring' Maven profile to be activated to resolve +the spring milestone and snapshot repositories. Use your preferred IDE to set this +profile to be active, or you may experience build errors. + ==== Importing into eclipse with m2eclipse We recommend the https://eclipse.org/m2e/[m2eclipse] eclipse plugin when working with eclipse. If you don't already have m2eclipse installed it is available from the "eclipse @@ -297,4 +303,4 @@ Go to `File` -> `Settings` -> `Other settings` -> `Checkstyle`. There click on t - `checkstyle.suppressions.file` - default suppressions. Please point it to the Spring Cloud Build's, `spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` file either in your cloned repo or via the `https://raw.githubusercontent.com/spring-cloud/spring-cloud-build/master/spring-cloud-build-tools/src/checkstyle/checkstyle-suppressions.xml` URL. - `checkstyle.additional.suppressions.file` - this variable corresponds to suppressions in your local project. E.g. you're working on `spring-cloud-contract`. Then point to the `project-root/src/checkstyle/checkstyle-suppressions.xml` folder. Example for `spring-cloud-contract` would be: `/home/username/spring-cloud-contract/src/checkstyle/checkstyle-suppressions.xml`. -IMPORTANT: Remember to set the `Scan Scope` to `All sources` since we apply checkstyle rules for production and test sources. \ No newline at end of file +IMPORTANT: Remember to set the `Scan Scope` to `All sources` since we apply checkstyle rules for production and test sources. diff --git a/docs/pom.xml b/docs/pom.xml index d56ae60e..088b2277 100644 --- a/docs/pom.xml +++ b/docs/pom.xml @@ -15,8 +15,8 @@ spring-cloud-gateway ${basedir}/.. - 2.1.x,2.2.x spring.cloud.gateway.* + deploy @@ -24,16 +24,6 @@ spring-cloud-starter-gateway - - - - maven-deploy-plugin - - true - - - - docs @@ -63,6 +53,9 @@ org.apache.maven.plugins maven-antrun-plugin + + maven-deploy-plugin + diff --git a/docs/src/main/asciidoc/_configprops.adoc b/docs/src/main/asciidoc/_configprops.adoc index 67528dc3..adbd0f2c 100644 --- a/docs/src/main/asciidoc/_configprops.adoc +++ b/docs/src/main/asciidoc/_configprops.adoc @@ -85,4 +85,4 @@ |spring.cloud.gateway.x-forwarded.proto-append | true | If appending X-Forwarded-Proto as a list is enabled. |spring.cloud.gateway.x-forwarded.proto-enabled | true | If X-Forwarded-Proto is enabled. -|=== +|=== \ No newline at end of file diff --git a/pom.xml b/pom.xml index 50c330ee..bbf94218 100644 --- a/pom.xml +++ b/pom.xml @@ -15,7 +15,7 @@ org.springframework.cloud spring-cloud-build - 2.3.0.RELEASE + 2.3.1.BUILD-SNAPSHOT From 87e8892fe4f34c609daedd48c8658d33e4b64516 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Tue, 14 Jul 2020 10:53:00 -0400 Subject: [PATCH 5/6] Add the ability to trip the circuit breaker based on returned status code. --- .../main/asciidoc/spring-cloud-gateway.adoc | 49 +++++++++++++++++++ .../factory/RetryGatewayFilterFactory.java | 16 +++--- ...pringCloudCircuitBreakerFilterFactory.java | 45 ++++++++++++++++- .../support/ServerWebExchangeUtils.java | 11 +++++ ...CloudCircuitBreakerFilterFactoryTests.java | 18 +++++++ .../SpringCloudCircuitBreakerTestConfig.java | 6 +++ .../src/test/resources/application.yml | 14 ++++++ 7 files changed, 148 insertions(+), 11 deletions(-) diff --git a/docs/src/main/asciidoc/spring-cloud-gateway.adoc b/docs/src/main/asciidoc/spring-cloud-gateway.adoc index 25130f29..e40b4349 100644 --- a/docs/src/main/asciidoc/spring-cloud-gateway.adoc +++ b/docs/src/main/asciidoc/spring-cloud-gateway.adoc @@ -831,6 +831,55 @@ It is added to the `ServerWebExchange` as the `ServerWebExchangeUtils.CIRCUITBRE For the external controller/handler scenario, headers can be added with exception details. You can find more information on doing so in the <>. +[[circuit-breaker-status-codes]] +==== Tripping The Circuit Breaker On Status Codes + +In some cases you might want to trip a circuit breaker based on the status code +returned from the route it wraps. The circuit breaker config object takes a list of +status codes that if returned will cause the the circuit breaker to be tripped. When setting the +status codes you want to trip the circuit breaker you can either use a integer with the status code +value or the String representation of the `HttpStatus` enumeration. + +.application.yml +==== +[source,yaml] +---- +spring: + cloud: + gateway: + routes: + - id: circuitbreaker_route + uri: lb://backing-service:8088 + predicates: + - Path=/consumingServiceEndpoint + filters: + - name: CircuitBreaker + args: + name: myCircuitBreaker + fallbackUri: forward:/inCaseOfFailureUseThis + statusCodes: + - 500 + - "NOT_FOUND" +---- +==== + +.Application.java +==== +[source,java] +---- +@Bean +public RouteLocator routes(RouteLocatorBuilder builder) { + return builder.routes() + .route("circuitbreaker_route", r -> r.path("/consumingServiceEndpoint") + .filters(f -> f.circuitBreaker(c -> c.name("myCircuitBreaker").fallbackUri("forward:/inCaseOfFailureUseThis").addStatusCode("INTERNAL_SERVER_ERROR")) + .rewritePath("/consumingServiceEndpoint", "/backingServiceEndpoint")).uri("lb://backing-service:8088") + .build(); +} +---- +==== + + + [[fallback-headers]] === The `FallbackHeaders` `GatewayFilter` Factory diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/RetryGatewayFilterFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/RetryGatewayFilterFactory.java index 50cf4292..71f135b3 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/RetryGatewayFilterFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/RetryGatewayFilterFactory.java @@ -20,9 +20,7 @@ import java.io.IOException; import java.time.Duration; import java.util.ArrayList; import java.util.Arrays; -import java.util.Collections; import java.util.List; -import java.util.Set; import java.util.function.Predicate; import java.util.function.Supplier; @@ -40,6 +38,7 @@ import org.springframework.cloud.gateway.event.EnableBodyCachingEvent; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.support.HasRouteId; +import org.springframework.cloud.gateway.support.ServerWebExchangeUtils; import org.springframework.cloud.gateway.support.TimeoutException; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; @@ -48,8 +47,6 @@ import org.springframework.util.Assert; import org.springframework.web.server.ServerWebExchange; import static org.springframework.cloud.gateway.support.GatewayToStringStyler.filterToStringCreator; -import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.CLIENT_RESPONSE_HEADER_NAMES; -import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.removeAlreadyRouted; public class RetryGatewayFilterFactory extends AbstractGatewayFilterFactory { @@ -211,13 +208,12 @@ public class RetryGatewayFilterFactory return exceeds; } + @Deprecated + /** + * Use {@link ServerWebExchangeUtils#reset(ServerWebExchange)} + */ public void reset(ServerWebExchange exchange) { - // TODO: what else to do to reset exchange? - Set addedHeaders = exchange.getAttributeOrDefault( - CLIENT_RESPONSE_HEADER_NAMES, Collections.emptySet()); - addedHeaders - .forEach(header -> exchange.getResponse().getHeaders().remove(header)); - removeAlreadyRouted(exchange); + ServerWebExchangeUtils.reset(exchange); } @Deprecated diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactory.java index 129a9e88..79461968 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactory.java @@ -17,7 +17,10 @@ package org.springframework.cloud.gateway.filter.factory; import java.net.URI; +import java.util.HashSet; import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; import reactor.core.publisher.Mono; @@ -27,8 +30,11 @@ import org.springframework.cloud.client.circuitbreaker.ReactiveCircuitBreakerFac import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.support.HasRouteId; +import org.springframework.cloud.gateway.support.HttpStatusHolder; +import org.springframework.http.HttpStatus; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.util.StringUtils; +import org.springframework.web.client.HttpStatusCodeException; import org.springframework.web.reactive.DispatcherHandler; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.util.UriComponentsBuilder; @@ -40,6 +46,7 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.C import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.containsEncodedParts; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.removeAlreadyRouted; +import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.reset; /** * @author Ryan Baxter @@ -85,7 +92,19 @@ public abstract class SpringCloudCircuitBreakerFilterFactory extends @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { - return cb.run(chain.filter(exchange), t -> { + return cb.run(chain.filter(exchange).doOnSuccess(v -> { + Set statuses = config.getStatusCodes().stream() + .map(HttpStatusHolder::parse) + .filter(statusHolder -> statusHolder.getHttpStatus() != null) + .map(HttpStatusHolder::getHttpStatus) + .collect(Collectors.toSet()); + if (statuses.contains(exchange.getResponse().getStatusCode())) { + HttpStatus status = exchange.getResponse().getStatusCode(); + exchange.getResponse().setStatusCode(null); + reset(exchange); + throw new CircuitBreakerStatusCodeException(status); + } + }), t -> { if (config.getFallbackUri() == null) { return Mono.error(t); } @@ -141,6 +160,8 @@ public abstract class SpringCloudCircuitBreakerFilterFactory extends private String routeId; + private Set statusCodes = new HashSet<>(); + @Override public void setRouteId(String routeId) { this.routeId = routeId; @@ -179,6 +200,28 @@ public abstract class SpringCloudCircuitBreakerFilterFactory extends return name; } + public Set getStatusCodes() { + return statusCodes; + } + + public Config setStatusCodes(Set statusCodes) { + this.statusCodes = statusCodes; + return this; + } + + public Config addStatusCode(String statusCode) { + this.statusCodes.add(statusCode); + return this; + } + + } + + public class CircuitBreakerStatusCodeException extends HttpStatusCodeException { + + public CircuitBreakerStatusCodeException(HttpStatus statusCode) { + super(statusCode); + } + } } diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java index 62cbfc86..20a77f67 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/support/ServerWebExchangeUtils.java @@ -17,9 +17,11 @@ package org.springframework.cloud.gateway.support; import java.net.URI; +import java.util.Collections; import java.util.HashMap; import java.util.LinkedHashSet; import java.util.Map; +import java.util.Set; import java.util.function.Function; import java.util.function.Predicate; @@ -193,6 +195,15 @@ public final class ServerWebExchangeUtils { return response; } + public static void reset(ServerWebExchange exchange) { + // TODO: what else to do to reset exchange? + Set addedHeaders = exchange.getAttributeOrDefault( + CLIENT_RESPONSE_HEADER_NAMES, Collections.emptySet()); + addedHeaders + .forEach(header -> exchange.getResponse().getHeaders().remove(header)); + removeAlreadyRouted(exchange); + } + public static boolean setResponseStatus(ServerWebExchange exchange, HttpStatusHolder statusHolder) { if (exchange.getResponse().isCommitted()) { diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactoryTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactoryTests.java index 77fbff73..7d750cc8 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactoryTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactoryTests.java @@ -104,4 +104,22 @@ public abstract class SpringCloudCircuitBreakerFilterFactoryTests .json("{\"from\":\"circuitbreakerfallbackcontroller3\"}"); } + @Test + public void filterStatusCodeFallback() { + testClient.get().uri("/status/500") + .header("Host", "www.circuitbreakerstatuscode.org").exchange() + .expectStatus().isOk().expectBody() + .json("{\"from\":\"statusCodeFallbackController\"}"); + + testClient.get().uri("/status/404") + .header("Host", "www.circuitbreakerstatuscode.org").exchange() + .expectStatus().isOk().expectBody() + .json("{\"from\":\"statusCodeFallbackController\"}"); + + testClient.get().uri("/status/200") + .header("Host", "www.circuitbreakerstatuscode.org").exchange() + .expectStatus().isOk().expectHeader() + .valueEquals(ROUTE_ID_HEADER, "circuitbreaker_fallback_test_statuscode"); + } + } diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerTestConfig.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerTestConfig.java index 070ab1f7..24b9979b 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerTestConfig.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerTestConfig.java @@ -36,6 +36,7 @@ import org.springframework.web.bind.annotation.RestController; import org.springframework.web.reactive.function.server.RouterFunction; import org.springframework.web.reactive.function.server.ServerRequest; import org.springframework.web.reactive.function.server.ServerResponse; +import org.springframework.web.server.ServerWebExchange; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.CIRCUITBREAKER_EXECUTION_EXCEPTION_ATTR; import static org.springframework.web.reactive.function.server.RequestPredicates.GET; @@ -69,6 +70,11 @@ public class SpringCloudCircuitBreakerTestConfig { return Collections.singletonMap("from", "circuitbreakerfallbackcontroller3"); } + @RequestMapping("/statusCodeFallbackController") + public Map statusCodeFallbackController(ServerWebExchange exchange) { + return Collections.singletonMap("from", "statusCodeFallbackController"); + } + @Bean public RouteLocator circuitBreakerRouteLocator(RouteLocatorBuilder builder) { return builder.routes().route("circuitbreaker_fallback_forward", diff --git a/spring-cloud-gateway-core/src/test/resources/application.yml b/spring-cloud-gateway-core/src/test/resources/application.yml index 455782b7..e00d030b 100644 --- a/spring-cloud-gateway-core/src/test/resources/application.yml +++ b/spring-cloud-gateway-core/src/test/resources/application.yml @@ -94,6 +94,20 @@ spring: name: fallbackcmd fallbackUri: forward:/circuitbreakerFallbackController + # ===================================== + - id: circuitbreaker_fallback_test_statuscode + uri: ${test.uri} + predicates: + - Host=**.circuitbreakerstatuscode.org + filters: + - name: CircuitBreaker + args: + name: fallbackcmd + statusCodes: + - 500 + - "NOT_FOUND" + fallbackUri: forward:/statusCodeFallbackController + # ===================================== - id: change_uri_test uri: ${test.uri} From ef32cb7e9b40759d398d31705bcb5f135349e904 Mon Sep 17 00:00:00 2001 From: Ryan Baxter Date: Tue, 21 Jul 2020 14:52:46 -0400 Subject: [PATCH 6/6] Filtering the status codes just once --- .../factory/SpringCloudCircuitBreakerFilterFactory.java | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactory.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactory.java index 79461968..07b64cbd 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactory.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactory.java @@ -87,17 +87,16 @@ public abstract class SpringCloudCircuitBreakerFilterFactory extends @Override public GatewayFilter apply(Config config) { ReactiveCircuitBreaker cb = reactiveCircuitBreakerFactory.create(config.getId()); + Set statuses = config.getStatusCodes().stream() + .map(HttpStatusHolder::parse) + .filter(statusHolder -> statusHolder.getHttpStatus() != null) + .map(HttpStatusHolder::getHttpStatus).collect(Collectors.toSet()); return new GatewayFilter() { @Override public Mono filter(ServerWebExchange exchange, GatewayFilterChain chain) { return cb.run(chain.filter(exchange).doOnSuccess(v -> { - Set statuses = config.getStatusCodes().stream() - .map(HttpStatusHolder::parse) - .filter(statusHolder -> statusHolder.getHttpStatus() != null) - .map(HttpStatusHolder::getHttpStatus) - .collect(Collectors.toSet()); if (statuses.contains(exchange.getResponse().getStatusCode())) { HttpStatus status = exchange.getResponse().getStatusCode(); exchange.getResponse().setStatusCode(null);