Java 路由 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 例項,然後新增一個“前置”過濾器,將 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 函式。
向 `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。