clarify and simplify
This commit is contained in:
6
pom.xml
6
pom.xml
@@ -32,6 +32,12 @@
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot.experimental</groupId>
|
||||
<artifactId>spring-boot-starter-web-reactive</artifactId>
|
||||
<exclusions>
|
||||
<exclusion>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-tomcat</artifactId>
|
||||
</exclusion>
|
||||
</exclusions>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.projectreactor.ipc</groupId>
|
||||
|
||||
@@ -16,9 +16,9 @@ import reactor.core.publisher.Mono;
|
||||
@Import(GatewayConfiguration.class)
|
||||
@SpringBootConfiguration
|
||||
@EnableAutoConfiguration
|
||||
public class SpringCloudGatewayApplication {
|
||||
public class GatewayApplication {
|
||||
|
||||
private static final Log log = LogFactory.getLog(SpringCloudGatewayApplication.class);
|
||||
private static final Log log = LogFactory.getLog(GatewayApplication.class);
|
||||
|
||||
// TODO: only apply filters to zuul?
|
||||
@Bean
|
||||
@@ -48,7 +48,7 @@ public class SpringCloudGatewayApplication {
|
||||
|
||||
public static void main(String[] args) {
|
||||
new SpringApplicationBuilder()
|
||||
.sources(SpringCloudGatewayApplication.class)
|
||||
.sources(GatewayApplication.class)
|
||||
//TODO: howto do programatically
|
||||
.run(args);
|
||||
}
|
||||
@@ -28,7 +28,7 @@ public class GatewayHandlerMapping extends AbstractUrlHandlerMapping {
|
||||
|
||||
protected void registerHandlers(Map<String, Route> routes) {
|
||||
for (Route route : routes.values()) {
|
||||
registerHandler(route.getPath(), this.gatewayWebHandler);
|
||||
registerHandler(route.getRequestPath(), this.gatewayWebHandler);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,6 +2,7 @@ package org.springframework.cloud.gateway;
|
||||
|
||||
import org.springframework.boot.context.properties.ConfigurationProperties;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
|
||||
@@ -27,41 +28,31 @@ public class GatewayProperties {
|
||||
|
||||
public static class Route {
|
||||
private String id;
|
||||
private String path;
|
||||
private String host;
|
||||
private String port;
|
||||
private String requestPath;
|
||||
private URI upstreamUrl;
|
||||
|
||||
public String getPath() {
|
||||
return this.path;
|
||||
public String getRequestPath() {
|
||||
return this.requestPath;
|
||||
}
|
||||
|
||||
public void setPath(String path) {
|
||||
this.path = path;
|
||||
public void setRequestPath(String requestPath) {
|
||||
this.requestPath = requestPath;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return this.host;
|
||||
public URI getUpstreamUrl() {
|
||||
return upstreamUrl;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public String getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(String port) {
|
||||
this.port = port;
|
||||
public void setUpstreamUrl(URI upstreamUrl) {
|
||||
this.upstreamUrl = upstreamUrl;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Route{" +
|
||||
"id='" + id + '\'' +
|
||||
", path='" + path + '\'' +
|
||||
", host='" + host + '\'' +
|
||||
", port='" + port + '\'' +
|
||||
", requestPath='" + requestPath + '\'' +
|
||||
", upstreamUrl='" + upstreamUrl + '\'' +
|
||||
'}';
|
||||
}
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ public class GatewayWebHandler implements WebHandler {
|
||||
|
||||
@Override
|
||||
public Mono<Void> handle(ServerWebExchange exchange) {
|
||||
Optional<URI> requestUrl = exchange.getAttribute("requestUri");
|
||||
Optional<URI> requestUrl = exchange.getAttribute("requestUrl");
|
||||
ServerHttpRequest request = exchange.getRequest();
|
||||
ClientRequest<Void> clientRequest = ClientRequest
|
||||
.method(request.getMethod(), requestUrl.get())
|
||||
|
||||
@@ -4,7 +4,8 @@ import org.apache.commons.logging.Log;
|
||||
import org.apache.commons.logging.LogFactory;
|
||||
import org.springframework.cloud.gateway.GatewayProperties;
|
||||
import org.springframework.cloud.gateway.GatewayProperties.Route;
|
||||
import org.springframework.cloud.gateway.SpringCloudGatewayApplication;
|
||||
import org.springframework.cloud.gateway.GatewayApplication;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.http.server.reactive.ServerHttpRequest;
|
||||
import org.springframework.util.AntPathMatcher;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
@@ -18,9 +19,9 @@ import java.net.URI;
|
||||
/**
|
||||
* @author Spencer Gibb
|
||||
*/
|
||||
public class FindRouteFilter implements WebFilter {
|
||||
public class FindRouteFilter implements WebFilter, Ordered {
|
||||
|
||||
private static final Log log = LogFactory.getLog(SpringCloudGatewayApplication.class);
|
||||
private static final Log log = LogFactory.getLog(GatewayApplication.class);
|
||||
|
||||
private final GatewayProperties properties;
|
||||
private final AntPathMatcher matcher;
|
||||
@@ -30,6 +31,11 @@ public class FindRouteFilter implements WebFilter {
|
||||
this.matcher = new AntPathMatcher();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getOrder() {
|
||||
return 500;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Mono<Void> filter(ServerWebExchange exchange, WebFilterChain chain) {
|
||||
log.info("FindRouteFilter start");
|
||||
@@ -38,13 +44,12 @@ public class FindRouteFilter implements WebFilter {
|
||||
URI uri = request.getURI();
|
||||
String path = uri.getPath();
|
||||
for (Route route : this.properties.getRoutes().values()) {
|
||||
if (this.matcher.match(route.getPath(), path)) {
|
||||
URI requestUri = UriComponentsBuilder.fromHttpRequest(request)
|
||||
.host(route.getHost())
|
||||
.port(route.getPort())
|
||||
if (this.matcher.match(route.getRequestPath(), path)) {
|
||||
URI requestUrl = UriComponentsBuilder.fromHttpRequest(request)
|
||||
.uri(route.getUpstreamUrl())
|
||||
.build(true)
|
||||
.toUri();
|
||||
exchange.getAttributes().put("requestUri", requestUri);
|
||||
exchange.getAttributes().put("requestUrl", requestUrl);
|
||||
return chain.filter(exchange);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,5 @@ spring:
|
||||
gateway:
|
||||
routes:
|
||||
test1:
|
||||
path: /**
|
||||
host: httpbin.org
|
||||
port: 80
|
||||
requestPath: /**
|
||||
upstreamUrl: http://httpbin.org:80
|
||||
|
||||
@@ -7,7 +7,7 @@ import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringCloudGatewayApplicationTests {
|
||||
public class GatewayApplicationTests {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
Reference in New Issue
Block a user