From d4637714e1bb3930bbda47ada7768d22f6c112e6 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 5 Oct 2018 14:20:59 -0400 Subject: [PATCH] Lazily inject DispatcherHandler. This prevents early initialization. --- .../config/GatewayAutoConfiguration.java | 4 +-- .../gateway/filter/ForwardRoutingFilter.java | 11 +++---- .../factory/HystrixGatewayFilterFactory.java | 29 ++++++++++--------- .../filter/ForwardRoutingFilterTests.java | 15 ++++++++-- .../HystrixGatewayFilterFactoryTests.java | 2 +- 5 files changed, 36 insertions(+), 25 deletions(-) diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java index e33285fe..89978c30 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java @@ -364,7 +364,7 @@ public class GatewayAutoConfiguration { @Bean @ConditionalOnBean(DispatcherHandler.class) - public ForwardRoutingFilter forwardRoutingFilter(DispatcherHandler dispatcherHandler) { + public ForwardRoutingFilter forwardRoutingFilter(ObjectProvider dispatcherHandler) { return new ForwardRoutingFilter(dispatcherHandler); } @@ -491,7 +491,7 @@ public class GatewayAutoConfiguration { @ConditionalOnClass({HystrixObservableCommand.class, RxReactiveStreams.class}) protected static class HystrixConfiguration { @Bean - public HystrixGatewayFilterFactory hystrixGatewayFilterFactory(DispatcherHandler dispatcherHandler) { + public HystrixGatewayFilterFactory hystrixGatewayFilterFactory(ObjectProvider dispatcherHandler) { return new HystrixGatewayFilterFactory(dispatcherHandler); } } diff --git a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ForwardRoutingFilter.java b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ForwardRoutingFilter.java index 50e81c8b..156225cc 100644 --- a/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ForwardRoutingFilter.java +++ b/spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ForwardRoutingFilter.java @@ -4,6 +4,9 @@ import java.net.URI; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; +import reactor.core.publisher.Mono; + +import org.springframework.beans.factory.ObjectProvider; import org.springframework.core.Ordered; import org.springframework.web.reactive.DispatcherHandler; import org.springframework.web.server.ServerWebExchange; @@ -12,15 +15,13 @@ import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.G import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.isAlreadyRouted; import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.setAlreadyRouted; -import reactor.core.publisher.Mono; - public class ForwardRoutingFilter implements GlobalFilter, Ordered { private static final Log log = LogFactory.getLog(ForwardRoutingFilter.class); - private final DispatcherHandler dispatcherHandler; + private final ObjectProvider dispatcherHandler; - public ForwardRoutingFilter(DispatcherHandler dispatcherHandler) { + public ForwardRoutingFilter(ObjectProvider dispatcherHandler) { this.dispatcherHandler = dispatcherHandler; } @@ -45,6 +46,6 @@ public class ForwardRoutingFilter implements GlobalFilter, Ordered { log.trace("Forwarding to URI: "+requestUrl); } - return this.dispatcherHandler.handle(exchange); + return this.dispatcherHandler.getIfAvailable().handle(exchange); } } 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 4de837b4..1376bf5e 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 @@ -23,6 +23,17 @@ import java.util.List; import java.util.function.Consumer; import java.util.function.Function; +import com.netflix.hystrix.HystrixCommandGroupKey; +import com.netflix.hystrix.HystrixCommandKey; +import com.netflix.hystrix.HystrixObservableCommand; +import com.netflix.hystrix.HystrixObservableCommand.Setter; +import com.netflix.hystrix.exception.HystrixRuntimeException; +import reactor.core.publisher.Mono; +import rx.Observable; +import rx.RxReactiveStreams; +import rx.Subscription; + +import org.springframework.beans.factory.ObjectProvider; import org.springframework.cloud.gateway.filter.GatewayFilter; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.http.HttpStatus; @@ -33,22 +44,11 @@ import org.springframework.web.reactive.DispatcherHandler; import org.springframework.web.server.ServerWebExchange; import org.springframework.web.util.UriComponentsBuilder; -import com.netflix.hystrix.HystrixCommandGroupKey; -import com.netflix.hystrix.HystrixCommandKey; -import com.netflix.hystrix.HystrixObservableCommand; -import com.netflix.hystrix.HystrixObservableCommand.Setter; -import com.netflix.hystrix.exception.HystrixRuntimeException; - import static com.netflix.hystrix.exception.HystrixRuntimeException.FailureType.TIMEOUT; 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.setResponseStatus; -import reactor.core.publisher.Mono; -import rx.Observable; -import rx.RxReactiveStreams; -import rx.Subscription; - /** * Depends on `spring-cloud-starter-netflix-hystrix`, {@see http://cloud.spring.io/spring-cloud-netflix/} * @author Spencer Gibb @@ -57,9 +57,9 @@ public class HystrixGatewayFilterFactory extends AbstractGatewayFilterFactory dispatcherHandler; - public HystrixGatewayFilterFactory(DispatcherHandler dispatcherHandler) { + public HystrixGatewayFilterFactory(ObjectProvider dispatcherHandler) { super(Config.class); this.dispatcherHandler = dispatcherHandler; } @@ -150,7 +150,8 @@ public class HystrixGatewayFilterFactory extends AbstractGatewayFilterFactory objectProvider; + @Mock private DispatcherHandler dispatcherHandler; @@ -42,6 +50,7 @@ public class ForwardRoutingFilterTests { @Before public void setup() { exchange = MockServerWebExchange.from(MockServerHttpRequest.get("localendpoint").build()); + when(objectProvider.getIfAvailable()).thenReturn(this.dispatcherHandler); } @Test diff --git a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactoryTests.java b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactoryTests.java index 1695aba9..bcaf3429 100644 --- a/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactoryTests.java +++ b/spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactoryTests.java @@ -47,7 +47,7 @@ import org.springframework.web.bind.annotation.RestController; import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT; @RunWith(SpringRunner.class) -@SpringBootTest(webEnvironment = RANDOM_PORT) +@SpringBootTest(webEnvironment = RANDOM_PORT, properties = "debug=true") @DirtiesContext public class HystrixGatewayFilterFactoryTests extends BaseWebClientTests {