Browse Source

Lazily inject DispatcherHandler.

This prevents early initialization.
pull/555/head
Spencer Gibb 6 years ago committed by Ryan Baxter
parent
commit
b42c45fb89
  1. 4
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java
  2. 11
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ForwardRoutingFilter.java
  3. 29
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactory.java
  4. 15
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ForwardRoutingFilterTests.java
  5. 2
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactoryTests.java

4
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/config/GatewayAutoConfiguration.java

@ -369,7 +369,7 @@ public class GatewayAutoConfiguration { @@ -369,7 +369,7 @@ public class GatewayAutoConfiguration {
@Bean
@ConditionalOnBean(DispatcherHandler.class)
public ForwardRoutingFilter forwardRoutingFilter(DispatcherHandler dispatcherHandler) {
public ForwardRoutingFilter forwardRoutingFilter(ObjectProvider<DispatcherHandler> dispatcherHandler) {
return new ForwardRoutingFilter(dispatcherHandler);
}
@ -496,7 +496,7 @@ public class GatewayAutoConfiguration { @@ -496,7 +496,7 @@ public class GatewayAutoConfiguration {
@ConditionalOnClass({HystrixObservableCommand.class, RxReactiveStreams.class})
protected static class HystrixConfiguration {
@Bean
public HystrixGatewayFilterFactory hystrixGatewayFilterFactory(DispatcherHandler dispatcherHandler) {
public HystrixGatewayFilterFactory hystrixGatewayFilterFactory(ObjectProvider<DispatcherHandler> dispatcherHandler) {
return new HystrixGatewayFilterFactory(dispatcherHandler);
}
}

11
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/ForwardRoutingFilter.java

@ -4,6 +4,9 @@ import java.net.URI; @@ -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 @@ -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> dispatcherHandler;
public ForwardRoutingFilter(DispatcherHandler dispatcherHandler) {
public ForwardRoutingFilter(ObjectProvider<DispatcherHandler> dispatcherHandler) {
this.dispatcherHandler = dispatcherHandler;
}
@ -45,6 +46,6 @@ public class ForwardRoutingFilter implements GlobalFilter, Ordered { @@ -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);
}
}

29
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactory.java

@ -23,6 +23,17 @@ import java.util.List; @@ -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;
@ -34,21 +45,10 @@ import org.springframework.web.server.ResponseStatusException; @@ -34,21 +45,10 @@ import org.springframework.web.server.ResponseStatusException;
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 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<Hy @@ -57,9 +57,9 @@ public class HystrixGatewayFilterFactory extends AbstractGatewayFilterFactory<Hy
public static final String FALLBACK_URI = "fallbackUri";
private final DispatcherHandler dispatcherHandler;
private final ObjectProvider<DispatcherHandler> dispatcherHandler;
public HystrixGatewayFilterFactory(DispatcherHandler dispatcherHandler) {
public HystrixGatewayFilterFactory(ObjectProvider<DispatcherHandler> dispatcherHandler) {
super(Config.class);
this.dispatcherHandler = dispatcherHandler;
}
@ -149,7 +149,8 @@ public class HystrixGatewayFilterFactory extends AbstractGatewayFilterFactory<Hy @@ -149,7 +149,8 @@ public class HystrixGatewayFilterFactory extends AbstractGatewayFilterFactory<Hy
ServerHttpRequest request = this.exchange.getRequest().mutate().uri(requestUrl).build();
ServerWebExchange mutated = exchange.mutate().request(request).build();
return RxReactiveStreams.toObservable(HystrixGatewayFilterFactory.this.dispatcherHandler.handle(mutated));
DispatcherHandler dispatcherHandler = HystrixGatewayFilterFactory.this.dispatcherHandler.getIfAvailable();
return RxReactiveStreams.toObservable(dispatcherHandler.handle(mutated));
}
}

15
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/ForwardRoutingFilterTests.java

@ -1,5 +1,7 @@ @@ -1,5 +1,7 @@
package org.springframework.cloud.gateway.filter;
import java.net.URI;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
@ -7,6 +9,8 @@ import org.mockito.ArgumentCaptor; @@ -7,6 +9,8 @@ import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.core.Ordered;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
@ -14,10 +18,11 @@ import org.springframework.web.reactive.DispatcherHandler; @@ -14,10 +18,11 @@ import org.springframework.web.reactive.DispatcherHandler;
import org.springframework.web.server.ServerWebExchange;
import org.springframework.web.util.UriComponentsBuilder;
import java.net.URI;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.*;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyNoMoreInteractions;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_ALREADY_ROUTED_ATTR;
import static org.springframework.cloud.gateway.support.ServerWebExchangeUtils.GATEWAY_REQUEST_URL_ATTR;
@ -33,6 +38,9 @@ public class ForwardRoutingFilterTests { @@ -33,6 +38,9 @@ public class ForwardRoutingFilterTests {
@Mock
private GatewayFilterChain chain;
@Mock
private ObjectProvider<DispatcherHandler> objectProvider;
@Mock
private DispatcherHandler dispatcherHandler;
@ -42,6 +50,7 @@ public class ForwardRoutingFilterTests { @@ -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

2
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/filter/factory/HystrixGatewayFilterFactoryTests.java

@ -50,7 +50,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen @@ -50,7 +50,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen
import static org.springframework.http.MediaType.TEXT_HTML;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = RANDOM_PORT)
@SpringBootTest(webEnvironment = RANDOM_PORT, properties = "debug=true")
@DirtiesContext
public class HystrixGatewayFilterFactoryTests extends BaseWebClientTests {

Loading…
Cancel
Save