Skip to content

Commit ee9d6ad

Browse files
author
Ignacio Lozano
authored
Correct the order when routes are refreshed by group (#3112)
1 parent 7becdc5 commit ee9d6ad

File tree

2 files changed

+60
-2
lines changed

2 files changed

+60
-2
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,9 @@ public void onApplicationEvent(RefreshRoutesEvent event) {
8888
.onErrorResume(s -> Mono.just(List.of()));
8989

9090
scopedRoutes.subscribe(scopedRoutesList -> {
91-
Flux.concat(Flux.fromIterable(scopedRoutesList), getNonScopedRoutes(event)).materialize()
92-
.collect(Collectors.toList()).subscribe(signals -> {
91+
Flux.concat(Flux.fromIterable(scopedRoutesList), getNonScopedRoutes(event))
92+
.sort(AnnotationAwareOrderComparator.INSTANCE).materialize().collect(Collectors.toList())
93+
.subscribe(signals -> {
9394
applicationEventPublisher.publishEvent(new RefreshRoutesResultEvent(this));
9495
cache.put(CACHE_KEY, signals);
9596
}, this::handleRefreshError);

spring-cloud-gateway-server/src/test/java/org/springframework/cloud/gateway/actuate/GatewayControllerEndpointTests.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Map;
2424
import java.util.UUID;
2525
import java.util.function.Predicate;
26+
import java.util.stream.Collectors;
2627

2728
import org.assertj.core.util.Maps;
2829
import org.junit.jupiter.api.Assertions;
@@ -193,6 +194,62 @@ public void testRefreshByGroup() {
193194
});
194195
}
195196

197+
@Test
198+
public void testOrderOfRefreshByGroup() {
199+
RouteDefinition testRouteDefinition = new RouteDefinition();
200+
testRouteDefinition.setUri(URI.create("http://example.org"));
201+
testRouteDefinition.setOrder(1000);
202+
String group1 = "group-1_" + UUID.randomUUID();
203+
testRouteDefinition.setMetadata(Map.of("groupBy", group1));
204+
205+
String routeId1 = "route-1_" + UUID.randomUUID();
206+
testClient.post().uri("http://localhost:" + port + "/actuator/gateway/routes/" + routeId1)
207+
.accept(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(testRouteDefinition)).exchange()
208+
.expectStatus().isCreated();
209+
210+
RouteDefinition testRouteDefinition2 = new RouteDefinition();
211+
testRouteDefinition2.setUri(URI.create("http://example.org"));
212+
testRouteDefinition2.setOrder(0);
213+
String group2 = "group-2_" + UUID.randomUUID();
214+
testRouteDefinition2.setMetadata(Map.of("groupBy", group2));
215+
String routeId2 = "route-2_" + UUID.randomUUID();
216+
testClient.post().uri("http://localhost:" + port + "/actuator/gateway/routes/" + routeId2)
217+
.accept(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(testRouteDefinition2)).exchange()
218+
.expectStatus().isCreated();
219+
220+
testClient.post().uri("http://localhost:" + port + "/actuator/gateway/refresh?metadata=groupBy:" + group1)
221+
.exchange().expectStatus().isOk();
222+
testClient.post().uri("http://localhost:" + port + "/actuator/gateway/refresh?metadata=groupBy:" + group2)
223+
.exchange().expectStatus().isOk();
224+
225+
testClient.get().uri("http://localhost:" + port + "/actuator/gateway/routes").exchange().expectStatus().isOk()
226+
.expectBodyList(Map.class).consumeWith(result -> {
227+
List<Map> responseBody = result.getResponseBody();
228+
229+
List ids = responseBody.stream()
230+
.map(route -> route.get("route_id"))
231+
.filter(id -> id.equals(routeId1) || id.equals(routeId2))
232+
.collect(Collectors.toList());
233+
assertThat(ids).containsExactly(routeId2, routeId1);
234+
});
235+
236+
testRouteDefinition2.setOrder(testRouteDefinition.getOrder() + 1);
237+
testClient.post().uri("http://localhost:" + port + "/actuator/gateway/routes/" + routeId2)
238+
.accept(MediaType.APPLICATION_JSON).body(BodyInserters.fromValue(testRouteDefinition2)).exchange()
239+
.expectStatus().isCreated();
240+
testClient.post().uri("http://localhost:" + port + "/actuator/gateway/refresh?metadata=groupBy:" + group2)
241+
.exchange().expectStatus().isOk();
242+
testClient.get().uri("http://localhost:" + port + "/actuator/gateway/routes").exchange().expectStatus().isOk()
243+
.expectBodyList(Map.class).consumeWith(result -> {
244+
List<Map> responseBody = result.getResponseBody();
245+
List ids = responseBody.stream()
246+
.map(route -> route.get("route_id"))
247+
.filter(id -> id.equals(routeId1) || id.equals(routeId2))
248+
.collect(Collectors.toList());
249+
assertThat(ids).containsExactly(routeId1, routeId2);
250+
});
251+
}
252+
196253
@Test
197254
public void testRefreshByGroup_whenRouteDefinitionsAreDeleted() {
198255
RouteDefinition testRouteDefinition = new RouteDefinition();

0 commit comments

Comments
 (0)