一:限速路由器的过滤器

1.1 限速在高并发场景中比较常用的手段之一,可以有效的保障服务的整体稳定性,Spring Cloud Gateway 提供了基于 Redis 的限流方案。
所以我们首先需要添加对应的依赖包spring-boot-starter-data-redis-reactive

1.2 修改 pom.xml 文件,代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"><modelVersion>4.0.0</modelVersion><parent><groupId>com.springcloud</groupId><artifactId>springcloud-hx</artifactId><version>1.0-SNAPSHOT</version><relativePath/> <!-- lookup parent from repository --></parent><groupId>com.example</groupId><artifactId>eureka-gateway-client</artifactId><version>0.0.1-SNAPSHOT</version><name>eureka-gateway-client</name><description>Demo project for Spring Boot</description><properties><java.version>1.8</java.version></properties><dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-dependencies</artifactId><version>Greenwich.RELEASE</version><type>pom</type><scope>import</scope></dependency></dependencies></dependencyManagement><dependencies><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-boot-starter-data-redis-reactive</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-gateway</artifactId></dependency><dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-cloud-starter-netflix-eureka-client</artifactId></dependency><!--<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency>--><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-test</artifactId><scope>test</scope></dependency></dependencies><build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId></plugin></plugins></build></project>

增加了spring-boot-starter-data-redis-reactive的依赖

        <dependency><groupId>org.springframework.cloud</groupId><artifactId>spring-boot-starter-data-redis-reactive</artifactId></dependency>

1.3 修改 application-predicate-mehtod.yml 文件,需要添加 Redis 地址和限流的相关配置,代码如下:

server:port: 8769#---         #三个横线表示再创建一个配置文件
spring:#profiles: predicate-method #配置文件名 和 spring.profiles.active 相对应#配置程序名为eureka-gateway-clientapplication:name: eureka-gateway-client#redis 配置  redis:host: localhostpassword: 123456port: 6379  cloud:#设置路由规则gateway:discovery:locator:#是否与服务注册于发现组件进行结合,通过 serviceId 转发到具体的服务实例。#默认为 false,设为 true 便开启通过服务中心的自动根据 serviceId 创建路由的功能enabled: true##表示将请求路径的服务名配置改成小写  因为服务注册的时候,向注册中心注册时将服务名转成大写的了lower-case-service-id: trueroutes:#我们自定义的路由 ID,保持唯一性- id: predicate_path#代表从注册中心获取服务,且以lb(load-balance)负载均衡方式转发uri: lb://eureka-client/#uri: http://localhost:8762#断言predicates:#表示GET请求,都会被路由到uri- Method=GETfilters:- name: RequestRateLimiterargs:redis-rate-limiter.replenishRate: 10redis-rate-limiter.burstCapacity: 20key-resolver: '#{@ipKeyResolver}'# 配置了RequestRateLimiter的限流过滤器,该过滤器需要配置三个参数replenishRate,burstCapacity,key-resolver# redis-rate-limiter.replenishRate:允许用户每秒处理多少个请求# redis-rate-limiter.burstCapacity:令牌桶的容量,允许在一秒钟内完成的最大请求数# key-resolver:使用 SpEL 按名称引用 bean (RateLimiterConfig 类中的bean)
logging:level:org.springframework.cloud.gateway: debugeureka:client:#服务注册地址serviceUrl:#注意: Eureka Server 的注册地址#将服务提供者注册到三个Eureka Server中去#defaultZone: http://peer1:8001/eureka/,http://peer2:8002/eureka/,http://peer3:8003/eureka/#defaultZone: http://peer1:8001/eureka/defaultZone: http://localhost:8761/eureka/

增加了这部分的代码

  1. filter 名称必须是 RequestRateLimiter
  2. redis-rate-limiter.replenishRate:允许用户每秒处理多少个请求
  3. redis-rate-limiter.burstCapacity:令牌桶的容量,允许在一秒钟内完成的最大请求数
  4. key-resolver:使用 SpEL 按名称引用 bean

1.4 在eureka-gateway-client 项目中设置限流的策略,创建 RateLimiterConfig 类。

package com.example.eurekagatewayclient.config;import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;@Configuration
public class RateLimiterConfig {/*** 根据请求参数中的 user 字段来限流** @return*//*@Beanpublic KeyResolver userKeyResolver() {return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("userId"));}*//*** 获取请求地址的uri作为限流key。* @return*//*@BeanKeyResolver apiKeyResolver() {return exchange -> Mono.just(exchange.getRequest().getPath().toString());}*//*** 根据请求 IP 地址来限流** @return*/@Beanpublic KeyResolver ipKeyResolver() {System.out.println("##############ipKeyResolver########################");return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());}
}

这样网关就可以根据不同策略来对请求进行限流了。

1.5 启动 eureka-serve, eureka-client (8762,8763 端口),eureka-gateway-client 服务,浏览器访问 http://localhost:8761/

用jmeter进行压测,配置10thread去循环请求lcoalhost:8769/HiController/aaa,循环间隔1s。从压测的结果上看到有部分请求通过,由部分请求失败。通过redis客户端去查看redis中存在的key。如下:

从结果中可以看出 filter 生效了。

SpringCloud Gateway 限速路由器的过滤器相关推荐

  1. SpringCloud Gateway 重试路由器的过滤器

    一:重试路由器的过滤器RetryGatewayFilter 1.1 RetryGatewayFilter 是 Spring Cloud Gateway 对请求重试提供的一个 GatewayFilter ...

  2. springcloud gateway 使用nacos 动态过滤器 记一次线上网关升级cpu升高的问题

    大家好,我是烤鸭: ​ 网关升级,想使用 springcloud gateway nacos 动态过滤器配置(原来是硬编码的方式),升级之后出了一些问题(cpu升高,ygc频繁),记录一下. 关于 s ...

  3. SpringCloud Gateway 服务网关,过滤器

    SpringCloud Gateway 过滤器有 pre 和 post 两种方式,请求首先经过 pre 过滤器处理,成功后被转发到对应的服务,然后经过 post 过滤器处理,将结果返回客户端.这种机制 ...

  4. SpringCloud(五)- GateWay简介及GlobalFilter 过滤器的使用

    唯能极于情,故能极于剑 本文转载于:http://www.codecow.cn/ 此文由四部分组成(GateWay简介.GlobalFilter过滤器 使用.实操.debug测试.总结),别着急,慢慢 ...

  5. 使用springcloud gateway搭建网关(分流,限流,熔断)

    Spring Cloud Gateway Spring Cloud Gateway 是 Spring Cloud 的一个全新项目,该项目是基于 Spring 5.0,Spring Boot 2.0 和 ...

  6. SpringCloud Gateway的工作方式

    SpringCloud Gateway工作方式 从官网的图来看,并不是特别复杂,首先客户端请求都会先经过Gateway Handler Mapping,匹配上就通过Gateway Web Handle ...

  7. SpringCloud Gateway的组成结构

    SpringCloud Gateway结构 SpringCloud Gateway的底层基于Netty,主要组成有Predicates(谓词或者断言).Route(路由).Filter(过滤器) 思维 ...

  8. springcloud gateway 自定义 accesslog elk

    大家好,我是烤鸭: ​ 最近用 springcloud gateway 时,想使用类似 logback-access的功能,用来做数据统计和图表绘制等等,发现没有类似的功能,只能自己开发了. 环境: ...

  9. 关于 springcloud gateway 设置 context-path 的问题

    大家好,我是烤鸭: 今天说一下遇到的问题,关于 springcloud gateway 设置 context-path 的问题. 1.  使用场景 由于没有申请二级域名,网关使用的地址是 xxx.co ...

最新文章

  1. Cacti on nginx + php-fpm
  2. Android Scroller 使用详解
  3. 数制系统之间的转换总结(各进制的转换)
  4. Ext.grid.Panel表格分页
  5. 在php中单引号和双引号的区别错误的是___________,浅谈PHP中单引号和双引号到底有啥区别呢?...
  6. VS2008+Windows DDK 7的环境配置(二)
  7. tcpwrapper的使用方法
  8. 基于Cocos2dx开发卡牌游戏Demo_放开那三国 2.0
  9. 【洛谷p1464】 Function
  10. linux nginx 系统服务,linux 把nginx加入到系统服务的方法
  11. final、finally、finalize差异
  12. 取消xp开机默认登陆账户
  13. 递归法:求两个串的最大公共子序列的长度
  14. 使用maven编译打包用javac还是eclipse的jdt的问题
  15. 计算机职业倾向自我评价50字,自我评价50字
  16. AUTO.JS脚本 实现小米、淘宝、京东抢购
  17. 大数据专业学起来会不会很累?
  18. java mina性能_高性能Java网络框架 MINA
  19. 2020-02-25
  20. 4K技术频抢戏 OLED电视现身更待何时

热门文章

  1. Dubbo:Zookeeper安装+Dubbo管理控制台部署
  2. html网页图片和文字水平居中垂直居中显示
  3. oracle创建数据库命令
  4. JavaScript异步加载,解决js阻塞
  5. 现在做游戏开发这个行业怎么样?
  6. x10max能升级鸿蒙系统吗,3部能直接升级鸿蒙2.0的荣耀旗舰,第一没有意外,第三实力最强...
  7. 10道关于垒球规则的选择题·你答对了多少
  8. Linux调试之(三)addr2line+vmlinux
  9. FFmpeg+SDL2开发播放器遇到问题
  10. 从Word生成有“导航窗格”的PDF(举例说明)