Spring Cloud Gateway 內置Filter
Spring Cloud Gateway中內置了很多過濾器,實現類有二十多個;
分類幾類:
AddRequestHeader
給請求加上一條header信息;
spring:
cloud:
gateway:
routes:
- id: add_request_header_route
uri: https://example.org
filters:
- AddRequestHeader=X-Request-red, blue
AddRequestParameter
給請求加上Paramter參數
spring:
cloud:
gateway:
routes:
- id: add_request_parameter_route
uri: https://example.org
filters:
- AddRequestParameter=key,value
RewritePath
Spring Cloud Gateway 的RewritePath可以替換Zuul的StripPrefix;
修改轉發的路徑
spring:
cloud:
gateway:
routes:
- id: rewritepath_route
uri: https://example.org
predicates:
- Path=/red/**
filters:
- RewritePath=/red(?<segment>/?.*), $\{segment}
AddResponseHeader
對網關的響應添加Header
spring:
cloud:
gateway:
routes:
- id: add_response_header_route
uri: https://example.org
filters:
- AddResponseHeader=X-Response-Red, Blue
StripPrefix
用於去除url的前綴
spring:
application:
name: sc-gateway-server
cloud:
gateway:
discovery:
locator:
enabled: false
lowerCaseServiceId: true
routes:
- id: service-hi
uri: lb://SERVICE-HI
predicates:
- Path=/demo/**
- Between=2017-01-20T17:42:47.789-07:00[America/Denver], 2021-01-21T17:42:47.789-07:00[America/Denver]
- Header=key,value
- Method=GET,POST
# - Cookie=mycookie,mycookievalue
# - After=2017-01-20T17:42:47.789-07:00[America/Denver]
# - Before=2020-01-20T17:42:47.789-07:00[America/Denver]
filters:
- StripPrefix=1
@Bean
public RouteLocator customerRouteLocator(RouteLocatorBuilder builder) {
return builder.routes()
.route(r -> r.path("/cpu/**")
.filters(f -> f.filter(gatewayRateLimitFilterByCpu).stripPrefix(1))
.uri("lb://SERVICE-HI")
.id("rateLimit_route11111")
)
.build();
}
PrefixPath
用於添加url前綴
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: https://example.org
filters:
- PrefixPath=/mypath
Retry
請求出現異常是進行重試
參數:
- retries:應嘗試的重試次數。
- statuses:應重試的HTTP狀態代碼,以表示org.springframework.http.HttpStatus。
- methods:應該重試的HTTP方法,以表示org.springframework.http.HttpMethod。
- series:要重試的一系列狀態代碼,使用表示org.springframework.http.HttpStatus.Series。
- exceptions:應重試的引發異常的列表。
- backoff:為重試配置的指數補償。重試在的退避間隔后執行firstBackoff * (factor ^ n),其中n為迭代。如果maxBackoff已配置,則應用的最大退避限制為maxBackoff。如果basedOnPreviousValue為true,則使用計算退避prevBackoff * factor。
spring:
cloud:
gateway:
routes:
- id: retry_test
uri: http://localhost:8080/flakey
predicates:
- Host=*.retry.com
filters:
- name: Retry
args:
retries: 3
statuses: BAD_GATEWAY
methods: GET,POST
backoff:
firstBackoff: 10ms
maxBackoff: 50ms
factor: 2
basedOnPreviousValue: false
Hystryix
進行服務熔斷 降級
spring:
cloud:
gateway:
routes:
- id: hystrix_route
uri: https://example.org
filters:
- Hystrix=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。請注意,此示例還演示了(可選)Spring Cloud Netflix Ribbon負載平衡(lb在目標URI上定義了前綴)。
主要方案是對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
要為前面顯示的示例路由設置五秒鍾的超時時間,可以使用以下配置:
hystrix.command.fallbackcmd.execution.isolation.thread.timeoutInMilliseconds: 5000