• spring cloud gateway


    1. Official website

    The RequestRateLimiter GatewayFilter Factory is uses a RateLimiter implementation to determine if the current request is allowed to proceed. If it is not, a status of HTTP 429 - Too Many Requests (by default) is returned.

    This filter takes an optional keyResolver parameter and parameters specific to the rate limiter (see below).

    keyResolver is a bean that implements the KeyResolver interface. In configuration, reference the bean by name using SpEL. #{@myKeyResolver} is a SpEL expression referencing a bean with the name myKeyResolver.

    KeyResolver.java. 

    public interface KeyResolver {
    	Mono<String> resolve(ServerWebExchange exchange);
    }

    The KeyResolver interface allows pluggable strategies to derive the key for limiting requests. In future milestones, there will be some KeyResolver implementations.

    The default implementation of KeyResolver is the PrincipalNameKeyResolver which retrieves the Principal from the ServerWebExchange and calls Principal.getName().

    [Note]

    The RequestRateLimiter is not configurable via the "shortcut" notation. The example below is invalid

    application.properties. 

    # INVALID SHORTCUT CONFIGURATION
    spring.cloud.gateway.routes[0].filters[0]=RequestRateLimiter=2, 2, #{@userkeyresolver}

    5.7.1 Redis RateLimiter

    The redis implementation is based off of work done at Stripe. It requires the use of the spring-boot-starter-data-redis-reactive Spring Boot starter.

    The algorithm used is the Token Bucket Algorithm.

    The redis-rate-limiter.replenishRate is how many requests per second do you want a user to be allowed to do, without any dropped requests. This is the rate that the token bucket is filled.

    The redis-rate-limiter.burstCapacity is the maximum number of requests a user is allowed to do in a single second. This is the number of tokens the token bucket can hold. Setting this value to zero will block all requests.

    A steady rate is accomplished by setting the same value in replenishRate and burstCapacity. Temporary bursts can be allowed by setting burstCapacity higher than replenishRate. In this case, the rate limiter needs to be allowed some time between bursts (according to replenishRate), as 2 consecutive bursts will result in dropped requests (HTTP 429 - Too Many Requests).

    application.yml. 

    spring:
      cloud:
        gateway:
          routes:
          - id: requestratelimiter_route
            uri: http://example.org
            filters:
            - name: RequestRateLimiter
              args:
                redis-rate-limiter.replenishRate: 10
                redis-rate-limiter.burstCapacity: 20

    Config.java. 

    @Bean
    KeyResolver userKeyResolver() {
        return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("user"));
    }

    This defines a request rate limit of 10 per user. A burst of 20 is allowed, but the next second only 10 requests will be available. The KeyResolver is a simple one that gets the user request parameter (note: this is not recommended for production).

    A rate limiter can also be defined as a bean implementing the RateLimiter interface. In configuration, reference the bean by name using SpEL. #{@myRateLimiter}is a SpEL expression referencing a bean with the name myRateLimiter.

    application.yml. 

    spring:
      cloud:
        gateway:
          routes:
          - id: requestratelimiter_route
            uri: http://example.org
            filters:
            - name: RequestRateLimiter
              args:
                rate-limiter: "#{@myRateLimiter}"
                key-resolver: "#{@userKeyResolver}"

    2. pom

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

    3. Config

    yml

    ---
    spring:
      cloud:
        gateway:
          discovery:
            locator:
              enabled: true
          routes:
          - id: user-service
            uri: lb://user-service
            predicates:
            - Path=/user/**
            filters:
            - StripPrefix=1
            - name: RequestRateLimiter
              args:
                key-resolver: "#{@remoteAddrKeyResolver}"
                redis-rate-limiter.replenishRate: 1
                redis-rate-limiter.burstCapacity: 5
    
    ---
    spring:
      redis:
        host: localhost
        port: 6379
        database: 0

    4. KeyResolve

    1) implement class

    import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
    import org.springframework.web.server.ServerWebExchange;
    
    import reactor.core.publisher.Mono;
    
    public class RemoteAddrKeyResolver implements KeyResolver {
    
        @Override
        public Mono<String> resolve(ServerWebExchange exchange) {
            // TODO Auto-generated method stub
            return Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
        }
    
    }

      2) component

    import org.springframework.context.annotation.Bean;
    import org.springframework.stereotype.Component;
    
    import com.example.demo.keyResolver.RemoteAddrKeyResolver;
    
    @Component
    public class RequestRateLimiterCustom {
    
        @Bean(name = "remoteAddrKeyResolver")
        public RemoteAddrKeyResolver remoteAddrKeyResolver() {
            return new RemoteAddrKeyResolver();
        }
    }

     5. pics

       

     HTTP 429 - (不停的按F5刷新页面)

    Refernce:

        1. 《Spring Cloud(十五):Spring Cloud Gateway(限流)

  • 相关阅读:
    微软 软件的 一组堆成快捷键
    C#事件 的讲解
    软件缺陷分析的几种方法
    一个长三角人对深圳的看法 (转)
    一次LoadRunner的CPC考试经历
    测试是一门武功
    ORACLE的性能测试经验总结
    深圳测试协会第九次论坛在深圳举行
    10月28日参加了IBM的产品推介会
    什么是web安全性测试?
  • 原文地址:https://www.cnblogs.com/xiaobin-hlj80/p/10078638.html
Copyright © 2020-2023  润新知