Skip to content

Commit 88fce67

Browse files
authored
Merge pull request #3543 from qnnn/circuitbreaker-fix
Enable body caching in CircuitBreakerFilterFactory
2 parents 160c820 + 611ae85 commit 88fce67

File tree

6 files changed

+40
-5
lines changed

6 files changed

+40
-5
lines changed

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/AbstractGatewayFilterFactory.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.cloud.gateway.filter.factory;
1818

19+
import org.springframework.cloud.gateway.event.EnableBodyCachingEvent;
1920
import org.springframework.cloud.gateway.support.AbstractConfigurable;
2021
import org.springframework.context.ApplicationEventPublisher;
2122
import org.springframework.context.ApplicationEventPublisherAware;
@@ -43,6 +44,13 @@ protected ApplicationEventPublisher getPublisher() {
4344
return this.publisher;
4445
}
4546

47+
protected void enableBodyCaching(String routeId) {
48+
if (routeId != null && getPublisher() != null) {
49+
// send an event to enable caching
50+
getPublisher().publishEvent(new EnableBodyCachingEvent(this, routeId));
51+
}
52+
}
53+
4654
@Override
4755
public void setApplicationEventPublisher(ApplicationEventPublisher publisher) {
4856
this.publisher = publisher;

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/RetryGatewayFilterFactory.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@
3535
import reactor.retry.Retry;
3636
import reactor.retry.RetryContext;
3737

38-
import org.springframework.cloud.gateway.event.EnableBodyCachingEvent;
3938
import org.springframework.cloud.gateway.filter.GatewayFilter;
4039
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
4140
import org.springframework.cloud.gateway.support.HasRouteId;
@@ -229,10 +228,7 @@ public void reset(ServerWebExchange exchange) {
229228
}
230229

231230
public GatewayFilter apply(String routeId, Repeat<ServerWebExchange> repeat, Retry<ServerWebExchange> retry) {
232-
if (routeId != null && getPublisher() != null) {
233-
// send an event to enable caching
234-
getPublisher().publishEvent(new EnableBodyCachingEvent(this, routeId));
235-
}
231+
enableBodyCaching(routeId);
236232
return (exchange, chain) -> {
237233
trace("Entering retry-filter");
238234

spring-cloud-gateway-server/src/main/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactory.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ public List<String> shortcutFieldOrder() {
8989

9090
@Override
9191
public GatewayFilter apply(Config config) {
92+
if (config.getFallbackUri() != null) {
93+
enableBodyCaching(config.getRouteId());
94+
}
9295
ReactiveCircuitBreaker cb = reactiveCircuitBreakerFactory.create(config.getId());
9396
Set<HttpStatus> statuses = config.getStatusCodes()
9497
.stream()

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerFilterFactoryTests.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.cloud.gateway.test.BaseWebClientTests;
2323
import org.springframework.http.HttpStatus;
24+
import org.springframework.web.reactive.function.BodyInserters;
2425

2526
import static org.assertj.core.api.Assertions.assertThat;
2627
import static org.springframework.http.MediaType.APPLICATION_JSON;
@@ -243,4 +244,11 @@ public void filterStatusCodeResumeWithoutError() {
243244
.valueEquals(ROUTE_ID_HEADER, "circuitbreaker_resume_without_error");
244245
}
245246

247+
@Test
248+
public void filterPostFallback() {
249+
testClient.post().uri("/post").body(BodyInserters.fromValue("hello"))
250+
.header("Host", "www.circuitbreakerfallbackpost.org").exchange().expectStatus()
251+
.isOk().expectBody().json("{\"body\":\"hello\"}");
252+
}
253+
246254
}

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/filter/factory/SpringCloudCircuitBreakerTestConfig.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import org.springframework.http.HttpStatus;
3939
import org.springframework.http.ResponseEntity;
4040
import org.springframework.web.bind.annotation.GetMapping;
41+
import org.springframework.web.bind.annotation.PostMapping;
42+
import org.springframework.web.bind.annotation.RequestBody;
4143
import org.springframework.web.bind.annotation.RequestMapping;
4244
import org.springframework.web.bind.annotation.RequestParam;
4345
import org.springframework.web.bind.annotation.RestController;
@@ -68,6 +70,11 @@ public Map<String, String> fallbackcontroller(@RequestParam("a") String a) {
6870
return Collections.singletonMap("from", "circuitbreakerfallbackcontroller");
6971
}
7072

73+
@PostMapping("/circuitbreakerPostFallbackController")
74+
public Map<String, String> postFallbackController(@RequestBody String body) {
75+
return Collections.singletonMap("body", body);
76+
}
77+
7178
@GetMapping("/circuitbreakerUriFallbackController/**")
7279
public Map<String, String> uriFallbackcontroller(ServerWebExchange exchange, @RequestParam("a") String a) {
7380
return Collections.singletonMap("uri", exchange.getRequest().getURI().toString());

spring-cloud-gateway-server/src/test/resources/application.yml

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ spring:
104104
name: fallbackcmd
105105
fallbackUri: forward:/circuitbreakerFallbackController
106106

107+
# =====================================
108+
- id: circuitbreaker_fallback_test_post
109+
uri: ${test.uri}
110+
predicates:
111+
- Host=**.circuitbreakerfallbackpost.org
112+
filters:
113+
- name: CircuitBreaker
114+
args:
115+
name: fallbackcmd
116+
statusCodes:
117+
- 200
118+
fallbackUri: forward:/circuitbreakerPostFallbackController
119+
107120
# =====================================
108121
- id: circuitbreaker_fallback_test_variables
109122
uri: ${test.uri}

0 commit comments

Comments
 (0)