純潔的微笑的Spring Cloud系列博客終於學完了,也對Spring Cloud有了初步的瞭解。html
修改請求路徑的過濾器react
StripPrefix Filter 是一個請求路徑截取的功能,咱們能夠利用這個功能來作特殊業務的轉發。redis
- 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頁面。spring
PrefixPath Filter 的做用和 StripPrefix 正相反,是在 URL 路徑前面添加一部分的前綴。瀏覽器
- id: prefixpath_route
uri: http://www.cnblogs.com
predicates:
- Method=GET
filters:
- PrefixPath=/5ishare
在瀏覽器輸入http://localhost:8081/p/11831586.html 時頁面會跳轉到 http://www.javashuo.com/article/p-yauhqgpv-do.html。併發
限速路由器app
限速在高併發場景中比較經常使用的手段之一,能夠有效的保障服務的總體穩定性,Spring Cloud Gateway 提供了基於 Redis 的限流方案。因此咱們首先須要添加對應的依賴包spring-boot-starter-data-redis-reactive。ide
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-redis-reactive</artifactId> <version>2.0.4.RELEASE</version> </dependency>
配置文件中須要添加 Redis 地址和限流的相關配置spring-boot
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.HttpStatusmethods:指定哪些方法的請求須要進行重試邏輯,默認值是 GET 方法,取值參考:org.springframework.http.HttpMethodseries:一些列的狀態碼配置,取值參考:org.springframework.http.HttpStatus.Series。符合的某段狀態碼纔會進行重試邏輯,默認值是 SERVER_ERROR,值是 5,也就是 5XX(5 開頭的狀態碼),共有5 個值。