纯洁的微笑的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 个值。