GateWay路由規(guī)則與動態(tài)路由詳細介紹
1、路由規(guī)則
Spring Cloud GateWay 幫我們內(nèi)置了很多 Predicates功能,實現(xiàn)了各種路由匹配規(guī)則(通過 Header、請求參數(shù)等作為條件)匹配到對應(yīng)的路由。

時間點后匹配
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
時間點前匹配
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
時間區(qū)間匹配
spring:
cloud:
gateway:
routes:
- id: between_route
uri: https://example.org
predicates:
- Between=2017-01-20T17:42:47.789-07:00[America/Denver],2017-01-21T17:42:47.789-07:00[America/Denver]
指定Cookie正則匹配指定值
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
指定Header正則匹配指定值
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
請求Host匹配指定值
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
請求Method匹配指定請求方式
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
請求路徑正則匹配
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
請求包含某參數(shù)
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
請求包含某參數(shù)并且參數(shù)值匹配正則表達式
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=red, gree.
遠程地址匹配
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
2、動態(tài)路由
GateWay支持自動從注冊中心中獲取服務(wù)列表并訪問,即所謂的動態(tài)路由
實現(xiàn)步驟如下
- pom.xml中添加注冊中心客戶端依賴(因為要獲取注冊中心服務(wù)列表,eureka客戶端已經(jīng)引入)
- 動態(tài)路由配置

注意:動態(tài)路由設(shè)置時,uri以 lb: //開頭(lb代表從注冊中心獲取服務(wù)),后面是需要轉(zhuǎn)發(fā)到的服務(wù)名稱
到此這篇關(guān)于GateWay路由規(guī)則與動態(tài)路由詳細介紹的文章就介紹到這了,更多相關(guān)GateWay路由規(guī)則內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Spring整合SpringMVC與Mybatis(SSM)實現(xiàn)完整登錄功能流程詳解
開學(xué)學(xué)校開始講servlet 后期要求做一個登錄功能,這個使用SSM先只做個簡單的只帶登錄功能的,且項目使用了MyBatis-Plus來簡化開發(fā)流程??辞闆r決定要不要升級功能或者換個寫法2022-09-09
Java JFinal框架中實現(xiàn)添加定時任務(wù)功能詳解
這篇文章主要為大家詳細介紹了如何在JFinal框架項目中添加定時任務(wù)的功能,文中的示例代碼講解詳細,感興趣的小伙伴可以跟隨小編一起學(xué)習一下2025-02-02
Java數(shù)據(jù)類型(八種基本數(shù)據(jù)類型+四種引用類型)以及數(shù)據(jù)類型轉(zhuǎn)換
java中除了基本數(shù)據(jù)類型之外,剩下的都是引用數(shù)據(jù)類型,下面這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)類型(八種基本數(shù)據(jù)類型?+?四種引用類型)以及數(shù)據(jù)類型轉(zhuǎn)換的相關(guān)資料,需要的朋友可以參考下2024-04-04
淺談Java中Collection和Collections的區(qū)別
下面小編就為大家?guī)硪黄獪\談Java中Collection和Collections的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2016-08-08
Maven項目在new module后,pom文件顯示為Ignored pom.xml問題
在Maven項目中,若創(chuàng)建過同名module后刪除,再次創(chuàng)建時可能導(dǎo)致pom.xml文件被IDEA忽略,原因是IDEA保留了之前module的痕跡,導(dǎo)致重建時將其視為已刪除的module,解決方法是進入IDEA設(shè)置,找到Maven的Ignored Files設(shè)置2024-09-09

