跳到主要内容
版本:5.0.1

Java Routes API

DeepSeek V3 中英对照 Java Routes API

Spring Cloud Gateway Server MVC 默认使用 Spring WebMvc.fn 的 RouterFunctions.Builder 来创建路由,这些路由是 WebMvc.fn 的 RouterFunction 实例。

通过调用 RouterFunctions.route() 可以获得一个 RouterFunctions.Builder 实例。

import static org.springframework.web.servlet.function.RouterFunctions.route;
import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
import static org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions.http;

class SimpleGateway {
@Bean
public RouterFunction<ServerResponse> getRoute() {
return route().GET("/get", http())
.before(uri("https://example.org"))
.build();
}
}

RouterFunctions.Builder 中为每个 HTTP 方法(GET、POST 等)提供了与路径谓词结合的方法,例如上述的 /get。最后一个参数是 HandlerFilterFunction,在本例中为 HandlerFunctions.http()。每个 HTTP 方法还有重载方法,用于接受额外的 RequestPredicate 参数,以及一个通用的 route(RequestPredicate, HandlerFunction) 方法供一般使用。

Gateway MVC 实现的 RouterFunctions.Builder​

一些高级过滤器需要在请求属性中添加一些元数据。为了满足这一需求,有一个 org.springframework.cloud.gateway.server.mvc.handler.GatewayRouterFunctions 类。GatewayRouterFunctions.route(String routeId) 创建一个 RouterFunctions.Builder 实例,然后添加一个 'before' 过滤器,将 routeId 作为请求元数据添加进去。

import static org.springframework.cloud.gateway.server.mvc.filter.BeforeFilterFunctions.uri;
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())
.before(uri("https://example.org"))
.build();
}
}

Gateway MVC 处理器函数​

各种 RouterFunctions.Builder 方法都需要一个 HandlerFunction<ServerResponse>。为了创建由 MVC Gateway 代理的路由,HandlerFunction 的实现由 org.springframework.cloud.gateway.server.mvc.handler.HandlerFunctions 提供。

HTTP 处理函数​

最基本的处理函数是 http() HandlerFunction。如果提供了 URI 作为参数,该 URI 将用作发送 HTTP 请求的下游目标(如上例所示)。如果未传递参数,该函数会在 org.springframework.cloud.gateway.server.mvc.common.MvcUtils.GATEWAY_REQUEST_URL_ATTR 请求属性中查找 URI。这允许动态目标(如负载均衡)设置 URI。

注意

自版本 4.1.7 起,HandlerFunctions.http(String) 和 HandlerFunctions.http(URI) 现已弃用。请改用 HandlerFunctions.http() 并结合 BeforeFilterFunctions.uri() 过滤器。这修复了在处理路由 URL 请求属性时的不一致性问题。

Spring Cloud Function 处理函数​

通过在类路径中添加 Spring Cloud Function,Spring Cloud Gateway 将自动配置路由来调用您定义为 Bean 的函数。函数的 Bean 名称将用作路由的路径。

例如,给定以下配置:

<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-function-context</artifactId>
</dependency>

一旦提供了 Spring Cloud Function 依赖,Java 函数 Bean 的名称就会成为可用于路由到函数的路径。

例如,假设有以下应用程序:

@SpringBootApplication
public class DemoFunctionGatewayApplication {

public static void main(String[] args) {
SpringApplication.run(DemoFunctionGatewayApplication.class, args);
}

@Bean
public Function<String, String> uppercase() {
return v -> v.toUpperCase();
}

@Bean
public Function<String, String> concat() {
return v -> v + v;
}
}

你可以通过向 /concat 或 /uppercase 发送 GET 或 POST 请求来调用 concat 或 uppercase 函数。

向 `http://localhost:8080/uppercase/hello 发送 GET 请求将调用 uppercase 函数,传入字符串 hello,并在 GET 响应体中返回 HELLO。

除了将函数参数作为路径参数传递,您也可以使用 POST 请求。例如,以下 cURL 命令可用于调用 concat 函数:

$ curl -d ‘"hello"' -H "Content-Type: application/json" -X POST http://localhost:8080/concat

响应体将包含 hellohello。

Spring Cloud Gateway 还支持通过向由逗号分隔的函数名组成的路径发出请求来进行函数组合。例如:

$ curl -d ‘"hello"' -H "Content-Type: application/json" -X POST http://localhost:8080/concat,uppercase

响应体将包含 HELLOHELLO。