Java 路由 API

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

通过调用 RouterFunctions.route() 获取 RouterFunctions.Builder 实例。

GatewaySampleApplication.java
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 实例,然后添加一个“前置”过滤器,将 routeId 添加为请求元数据。

GatewaySampleApplication.java
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 发出 GETPOST 请求来调用 concatuppercase 函数。

`https://:8080/uppercase/hello 发出 GET 请求将使用字符串 hello 调用 uppercase 函数,并在 GET 响应体中返回 HELLO

除了将函数参数作为路径参数传递之外,您还可以使用 POST 请求。例如,可以发出以下 cURL 命令来调用 concat 函数

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

响应体将包含 hellohello

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

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

响应体将包含 HELLOHELLO

© . This site is unofficial and not affiliated with VMware.