springcloud(十七):服务网关 Spring Cloud GateWay 熔断、限流、重试
上篇文章介绍了 Gataway 和注册中心的使用,以及 Gataway 中 Filter 的基本使用,这篇文章我们将继续介绍 Filter 的一些常用功能。
修改请求路径的过滤器
StripPrefix Filter
StripPrefix Filter 是一个请求路径截取的功能,我们可以利用这个功能来做特殊业务的转发。
application.yml 配置如下:
spring:
cloud:
gateway:
routes:
- id: nameRoot
uri: http://nameservice
predicates:
- Path=/name/**
filters:
- StripPrefix=2
上面这个配置的例子表示,当请求路径匹配到/name/**
会将包含name和后边的字符串接去掉转发, StripPrefix=2
就代表截取路径的个数,这样配置后当请求/name/bar/foo
后端匹配到的请求路径就会变成http://nameservice/foo
。
我们还是在 cloud-gateway-eureka 项目中进行测试,修改 application.yml 如下:
spring:
cloud:
routes:
- id: nameRoot
uri: lb://spring-cloud-producer
predicates:
- Path=/name/**
filters:
- StripPrefix=2
配置完后重启 cloud-gateway-eureka 项目,访问地址:http://localhost:8888/name/foo/hello
页面会交替显示:
hello world!
hello world smile!
和直接访问地址 http://localhost:8888/hello
展示的效果一致,说明请求路径中的 name/foo/
已经被截取。
PrefixPath Filter
PrefixPath Filter 的作用和 StripPrefix 正相反,是在 URL 路径前面添加一部分的前缀
spring:
cloud:
gateway:
routes:
- id: prefixpath_route
uri: http://example.org
filters:
- PrefixPath=/mypath
大家可以下来去测试,这里不在演示。
限速路由器
限速在高并发场景中比较常用的手段之一,可以有效的保障服务的整体稳定性,Spring Cloud Gateway 提供了基于 Redis 的限流方案。所以我们首先需要添加对应的依赖包spring-boot-starter-data-redis-reactive
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
配置文件中需要添加 Redis 地址和限流的相关配置
spring:
application:
name: cloud-gateway-eureka
redis:
host: localhost
password:
port:
cloud:
gateway:
discovery:
locator:
enabled: true
routes:
- id: requestratelimiter_route
uri: http://example.org
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate:
redis-rate-limiter.burstCapacity:
key-resolver: "#{@userKeyResolver}"
predicates:
- Method=GET
- filter 名称必须是 RequestRateLimiter
- redis-rate-limiter.replenishRate:允许用户每秒处理多少个请求
- redis-rate-limiter.burstCapacity:令牌桶的容量,允许在一秒钟内完成的最大请求数
- key-resolver:使用 SpEL 按名称引用 bean
项目中设置限流的策略,创建 Config 类。
public class Config { @Bean
KeyResolver userKeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
}
}
根据请求参数中的 user 字段来限流,也可以设置根据请求 IP 地址来限流,设置如下:
@Bean
public KeyResolver ipKeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
}
这样网关就可以根据不同策略来对请求进行限流了。
熔断路由器
在之前的 Spring Cloud 系列文章中,大家对熔断应该有了一定的了解,如过不了解可以先读这篇文章:熔断器 Hystrix
Spring Cloud Gateway 也可以利用 Hystrix 的熔断特性,在流量过大时进行服务降级,同样我们还是首先给项目添加上依赖。
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-hystrix</artifactId>
</dependency>
配置示例
spring:
cloud:
gateway:
routes:
- id: hystrix_route
uri: http://example.org
filters:
- Hystrix=myCommandName
配置后,gateway 将使用 myCommandName 作为名称生成 HystrixCommand 对象来进行熔断管理。如果想添加熔断后的回调内容,需要在添加一些配置。
spring:
cloud:
gateway:
routes:
- 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
配置示例:
spring:
cloud:
gateway:
routes:
- id: retry_test
uri: lb://spring-cloud-producer
predicates:
- Path=/retry
filters:
- name: Retry
args:
retries:
statuses: BAD_GATEWAY
Retry GatewayFilter 通过这四个参数来控制重试机制: retries, statuses, methods, 和 series。
- 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 个值。
以上便是项目中常用的一些网关操作,更多关于 Spring Cloud GateWay 的使用请参考官网。、
本文来自:http://www.ityouknow.com/springcloud/2019/01/26/spring-cloud-gateway-limit.html
感谢大神
springcloud(十七):服务网关 Spring Cloud GateWay 熔断、限流、重试的更多相关文章
- 微服务网关 Spring Cloud Gateway
1. 为什么是Spring Cloud Gateway 一句话,Spring Cloud已经放弃Netflix Zuul了.现在Spring Cloud中引用的还是Zuul 1.x版本,而这个版本是 ...
- spring cloud:服务网关 Spring Cloud GateWay 入门
Spring 官方最终还是按捺不住推出了自己的网关组件:Spring Cloud Gateway ,相比之前我们使用的 Zuul(1.x) 它有哪些优势呢?Zuul(1.x) 基于 Servlet,使 ...
- spring cloud gateway 之限流篇
转载请标明出处: https://www.fangzhipeng.com 本文出自方志朋的博客 在高并发的系统中,往往需要在系统中做限流,一方面是为了防止大量的请求使服务器过载,导致服务不可用,另一方 ...
- 深入了解springcloud gateway 的限流重试机制
前言 前面给大家介绍了Spring Cloud Gateway的入门教程,这篇给大家探讨下Spring Cloud Gateway的一些其他功能. Spring Cloud Gateway中的重试 我 ...
- Gateway的限流重试机制详解
前言 想要源码地址的可以加上此微信:Lemon877164954 前面给大家介绍了Spring Cloud Gateway的入门教程,这篇给大家探讨下Spring Cloud Gateway的一些其 ...
- API网关spring cloud gateway和负载均衡框架ribbon实战
通常我们如果有一个服务,会部署到多台服务器上,这些微服务如果都暴露给客户,是非常难以管理的,我们系统需要有一个唯一的出口,API网关是一个服务,是系统的唯一出口.API网关封装了系统内部的微服务,为客 ...
- spring boot gateway自定义限流
参考:https://blog.csdn.net/ErickPang/article/details/84680132 采用自带默认网关请参照微服务架构spring cloud - gateway网关 ...
- Spring Cloud 微服务三: API网关Spring cloud gateway
前言:前面介绍了一款API网关组件zuul,不过发现spring cloud自己开发了一个新网关gateway,貌似要取代zuul,spring官网上也已经没有zuul的组件了(虽然在仓库中可以更新到 ...
- 微服务架构 - 网关 Spring Cloud Gateway
Spring Cloud Gateway 工作原理 客户端向 Spring Cloud Gateway 发出请求,如果请求与网关程序定义的路由匹配,则将其发送到网关 Web 处理程序,此处理程序运行特 ...
随机推荐
- vim编辑Dockerfile时语法高亮
参考Dockerfile构建容器---语法高亮 三个文件扔进相关的目录即可 1. /usr/share/vim/vimfiles/doc/dockerfile.txt *dockerfile.txt* ...
- 實現QQ第三方登錄
<?php // 写几个函数,分别用于获取code,token,openid,用户信息 // 跳转到QQ授权登录页面 function code(){ $response_type='code' ...
- Python基础数据类型int
整型int 赋值运算符 a=1 a+=1 #a=a+1 a-=1 #a=a-1 a*=1 #a=a*1 a/=1 #a=a/1 a**=1 #a=a**1 a%=1 #a=a%1 算数运算符 + - ...
- os.path路径拓展 python3
os.path-对路径path进行的操作 在调用os.path时, 根据操作系统的不同 程序会选择使用posixpath.py或ntpath.py(由os中的代码实现). 对文件命名时应当使用unic ...
- docker之本地连接
docker安装成功,之后我们需要连接进入docker中,这里罗列连接方式 1. Windows7 一般的虚拟ip地址: 192.168.99.100 查看ip地址: 1) C:\Users\thi ...
- VMware三种连接方式bridge, nat, host-only
大家在安装完虚拟机后,默认安装了两个虚拟网卡,VMnet1和 VMnet8,其他的未安装(当然也可以手动安装其他的).其中VMnet1是host网卡,用于host方式连接网络的.VMnet8是NAT网 ...
- Laravel5学习
[1]缓存问题 laravel中使用的配置文件,都会放在config目录下面,当变更某个参数的时候,如果发现不生效,可以在命令行下面执行 php artisan config:clear 命令才清除 ...
- TCP/IP的网络客户端和服务器端程序
服务器端的过程可以分为以下几个步骤: (1) 初始化套接字的版本信息WSAStartup (2)创建套接字 ,需要两个套接字及客户端和服务器端的套接字 (3)绑定服务器(bind),该函数用于绑定服 ...
- JS获取当前日期和时间的方法,并按照YYYY-MM-DD格式化
Js获取当前日期时间及其它操作 var myDate = new Date(); myDate.getYear(); //获取当前年份(2位) myDate.getFullYear(); ...
- 【LeetCode】堆 heap(共31题)
链接:https://leetcode.com/tag/heap/ [23] Merge k Sorted Lists [215] Kth Largest Element in an Array (无 ...