上一篇咱們講述了gateway 的路由功能其實也相似與zuul服務的路由轉發。
今天主要講一下斷言機制。html
能夠看到gateway 提供如此之多豐富的斷言,方式。react
具體使用還得參照業務場景來選擇更適合咱們的業務場景。git
gateway 已經給咱們提供了很是豐富的過濾器github
AddRequestHeader GatewayFilter工廠採用名稱和值參數。web
這會將X-Request-Foo:Bar標頭添加到全部匹配請求的下游請求的標頭中。 spring: cloud: gateway: routes: - id: add_request_header_route uri: https://example.org filters: - AddRequestHeader=X-Request-Foo, Bar AddRequestHeader知道用於匹配路徑或主機的URI變量。URI變量可用於該值,並將在運行時擴展。 spring: cloud: gateway: routes: - id: add_request_header_route uri: https://example.org predicates: - Path=/foo/{segment} filters: - AddRequestHeader=X-Request-Foo, Bar-{segment}
-AddResponseHeader GatewayFilter工廠採用名稱和值參數。正則表達式
spring: cloud: gateway: routes: - id: add_request_parameter_route uri: https://example.org filters: - AddRequestParameter=foo, bar
這將添加foo=bar到全部匹配請求的下游請求的查詢字符串中。redis
AddRequestParameter知道用於匹配路徑或主機的URI變量。URI變量可用於該值,並將在運行時擴展。spring
spring: cloud: gateway: routes: - id: add_request_parameter_route uri: https://example.org predicates: - Host: {segment}.myhost.org filters: - AddRequestParameter=foo, bar-{segment}
AddResponseHeader GatewayFilter工廠採用名稱和值參數。apache
spring:
cloud:
gateway:
routes:
- id: add_response_header_route
uri: https://example.org
filters:
- AddResponseHeader=X-Response-Foo, Barbootstrap
這會將X-Response-Foo:Bar標頭添加到全部匹配請求的下游響應的標頭中。
AddResponseHeader知道用於匹配路徑或主機的URI變量。URI變量可用於該值,並將在運行時擴展。
spring:
cloud:
gateway:
routes:
- id: add_response_header_route
uri: https://example.org
predicates:
- Host: {segment}.myhost.org
filters:
- AddResponseHeader=foo, bar-{segment}
DedupeResponseHeader GatewayFilter工廠採用一個name參數和一個可選strategy參數。name能夠包含標題名稱列表,以空格分隔。
spring:
cloud:
gateway:
routes:
- id: dedupe_response_header_route
uri: https://example.org
filters:
- DedupeResponseHeader=Access-Control-Allow-Credentials Access-Control-Allow-Origin
若是網關CORS邏輯和下游邏輯都添加了重複的值Access-Control-Allow-Credentials和Access-Control-Allow-Origin響應標頭,則這將刪除它們。
DedupeResponseHeader過濾器還接受可選strategy參數。可接受的值爲RETAIN_FIRST(默認值)RETAIN_LAST,和RETAIN_UNIQUE
Hystrix是Netflix的一個庫,用於實現斷路器模式。Hystrix GatewayFilter容許您將斷路器引入網關路由,保護您的服務免受級聯故障的影響,並容許您在下游故障的狀況下提供後備響應
要在項目中啓用Hystrix GatewayFilters,請spring-cloud-starter-netflix-hystrix從Spring Cloud Netflix添加依賴項。
Hystrix GatewayFilter工廠須要一個name參數,它是的名稱HystrixCommand。
spring:
cloud:
gateway:
routes:
- id: hystrix_route
uri: https://example.org
filters:
- Hystrix=myCommandName
這會將其他的過濾器包裝在HystrixCommand帶有命令名的中myCommandName。
Hystrix過濾器還能夠接受可選fallbackUri參數。當前,僅forward:支持計劃的URI。若是調用了後備,則請求將被轉發到與URI相匹配的控制器。
spring: cloud: gateway: routes: - id: hystrix_route uri: lb://backing-service:8088 predicates: - Path=/consumingserviceendpoint filters: - name: Hystrix args: name: fallbackcmd fallbackUri: forward:/incaseoffailureusethis - RewritePath=/consumingserviceendpoint, /backingserviceendpoint
/incaseoffailureusethis調用Hystrix後備時,它將轉發到URI。請注意,此示例還經過lb目標URI 上的前綴演示了(可選)Spring Cloud Netflix Ribbon負載平衡。
主要方案是對fallbackUri網關應用程序中的內部控制器或處理程序使用。可是,也能夠將請求從新路由到外部應用程序中的控制器或處理程序,以下所示:
spring: cloud: gateway: routes: - id: ingredients uri: lb://ingredients predicates: - Path=//ingredients/** filters: - name: Hystrix args: name: fetchIngredients fallbackUri: forward:/fallback - id: ingredients-fallback uri: http://localhost:9994 predicates: - Path=/fallback
在此示例中,fallback網關應用程序中沒有終結點或處理程序,可是另外一個應用程序中有一個終結點或處理程序,在下注冊localhost:9994。
若是將請求轉發給後備,則Hystrix網關過濾器還會提供Throwable引發請求的。它已ServerWebExchange做爲 ServerWebExchangeUtils.HYSTRIX_EXECUTION_EXCEPTION_ATTR屬性添加到,能夠在網關應用程序中處理後備時使用。
對於外部控制器/處理程序方案,能夠添加帶有異常詳細信息的標頭。您能夠在FallbackHeaders GatewayFilter Factory部分中找到有關它的更多信息。
Hystrix設置(例如超時)可使用全局默認值配置,也可使用Hystrix Wiki上說明的應用程序屬性在逐條路由的基礎上進行配置。
要爲上述示例路由設置5秒超時,將使用如下配置:
hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds: 5000
在此示例中,在運行時發生執行異常後HystrixCommand,該請求將轉發到在上fallback運行的應用中的端點或處理程序localhost:9994。具備異常類型,消息和-if available-根本緣由異常類型和消息的標頭將由FallbackHeaders過濾器添加到該請求。
經過設置下面列出的參數的值及其默認值,能夠在配置中覆蓋標頭的名稱:
- executionExceptionTypeHeaderName("Execution-Exception-Type") - executionExceptionMessageHeaderName("Execution-Exception-Message") - rootCauseExceptionTypeHeaderName("Root-Cause-Exception-Type") - rootCauseExceptionMessageHeaderName("Root-Cause-Exception-Message")
您能夠在Hystrix GatewayFilter Factory部分中找到有關Hystrix如何與Gateway一塊兒工做的更多信息。
MapRequestHeader GatewayFilter工廠採用'fromHeader'和'toHeader'參數。它建立一個新的命名標頭(toHeader),並從傳入的HTTP請求中從現有的命名標頭(fromHeader)中提取值。若是輸入標頭不存在,則過濾器不起做用。若是新的命名標頭已經存在,則其值將使用新值進行擴充。
spring: cloud: gateway: routes: - id: map_request_header_route uri: https://example.org filters: - MapRequestHeader=Bar, X-Request-Foo
這會將X-Request-Foo:
這將/mypath做爲全部匹配請求的路徑的前綴。所以,對的請求/hello將發送給/mypath/hello。
PreserveHostHeader GatewayFilter工廠沒有參數。此過濾器設置請求屬性,路由過濾器將檢查該請求屬性,以肯定是否應發送原始主機頭,而不是由HTTP客戶端肯定的主機頭。
spring: cloud: gateway: routes: - id: preserve_host_route uri: https://example.org filters: - PreserveHostHeader
RequestRateLimiter GatewayFilter Factory使用一種RateLimiter實現來肯定是否容許繼續當前請求。若是不是,HTTP 429 - Too Many Requests則返回狀態(默認)。此過濾器採用一個可選keyResolver參數和特定於速率限制器的參數。
全局過濾器和GatewayFilter的組合訂購
由於spring cloud gateway 提供的內置過濾器太多了。不在這裏一一介紹
能夠查看官方的文檔 進行了解學習
https://cloud.spring.io/spring-cloud-gateway/reference/html/
接下來說一下,全局過濾器GlobalFilter接口。
能夠看一下默認的實現的全局過濾器 ,除去AuthorizeFilter過濾器都是默認的過濾器
具體的裏面的做用,其實上面的已經有了簡單的描述不在複述。有興趣的同窗能夠看看裏面的實現,都是利用過濾器作轉發或者一些對流量請求的修改、鑑權、等操做
一、pom引入spring-boot-starter-actuator 。由於以前就直接在parent pom 進行了引入操做。再也不次引入
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency>
二、配置文件bootstrap.yml中開啓監控管理端點
management: endpoints: web: exposure: include: "*" endpoint: health: show-details: ALWAYS
三、請求瀏覽器 http://localhost:9000/actuator/gateway/globalfilters
{ org.springframework.cloud.gateway.filter.AdaptCachedBodyGlobalFilter@4b957db0: -2147482648, org.springframework.cloud.gateway.filter.WebsocketRoutingFilter@273fa9e: 2147483646, com.xian.cloud.filter.AuthorizeFilter@4b03cbad: 0, org.springframework.cloud.gateway.filter.ForwardRoutingFilter@8840c98: 2147483647, org.springframework.cloud.gateway.filter.NettyRoutingFilter@5c313224: 2147483647, org.springframework.cloud.gateway.filter.NettyWriteResponseFilter@1e1e837d: -1, org.springframework.cloud.gateway.filter.RouteToRequestUrlFilter@5d71b500: 10000, org.springframework.cloud.gateway.filter.LoadBalancerClientFilter@5b29ab61: 10100, org.springframework.cloud.gateway.filter.GatewayMetricsFilter@527a8665: -2147473648, org.springframework.cloud.gateway.filter.ForwardPathFilter@626b639e: 0 }
觀察這些實現類。都是實現 GlobalFilter、Ordered倆個接口
實現本身的全局過濾器
建立 AuthorizeFilter
package com.xian.cloud.filter; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; import org.springframework.cloud.gateway.filter.GatewayFilterChain; import org.springframework.cloud.gateway.filter.GlobalFilter; import org.springframework.core.Ordered; import org.springframework.http.HttpHeaders; import org.springframework.http.server.reactive.ServerHttpRequest; import org.springframework.stereotype.Component; import org.springframework.web.server.ServerWebExchange; import reactor.core.publisher.Mono; /** * <Description> * * @author xianliru@100tal.com * @version 1.0 * @createDate 2019/11/04 18:06 */ @Component @Slf4j public class AuthorizeFilter implements GlobalFilter, Ordered { private static final String AUTHORIZE_TOKEN = "Authorization"; private static final String AUTHORIZE_UID = "uid"; @Override public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) { ServerHttpRequest request = exchange.getRequest(); HttpHeaders headers = request.getHeaders(); ServerHttpRequest.Builder mutate = request.mutate(); String token = headers.getFirst( AUTHORIZE_TOKEN ); String uid = headers.getFirst( AUTHORIZE_UID ); String method = request.getMethodValue(); log.info( "AuthorizeFilter token 全局過濾器 token:{},uid:{}",token,uid ); if (token == null) { token = request.getQueryParams().getFirst( AUTHORIZE_TOKEN ); } if(StringUtils.isNotBlank(token)){ //TODO 權限驗證 } return chain.filter( exchange ); } @Override public int getOrder() { return 0; } }
而後啓動服務。
curl http://localhost:9000/client/client/test
日誌打印
[2019-11-05 19:30:52.802] [INFO ] com.xian.cloud.filter.AuthorizeFilter - AuthorizeFilter token 全局過濾器 token:null,uid:null
下一篇咱們將介紹定製的過濾器,針對下游服務對應過濾器
摘自參考 spring cloud 官方文檔
服務器nacos 地址 http://47.99.209.72:8848/nacos
往期地址 spring cloud alibaba 地址
Spring Cloud Alibaba (nacos 註冊中心搭建)
Spring Cloud Alibaba 使用nacos 註冊中心
Spring Cloud Alibaba nacos 配置中心使用
Spring Cloud alibaba網關 sentinel zuul 四 限流熔斷
如何喜歡能夠關注分享本公衆號。