Browse Source

Adds instance method support to SpEL evaluation in Discovery Client Locator.

This allows you to use `serviceId.toLowerCase()` for example.

See gh-302
pull/305/head
Spencer Gibb 7 years ago
parent
commit
3d2364a570
No known key found for this signature in database
GPG Key ID: 7788A47380690861
  1. 5
      spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteDefinitionLocator.java
  2. 20
      spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteDefinitionLocatorTests.java

5
spring-cloud-gateway-core/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteDefinitionLocator.java

@ -56,7 +56,10 @@ public class DiscoveryClientRouteDefinitionLocator implements RouteDefinitionLoc @@ -56,7 +56,10 @@ public class DiscoveryClientRouteDefinitionLocator implements RouteDefinitionLoc
@Override
public Flux<RouteDefinition> getRouteDefinitions() {
SimpleEvaluationContext evalCtxt = SimpleEvaluationContext.forReadOnlyDataBinding().build();
SimpleEvaluationContext evalCtxt = SimpleEvaluationContext
.forReadOnlyDataBinding()
.withInstanceMethods()
.build();
SpelExpressionParser parser = new SpelExpressionParser();
Expression includeExpr = parser.parseExpression(properties.getIncludeExpression());

20
spring-cloud-gateway-core/src/test/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteDefinitionLocatorTests.java

@ -48,7 +48,13 @@ import static org.springframework.cloud.gateway.handler.predicate.RoutePredicate @@ -48,7 +48,13 @@ import static org.springframework.cloud.gateway.handler.predicate.RoutePredicate
@SpringBootTest(classes = DiscoveryClientRouteDefinitionLocatorTests.Config.class,
properties = {"spring.cloud.gateway.discovery.locator.enabled=true",
"spring.cloud.gateway.discovery.locator.route-id-prefix=testedge_",
"spring.cloud.gateway.discovery.locator.include-expression=metadata['edge'] == 'true'"})
"spring.cloud.gateway.discovery.locator.include-expression=metadata['edge'] == 'true'",
"spring.cloud.gateway.discovery.locator.predicates[0].name=Path",
"spring.cloud.gateway.discovery.locator.predicates[0].args[pattern]='/'+serviceId.toLowerCase()+'/**'",
"spring.cloud.gateway.discovery.locator.filters[0].name=RewritePath",
"spring.cloud.gateway.discovery.locator.filters[0].args[regexp]='/' + serviceId.toLowerCase() + '/(?<remaining>.*)'",
"spring.cloud.gateway.discovery.locator.filters[0].args[replacement]='/$\\\\{remaining}'",
})
public class DiscoveryClientRouteDefinitionLocatorTests {
@Autowired(required = false)
@ -64,9 +70,9 @@ public class DiscoveryClientRouteDefinitionLocatorTests { @@ -64,9 +70,9 @@ public class DiscoveryClientRouteDefinitionLocatorTests {
assertThat(definitions).hasSize(1);
RouteDefinition definition = definitions.get(0);
assertThat(definition.getId()).isEqualTo("testedge_service1");
assertThat(definition.getId()).isEqualTo("testedge_SERVICE1");
assertThat(definition.getUri()).hasScheme("lb")
.hasHost("service1");
.hasHost("SERVICE1");
assertThat(definition.getPredicates()).hasSize(1);
PredicateDefinition predicate = definition.getPredicates().get(0);
@ -78,7 +84,7 @@ public class DiscoveryClientRouteDefinitionLocatorTests { @@ -78,7 +84,7 @@ public class DiscoveryClientRouteDefinitionLocatorTests {
assertThat(filter.getName()).isEqualTo("RewritePath");
assertThat(filter.getArgs()).hasSize(2)
.containsEntry(REGEXP_KEY, "/service1/(?<remaining>.*)")
.containsEntry(REPLACEMENT_KEY, "/${remaining}");
.containsEntry(REPLACEMENT_KEY, "/$\\{remaining}");
}
@SpringBootConfiguration
@ -88,9 +94,9 @@ public class DiscoveryClientRouteDefinitionLocatorTests { @@ -88,9 +94,9 @@ public class DiscoveryClientRouteDefinitionLocatorTests {
@Bean
DiscoveryClient discoveryClient() {
DiscoveryClient discoveryClient = mock(DiscoveryClient.class);
when(discoveryClient.getServices()).thenReturn(Arrays.asList("service1", "service2"));
whenInstance(discoveryClient, "service1", Collections.singletonMap("edge", "true"));
whenInstance(discoveryClient, "service2", Collections.emptyMap());
when(discoveryClient.getServices()).thenReturn(Arrays.asList("SERVICE1", "Service2"));
whenInstance(discoveryClient, "SERVICE1", Collections.singletonMap("edge", "true"));
whenInstance(discoveryClient, "Service2", Collections.emptyMap());
return discoveryClient;
}

Loading…
Cancel
Save