Merge branch 'master' into 2.0.x

This commit is contained in:
Dave Syer
2017-06-30 15:52:32 +01:00
2 changed files with 65 additions and 0 deletions

View File

@@ -21,6 +21,31 @@ This project provides an API Gateway built on top of the Spring Ecosystem, inclu
* API or configuration driven
* Supports Spring Cloud `DiscoveryClient` for configuring Routes
== Spring Cloud Gateway MVC
Features:
* Spring MVC `@RestController` method injection of a `ProxyExchange`
* Build up complex routing and request enhancement using the standard Spring Web toolkit
Example (proxying a request to "/test" downstream to a remote server):
```java
@RestController
@SpringBootApplication
public class GatewaySampleApplication {
@Value("${remote.home}")
private URI home;
@GetMapping("/test")
public ResponseEntity<?> proxy(ProxyExchange<Object> proxy) throws Exception {
return proxy.uri(home.toString() + "/image/png").get();
}
}
```
== Building
:jdkversion: 1.8

View File

@@ -225,3 +225,43 @@ TODO: document writing Custom Global Filters
=== Writing Custom Route Locators and Writers
TODO: document writing Custom Route Locators and Writers
== Building a Simple Gateway Using Spring MVC
Spring Cloud Gateway provides a utility object called `ProxyExchange` which you can use inside a regular Spring MVC handler as a method parameter. It supports basic downstream HTTP exchanges via methods that mirror the HTTP verbs, or forwarding to a local handler via the `forward()` method.
Example (proxying a request to "/test" downstream to a remote server):
```java
@RestController
@SpringBootApplication
public class GatewaySampleApplication {
@Value("${remote.home}")
private URI home;
@GetMapping("/test")
public ResponseEntity<?> proxy(ProxyExchange<Object> proxy) throws Exception {
return proxy.uri(home.toString() + "/image/png").get();
}
}
```
There are convenience methods on the `ProxyExchange` to enable the handler method to discover and enhance the URI path of the incoming request. For example you might want to extract the trailing elements of a path to pass them downstream:
```java
@GetMapping("/proxy/path/**")
public ResponseEntity<?> proxyPath(ProxyExchange<?> proxy) throws Exception {
String path = proxy.path("/proxy/path/");
return proxy.uri(home.toString() + "/foos/" + path).get();
}
```
All the features of Spring MVC are available to Gateway handler methods. So you can inject request headers and query parameters, for instance, and you can constrain the incoming requests with declarations in the mapping annotation. See the documentation for `@RequestMapping` in Spring MVC for more details of those features.
Headers can be added to the downstream response using the `header()` methods on `ProxyExchange`.
You can also manipulate response headers (and anything else you like in the response) by adding a mapper to the `get()` etc. method. The mapper is a `Function` that takes the incoming `ResponseEntity` and converts it to an outgoing one.
First class support is provided for "sensitive" headers ("cookie" and "authorization" by default) which are not passed downstream, and for "proxy" headers (`x-forwarded-*`).