Proxy Exchange Gateway with Spring MVC or Webflux

以下是备用样式网关的说明。后面的内容均不适用于 Spring Cloud Gateway Server 文档。

The following describes an alternative style gateway. None of the Spring Cloud Gateway Server documentation applies to what follows.

How to Include Spring Cloud Gateway Proxy Exchange

要将 Spring Cloud Gateway Proxy Exchange 包含到您的项目中,对于 MVC Proxy Exchange,请使用组 ID 为 org.springframework.cloud 和工件 ID 为 spring-cloud-gateway-mvc 的工件。对于 WebFlux Proxy Exchange,请使用组 ID 为 org.springframework.cloud 和工件 ID 为 spring-cloud-gateway-webflux 的工件。

To include Spring Cloud Gateway Proxy Exchange in your project, use the artifact with a group ID of org.springframework.cloud and an artifact ID of spring-cloud-gateway-mvc for the MVC Proxy Exchange. For the WebFlux Proxy Exchange use artifact with a group ID of org.springframework.cloud and an artifact ID of spring-cloud-gateway-webflux.

有关使用当前 Spring Cloud 发布版设置构建系统,请参阅 Spring Cloud Project page

See the Spring Cloud Project page for details on setting up your build system with the current Spring Cloud Release Train.

Using Proxy Exchange

Spring Cloud Gateway 提供了一个名为 ProxyExchange 的实用对象。您可以在常规 Spring Web 处理程序中将其用作方法参数。它通过反映 HTTP 动词的方法支持基本的下游 HTTP 交换。使用 MVC 时,它还支持通过 forward() 方法转发到本地处理程序。要使用 ProxyExchange,请在您的类路径中包含正确的模块(spring-cloud-gateway-mvcspring-cloud-gateway-webflux)。

Spring Cloud Gateway provides a utility object called ProxyExchange. You can use it inside a regular Spring web handler as a method parameter. It supports basic downstream HTTP exchanges through methods that mirror the HTTP verbs. With MVC, it also supports forwarding to a local handler through the forward() method. To use the ProxyExchange, include the right module in your classpath (either spring-cloud-gateway-mvc or spring-cloud-gateway-webflux).

以下 MVC 示例通过下游 ProxyExchange/test 请求代理到远程服务器:

The following MVC example proxies a request to /test downstream to a remote server:

@RestController
@SpringBootApplication
public class GatewaySampleApplication {

	@Value("${remote.home}")
	private URI home;

	@GetMapping("/test")
	public ResponseEntity<?> proxy(ProxyExchange<byte[]> proxy) throws Exception {
		return proxy.uri(home.toString() + "/image/png").get();
	}

}

以下示例通过 Webflux 执行相同的操作:

The following example does the same thing with Webflux:

@RestController
@SpringBootApplication
public class GatewaySampleApplication {

	@Value("${remote.home}")
	private URI home;

	@GetMapping("/test")
	public Mono<ResponseEntity<?>> proxy(ProxyExchange<byte[]> proxy) throws Exception {
		return proxy.uri(home.toString() + "/image/png").get();
	}

}

ProxyExchange 上的便利方法支持处理程序方法发现和增强传入请求的 URI 路径。例如,您可能希望提取路径的尾部元素以在 downstream 传递它们:

Convenience methods on the ProxyExchange 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:

@GetMapping("/proxy/path/**")
public ResponseEntity<?> proxyPath(ProxyExchange<byte[]> proxy) throws Exception {
  String path = proxy.path("/proxy/path/");
  return proxy.uri(home.toString() + "/foos/" + path).get();
}

Spring MVC 和 Webflux 的所有功能都适用于 gateway 处理程序方法。因此,您可以注入请求标头和查询参数,还可以通过映射注释中的声明来限制传入请求。有关这些功能的更多详细信息,请参阅 Spring MVC 中的 @RequestMapping 文档。

All the features of Spring MVC and Webflux are available to gateway handler methods. As a result, 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.

您可以使用 ProxyExchange 上的 header() 方法将标头添加到 downstream 响应中。

You can add headers to the downstream response by using the header() methods on ProxyExchange.

您还可以通过将映射器添加到 get() 方法(以及其他方法)来处理响应标头(以及响应中的任何其他内容)。映射器是一个 Function,它接受传入的 ResponseEntity 并将其转换为传出的 ResponseEntity

You can also manipulate response headers (and anything else you like in the response) by adding a mapper to the get() method (and other methods). The mapper is a Function that takes the incoming ResponseEntity and converts it to an outgoing one.

为默认情况下不会向下游传递的“敏感”标头(例如 cookieauthorization)以及“代理”(x-forwarded-*)标头提供一流支持。

First-class support is provided for “sensitive” headers (by default, cookie and authorization), which are not passed downstream, and for “proxy” (x-forwarded-*) headers.