Spring Cloud Gateway-RateLimter无法正常工作

时间:2019-11-15 23:18:17

标签: spring-cloud-gateway

我正在尝试使用Spring Cloud Gateway Rate Limiter限制对REST端点的请求。 它只是行不通。这是我的实现

package com.test;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController
@SpringBootApplication
@RequestMapping("/test")
public class GatewayApplication {

   public static void main(String[] args) {
     SpringApplication.run(GatewayApplication.class, args);
   }

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

   @RequestMapping(path = "/echo", method = RequestMethod.GET)
   public String echo() {
    return "hello";
   }
} 

RemoteAddrKeyResolver.java 这是实现KeyResolver的类

package com.test;

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
    String str = exchange.getRequest().getRemoteAddress().getAddress().getHostAddress();
    return Mono.just(str);
  }
}

,这是配置 application.yml

spring:
 cloud: 
    gateway:
      routes:
      - id: greeting-service
        uri: http://localhost:8080
        predicates:
      - Path=/test/echo
      filters:
      - StripPrefix=1
      - name: RequestRateLimiter
        args:
          key-resolver: "#{@remoteAddrKeyResolver}"
 spring:
  redis:
    host: localhost
    port: 6234
    database: 0

速率限制器不起作用。 我触发了400个并发请求,但仍然全部返回200。 有什么问题吗?

0 个答案:

没有答案