Adds RouteDefinition.enabled
enabled is true by default. Adds the ability to disable routes. Fixes gh-3026
This commit is contained in:
@@ -69,7 +69,7 @@ public class GatewayProperties {
|
||||
private boolean failOnRouteDefinitionError = true;
|
||||
|
||||
public List<RouteDefinition> getRoutes() {
|
||||
return routes;
|
||||
return routes.stream().filter(RouteDefinition::isEnabled).toList();
|
||||
}
|
||||
|
||||
public void setRoutes(List<RouteDefinition> routes) {
|
||||
|
||||
@@ -56,6 +56,8 @@ public class RouteDefinition {
|
||||
|
||||
private int order = 0;
|
||||
|
||||
private boolean enabled = true;
|
||||
|
||||
public RouteDefinition() {
|
||||
}
|
||||
|
||||
@@ -125,6 +127,14 @@ public class RouteDefinition {
|
||||
this.metadata = metadata;
|
||||
}
|
||||
|
||||
public boolean isEnabled() {
|
||||
return enabled;
|
||||
}
|
||||
|
||||
public void setEnabled(boolean enabled) {
|
||||
this.enabled = enabled;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object o) {
|
||||
if (this == o) {
|
||||
@@ -136,18 +146,19 @@ public class RouteDefinition {
|
||||
RouteDefinition that = (RouteDefinition) o;
|
||||
return this.order == that.order && Objects.equals(this.id, that.id)
|
||||
&& Objects.equals(this.predicates, that.predicates) && Objects.equals(this.filters, that.filters)
|
||||
&& Objects.equals(this.uri, that.uri) && Objects.equals(this.metadata, that.metadata);
|
||||
&& Objects.equals(this.uri, that.uri) && Objects.equals(this.metadata, that.metadata)
|
||||
&& Objects.equals(this.enabled, that.enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(this.id, this.predicates, this.filters, this.uri, this.metadata, this.order);
|
||||
return Objects.hash(this.id, this.predicates, this.filters, this.uri, this.metadata, this.order, this.enabled);
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "RouteDefinition{" + "id='" + id + '\'' + ", predicates=" + predicates + ", filters=" + filters
|
||||
+ ", uri=" + uri + ", order=" + order + ", metadata=" + metadata + '}';
|
||||
+ ", uri=" + uri + ", order=" + order + ", metadata=" + metadata + ", enabled=" + enabled + '}';
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -82,6 +82,35 @@ public class RouteDefinitionRouteLocatorTests {
|
||||
}).expectComplete().verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void disabledRoutesAreNotLoaded() {
|
||||
List<RoutePredicateFactory> predicates = Arrays.asList(new HostRoutePredicateFactory());
|
||||
List<GatewayFilterFactory> gatewayFilterFactories = Arrays.asList(
|
||||
new RemoveResponseHeaderGatewayFilterFactory(), new AddResponseHeaderGatewayFilterFactory(),
|
||||
new TestOrderedGatewayFilterFactory());
|
||||
GatewayProperties gatewayProperties = new GatewayProperties();
|
||||
gatewayProperties.setRoutes(List.of(new RouteDefinition() {
|
||||
{
|
||||
setId("bar");
|
||||
setUri(URI.create("https://bar.example.com"));
|
||||
setEnabled(false);
|
||||
setPredicates(List.of(new PredicateDefinition("Host=*.example.com")));
|
||||
setFilters(Arrays.asList(new FilterDefinition("RemoveResponseHeader=Server"),
|
||||
new FilterDefinition("TestOrdered="),
|
||||
new FilterDefinition("AddResponseHeader=X-Response-Foo, Bar")));
|
||||
}
|
||||
}));
|
||||
|
||||
PropertiesRouteDefinitionLocator routeDefinitionLocator = new PropertiesRouteDefinitionLocator(
|
||||
gatewayProperties);
|
||||
@SuppressWarnings("deprecation")
|
||||
RouteDefinitionRouteLocator routeDefinitionRouteLocator = new RouteDefinitionRouteLocator(
|
||||
new CompositeRouteDefinitionLocator(Flux.just(routeDefinitionLocator)), predicates,
|
||||
gatewayFilterFactories, gatewayProperties, new ConfigurationService(null, () -> null, () -> null));
|
||||
|
||||
StepVerifier.create(routeDefinitionRouteLocator.getRoutes()).expectComplete().verify();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void simpleRetryDefinitionLoads() {
|
||||
List<RoutePredicateFactory> predicates = Arrays.asList(new HostRoutePredicateFactory());
|
||||
|
||||
@@ -19,6 +19,7 @@ package org.springframework.cloud.gateway.test;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
|
||||
import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
@@ -38,6 +39,8 @@ 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.cloud.gateway.route.Route;
|
||||
import org.springframework.cloud.gateway.route.RouteDefinition;
|
||||
import org.springframework.context.ApplicationContext;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
@@ -80,6 +83,15 @@ class GatewayIntegrationTests extends BaseWebClientTests {
|
||||
.isTrue();
|
||||
}
|
||||
|
||||
@Test
|
||||
void checkDisabledFilterNotPresent() {
|
||||
Optional<RouteDefinition> disabledRoute = properties.getRoutes().stream()
|
||||
.filter(r -> "disabled_config_test".equals(r.getId()))
|
||||
.findFirst();
|
||||
assertThat(disabledRoute).as("Disabled route is not present")
|
||||
.isEmpty();
|
||||
}
|
||||
|
||||
@Test
|
||||
void complexContentTypeWorks() {
|
||||
testClient.post()
|
||||
|
||||
@@ -525,6 +525,15 @@ spring:
|
||||
- Host=**.weighthigh.org
|
||||
- Weight=group1, 8
|
||||
|
||||
# =====================================
|
||||
- id: disabled_config_test
|
||||
uri: ${test.uri}
|
||||
enabled: false
|
||||
predicates:
|
||||
- name: Path
|
||||
args:
|
||||
pattern: /**
|
||||
|
||||
# =====================================
|
||||
# should be last and not follow alphabetical order
|
||||
- id: default_path_to_httpbin
|
||||
|
||||
Reference in New Issue
Block a user