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

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

注意:動(dòng)態(tài)路由設(shè)置時(shí),uri以 lb: //開頭(lb代表從注冊(cè)中心獲取服務(wù)),后面是需要轉(zhuǎn)發(fā)到的服務(wù)名稱
到此這篇關(guān)于GateWay路由規(guī)則與動(dòng)態(tài)路由詳細(xì)介紹的文章就介紹到這了,更多相關(guān)GateWay路由規(guī)則內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
- Spring?Cloud?Gateway動(dòng)態(tài)路由Apollo實(shí)現(xiàn)詳解
- SpringCloud Gateway使用redis實(shí)現(xiàn)動(dòng)態(tài)路由的方法
- Spring Cloud Gateway + Nacos 實(shí)現(xiàn)動(dòng)態(tài)路由
- SpringCloud?Gateway詳細(xì)分析實(shí)現(xiàn)負(fù)載均衡與熔斷和限流
- springcloud gateway如何實(shí)現(xiàn)路由和負(fù)載均衡
- GateWay動(dòng)態(tài)路由與負(fù)載均衡詳細(xì)介紹
相關(guān)文章
Spring整合SpringMVC與Mybatis(SSM)實(shí)現(xiàn)完整登錄功能流程詳解
開學(xué)學(xué)校開始講servlet 后期要求做一個(gè)登錄功能,這個(gè)使用SSM先只做個(gè)簡(jiǎn)單的只帶登錄功能的,且項(xiàng)目使用了MyBatis-Plus來簡(jiǎn)化開發(fā)流程。看情況決定要不要升級(jí)功能或者換個(gè)寫法2022-09-09
Java JFinal框架中實(shí)現(xiàn)添加定時(shí)任務(wù)功能詳解
這篇文章主要為大家詳細(xì)介紹了如何在JFinal框架項(xiàng)目中添加定時(shí)任務(wù)的功能,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下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ū)別。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08
Maven項(xiàng)目在new module后,pom文件顯示為Ignored pom.xml問題
在Maven項(xiàng)目中,若創(chuàng)建過同名module后刪除,再次創(chuàng)建時(shí)可能導(dǎo)致pom.xml文件被IDEA忽略,原因是IDEA保留了之前module的痕跡,導(dǎo)致重建時(shí)將其視為已刪除的module,解決方法是進(jìn)入IDEA設(shè)置,找到Maven的Ignored Files設(shè)置2024-09-09

