Adds DiscoveryClientRouteReader

fixes gh-14
This commit is contained in:
Spencer Gibb
2017-01-27 17:08:51 -07:00
parent c0ec9cdd16
commit 77962dffc5
3 changed files with 85 additions and 0 deletions

12
pom.xml
View File

@@ -81,6 +81,18 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-eureka</artifactId>
<scope>test</scope>
<exclusions>
<exclusion>
<!-- TODO: this should be optional -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>

View File

@@ -0,0 +1,51 @@
package org.springframework.cloud.gateway.discovery;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.gateway.api.RouteReader;
import org.springframework.cloud.gateway.config.FilterDefinition;
import org.springframework.cloud.gateway.config.PredicateDefinition;
import org.springframework.cloud.gateway.config.Route;
import reactor.core.publisher.Flux;
import java.net.URI;
/**
* @author Spencer Gibb
*/
public class DiscoveryClientRouteReader implements RouteReader {
private final DiscoveryClient discoveryClient;
public DiscoveryClientRouteReader(DiscoveryClient discoveryClient) {
this.discoveryClient = discoveryClient;
}
@Override
public Flux<Route> getRoutes() {
return Flux.fromIterable(discoveryClient.getServices())
.map(serviceId -> {
Route route = new Route();
route.setUri(URI.create("lb://" + serviceId));
// add a predicate that matches the url at /serviceId/**
PredicateDefinition predicate = new PredicateDefinition();
predicate.setName("Url");
predicate.setValue("/" + serviceId + "/**");
route.getPredicates().add(predicate);
//TODO: support for other default predicates
// add a filter that removes /serviceId by default
FilterDefinition filter = new FilterDefinition();
filter.setName("RewritePath");
filter.setValue("/" + serviceId + "/(?<remaining>.*)");
filter.setArgs(new String[]{ "/${remaining}" });
route.getFilters().add(filter);
//TODO: support for default filters
return route;
});
}
}

View File

@@ -3,11 +3,33 @@ package org.springframework.cloud.gateway.test;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.gateway.discovery.DiscoveryClientRouteReader;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
@SpringBootConfiguration
@EnableAutoConfiguration
public class GatewayTestApplication {
/*
TO test run `spring cloud configserver eureka`,
then run this app with `--spring.profiles.active=discovery`
should be able to hit http://localhost:8008/configserver/foo/default a normal configserver api
*/
@Configuration
@EnableDiscoveryClient
@Profile("discovery")
protected static class GatewayDiscoveryConfiguration {
@Bean
public DiscoveryClientRouteReader discoveryClientRouteReader(DiscoveryClient discoveryClient) {
return new DiscoveryClientRouteReader(discoveryClient);
}
}
public static void main(String[] args) {
SpringApplication.run(GatewayTestApplication.class, args);
}