Lazily inject DispatcherHandler.
This prevents early initialization.
This commit is contained in:
@@ -364,7 +364,7 @@ public class GatewayAutoConfiguration {
|
||||
|
||||
@Bean
|
||||
@ConditionalOnBean(DispatcherHandler.class)
|
||||
public ForwardRoutingFilter forwardRoutingFilter(DispatcherHandler dispatcherHandler) {
|
||||
public ForwardRoutingFilter forwardRoutingFilter(ObjectProvider<DispatcherHandler> 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> dispatcherHandler) {
|
||||
return new HystrixGatewayFilterFactory(dispatcherHandler);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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> dispatcherHandler;
|
||||
|
||||
public ForwardRoutingFilter(DispatcherHandler dispatcherHandler) {
|
||||
public ForwardRoutingFilter(ObjectProvider<DispatcherHandler> 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);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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<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;
|
||||
}
|
||||
@@ -150,7 +150,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));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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;
|
||||
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;
|
||||
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 {
|
||||
@Mock
|
||||
private GatewayFilterChain chain;
|
||||
|
||||
@Mock
|
||||
private ObjectProvider<DispatcherHandler> 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
|
||||
|
||||
@@ -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 {
|
||||
|
||||
|
||||
Reference in New Issue
Block a user