带有Spring-boot 2.0.5.RELEASE的Redis RateLimiter

时间:2018-12-01 21:55:37

标签: spring-boot redis spring-cloud-gateway

我们正在尝试使用spring-boot(2.0.5.RELEASE)和redis实现RateLimiter。但是我们没有收到预期的响应,即httpStatusCode 429(请求过多)错误。我们看到应用程序已连接到Redis服务器。但我们没有在Redis服务器中看到密钥。我们没有任何错误。任何想法或任何可行的示例都将有所帮助。

请在下面找到pom.xml。

 <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
    <spring-cloud.version>Finchley.RELEASE</spring-cloud.version>
</properties>
<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId> 
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-redis-reactive</artifactId>
    </dependency>

    <!-- https://mvnrepository.com/artifact/javax.servlet/servlet-api -->
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>servlet-api</artifactId>
        <version>2.3</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-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>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-gateway</artifactId>
            <version>2.0.0.RC2</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>


spring:
  cloud:
    gateway:
      routes:
      - id: requestratelimiter_route
        uri: http://localhost:8080/gateway/v1/api/get
        predicates:
        - Path=/**
        filters:
        - name: RequestRateLimiter
          args:
            redis-rate-limiter.replenishRate: 10
            redis-rate-limiter.burstCapacity: 20
  redis:
    host: localhost
    port: 6379
  security:
    user:
      name: user
      password: user


@Bean
    KeyResolver userKeyResolver() {

        return new KeyResolver() {
            @Override
            public Mono<String> resolve(ServerWebExchange exchange) {
                LOGGER.info("serverWebExchange.getClass().getName(): " + exchange.getClass().getName());
                return Mono.just(exchange.getSession().subscribe().toString());

            }
        };
    }

    @Bean
    RedisRateLimiter myRateLimiter() {
        return new RedisRateLimiter(10, 20);
    }

0 个答案:

没有答案