Browse Source

Ports refresh from CachingRouteDefinitionLocator

pull/1489/head
Spencer Gibb 5 years ago
parent
commit
1a2643d800
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 15
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/route/CachingRouteLocator.java

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

@ -19,6 +19,7 @@ package org.springframework.cloud.gateway.route; @@ -19,6 +19,7 @@ package org.springframework.cloud.gateway.route;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;
import reactor.cache.CacheFlux;
import reactor.core.publisher.Flux;
@ -33,6 +34,8 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator; @@ -33,6 +34,8 @@ import org.springframework.core.annotation.AnnotationAwareOrderComparator;
public class CachingRouteLocator
implements RouteLocator, ApplicationListener<RefreshRoutesEvent> {
private static final String CACHE_KEY = "routes";
private final RouteLocator delegate;
private final Flux<Route> routes;
@ -41,9 +44,12 @@ public class CachingRouteLocator @@ -41,9 +44,12 @@ public class CachingRouteLocator
public CachingRouteLocator(RouteLocator delegate) {
this.delegate = delegate;
routes = CacheFlux.lookup(cache, "routes", Route.class)
.onCacheMissResume(() -> this.delegate.getRoutes()
.sort(AnnotationAwareOrderComparator.INSTANCE));
routes = CacheFlux.lookup(cache, CACHE_KEY, Route.class)
.onCacheMissResume(this::fetch);
}
private Flux<Route> fetch() {
return this.delegate.getRoutes().sort(AnnotationAwareOrderComparator.INSTANCE);
}
@Override
@ -62,7 +68,8 @@ public class CachingRouteLocator @@ -62,7 +68,8 @@ public class CachingRouteLocator
@Override
public void onApplicationEvent(RefreshRoutesEvent event) {
refresh();
fetch().materialize().collect(Collectors.toList())
.map(routes -> cache.put(CACHE_KEY, routes)).subscribe();
}
@Deprecated

Loading…
Cancel
Save