純潔的微笑的Spring Cloud系列博客終於學完了,也對Spring Cloud有了初步的了解。
修改請求路徑的過濾器
StripPrefix Filter 是一個請求路徑截取的功能,我們可以利用這個功能來做特殊業務的轉發。
- id: StripPrefix
uri: http://www.cnblogs.com
predicates:
- Path=/name/**
filters:
- StripPrefix=2
StripPrefix是當請求路徑匹配到/name/**會將包含name和后邊的字符串接去掉轉發, StripPrefix=2就代表截取路徑的個數,當訪問http://localhost:8081/name/aa/5ishare時會跳轉到https://www.cnblogs.com/5ishare頁面。
PrefixPath Filter 的作用和 StripPrefix 正相反,是在 URL 路徑前面添加一部分的前綴。
- id: prefixpath_route
uri: http://www.cnblogs.com
predicates:
- Method=GET
filters:
- PrefixPath=/5ishare
在瀏覽器輸入http://localhost:8081/p/11831586.html 時頁面會跳轉到 https://www.cnblogs.com/5ishare/p/11831586.html。
限速路由器
限速在高並發場景中比較常用的手段之一,可以有效的保障服務的整體穩定性,Spring Cloud Gateway 提供了基於 Redis 的限流方案。所以我們首先需要添加對應的依賴包spring-boot-starter-data-redis-reactive。
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis-reactive</artifactId> <version>2.0.4.RELEASE</version> </dependency>
配置文件中需要添加 Redis 地址和限流的相關配置

server:
port: 8081
eureka:
client:
service-url:
defaultZone: http://localhost:8088/eureka/
logging:
level:
org.springframework.cloud.gateway: debug
spring:
application:
name: SpringCloudGatewayDemo
redis:
host: localhost
password:
port: 6379
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: requestratelimiter_route
uri: http://example.org
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 20
key-resolver:"#{@userKeyResolver}"
predicates:
- Method=GET
filter 名稱必須是 RequestRateLimiter
redis-rate-limiter.replenishRate:允許用戶每秒處理多少個請求
redis-rate-limiter.burstCapacity:令牌桶的容量,允許在一秒鍾內完成的最大請求數
key-resolver:使用 SpEL 按名稱引用 bean
項目中設置限流的策略,創建 Config 類。根據請求參數中的 user 字段來限流,也可以設置根據請求 IP 地址來限流,設置如下:

package com.example.demo; import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver; import org.springframework.context.annotation.Bean; import reactor.core.publisher.Mono; public class Config { @Bean public KeyResolver ipKeyResolver() { return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName()); } @Bean KeyResolver userKeyResolver() { return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user")); } }
熔斷路由器
Spring Cloud Gateway 也可以利用 Hystrix 的熔斷特性,在流量過大時進行服務降級,同樣我們還是首先給項目添加上依賴。
<dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-starter-netflix-hystrix</artifactId> <version>2.1.3.RELEASE</version> </dependency>
- id: hystrix_route
uri: lb://spring-cloud-producer
predicates:
- Path=/consumingserviceendpoint
filters:
- name: Hystrix
args:
name: fallbackcmd
fallbackUri: forward:/incaseoffailureusethis
fallbackUri: forward:/incaseoffailureusethis配置了 fallback 時要會調的路徑,當調用 Hystrix 的 fallback 被調用時,請求將轉發到/incaseoffailureuset這個 URI。
重試路由器
RetryGatewayFilter 是 Spring Cloud Gateway 對請求重試提供的一個 GatewayFilter Factory
- id: retry_test
uri: lb://spring-cloud-producer
predicates:
- Path=/retry
filters:
- name: Retry
args:
retries: 3
statuses: BAD_GATEWAY
retries:重試次數,默認值是 3 次
statuses:HTTP 的狀態返回碼,取值請參考:org.springframework.http.HttpStatus
methods:指定哪些方法的請求需要進行重試邏輯,默認值是 GET 方法,取值參考:org.springframework.http.HttpMethod
series:一些列的狀態碼配置,取值參考:org.springframework.http.HttpStatus.Series。符合的某段狀態碼才會進行重試邏輯,默認值是 SERVER_ERROR,值是 5,也就是 5XX(5 開頭的狀態碼),共有5 個值。