国产 无码 综合区,色欲AV无码国产永久播放,无码天堂亚洲国产AV,国产日韩欧美女同一区二区

【深入解析spring cloud gateway】02 網(wǎng)關(guān)路由斷言

這篇具有很好參考價值的文章主要介紹了【深入解析spring cloud gateway】02 網(wǎng)關(guān)路由斷言。希望對大家有所幫助。如果存在錯誤或未考慮完全的地方,請大家不吝賜教,您也可以點擊"舉報違法"按鈕提交疑問。

一、斷言(Predicate)的意義

斷言是路由配置的一部分,當(dāng)斷言條件滿足,即執(zhí)行Filter的邏輯,如下例所示

spring:
  cloud:
    gateway:
      routes:
      - id: add_request_header_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment}
        filters:
        - AddRequestHeader=X-Request-Red, Blue-{segment}

當(dāng)請求路徑滿足條件/red/,即添加頭信息:X-Request-Red,value為Blue-{segment},segment是路徑里面帶的信息。

gateWay的主要功能之一是轉(zhuǎn)發(fā)請求,轉(zhuǎn)發(fā)規(guī)則的定義主要包含三個部分

Route(路由) 路由是網(wǎng)關(guān)的基本單元,由ID、URI、一組Predicate、一組Filter組成,根據(jù)Predicate進行匹配轉(zhuǎn)發(fā)。
Predicate(謂語、斷言) 路由轉(zhuǎn)發(fā)的判斷條件,目前SpringCloud Gateway支持多種方式,常見如:Path、Query、Method、Header等,寫法必須遵循 key=vlue的形式
Filter(過濾器) 過濾器是路由轉(zhuǎn)發(fā)請求時所經(jīng)過的過濾邏輯,可用于修改請求、響應(yīng)內(nèi)容

其中Route和Predicate必須同時申明
【深入解析spring cloud gateway】02 網(wǎng)關(guān)路由斷言,深入解析析SpringCloud Gateway,spring cloud,gateway

以下內(nèi)容,來自于SpringCloud Gateway官網(wǎng),經(jīng)過整理得來。

二、配置路由斷言

注意:以下的配置,只寫了routes配置的predicates的部分,實際上,predicates單獨用沒有意義,一般要配置filter來用。如上面的添加頭信息的示例。

1.1 簡寫配置

由過濾器名稱,后跟等號,后跟逗號分割的參數(shù)值

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - Cookie=mycookie,mycookievalue

1.2 展開的寫法

spring:
  cloud:
    gateway:
      routes:
      - id: test_cookie
        uri: https://example.org
        predicates:
        - name: Cookie
          args:
            name: mycookie
            regexp: mycookievalue

以上兩個寫法,效果是一樣的。
問題1:展開的predicates的展開寫法為什么是這么寫?
原因是,這個配置對應(yīng)的配置類是這么寫的
【深入解析spring cloud gateway】02 網(wǎng)關(guān)路由斷言,深入解析析SpringCloud Gateway,spring cloud,gateway

問題2:name:Cookie是怎么來的?
根據(jù)類名來的,CookieRoutePredicateFactory,取的Cookie前綴,還有很多斷言工廠,如下:
【深入解析spring cloud gateway】02 網(wǎng)關(guān)路由斷言,深入解析析SpringCloud Gateway,spring cloud,gateway

問題3:為啥args下面是name和regexp?
【深入解析spring cloud gateway】02 網(wǎng)關(guān)路由斷言,深入解析析SpringCloud Gateway,spring cloud,gateway

三、路由斷言工廠示例

Spring Cloud Gateway 路由匹配作為Spring WebFlux HandlerMapping 基礎(chǔ)設(shè)施的一部分。Spring Cloud Gateway內(nèi)置了很多路由斷言工廠。用于匹配HTTP請求的不同屬性。
注意:以下的配置,只寫了routes配置的predicates的部分,實際上,predicates單獨用沒有意義,一般要配置filter來用。如上面的添加頭信息的示例。

2.1 The After Route Predicate Factory

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]

匹配2017-01-20T17:42:47.789-07:00之后的請求

2.2 The Before Route Predicate Factory

spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - Before=2017-01-20T17:42:47.789-07:00[America/Denver]

匹配2017-01-20T17:42:47.789-07:00之前的請求

2.3 The Between Route Predicate Factory

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]

表示在第一個時間之后,第二個時間之前的請求才能正確匹配路由

2.4 The Cookie Route Predicate Factory

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, ch.p

接收兩個參數(shù),分別為name 和 regexp(Java正則表達(dá)式),表示cookie中攜帶的name值滿足正則表達(dá)式regexp,則被路由

2.5 The Header Route Predicate Factory

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, \d+

接收兩個參數(shù),header 和 regexp(Java正則表達(dá)式),表示header中攜帶的name值滿足正則表達(dá)式regexp,則被路由

2.6 The Host Route Predicate Factory

spring:
  cloud:
    gateway:
      routes:
      - id: host_route
        uri: https://example.org
        predicates:
        - Host=**.somehost.org,**.anotherhost.org

支持URI模板變量(如{sub}.myhost.org)。當(dāng)Host 頭的值為 www.somehost.org 或 beta.somehost.org 或 www.anotherhost.org 都能匹配該路由。
Predicate 會提取URI模板變量作為map集合,并放置在 ServerWebExchange.getAttributes() 中,key定義為 ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE ,這些值能在 GatewayFilter工廠中使用。

2.7 The Method Route Predicate Factory

spring:
  cloud:
    gateway:
      routes:
      - id: method_route
        uri: https://example.org
        predicates:
        - Method=GET,POST

上面示例表示匹配 GET ,POST請求

2.8 The Path Route Predicate Factory

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment},/blue/{segment}

如果請求地址是 /red/1 或 /red/1/ 或 /red/blue 或 /blue/green ,那么路由將會被匹配。
如果 matchTrailingSlash 設(shè)置為 false ,那么 /red/1/ 不會被匹配。
Predicate 會提取URI模板變量作為map集合,并放置在 ServerWebExchange.getAttributes() 中,key定義
為 ServerWebExchangeUtils.URI_TEMPLATE_VARIABLES_ATTRIBUTE ,這些值能在 GatewayFilter 工廠中使用。
可以使用個實用法(調(diào)用get)來簡化對這些變量的訪問。下面的例示展示了如何使用get方法:

Map<String, String> uriVariables = ServerWebExchangeUtils.getPathPredicateVariables(exchange);

String segment = uriVariables.get("segment");

2.9 The Query Route Predicate Factory

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=green

接收兩個參數(shù),分別是一個必須的param和一個可選的regexp。

spring:
  cloud:
    gateway:
      routes:
      - id: query_route
        uri: https://example.org
        predicates:
        - Query=red, gree.

red和gree兩個參數(shù)都有才滿足

2.10 The RemoteAddr Route Predicate Factory

spring:
  cloud:
    gateway:
      routes:
      - id: remoteaddr_route
        uri: https://example.org
        predicates:
        - RemoteAddr=192.168.1.1/24.

接收?個sources列表(最小1個),CIDR表示法(IPv4或IPv6)字符串,例如192.168.0.1/16(其中192.168.0.1是
?個IP地址,16是?個?網(wǎng)掩碼)。
如果請求的遠(yuǎn)程地址是 192.168.1.10 ,則此路由被匹配

2.11 The Weight Route Predicate Factory

spring:
  cloud:
    gateway:
      routes:
      - id: weight_high
        uri: https://weighthigh.org
        predicates:
        - Weight=group1, 8
      - id: weight_low
        uri: https://weightlow.org
        predicates:
        - Weight=group1, 2

將有80%的流量被路由到 weighthigh.org ,20%的流量被路由到 weightlow.org 。文章來源地址http://www.zghlxwxcb.cn/news/detail-696391.html

到了這里,關(guān)于【深入解析spring cloud gateway】02 網(wǎng)關(guān)路由斷言的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!

本文來自互聯(lián)網(wǎng)用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務(wù),不擁有所有權(quán),不承擔(dān)相關(guān)法律責(zé)任。如若轉(zhuǎn)載,請注明出處: 如若內(nèi)容造成侵權(quán)/違法違規(guī)/事實不符,請點擊違法舉報進行投訴反饋,一經(jīng)查實,立即刪除!

領(lǐng)支付寶紅包贊助服務(wù)器費用

相關(guān)文章

  • 【Spring Cloud Alibaba】8.路由網(wǎng)關(guān)(Gateway)

    【Spring Cloud Alibaba】8.路由網(wǎng)關(guān)(Gateway)

    接下來對服務(wù)消費者添加路由網(wǎng)關(guān)來實現(xiàn)統(tǒng)一訪問接口,本操作先要完成之前的步驟,詳情請參照【Spring Cloud Alibaba】Spring Cloud Alibaba 搭建教程 Spring Cloud Gateway 是 Spring 官方基于 Spring 5.0 , Spring Boot 2.0 和 Project Reactor 等技術(shù)開發(fā)的網(wǎng)關(guān),該項目提供了一個庫,用于在 Spring W

    2023年04月24日
    瀏覽(22)
  • Spring Cloud Gateway 監(jiān)控、多網(wǎng)關(guān)實例路由共享 | Spring Cloud 18

    Actuator 是 Spring Boot 提供的用來對應(yīng)用系統(tǒng)進行監(jiān)控的功能模塊,借助于 Actuator 開發(fā)者可以很方便地對應(yīng)用系統(tǒng)某些監(jiān)控指標(biāo)進行查看、統(tǒng)計等。 Actuator 的核心是端點 Endpoint 。 Endpoint 可以讓我們監(jiān)視應(yīng)用程序并與其交互。 Spring Boot 包含許多內(nèi)置端點,并允許您添加自己的端

    2024年02月09日
    瀏覽(27)
  • 第八章 : Spring cloud 網(wǎng)關(guān)中心 Gateway (動態(tài)路由)

    第八章 : Spring cloud 網(wǎng)關(guān)中心 Gateway (動態(tài)路由) 前言 本章知識點:重點介紹動態(tài)網(wǎng)關(guān)路由的背景、動態(tài)路由與靜態(tài)路由的概念,以及如何基于Nacos實現(xiàn)動態(tài)網(wǎng)關(guān)路由 的實戰(zhàn)案例。 背景 前面章節(jié)介紹了Spring Cloud Gateway提供的配置路由規(guī)則的兩種方法,但都是在Spring Cloud Ga

    2024年01月19日
    瀏覽(36)
  • 第七章 : Spring cloud 網(wǎng)關(guān)中心 Gateway (靜態(tài)路由)

    第七章 : Spring cloud 網(wǎng)關(guān)中心 Gateway (靜態(tài)路由) 前言 本章知識點:本章將會介紹什么是Spring Cloud Gateway、為什么會出現(xiàn)Spring Cloud Gateway,以及Spring Cloud Gateway的工作原理和實戰(zhàn)用法,以及Spring Cloud Gateway 路由概念以及基于nacos注冊中心Spring Cloud Gateway 靜態(tài)路由的實戰(zhàn)。 什么

    2024年02月02日
    瀏覽(37)
  • 【深入解析spring cloud gateway】06 gateway源碼簡要分析

    【深入解析spring cloud gateway】06 gateway源碼簡要分析

    上一節(jié)做了一個很簡單的示例,微服務(wù)通過注冊到eureka上,然后網(wǎng)關(guān)通過服務(wù)發(fā)現(xiàn)訪問到對應(yīng)的微服務(wù)。本節(jié)將簡單地對整個gateway請求轉(zhuǎn)發(fā)過程做一個簡單的分析。 主要流程: Gateway Client向 Spring Cloud Gateway 發(fā)送請求 請求首先會被HttpWebHandlerAdapter 進行提取組裝成網(wǎng)關(guān)上下文

    2024年02月10日
    瀏覽(25)
  • 【深入解析spring cloud gateway】05 gateway請求轉(zhuǎn)發(fā)實驗

    三個工程: eureka-server eureka-client gateway 實驗?zāi)康模和ㄟ^網(wǎng)關(guān)訪問對應(yīng)的微服務(wù):eureka-client。gateway和eureka-client注冊到eureka-server上 eureka-server略 eureka-client application.yml 提供一個接口 pom.xml application.yml 定義一個filter用于去掉路徑中的/gateway 自定義一個GlobalFilter,用于去掉路徑

    2024年02月10日
    瀏覽(18)
  • 深入解析Spring Cloud Gateway的GlobalFilter

    深入解析Spring Cloud Gateway的GlobalFilter

    本文將詳細(xì)介紹Spring Cloud Gateway中的GlobalFilter,解釋其作用以及如何使用。通過代碼示例,讀者將深入了解GlobalFilter在Spring Cloud Gateway中的應(yīng)用,以及如何自定義和配置GlobalFilter來實現(xiàn)定制化的網(wǎng)關(guān)邏輯。 Spring Cloud Gateway是Spring Cloud生態(tài)系統(tǒng)中的一員,是基于Spring Framework 5、

    2024年04月10日
    瀏覽(62)
  • 【深入解析spring cloud gateway】04 Global Filters

    上一節(jié)學(xué)習(xí)了GatewayFilter。 回憶一下一個關(guān)鍵點: GateWayFilterFactory的本質(zhì)就是:針對配置進行解析,為指定的路由,添加Filter,以便對請求報文進行處理。 GlobalFilter又是啥?先看一下接口定義 再看一下GatewayFilter 可以看到GatewayFilter和GlobalFilter方法簽名是一模一樣的,那為啥又

    2024年02月09日
    瀏覽(18)
  • 【深入解析spring cloud gateway】08 Reactor 知識掃盲

    【深入解析spring cloud gateway】08 Reactor 知識掃盲

    1.1 背景知識 為了應(yīng)對高并發(fā)服務(wù)器端開發(fā)場景,在2009 年,微軟提出了一個更優(yōu)雅地實現(xiàn)異步編程的方式——Reactive Programming,我們稱之為響應(yīng)式編程。隨后,Netflix 和LightBend 公司提供了RxJava 和Akka Stream 等技術(shù),使得Java 平臺也有了能夠?qū)崿F(xiàn)響應(yīng)式編程的框架。 在2017 年9 月

    2024年02月09日
    瀏覽(14)
  • 【深入解析spring cloud gateway】07 自定義異常返回報文

    【深入解析spring cloud gateway】07 自定義異常返回報文

    Servlet的HttpResponse對象,返回響應(yīng)報文,一般是這么寫的,通過輸出流直接就可以將返回報文輸出。 在filter中如果發(fā)生異常(例如請求參數(shù)不合法),拋出異常信息的時候,調(diào)用方收到的返回碼和body都是Spring Cloud Gateway框架處理來處理的。這一節(jié)我們分析一下,gateway的異常返

    2024年02月10日
    瀏覽(26)

覺得文章有用就打賞一下文章作者

支付寶掃一掃打賞

博客贊助

微信掃一掃打賞

請作者喝杯咖啡吧~博客贊助

支付寶掃一掃領(lǐng)取紅包,優(yōu)惠每天領(lǐng)

二維碼1

領(lǐng)取紅包

二維碼2

領(lǐng)紅包