support ordering routes
This commit is contained in:
@@ -21,6 +21,7 @@ import java.util.List;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import org.springframework.context.event.EventListener;
|
||||
import org.springframework.core.annotation.AnnotationAwareOrderComparator;
|
||||
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
@@ -52,7 +53,9 @@ public class CachingRouteLocator implements RouteLocator {
|
||||
}
|
||||
|
||||
private List<Route> collectRoutes() {
|
||||
return this.delegate.getRoutes().collectList().block();
|
||||
List<Route> routes = this.delegate.getRoutes().collectList().block();
|
||||
AnnotationAwareOrderComparator.sort(routes);
|
||||
return routes;
|
||||
}
|
||||
|
||||
@EventListener(RefreshRoutesEvent.class)
|
||||
|
||||
@@ -25,6 +25,7 @@ import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Predicate;
|
||||
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.util.Assert;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import org.springframework.web.server.WebFilter;
|
||||
@@ -32,12 +33,14 @@ import org.springframework.web.server.WebFilter;
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class Route {
|
||||
public class Route implements Ordered {
|
||||
|
||||
private final String id;
|
||||
|
||||
private final URI uri;
|
||||
|
||||
private final int order;
|
||||
|
||||
private final Predicate<ServerWebExchange> predicate;
|
||||
|
||||
private final List<WebFilter> webFilters;
|
||||
@@ -49,12 +52,14 @@ public class Route {
|
||||
public static Builder builder(RouteDefinition routeDefinition) {
|
||||
return new Builder()
|
||||
.id(routeDefinition.getId())
|
||||
.uri(routeDefinition.getUri());
|
||||
.uri(routeDefinition.getUri())
|
||||
.order(routeDefinition.getOrder());
|
||||
}
|
||||
|
||||
public Route(String id, URI uri, Predicate<ServerWebExchange> predicate, List<WebFilter> webFilters) {
|
||||
public Route(String id, URI uri, int order, Predicate<ServerWebExchange> predicate, List<WebFilter> webFilters) {
|
||||
this.id = id;
|
||||
this.uri = uri;
|
||||
this.order = order;
|
||||
this.predicate = predicate;
|
||||
this.webFilters = webFilters;
|
||||
}
|
||||
@@ -64,6 +69,8 @@ public class Route {
|
||||
|
||||
private URI uri;
|
||||
|
||||
private int order = 0;
|
||||
|
||||
private Predicate<ServerWebExchange> predicate;
|
||||
|
||||
private List<WebFilter> webFilters = new ArrayList<>();
|
||||
@@ -80,6 +87,11 @@ public class Route {
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder order(int order) {
|
||||
this.order = order;
|
||||
return this;
|
||||
}
|
||||
|
||||
public Builder uri(URI uri) {
|
||||
this.uri = uri;
|
||||
return this;
|
||||
@@ -110,7 +122,7 @@ public class Route {
|
||||
Assert.notNull(this.uri, "uri can not be null");
|
||||
//TODO: Assert.notNull(this.predicate, "predicate can not be null");
|
||||
|
||||
return new Route(this.id, this.uri, this.predicate, this.webFilters);
|
||||
return new Route(this.id, this.uri, this.order, this.predicate, this.webFilters);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -122,6 +134,10 @@ public class Route {
|
||||
return this.uri;
|
||||
}
|
||||
|
||||
public int getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
public Predicate<ServerWebExchange> getPredicate() {
|
||||
return this.predicate;
|
||||
}
|
||||
@@ -137,6 +153,7 @@ public class Route {
|
||||
Route route = (Route) o;
|
||||
return Objects.equals(id, route.id) &&
|
||||
Objects.equals(uri, route.uri) &&
|
||||
Objects.equals(order, route.order) &&
|
||||
Objects.equals(predicate, route.predicate) &&
|
||||
Objects.equals(webFilters, route.webFilters);
|
||||
}
|
||||
@@ -151,6 +168,7 @@ public class Route {
|
||||
final StringBuffer sb = new StringBuffer("Route{");
|
||||
sb.append("id='").append(id).append('\'');
|
||||
sb.append(", uri=").append(uri);
|
||||
sb.append(", order=").append(order);
|
||||
sb.append(", predicate=").append(predicate);
|
||||
sb.append(", webFilters=").append(webFilters);
|
||||
sb.append('}');
|
||||
|
||||
@@ -49,6 +49,8 @@ public class RouteDefinition {
|
||||
@NotNull
|
||||
private URI uri;
|
||||
|
||||
private int order = 0;
|
||||
|
||||
public RouteDefinition() {}
|
||||
|
||||
public RouteDefinition(String text) {
|
||||
@@ -101,6 +103,14 @@ public class RouteDefinition {
|
||||
this.uri = uri;
|
||||
}
|
||||
|
||||
public int getOrder() {
|
||||
return order;
|
||||
}
|
||||
|
||||
public void setOrder(int order) {
|
||||
this.order = order;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) return true;
|
||||
@@ -108,6 +118,7 @@ public class RouteDefinition {
|
||||
RouteDefinition routeDefinition = (RouteDefinition) o;
|
||||
return Objects.equals(id, routeDefinition.id) &&
|
||||
Objects.equals(predicates, routeDefinition.predicates) &&
|
||||
Objects.equals(order, routeDefinition.order) &&
|
||||
Objects.equals(uri, routeDefinition.uri);
|
||||
}
|
||||
|
||||
@@ -123,6 +134,7 @@ public class RouteDefinition {
|
||||
", predicates=" + predicates +
|
||||
", filters=" + filters +
|
||||
", uri=" + uri +
|
||||
", order=" + order +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -17,7 +17,6 @@
|
||||
|
||||
package org.springframework.cloud.gateway.route;
|
||||
|
||||
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||
import reactor.core.publisher.Flux;
|
||||
|
||||
/**
|
||||
|
||||
@@ -178,6 +178,7 @@ spring:
|
||||
# =====================================
|
||||
- id: default_path_to_httpbin
|
||||
uri: ${test.uri}
|
||||
order: 10000
|
||||
predicates:
|
||||
- name: Path
|
||||
args:
|
||||
|
||||
@@ -13,6 +13,7 @@ spring:
|
||||
# =====================================
|
||||
- id: default_path_to_httpbin
|
||||
uri: ${test.uri}
|
||||
order: 10000
|
||||
predicates:
|
||||
- Path=/**
|
||||
|
||||
|
||||
Reference in New Issue
Block a user