Update to require /gateway/refresh for route changes to be affected.

fixes gh-5
This commit is contained in:
Spencer Gibb
2017-02-17 13:33:42 -07:00
parent c47a31a94a
commit c4ae1065da
5 changed files with 56 additions and 20 deletions

View File

@@ -15,6 +15,7 @@ import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.cloud.gateway.filter.route.RouteFilter;
import org.springframework.cloud.gateway.handler.FilteringWebHandler;
import org.springframework.cloud.gateway.support.CachingRouteLocator;
import org.springframework.cloud.gateway.support.NotFoundException;
import org.springframework.core.Ordered;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
@@ -100,10 +101,7 @@ public class GatewayEndpoint {/*extends AbstractEndpoint<Map<String, Object>> {*
}
/*
http POST :8080/admin/gateway/routes/addreqhead2 \
uri=http://httpbin.org/headers \
predicates:='["Host=**.addrequestheader.org", "Url=/headers"]' \
filters:='["AddRequestHeader=X-Request-Foo, Bar"]'
http POST :8080/admin/gateway/routes/apiaddreqhead uri=http://httpbin.org:80 predicates:='["Host=**.apiaddrequestheader.org", "Url=/headers"]' filters:='["AddRequestHeader=X-Request-ApiFoo, ApiBar"]'
*/
@PostMapping("/routes/{id}")
public Mono<ResponseEntity<Void>> save(@PathVariable String id, @RequestBody Mono<Route> route) {
@@ -111,25 +109,25 @@ filters:='["AddRequestHeader=X-Request-Foo, Bar"]'
r.setId(id);
log.debug("Saving route: " + route);
return r;
})).then(() -> {
GatewayEndpoint.this.refresh();
return Mono.just(ResponseEntity.created(URI.create("/routes/"+id)).build());
});
})).then(() ->
Mono.just(ResponseEntity.created(URI.create("/routes/"+id)).build())
);
}
@DeleteMapping("/routes/{id}")
public Mono<ResponseEntity<Void>> delete(@PathVariable Mono<String> id) {
return this.routeWriter.delete(id).then(() -> {
GatewayEndpoint.this.refresh();
return Mono.just(ResponseEntity.ok().build());
});
public Mono<ResponseEntity<Object>> delete(@PathVariable String id) {
return this.routeWriter .delete(Mono.just(id))
.then(() -> Mono.just(ResponseEntity.ok().build()))
.otherwise(t -> t instanceof NotFoundException, t -> Mono.just(ResponseEntity.notFound().build()));
}
@GetMapping("/routes/{id}")
public Mono<Route> route(@PathVariable String id) {
public Mono<ResponseEntity<Route>> route(@PathVariable String id) {
return this.routeLocator.getRoutes()
.filter(route -> route.getId().equals(id))
.singleOrEmpty();
.singleOrEmpty()
.map(route -> ResponseEntity.ok(route))
.otherwiseIfEmpty(Mono.just(ResponseEntity.notFound().build()));
}
@GetMapping("/routes/{id}/combinedfilters")

View File

@@ -21,16 +21,20 @@ public class InMemoryRouteRepository implements RouteLocator, RouteWriter {
@Override
public Mono<Void> save(Mono<Route> route) {
return route.doOnNext(r -> routes.put(r.getId(), r)).then();
//route.subscribe(r -> );
//return Mono.empty();
return route.then( r -> {
routes.put(r.getId(), r);
return Mono.empty();
});
}
@Override
public Mono<Void> delete(Mono<String> routeId) {
return routeId.then(id -> {
routes.remove(id);
return Mono.empty();
if (routes.containsKey(id)) {
routes.remove(id);
return Mono.empty();
}
return Mono.error(new NotFoundException("Route not found: "+routeId));
});
}

View File

@@ -0,0 +1,14 @@
package org.springframework.cloud.gateway.support;
/**
* @author Spencer Gibb
*/
public class NotFoundException extends RuntimeException {
public NotFoundException(String message) {
super(message);
}
public NotFoundException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -54,6 +54,25 @@ public class GatewayTestApplication {
}
}
@Configuration
@Profile("!discovery")
protected static class GatewayInMemoryConfiguration {
@Bean
public PropertiesRouteLocator propertiesRouteLocator(GatewayProperties properties) {
return new PropertiesRouteLocator(properties);
}
@Bean
@Primary
public RouteLocator compositeRouteLocator(InMemoryRouteRepository inMemoryRouteRepository,
PropertiesRouteLocator propertiesRouteLocator) {
final Flux<RouteLocator> flux = Flux.just(inMemoryRouteRepository, propertiesRouteLocator);
final CompositeRouteLocator composite = new CompositeRouteLocator(flux);
return new CachingRouteLocator(composite);
}
}
public static void main(String[] args) {
System.setProperty("java.net.preferIPv4Stack", "true"); //Remove when configurable
SpringApplication.run(GatewayTestApplication.class, args);

View File

@@ -179,6 +179,7 @@ logging:
level:
org.springframework.cloud.gateway: TRACE
org.springframework.http.server.reactive: DEBUG
org.springframework.web.reactive: DEBUG
reactor.ipc.netty: DEBUG
management: