Spring Cloud 框架 -- Spring Cloud Gateway
Spring Cloud Gateway 介紹
特點:
-
限流
-
路徑重寫
-
動態路由
-
整合 Spring Cloud DiscoveryClient
-
整合 Hystrix 斷路器
和 zuul 對比:
-
zuul 是 Netflix 公司的開源產品,Spring Cloud Gateway 是 Spring 家族中的產品,可以和 Spring 家族中其他元件更好的融合。
-
zuul1 不支援長連線,例如 websocket
-
Spring Cloud Gateway 支援限流
-
Spring Cloud Gateway 基於 Netty 來開發,實現了非同步和非阻塞,佔用資源更小,效能強於 zuul。
Spring Cloud Gateway 的基本用法
Spring Cloud Gateway 支援兩種不同的用法:
-
編碼式
-
properties / yml 配置
編碼式
首先建立一個 Spring Boot 專案,僅新增 Spring Cloud Gateway 模組:
專案建立成功後,在專案啟動類上配置一個 RouteLocator 這樣一個 Bean ,就可以實現請求轉發:
@SpringBootApplication public class GatewayApplication { public static void main(String[] args) { SpringApplication.run(GatewayApplication.class, args); } @Bean RouteLocator routeLocator(RouteLocatorBuilder builder){ return builder.routes() .route("you", r -> r.path("/get").uri("http://httpbin.org")) .build(); } }
這裡利用了一個現成的介面 http://httpbin.org
, 直接將 get 請求轉發到這上面去。
執行 gateway 專案。
訪問 http://localhost:8080/get
, 如下圖:
如上,實現了請求轉發。
properties 配置
註釋掉啟動類中的 Bean ,在 properties 配置如下:
spring.cloud.gateway.routes[0].id=you
spring.cloud.gateway.routes[0].uri=http://httpbin.org
spring.cloud.gateway.routes[0].predicates[0]=Path=/get
啟動 gateway,訪問http://localhost:8080/get
yml 配置
在 resources 目錄下, 新建 application.yml 檔案,寫入下面配置:
spring:
cloud:
gateway:
routes:
- id: you
uri: http://httpbin.org
predicates:
- Path=/get
刪除原有配置檔案 application.properties ,重啟專案,訪問 http://localhost:8080/get
, 效果和上面一樣。
Spring Cloud Gateway 結合微服務
首先給 gateway 新增依賴, 將之註冊到 eureka 上。
加依賴:
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
加配置
spring:
cloud:
gateway:
discovery:
locator:
enabled: true # 開啟自動代理
application:
name: gateway
eureka:
client:
service-url:
defaultZone: http://localhost:1111/eureka
logging:
level:
org.springframework.cloud.gateway: debug
接下來,就可以通過 gateway 訪問到其他註冊到 eureka 上的服務了,訪問方式和 zuul 一樣。
先開啟 eureka 後臺 http://localhost:1111/
,檢視服務註冊情況:
再訪問http://localhost:8080/PROVIDER/hello
, 效果如下:
注意:eureka 服務是區分大小寫的,記得使用 PROVIDER 而不是 provider。
Predicate
用來宣告匹配規則。
通過時間匹配:
spring:
cloud:
gateway:
routes:
- id: you
uri: http://httpbin.org
predicates:
- After=2021-01-01T01:01:01+08:00[Asia/Shanghai]
這個配置,表示,請求時間在 2021-01-01T01:01:01+08:00[Asia/Shanghai] 時間之後,才會被路由。
除了 After 之外,還有兩個關鍵字:
-
Before: 表示在某個時間點之前進行請求轉發
-
Between:表示在兩個時間點之間進行請求轉發,兩個時間點用 , 隔開
也可以通過請求方式匹配,就是請求方法:
spring:
cloud:
gateway:
routes:
- id: you
uri: http://httpbin.org
predicates:
- Method=GET
這個配置,表示只給 GET 請求進行路由。訪問http://localhost:8080/get
,如下圖:
通過請求路徑匹配:
spring:
cloud:
gateway:
routes:
- id: you
uri: http://httpbin.org
predicates:
- Path=/get
這個配置表示,路徑滿足 /get 這個規則,就會進行轉發,如http://localhost:8080/get
通過引數進行匹配:
spring:
cloud:
gateway:
routes:
- id: you
uri: http://httpbin.org
predicates:
- Query=name
表示,請求中一定要有 name 引數才會進行轉發,否則不會進行轉發。
也可以指定引數和引數的值。
例如,引數的 key 為 name ,value 必須要以 java 開始:
spring:
cloud:
gateway:
routes:
- id: you
uri: http://httpbin.org
predicates:
- Query=name,java.*
多種匹配方式可以組合使用:
spring:
cloud:
gateway:
routes:
- id: you
uri: http://httpbin.org
predicates:
- Query=name,java.*
- Method=GET
- After=2021-01-01T01:01:01+08:00[Asia/Shanghai]
Filter
Spring Cloud Gateway 中的過濾器分為兩大類:
-
GatewayFilter: 區域性路由
-
GlobalFilter: 全域性路由
如,AddRequestParameter 過濾器的使用:
spring:
cloud:
gateway:
routes:
- id: you
uri: lb://provider
filters:
- AddRequestParameter=name,you
predicates:
- Method=GET
這個過濾器就是在請求轉發路由的時候,自動額外新增引數。
訪問http://localhost:8080/hello2
,效果如下:
每天學習一點點,每天進步一點點。