javascript
Spring Cloud(六) 服务网关GateWay 入门
前文回顧:
Spring Cloud(一)Eureka Server-單體及集群搭建
Spring Cloud(二) 配置Eureka Client
Spring Cloud(三) 熔斷器Hystrix
Spring Cloud(四) API網關Zuul
Spring Cloud(五) Zuul Filter
一.簡介
Spring Cloud Gateway 是 Spring Cloud 的一個全新項目,該項目是基于 Spring 5.0,Spring Boot 2.0 和 Project Reactor 等技術開發的網關,它旨在為微服務架構提供一種簡單有效的統一的 API 路由管理方式。
Spring Cloud Gateway 作為 Spring Cloud 生態系統中的網關,目標是替代 Netflix Zuul,其不僅提供統一的路由方式,并且基于 Filter 鏈的方式提供了網關基本的功能,例如:安全,監控/指標,和限流。
二.快速入門
Spring Cloud Gateway 網關路由有兩種配置方式:
-
在配置文件 yml 中配置
-
通過@Bean自定義 RouteLocator,在啟動主類 Application 中配置
(1)pom中添加依賴
<parent><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-parent</artifactId><version>2.0.6.RELEASE</version><relativePath/> </parent> ? <dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Finchley.SR2</version><type>pom</type><scope>import</scope></dependency></dependencies> </dependencyManagement> ?<dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies>(2)配置文件
server:port: 8080 spring:cloud:gateway:routes:- id: neo_routeuri: https://blog.csdn.netpredicates:- Path=/fy_java1995各字段含義如下:
-
id:我們自定義的路由 ID,保持唯一
-
uri:目標服務地址
-
predicates:路由條件,Predicate 接受一個輸入參數,返回一個布爾值結果。該接口包含多種默認方法來將 Predicate 組合成其他復雜的邏輯(比如:與,或,非)。
-
filters:過濾規則,本示例暫時沒用。
上面這段配置的意思是,配置了一個 id 為 neo_route 的路由規則,當訪問地址 http://localhost:8080/fy_java1995時會自動轉發到地址:https://blog.csdn.net/fy_java1995`。
(3)啟動類配置
我們可以在啟動類 GateWayApplication 中添加方法 customRouteLocator() 來定制轉發規則。
@SpringBootApplication public class GateWayApplication { ?public static void main(String[] args) {SpringApplication.run(GateWayApplication.class, args);} ?@Beanpublic RouteLocator customRouteLocator(RouteLocatorBuilder builder) {return builder.routes().route("path_route", r -> r.path("/fy_java1995").uri("https://blog.csdn.net")).build();} ? }三.路由規則
Spring Cloud Gateway 是通過 Spring WebFlux 的 HandlerMapping 做為底層支持來匹配到轉發路由,Spring Cloud Gateway 內置了很多 Predicates 工廠,這些 Predicates 工廠通過不同的 HTTP 請求參數來匹配,多個 Predicates 工廠可以組合使用。
Predicate 來源于 Java 8,是 Java 8 中引入的一個函數,Predicate 接受一個輸入參數,返回一個布爾值結果。該接口包含多種默認方法來將 Predicate 組合成其他復雜的邏輯(比如:與,或,非)。可以用于接口請求參數校驗、判斷新老數據是否有變化需要進行更新操作。
在 Spring Cloud Gateway 中 Spring 利用 Predicate 的特性實現了各種路由匹配規則,有通過 Header、請求參數等不同的條件來進行作為條件匹配到對應的路由。網上有一張圖總結了 Spring Cloud 內置的幾種 Predicate 的實現。
(1)通過時間匹配
spring:cloud:gateway:routes:- id: time_routeuri: https://blog.csdn.net/fy_java1995predicates:- After=2018-01-20T06:06:06+08:00[Asia/Shanghai]Spring 是通過 ZonedDateTime 來對時間進行的對比,ZonedDateTime 是 Java 8 中日期時間功能里,用于表示帶時區的日期與時間信息的類,ZonedDateTime 支持通過時區來設置時間,中國的時區是:Asia/Shanghai。
After Route Predicate 是指在這個時間之后的請求都轉發到目標地址。上面的示例是指,請求時間在 2018年1月20日6點6分6秒之后的所有請求都轉發到地址https://blog.csdn.net/fy_java1995。+08:00是指時間和UTC時間相差八個小時,時間地區為Asia/Shanghai。
添加完路由規則之后,訪問地址http://localhost:8080會自動轉發到https://blog.csdn.net/fy_java1995。
Before Route Predicate 剛好相反,在某個時間之前的請求的請求都進行轉發。我們把上面路由規則中的 After 改為 Before,如下:
spring:cloud:gateway:routes:- id: after_routeuri: https://blog.csdn.net/fy_java1995predicates:- Before=2018-01-20T06:06:06+08:00[Asia/Shanghai](2)通過Cookie匹配
spring:cloud:gateway:routes:- id: cookie_routeuri: https://blog.csdn.net/fy_java1995predicates:- Cookie=ityouknow, kee.e使用 curl 測試,命令行輸入:
curl http://localhost:8080 --cookie "ityouknow=kee.e"(3)通過Header匹配
spring:cloud:gateway:routes:- id: header_routeuri: https://blog.csdn.net/fy_java1995predicates:- Header=X-Request-Id, \d+使用 curl 測試,命令行輸入:
curl http://localhost:8080 ?-H "X-Request-Id:666666"(4)通過Method匹配
spring:cloud:gateway:routes:- id: method_routeuri: https://blog.csdn.net/fy_java1995predicates:- Method=GET 使用 curl 測試,命令行輸入: # curl 默認是以 GET 的方式去請求 curl http://localhost:8080(5)通過Path匹配
spring:cloud:gateway:routes:- id: host_routeuri: https://blog.csdn.net/fy_java1995predicates:- Path=/foo/{segment}如果請求路徑符合要求,則此路由將匹配,例如:/foo/1 或者 /foo/bar。
使用 curl 測試,命令行輸入:
curl http://localhost:8080/foo/1 curl http://localhost:8080/foo/xx curl http://localhost:8080/boo/xx經過測試第一和第二條命令可以正常獲取到頁面返回值,最后一個命令報404,證明路由是通過指定路由來匹配。
(6)通過查詢參數匹配
spring:cloud:gateway:routes:- id: query_routeuri: https://blog.csdn.net/fy_java1995predicates:- Query=smile這樣配置,只要請求中包含 smile 屬性的參數即可匹配路由。
使用 curl 測試,命令行輸入:
curl localhost:8080?smile=x總結
以上是生活随笔為你收集整理的Spring Cloud(六) 服务网关GateWay 入门的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Spring Cloud(五) Zuul
- 下一篇: Spring Cloud(七) Gate