关于springcloud Gateway中的限流

栏目: Java · 发布时间: 6年前

内容简介:我们在开发系统的时候可能会对系统进行限流的需求, springcloudGateway有自带限流的方案,在此之前可以先去学习一下springcloud gateway中的filter。springcloud Gateway中提供了一个RequestRateLimiterGatewayFilterFactory。这种限流方式用到了redis, 先添加redis的依赖。

我们在开发系统的时候可能会对系统进行限流的需求, springcloudGateway有自带限流的方案,在此之前可以先去学习一下springcloud gateway中的filter。

springcloud Gateway中提供了一个RequestRateLimiterGatewayFilterFactory。

这种限流方式用到了redis, 先添加 redis 的依赖。

配置类如下:

public class RemoteAddrKeyResolver implements KeyResolver {

    public static final String BEAN_NAME = "remoteAddrKeyResolver";

    @Override
    public Mono<String> resolve(ServerWebExchange exchange) {
        System.out.println("hello");
        Mono<String> just = Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
        return just;
    }
}
@Configuration
public class RemoteKeyResolver {

    @Bean(name="remoteAddrKeyResolver")
    public RemoteAddrKeyResolver remoteAddrKeyResolver() {
        return new RemoteAddrKeyResolver();
    }
}

在此我们是根据ip地址限流的, Mono<String> just = Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress())

application.yml配置文件配置如下:

spring:
  application:
    name: gateway-service
#  redis:
#    database: 0
#    host: localhost
#    port: 6379
#    password:
#    timeout: 1000ms
#    lettuce:
#      pool:
#        max-active: 8
#        max-idle: 8
#        min-idle: 1
#        max-wait: 1000ms
#  cache:
#    type: REDIS
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true
      routes:
        - id: order
          uri: lb://order-service
          predicates:
          - Path=/api/order-service/**
          filters:
          - StripPrefix=1
          - name: RequestRateLimiter
            args:
              key-resolver: '#{@remoteAddrKeyResolver}'
              redis-rate-limiter.replenishRate: 1
              redis-rate-limiter.burstCapacity: 2

上面贴出的是完整的springcloud Gateway的配置, 主要配置为filters下的-RequestRateLimiter,

key-resolver表示使用名为remoteAddKeyResolver的限流配置配置类,此限流方式采用的是令牌桶算法的限流方式

redis-rate-limiter.repleushRate :令牌桶每秒填充平均速率。

redis-rate-limiter.burstCapacity: 令牌桶的总容量


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

复杂性思考

复杂性思考

Allen B. Downey / 张龙 / 机械工业出版社 / 2013-5 / 49.00元

本书的灵感来源于无聊与迷恋的感觉:对常规的数据结构与算法介绍的无聊,对复杂系统的迷恋。数据结构的问题在于教师在教授这门课程的时候通常不会调动起学生的积极性;复杂性科学的问题在于学校通常不会教授这门课程。 2005年,我在欧林学院讲授了一门新课程,学生要阅读关于复杂性的主题,使用Python进行实验,并学习算法与数据结构。当我在2008年再次讲授这门课程时,我写了本书的初稿。 在2011......一起来看看 《复杂性思考》 这本书的介绍吧!

CSS 压缩/解压工具
CSS 压缩/解压工具

在线压缩/解压 CSS 代码

随机密码生成器
随机密码生成器

多种字符组合密码

HSV CMYK 转换工具
HSV CMYK 转换工具

HSV CMYK互换工具