Browse Source

Makes CachingRouteLocator earlier for event listeners.

fixes gh-1377
pull/1511/head
Spencer Gibb 5 years ago
parent
commit
81ff34f858
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 8
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/CachingRouteLocator.java
  2. 26
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java

8
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/CachingRouteLocator.java

@ -26,13 +26,14 @@ import reactor.core.publisher.Flux; @@ -26,13 +26,14 @@ import reactor.core.publisher.Flux;
import org.springframework.cloud.gateway.event.RefreshRoutesEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
/**
* @author Spencer Gibb
*/
public class CachingRouteLocator
implements RouteLocator, ApplicationListener<RefreshRoutesEvent> {
implements Ordered, RouteLocator, ApplicationListener<RefreshRoutesEvent> {
private static final String CACHE_KEY = "routes";
@ -77,4 +78,9 @@ public class CachingRouteLocator @@ -77,4 +78,9 @@ public class CachingRouteLocator
refresh();
}
@Override
public int getOrder() {
return 0;
}
}

26
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/test/GatewayIntegrationTests.java

@ -16,6 +16,8 @@ @@ -16,6 +16,8 @@
package org.springframework.cloud.gateway.test;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.apache.commons.logging.Log;
@ -32,11 +34,17 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; @@ -32,11 +34,17 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.cloud.gateway.config.GatewayProperties;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.WeightCalculatorWebFilter;
import org.springframework.cloud.gateway.filter.headers.ForwardedHeadersFilter;
import org.springframework.cloud.gateway.filter.headers.XForwardedHeadersFilter;
import org.springframework.cloud.gateway.handler.RoutePredicateHandlerMapping;
import org.springframework.cloud.gateway.route.CachingRouteLocator;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.support.AbstractApplicationContext;
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
import org.springframework.core.annotation.Order;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
@ -60,6 +68,24 @@ public class GatewayIntegrationTests extends BaseWebClientTests { @@ -60,6 +68,24 @@ public class GatewayIntegrationTests extends BaseWebClientTests {
@Autowired
private GatewayProperties properties;
@Autowired
private ApplicationContext context;
@Test
public void listenersInOrder() {
assertThat(context).isInstanceOf(AbstractApplicationContext.class);
AbstractApplicationContext ctxt = (AbstractApplicationContext) context;
List<ApplicationListener<?>> applicationListeners = new ArrayList<>(
ctxt.getApplicationListeners());
AnnotationAwareOrderComparator.sort(applicationListeners);
int weightFilterIndex = applicationListeners
.indexOf(context.getBean(WeightCalculatorWebFilter.class));
int routeLocatorIndex = applicationListeners
.indexOf(context.getBean(CachingRouteLocator.class));
assertThat(weightFilterIndex > routeLocatorIndex)
.as("CachingRouteLocator is after WeightCalculatorWebFilter").isTrue();
}
@Test
public void complexContentTypeWorks() {
testClient.post().uri("/headers").contentType(MediaType.APPLICATION_JSON_UTF8)

Loading…
Cancel
Save