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 处理程序,此处理程序运行特 ...
随机推荐
- PAT B1021 个位数统计 (15)
AC代码 #include <cstdio> #include <iostream> #include <cstring> using namespace std; ...
- Codeforces 1156F Card Bag(概率DP)
设dp[i][j]表示选到了第i张牌,牌号在j之前包括j的概率,cnt[i]表示有i张牌,inv[i]表示i在mod下的逆元,那我们可以考虑转移,dp[i][j]=dp[i-1][j-1]*cnt[j ...
- fatal: refusing to merge unrelated histories问题解决
git中pull或merge时偶尔出现
- AtCoder Beginner Contest 072
这应该是我第二次打AtCoder, 题目其实并不难,就是自己经验不足想复杂了,再加上自己很笨,愣是做了97分钟才全做出来(最后三分钟,有点小激动..),看着前面大牛半个小时都搞完了,真心膜拜一下,代码 ...
- MySQL中的索引优化
MySQL中的SQL的常见优化策略 MySQL中的索引优化 MySQL中的索引简介 过多的使用索引将会造成滥用.因此索引也会有它的缺点.虽然索引大大提高了查询速度,同时却会降低更新表的速度,如对表进行 ...
- POJ 3249 Test for Job (拓扑排序+DP)
POJ 3249 Test for Job (拓扑排序+DP) <题目链接> 题目大意: 给定一个有向图(图不一定连通),每个点都有点权(可能为负),让你求出从源点走向汇点的路径上的最大点 ...
- java集合之 ConcurrentHashMap的产生
ConcurrentHashMap: 在java集合中 最常用的是ArrayList 效率最高的还是HashMap 但是线程不安全 HashTable是线程安全的(里面的方法是同步方法) 但相比 ...
- .net core 调用webservice
原文:.net core 调用webservice 1.点击core项目添加链接的服务 2.键入对应的webservice地址,下载对应的代理服务 4.由于.net core 代理类只支持异步方法 ...
- luogu P4548 [CTSC2006]歌唱王国
传送门 这题\(\mathrm{YMD}\)去年就讲了,然而我今年才做(捂脸) 考虑生成函数,设\(f_i\)表示最终串长为\(i\)的概率,其概率生成函数为\(F(x)=\sum f_ix^i\), ...
- scala学习笔记(1)
scala ------------------------- java语言脚本化 1.安装scala-2.12.1.msi 2.进入到scala的命令行 3.Tab键会有补全的功能 1.scala程 ...