柚子快報激活碼778899分享:GateWay服務(wù)網(wǎng)關(guān)
柚子快報激活碼778899分享:GateWay服務(wù)網(wǎng)關(guān)
目錄
概述作用GateWay三大核心application.yml配置示例圖理解
工作流程入門配置路由映射GateWay高級特性Route 以服務(wù)名動態(tài)獲取服務(wù)uriPredicate斷言(謂詞)The After Route Predicate FactoryThe Before Route Predicate FactoryThe Between Route Predicate FactoryThe Cookie Route Predicate FactoryThe Header Route Predicate FactoryThe Host Route Predicate FactoryThe Method Route Predicate FactoryThe Path Route Predicate FactoryThe Query Route Predicate FactoryThe RemoteAddr Route Predicate FactoryThe Weight Route Predicate FactoryThe XForwarded Remote Addr Route Predicate Factory自定義斷言
Filter 過濾單一內(nèi)置過濾器請求頭相關(guān)AddRequestHeader GatewayFilter FactoryRemoveRequestHeader GatewayFilter FactorySetRequestHeader GatewayFilter Factory
請求參數(shù)相關(guān)AddRequestParameter GatewayFilter FactoryRemoveRequestParameter GatewayFilter Factory
響應(yīng)頭相關(guān)AddResponseHeader GatewayFilter FactorySetResponseHeader GatewayFilter FactoryRemoveResponseHeader GatewayFilter Factory
前綴路徑相關(guān)PrefixPath GatewayFilter FactorySetPath GatewayFilter FactoryRedirectTo GatewayFilter Factory
其他Default Filters
自定義單一內(nèi)置過濾器
全局默認過濾器自定義全局過濾器實現(xiàn)統(tǒng)計服務(wù)接口請求耗時
相關(guān)文獻
概述
Gateway是在Spring生態(tài)系統(tǒng)之上構(gòu)建的API網(wǎng)關(guān)服務(wù),基于Spring6,Spring Boot 3和Project Reactor等技術(shù)。它旨在為微服務(wù)架構(gòu)提供一種簡單有效的統(tǒng)一的 API 路由管理方式,并為它們提供跨領(lǐng)域的關(guān)注點,例如:安全性、監(jiān)控/度量和恢復(fù)能力。
在Spring Cloud中,在1.x版本中都是采用的Zuul網(wǎng)關(guān); 但在2.x版本中,zuul的升級一直跳票,SpringCloud最后自己研發(fā)了一個網(wǎng)關(guān)SpringCloud Gateway替代Zuul
微服務(wù)架構(gòu)網(wǎng)關(guān)位置圖如下:
作用
反向代理鑒權(quán)流量控制熔斷日志監(jiān)控
總結(jié): Spring Cloud Gateway組件的核心是一系列的過濾器,通過這些過濾器可以將客戶端發(fā)送的請求轉(zhuǎn)發(fā)(路由)到對應(yīng)的微服務(wù)。 Spring Cloud Gateway是加在整個微服務(wù)最前沿的防火墻和代理器,隱藏微服務(wù)結(jié)點IP端口信息,從而加強安全保護。 Spring Cloud Gateway本身也是一個微服務(wù),需要注冊進服務(wù)注冊中心(像consul)。
GateWay三大核心
Route-路由 網(wǎng)關(guān)的基本構(gòu)建塊。它由 ID、目標 URI、謂詞集合和篩選器集合定義。如果聚合謂詞為 true,則匹配路由。Predicate-斷言 這是一個 Java 8 函數(shù)謂詞。輸入類型是 Spring Framework ServerWebExchange 這使您可以匹配 HTTP 請求中的任何內(nèi)容,例如標頭或參數(shù),如果請求和斷言相匹配則進行路由。Filter-過濾器 這些是使用特定工廠構(gòu)建的 GatewayFilter 實例。在這里,您可以在發(fā)送下游請求之前或之后修改請求和響應(yīng)。
application.yml配置示例
GateWay有兩種配置展現(xiàn)形式,Shortcut Configuration(快捷方式配置)、Fully Expanded Arguments(完全展開的參數(shù))
Shortcut Configuration:
spring:
cloud:
gateway:
routes:
- id: after_route #我們自定義的路由 ID,保持唯一
uri: https://example.org #目標服務(wù)地址
predicates: #路由條件,Predicate接受一個輸入?yún)?shù)返回一個布爾值。
- Cookie=mycookie,mycookievalue #該屬性包含多種默認方法來將Predicate組合成其他復(fù)雜的邏輯(比如:與,或,非)
Fully Expanded Arguments:
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- name: Cookie
args:
name: mycookie
regexp: mycookievalue
圖理解
如下圖
web前端請求,通過一些匹配條件,定位到真正的服務(wù)節(jié)點。并在這個轉(zhuǎn)發(fā)過程的前后,進行一些精細化控制。
predicate就是我們的匹配條件;
filter,就可以理解為一個無所不能的過濾器。有了這兩個元素,再加上目標uri,就可以實現(xiàn)一個具體的路由了
舉個例子: 我要去華為專賣店買手機,那么我肯定不能去蘋果專賣店去,那么這個去哪個店就是路由,我到了華為專賣店,我要買華為黑色的mate60pro,店里有黑色、白色的,剛好有我想要的黑色,那么顏色匹配上我想要的,這個判斷就是斷言,如果沒有匹配上,那我就買不了,我買上了心愛的手機,這時候?qū)Yu店給我送了個屏幕膜,這時候給我貼上了給我,舉動非常貼心,在我買手機后給我在裸機上貼了個膜,這就是過濾器的post動作。
工作流程
客戶端向 Spring Cloud Gateway 發(fā)出請求。然后在 Gateway Handler Mapping 中找到與請求相匹配的路由,將其發(fā)送到 Gateway Web Handler。Handler 再通過指定的過濾器鏈來將請求發(fā)送到我們實際的服務(wù)執(zhí)行業(yè)務(wù)邏輯,然后返回。
過濾器之間用虛線分開是因為過濾器可能會在發(fā)送代理請求之前(Pre)或之后(Post)執(zhí)行業(yè)務(wù)邏輯。
在“pre”類型的過濾器可以做參數(shù)校驗、權(quán)限校驗、流量監(jiān)控、日志輸出、協(xié)議轉(zhuǎn)換等;
在“post”類型的過濾器中可以做響應(yīng)內(nèi)容、響應(yīng)頭的修改,日志的輸出,流量監(jiān)控等有著非常重要的作用。
核心邏輯就是:路由轉(zhuǎn)發(fā)+斷言判斷+執(zhí)行過濾器鏈
入門配置
網(wǎng)關(guān)依賴引入:
將網(wǎng)關(guān)服務(wù)注冊進去(只提供思路,具體代碼實現(xiàn)可參考其他文章 ,關(guān)鍵詞:consul服務(wù)注冊)
路由映射
訴求:不想暴露真實的ip地址、服務(wù)名等等給外部使用,在外面套一層網(wǎng)關(guān) 新起一個服務(wù)要求訪問:http://localhost:8001/pay/gateway/get/1,http://localhost:8001/pay/gateway/info,可通 網(wǎng)關(guān)服務(wù)yml新增配置: 然后重啟網(wǎng)關(guān)服務(wù)。 如網(wǎng)關(guān)yml配置正確,則訪問http://localhost:9527/pay/gateway/get/1,http://localhost:9527/pay/gateway/info可通,這樣就達到了,外部服務(wù)只需要知道9527端口,不知實際8001真實端口的要求。
GateWay高級特性
Route 以服務(wù)名動態(tài)獲取服務(wù)uri
上面的例子很簡單,但是問題也來了,一個服務(wù)簡單的寫寫ip端口沒問題,如果是部署集群呢,會很麻 怎么解決呢? 網(wǎng)關(guān)yml修改前: 網(wǎng)關(guān)yml修改后:通過lb后面跟著服務(wù)名,這樣就能不用關(guān)心服務(wù)的ip端口了
Predicate斷言(謂詞)
Spring Cloud Gateway 將路由作為 Spring WebFlux HandlerMapping 基礎(chǔ)結(jié)構(gòu)的一部分進行匹配。Spring Cloud Gateway 包含許多內(nèi)置的路由謂詞工廠。所有這些謂詞都與 HTTP 請求的不同屬性匹配。您可以將多個路由謂詞工廠與邏輯 and 語句組合在一起。以下圖右側(cè)是內(nèi)置的斷言。
The After Route Predicate Factory
After 路由謂詞工廠采用一個參數(shù) a datetime (即 java ZonedDateTime )。此謂詞匹配在指定日期時間之后發(fā)生的請求。以下示例配置路由后謂詞:
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
案例說明:只能通過2017 年 1 月 20 日 17:42 Mountain Time (Denver) 之后的請求
After、Before、Between 時間參數(shù)獲取方式:
ZonedDateTime zbj = ZonedDateTime.now(); // 默認時區(qū)
The Before Route Predicate Factory
Before 路由謂詞工廠采用一個參數(shù) a datetime (即 java ZonedDateTime )。此謂詞匹配在指定的 datetime .以下示例配置 before route 謂詞:
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
案例說明:只能通過 2017 年 1 月 20 日 17:42 Mountain Time (Denver) 之前的請求
The Between Route Predicate Factory
Between 路由謂詞工廠采用兩個參數(shù), datetime1 datetime2 它們是 java ZonedDateTime 對象。此謂詞匹配在 datetime1 之后和之前 datetime2 發(fā)生的請求。該 datetime2 參數(shù)必須位于 datetime1 之后。以下示例配置 between route 謂詞:
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]
案例說明:只能通過2017 年 1 月 20 日 17:42 山地時間(丹佛)之后和 2017 年 1 月 21 日 17:42 山地時間(丹佛)之前的請求
時間的之前、之后、中間,很符合現(xiàn)在活動業(yè)務(wù)中的秒殺業(yè)務(wù)。
The Cookie Route Predicate Factory
Cookie 路由謂詞工廠采用兩個參數(shù),cookie name 和 a regexp (Java 正則表達式)。此謂詞匹配具有給定名稱且其值與正則表達式匹配的 cookie。以下示例配置 cookie 路由謂詞工廠:
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
案例說明: 此路由匹配具有名為 chocolate cookie 的請求,其值與 ch.p 正則表達式匹配。
The Header Route Predicate Factory
Header 路由謂詞工廠采用兩個參數(shù),即 和 header a regexp (這是一個 Java 正則表達式)。此謂詞與具有給定名稱的標頭匹配,其值與正則表達式匹配。以下示例配置標頭路由謂詞:
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
案例說明: 如果請求具有名為 X-Request-Id 其值與 \d+ 正則表達式匹配的標頭(即,它的值為一位或多位),則此路由匹配。
The Host Route Predicate Factory
Host 路由謂詞工廠采用一個參數(shù):主機名 patterns 列表。該圖案是螞 . 蟻風(fēng)格的圖案,作為分隔符。此謂詞與 Host 與模式匹配的標頭匹配。以下示例配置主機路由謂詞:
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
案例說明: 還支持 URI 模板變量(如 {sub}.myhost.org )。如果請求的 Host 標頭值為 www.somehost.org or beta.somehost.org 或 www.anotherhost.org ,則此路由匹配。
The Method Route Predicate Factory
Method 路由謂詞工廠采用一個 methods 參數(shù),該參數(shù)是一個或多個參數(shù):要匹配的 HTTP 方法。下面的示例配置方法路由謂詞:
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
案例說明: 如果請求方法是 a GET 或 . POST則匹配
The Path Route Predicate Factory
Path 路由謂詞工廠采用兩個參數(shù):Spring PathMatcher patterns 列表和名為 matchTrailingSlash (默認為 true ) 的可選標志。以下示例配置路徑路由謂詞:
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 。
The Query Route Predicate Factory
Query 路由謂詞工廠采用兩個參數(shù):必需 param 參數(shù)和可選參數(shù) regexp (Java 正則表達式)。以下示例配置查詢路由謂詞:
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
案例說明: 如果請求包含 green 查詢參數(shù),則上述路由匹配。
The RemoteAddr Route Predicate Factory
RemoteAddr 路由謂詞工廠采用 sources 的列表(最小大小為 1),這些列表是 CIDR 表示法(IPv4 或 IPv6)字符串,例如 192.168.0.1/16 (where 192.168.0.1 是 IP 地址, 16 是子網(wǎng)掩碼)。以下示例配置 RemoteAddr 路由謂詞:
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
案例說明: 如果請求的遠程地址為 ,則此路由匹配 192.168.1.10
The Weight Route Predicate Factory
Weight 路由謂詞工廠采用兩個參數(shù): group 和 weight (一個 int)。權(quán)重是按組計算的。以下示例配置權(quán)重路由謂詞:
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% 的流量轉(zhuǎn)發(fā)到 weighthigh.org,將 ~20% 的流量轉(zhuǎn)發(fā)到 weightlow.org
The XForwarded Remote Addr Route Predicate Factory
XForwarded Remote Addr 路由謂詞工廠采用 sources 的列表(最小大小為 1),這些列表是 CIDR 表示法(IPv4 或 IPv6)字符串,例如 192.168.0.1/16 (where 192.168.0.1 是 IP 地址, 16 是子網(wǎng)掩碼)。此路由謂詞允許根據(jù) X-Forwarded-For HTTP 標頭篩選請求。這可用于反向代理,例如負載平衡器或 Web 應(yīng)用程序防火墻,其中僅當請求來自這些反向代理使用的受信任 IP 地址列表時,才應(yīng)允許請求。
spring:
cloud:
gateway:
routes:
- id: xforwarded_remoteaddr_route
uri: https://example.org
predicates:
- XForwardedRemoteAddr=192.168.1.1/24
案例說明: 如果 X-Forwarded-For 標頭包含,例如, 192.168.1.10 此路由匹配。
自定義斷言
如果以上內(nèi)置斷言不滿足需求,那么可以自定義斷言,可參考AbstractRoutePredicateFactory類源碼,實現(xiàn)自定義斷言
步驟: 1、新建XXXRoutePredicateFactory,要以RoutePredicateFactory結(jié)尾,且繼承AbstractRoutePredicateFactory類 2、重寫apply方法 3、新建config內(nèi)部類,用來斷言的重要key 4、空參調(diào)用方法,內(nèi)部調(diào)用sup
@Component
public class MyRoutePredicateFactory extends AbstractRoutePredicateFactory
{
public MyRoutePredicateFactory()
{
super(MyRoutePredicateFactory.Config.class);
}
@Validated
public static class Config{
@Setter
@Getter
@NotEmpty
private String userType; //鉆、金、銀等用戶等級
}
@Override
public Predicate
{
return new Predicate
{
@Override
public boolean test(ServerWebExchange serverWebExchange)
{
//檢查request的參數(shù)里面,userType是否為指定的值,符合配置就通過
String userType = serverWebExchange.getRequest().getQueryParams().getFirst("userType");
if (userType == null) return false;
//如果說參數(shù)存在,就和config的數(shù)據(jù)進行比較
if(userType.equals(config.getUserType())) {
return true;
}
return false;
}
};
}
@Override
public List
return Collections.singletonList("userType");
}
}
yml配置(自定義斷言要用Fully Expanded Arguments(完全展開的參數(shù)),如果用Shortcut Configuration會報錯?。?/p>
spring:
cloud:
gateway:
routes:
- id: xforwarded_remoteaddr_route
uri: https://example.org
predicates:
- name: My
args:
userType: diamond
Filter 過濾
路由過濾器允許以某種方式修改傳入的 HTTP 請求或傳出的 HTTP 響應(yīng)。路由篩選器的作用域為特定路由。Spring Cloud Gateway 包含許多內(nèi)置的 GatewayFilter 工廠。 GateWay過濾器分Global Filters(全局默認過濾器)和GateWay Filter(單一內(nèi)置過濾器),區(qū)別在于,全局默認過濾器是作用于所有路由,不需要在yml中配置,實現(xiàn)GlobalFilter接口即可,單一內(nèi)置過濾器需要在yml中配置,作用于單一路由或者路由組。
單一內(nèi)置過濾器
官網(wǎng)有幾十種,在這里我就不一一舉例了,分了幾組給大家看看只舉幾個例子
請求頭相關(guān)
AddRequestHeader GatewayFilter Factory
采用 and name value 參數(shù)。以下示例配置: AddRequestHeader GatewayFilter
spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: https://example.org
filters:
- AddRequestHeader=X-Request-red, blue
此列表將標頭添加到 X-Request-red:blue 所有匹配請求的下游請求的標頭中。
RemoveRequestHeader GatewayFilter Factory
RemoveRequestHeader GatewayFilter 工廠采用一個 name 參數(shù)。它是要刪除的標頭的名稱。以下列表配置:
spring:
cloud:
gateway:
routes:
- id: removerequestheader_route
uri: https://example.org
filters:
- RemoveRequestHeader=X-Request-Foo
這將在標 X-Request-Foo 頭發(fā)送到下游之前將其刪除。
SetRequestHeader GatewayFilter Factory
SetRequestHeader GatewayFilter 工廠采取 name 和 value 參數(shù)。以下列表配置: SetRequestHeader GatewayFilter
spring:
cloud:
gateway:
routes:
- id: setrequestheader_route
uri: https://example.org
filters:
- SetRequestHeader=X-Request-Red, Blue
這會 GatewayFilter 用給定的名稱替換(而不是添加)所有標頭。因此,如果下游服務(wù)器響應(yīng) X-Request-Red:1234 ,它將被替換為 X-Request-Red:Blue ,這是下游服務(wù)將接收的內(nèi)容。
請求參數(shù)相關(guān)
AddRequestParameter GatewayFilter Factory
AddRequestParameter GatewayFilter 工廠采用 and name value 參數(shù)。以下示例配置:
spring:
cloud:
gateway:
routes:
- id: add_request_parameter_route
uri: https://example.org
filters:
- AddRequestParameter=red, blue
這將添加到 red=blue 所有匹配請求的下游請求的查詢字符串中。
RemoveRequestParameter GatewayFilter Factory
RemoveRequestParameter GatewayFilter 工廠采用一個 name 參數(shù)。它是要刪除的查詢參數(shù)的名稱。以下示例配置:
spring:
cloud:
gateway:
routes:
- id: removerequestparameter_route
uri: https://example.org
filters:
- RemoveRequestParameter=red
這將在 red 參數(shù)發(fā)送到下游之前將其刪除。
響應(yīng)頭相關(guān)
AddResponseHeader GatewayFilter Factory
AddResponseHeader GatewayFilter 工廠采用 and name value 參數(shù)。以下示例配置:
spring:
cloud:
gateway:
routes:
- id: add_response_header_route
uri: https://example.org
filters:
- AddResponseHeader=X-Response-Red, Blue
這會將標頭添加到 X-Response-Red:Blue 所有匹配請求的下游響應(yīng)標頭中。
SetResponseHeader GatewayFilter Factory
spring:
cloud:
gateway:
routes:
- id: setresponseheader_route
uri: https://example.org
filters:
- SetResponseHeader=X-Response-Red, Blue
此 GatewayFilter 將所有標頭替換(而不是添加)為給定名稱。因此,如果下游服務(wù)器響應(yīng) X-Response-Red:1234 ,它將被替換為 X-Response-Red:Blue ,這是網(wǎng)關(guān)客戶端將接收的內(nèi)容。
RemoveResponseHeader GatewayFilter Factory
spring:
cloud:
gateway:
routes:
- id: removeresponseheader_route
uri: https://example.org
filters:
- RemoveResponseHeader=X-Response-Foo
這將在將 X-Response-Foo 標頭返回到網(wǎng)關(guān)客戶端之前從響應(yīng)中刪除標頭。
前綴路徑相關(guān)
PrefixPath GatewayFilter Factory
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: https://example.org
filters:
- PrefixPath=/mypath
這為所有匹配請求的路徑添加前綴 /mypath 。因此,將請求 /hello 發(fā)送到 /mypath/hello
SetPath GatewayFilter Factory
SetPath GatewayFilter 工廠采用路徑 template 參數(shù)。它提供了一種簡單的方法來操作請求路徑,允許路徑的模板化段。這使用 Spring Framework 中的 URI 模板。允許多個匹配的區(qū)段。以下示例配置:
spring:
cloud:
gateway:
routes:
- id: setpath_route
uri: https://example.org
predicates:
- Path=/red/{segment}
filters:
- SetPath=/{segment}
對于請求 /red/blue 路徑 ,這會在發(fā)出下游請求之前設(shè)置路徑 {segment}就是個占位符,等價于SetPath后面指定的{segment}內(nèi)容
RedirectTo GatewayFilter Factory
RedirectTo GatewayFilter 工廠采用三個參數(shù), status 、 url 和 includeRequestParams 可選 。該 status 參數(shù)應(yīng)為 300 系列重定向 HTTP 代碼,例如 301。該 url 參數(shù)應(yīng)為有效的 URL。這是 Location 標頭的值。該 includeRequestParams 參數(shù)指示是否應(yīng)將請求查詢參數(shù)包含在 url 上。如果未設(shè)置,它將被視為 false .對于相對重定向,應(yīng)用作 uri: no://op 路由定義的 uri。以下列表配置:
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: https://example.org
filters:
- RedirectTo=302, https://acme.org
這將發(fā)送帶有 Location:https://acme.org 標頭的狀態(tài) 302 以執(zhí)行重定向。
其他
Default Filters
要添加過濾器并將其應(yīng)用于所有路由,可以使用 spring.cloud.gateway.default-filters .此屬性采用篩選器列表。以下列表定義了一組默認篩選器:
spring:
cloud:
gateway:
default-filters:
- AddResponseHeader=X-Response-Default-Red, Default-Blue
- PrefixPath=/httpbin
這個就功能類似于全局默認過濾器,作用于所有路由的過濾
自定義單一內(nèi)置過濾器
查看SetPathGatewayFilterFactory源碼,參考源碼實現(xiàn) 新建MyGatewayFilterFactory類,以XXXXGatewayFilterFactory文件名結(jié)尾 創(chuàng)建xxGatewayFilterFactory.Config內(nèi)部類 重寫apply、shortcutFieldOrder方法 空參構(gòu)造方法,重寫super
@Component
public class MyGatewayFilterFactory extends AbstractGatewayFilterFactory
{
public MyGatewayFilterFactory()
{
super(MyGatewayFilterFactory.Config.class);
}
@Override
public GatewayFilter apply(MyGatewayFilterFactory.Config config)
{
return new GatewayFilter()
{
@Override
public Mono
{
ServerHttpRequest request = exchange.getRequest();
System.out.println("進入了自定義網(wǎng)關(guān)過濾器MyGatewayFilterFactory,status:"+config.getStatus());
if(request.getQueryParams().containsKey("atguigu")){
return chain.filter(exchange);
}else{
exchange.getResponse().setStatusCode(HttpStatus.BAD_REQUEST);
return exchange.getResponse().setComplete();
}
}
};
}
@Override
public List
return Arrays.asList("status");
}
public static class Config
{
@Getter@Setter
private String status;//設(shè)定一個狀態(tài)值/標志位,它等于多少,匹配和才可以訪問
}
}
//單一內(nèi)置過濾器GatewayFilter
yml:
spring:
cloud:
gateway:
routes:
- id: gateway_1
uri: lb://cloud-payment-service
predicates:
- Path=/pay/gateway/filter/**
filters:
- My=xixi
訪問http://localhost:9527/pay/gateway/filter?xixi=dabai 通
全局默認過濾器
當請求與路由匹配時,篩選 Web 處理程序會 GatewayFilter 將 的所有 GlobalFilter 實例和所有特定于路由的實例添加到篩選器鏈中。
由于Spring Cloud Gateway區(qū)分了過濾器邏輯執(zhí)行的“pre”和“post”階段(請參閱工作原理),因此優(yōu)先級最高的過濾器是“pre”階段的第一個,最后一個是“post”階段的最后一個。
官網(wǎng)代碼
自定義全局過濾器實現(xiàn)統(tǒng)計服務(wù)接口請求耗時
@Component
@Slf4j
public class MyGlobalFilter implements GlobalFilter, Ordered
{
/**
* 數(shù)字越小優(yōu)先級越高
* @return
*/
@Override
public int getOrder()
{
return 0;
}
private static final String BEGIN_VISIT_TIME = "begin_visit_time";//開始訪問時間
/**
*第2版,各種統(tǒng)計
* @param exchange
* @param chain
* @return
*/
@Override
public Mono
//先記錄下訪問接口的開始時間
exchange.getAttributes().put(BEGIN_VISIT_TIME, System.currentTimeMillis());
return chain.filter(exchange).then(Mono.fromRunnable(()->{
Long beginVisitTime = exchange.getAttribute(BEGIN_VISIT_TIME);
if (beginVisitTime != null){
log.info("訪問接口主機: " + exchange.getRequest().getURI().getHost());
log.info("訪問接口端口: " + exchange.getRequest().getURI().getPort());
log.info("訪問接口URL: " + exchange.getRequest().getURI().getPath());
log.info("訪問接口URL參數(shù): " + exchange.getRequest().getURI().getRawQuery());
log.info("訪問接口時長: " + (System.currentTimeMillis() - beginVisitTime) + "ms");
log.info("我是美麗分割線: ###################################################");
System.out.println();
}
}));
}
}
效果圖
相關(guān)文獻
官網(wǎng):https://docs.spring.io/spring-cloud-gateway/reference/index.html
就先說到這
\color{#008B8B}{ 就先說到這}
就先說到這
在下
A
p
o
l
l
o
\color{#008B8B}{在下Apollo}
在下Apollo
一個愛分享
J
a
v
a
、生活的小人物,
\color{#008B8B}{一個愛分享Java、生活的小人物,}
一個愛分享Java、生活的小人物,
咱們來日方長,有緣江湖再見,告辭!
\color{#008B8B}{咱們來日方長,有緣江湖再見,告辭!}
咱們來日方長,有緣江湖再見,告辭!
柚子快報激活碼778899分享:GateWay服務(wù)網(wǎng)關(guān)
參考文章
本文內(nèi)容根據(jù)網(wǎng)絡(luò)資料整理,出于傳遞更多信息之目的,不代表金鑰匙跨境贊同其觀點和立場。
轉(zhuǎn)載請注明,如有侵權(quán),聯(lián)系刪除。