Spring Cloud 系列之 Gateway 服務網關(二)
- 2020 年 4 月 21 日
- 筆記
- Gateway, Spring Cloud, 微服務, 網關
本篇文章為系列文章,未讀第一集的同學請猛戳這裡:Spring Cloud 系列之 Gateway 服務網關(一)
本篇文章講解 Gateway 網關的多種路由規則、動態路由規則(配合服務發現的路由規則)。
路由規則
點擊鏈接觀看:路由規則(獲取更多請關注公眾號「哈嘍沃德先生」)
Spring Cloud Gateway 創建 Route 對象時, 使用 RoutePredicateFactory 創建 Predicate 對象,Predicate 對象可以賦值給 Route。
- Spring Cloud Gateway 包含許多內置的 Route Predicate Factories。
- 所有這些斷言都匹配 HTTP 請求的不同屬性。
- 多個 Route Predicate Factories 可以通過邏輯與(and)結合起來一起使用。
路由斷言工廠 RoutePredicateFactory 包含的主要實現類如圖所示,包括 Datetime、 請求的遠端地址、 路由權重、 請求頭、 Host 地址、 請求方法、 請求路徑和請求參數等類型的路由斷言。
接下來我們舉例說明其中一部分如何使用,其餘等大家工作中需要應用時再查詢資料學習或者諮詢我也可以。
Path
spring:
application:
name: gateway-server # 應用名稱
cloud:
gateway:
# 路由規則
routes:
- id: product-service # 路由 ID,唯一
uri: //localhost:7070/ # 目標 URI,路由到微服務的地址
predicates: # 斷言(判斷條件)
- Path=/product/** # 匹配對應 URL 的請求,將匹配到的請求追加在目標 URI 之後
- 請求
//localhost:9000/product/1
將會路由至//localhost:7070/product/1
Query
spring:
application:
name: gateway-server # 應用名稱
cloud:
gateway:
# 路由規則
routes:
- id: product-service # 路由 ID,唯一
uri: //localhost:7070/ # 目標 URI,路由到微服務的地址
predicates: # 斷言(判斷條件)
#- Query=token # 匹配請求參數中包含 token 的請求
- Query=token, abc. # 匹配請求參數中包含 token 並且其參數值滿足正則表達式 abc. 的請求
Query=token
:比如,//localhost:9000/product/1?token=123Query=token, abc.
:比如,//localhost:9000/product/1?token=abc1
Method
spring:
application:
name: gateway-server # 應用名稱
cloud:
gateway:
# 路由規則
routes:
- id: product-service # 路由 ID,唯一
uri: //localhost:7070/ # 目標 URI,路由到微服務的地址
predicates: # 斷言(判斷條件)
- Method=GET # 匹配任意 GET 請求
Datetime
spring:
application:
name: gateway-server # 應用名稱
cloud:
gateway:
# 路由規則
routes:
- id: product-service # 路由 ID,唯一
uri: //localhost:7070/ # 目標 URI,路由到微服務的地址
predicates: # 斷言(判斷條件)
# 匹配中國上海時間 2020-02-02 20:20:20 之後的請求
- After=2020-02-02T20:20:20.000+08:00[Asia/Shanghai]
RemoteAddr
spring:
application:
name: gateway-server # 應用名稱
cloud:
gateway:
# 路由規則
routes:
- id: product-service # 路由 ID,唯一
uri: //localhost:7070/ # 目標 URI,路由到微服務的地址
predicates: # 斷言(判斷條件)
- RemoteAddr=192.168.10.1/0 # 匹配遠程地址請求是 RemoteAddr 的請求,0表示子網掩碼
RemoteAddr=192.168.10.1/0
:比如,//192.168.10.1:9000/product/1
Header
spring:
application:
name: gateway-server # 應用名稱
cloud:
gateway:
# 路由規則
routes:
- id: product-service # 路由 ID,唯一
uri: //localhost:7070/ # 目標 URI,路由到微服務的地址
predicates: # 斷言(判斷條件)
# 匹配請求頭包含 X-Request-Id 並且其值匹配正則表達式 \d+ 的請求
- Header=X-Request-Id, \d+
動態路由(服務發現的路由規則)
動態路由其實就是面向服務的路由,Spring Cloud Gateway 支援與 Eureka 整合開發,根據 serviceId 自動從註冊中心獲取服務地址並轉發請求,這樣做的好處不僅可以通過單個端點來訪問應用的所有服務,而且在添加或移除服務實例時不用修改 Gateway 的路由配置。
添加依賴
<!-- netflix eureka client 依賴 -->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
動態獲取 URI
點擊鏈接觀看:動態獲取 URI(獲取更多請關注公眾號「哈嘍沃德先生」)
配置文件
配置註冊中心和動態路由規則。
server:
port: 9000 # 埠
spring:
application:
name: gateway-server # 應用名稱
cloud:
gateway:
# 路由規則
routes:
- id: product-service # 路由 ID,唯一
uri: lb://product-service # lb:// 根據服務名稱從註冊中心獲取服務請求地址
predicates: # 斷言(判斷條件)
- Path=/product/** # 匹配對應 URL 的請求,將匹配到的請求追加在目標 URI 之後
# 配置 Eureka Server 註冊中心
eureka:
instance:
prefer-ip-address: true # 是否使用 ip 地址註冊
instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
client:
service-url: # 設置服務註冊中心地址
defaultZone: //localhost:8761/eureka/,//localhost:8762/eureka/
啟動類
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// 開啟 EurekaClient 註解,目前版本如果配置了 Eureka 註冊中心,默認會開啟該註解
//@EnableEurekaClient
@SpringBootApplication
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
訪問
訪問://localhost:9000/product/1 結果如下:
服務名稱轉發
點擊鏈接觀看:服務名稱轉發(獲取更多請關注公眾號「哈嘍沃德先生」)
即使配置了動態獲取 URI 的方式,項目中微服務一旦過多幾十上百個時,配置中仍然要寫很多配置,這時候就可以使用服務名稱轉發,與服務發現組件進行結合,通過 serviceId
轉發到具體服務實例。默認匹配URL /微服務名稱/**
路由到具體微服務。
配置文件
配置註冊中心和動態路由規則。
server:
port: 9000 # 埠
spring:
application:
name: gateway-server # 應用名稱
cloud:
gateway:
discovery:
locator:
# 是否與服務發現組件進行結合,通過 serviceId 轉發到具體服務實例。
enabled: true # 是否開啟基於服務發現的路由規則
lower-case-service-id: true # 是否將服務名稱轉小寫
# 配置 Eureka Server 註冊中心
eureka:
instance:
prefer-ip-address: true # 是否使用 ip 地址註冊
instance-id: ${spring.cloud.client.ip-address}:${server.port} # ip:port
client:
service-url: # 設置服務註冊中心地址
defaultZone: //localhost:8761/eureka/,//localhost:8762/eureka/
啟動類
package com.example;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
// 開啟 EurekaClient 註解,目前版本如果配置了 Eureka 註冊中心,默認會開啟該註解
//@EnableEurekaClient
@SpringBootApplication
public class GatewayServerApplication {
public static void main(String[] args) {
SpringApplication.run(GatewayServerApplication.class, args);
}
}
訪問
配置文件中沒有配置任何訂單服務的資訊,訪問://localhost:9000/order-service/order/1 結果如下:
下一篇我們講解 Gateway 網關過濾器和全局過濾器以及自定義過濾器的使用,記得關注噢~
本文採用 知識共享「署名-非商業性使用-禁止演繹 4.0 國際」許可協議
。
大家可以通過 分類
查看更多關於 Spring Cloud
的文章。
🤗 您的點贊
和轉發
是對我最大的支援。
📢 掃碼關注 哈嘍沃德先生
「文檔 + 影片」每篇文章都配有專門影片講解,學習更輕鬆噢 ~