Java Routes API
Spring Cloud Gateway Server MVC 使用 Spring WebMvc.fn RouterFunctions.Builder 作为创建路由(这是 WebMvc.fn RouterFunction 实例)的默认方式。
Spring Cloud Gateway Server MVC uses the Spring WebMvc.fn RouterFunctions.Builder as the default way to create Routes, which are WebMvc.fn RouterFunction instances.
可以通过调用 RouterFunctions.route() 来获取 RouterFunctions.Builder
实例。
A RouterFunctions.Builder
instance is obtained by calling RouterFunctions.route()
import static org.springframework.web.servlet.function.RouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
class SimpleGateway {
@Bean
public RouterFunction<ServerResponse> getRoute() {
return route().GET("/get", http("https://httpbin.org")).build();
}
}
对于每个 HTTP 方法(GET、POST 等)以及路径谓词,例如上面的 /get
,在 RouterFunctions.Builder
中都有方法。最终参数是 HandlerFilterFunction
,本例中它是 HandlerFunctions.http()
。为每个 HTTP 方法都有重载方法用于其他 RequestPredicate
参数,还提供了一个通用的 route(RequestPredicate, HandlerFunction
) 方法以供一般使用。
There are methods in RouterFunctions.Builder
for each HTTP methods (GET, POST, etc…) combined with a path predicate, such as /get
as above. The final parameter is the HandlerFilterFunction
, in this case HandlerFunctions.http()
. There are overloaded methods for each HTTP method for additional RequestPredicate
parameters as well as a generic route(RequestPredicate, HandlerFunction
) method for general use.
Gateway MVC implementation of RouterFunctions.Builder
一些高级过滤器要求向请求属性中添加一些元数据。为了适应这种情况,有一个 org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions
类。GatewayRouterFunctions.route(String routeId)
创建一个 RouterFunctions.Builder
实例,然后添加一个 "before" 过滤器,以将 routeId
添加为请求元数据。
Some advanced filters require some metadata to be added to request attributes. To accommodate this, there is a org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions
class. GatewayRouterFunctions.route(String routeId) creates a `RouterFunctions.Builder
instance then adds a 'before' filter to add the routeId
as request metadata.
import static org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;
class SimpleGateway {
@Bean
public RouterFunction<ServerResponse> getRoute() {
return route("simple_route").GET("/get", http("https://httpbin.org")).build();
}
}
Gateway MVC Handler Functions
各种 RouterFunctions.Builder
方法需要一个 HandlerFunction<ServerResponse>
。要创建由 MVC Gateway 代理的路由,HandlerFunction
实现会在 org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions
中提供。最基本的是 http()
HandlerFunction
。如果将 URI
作为一个参数提供,那就是用于将 HTTP 请求发送给下游目标的 URI
(如下面的示例中所见)。如果未传递任何参数,该函数将在 org.springframework.cloud.gateway.server.mvc.common.MvcUtils.GATEWAY_REQUEST_URL_ATTR
请求属性中查找 URI
。这允许动态目标(例如负载平衡)设置 URI
。
Various RouterFunctions.Builder
methods require a HandlerFunction<ServerResponse>
. To create a route that is proxied by the MVC Gateway, HandlerFunction
implementations are supplied in org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions
. The most basic is the http()
HandlerFunction
. If a URI
is supplied as a parameter, that is the URI
used as the downstream target for sending the HTTP requests (as seen in the example above). If no parameter is passed, the function looks for a URI
in the org.springframework.cloud.gateway.server.mvc.common.MvcUtils.GATEWAY_REQUEST_URL_ATTR
request attribute. This allows for dynamic targets such as load balancing to set the URI
.