From 77962dffc56a2f89afb4e53f917c9498d9c772c8 Mon Sep 17 00:00:00 2001 From: Spencer Gibb Date: Fri, 27 Jan 2017 17:08:51 -0700 Subject: [PATCH] Adds DiscoveryClientRouteReader fixes gh-14 --- pom.xml | 12 +++++ .../discovery/DiscoveryClientRouteReader.java | 51 +++++++++++++++++++ .../gateway/test/GatewayTestApplication.java | 22 ++++++++ 3 files changed, 85 insertions(+) create mode 100644 src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteReader.java diff --git a/pom.xml b/pom.xml index c8a8a254..ae929191 100644 --- a/pom.xml +++ b/pom.xml @@ -81,6 +81,18 @@ org.springframework.boot spring-boot-devtools + + org.springframework.cloud + spring-cloud-starter-eureka + test + + + + org.springframework.boot + spring-boot-starter-web + + + org.springframework.boot spring-boot-starter-test diff --git a/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteReader.java b/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteReader.java new file mode 100644 index 00000000..075526b2 --- /dev/null +++ b/src/main/java/org/springframework/cloud/gateway/discovery/DiscoveryClientRouteReader.java @@ -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 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 + "/(?.*)"); + filter.setArgs(new String[]{ "/${remaining}" }); + route.getFilters().add(filter); + + //TODO: support for default filters + + return route; + }); + } +} diff --git a/src/test/java/org/springframework/cloud/gateway/test/GatewayTestApplication.java b/src/test/java/org/springframework/cloud/gateway/test/GatewayTestApplication.java index 86017a18..680e0246 100644 --- a/src/test/java/org/springframework/cloud/gateway/test/GatewayTestApplication.java +++ b/src/test/java/org/springframework/cloud/gateway/test/GatewayTestApplication.java @@ -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); }