问题遇到的现象和发生背景
写了一个网关,路由功能正常,但是限流功能一直不生效,引入的spring-cloud-starter-gateway版本也从2.2.1换到2.2.9,但是限流始终不生效,不知道有没有大lao能帮忙看一下,下面贴上代码
问题相关代码,请勿粘贴截图
引入的依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<!--gateway-->
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>2.2.3.RELEASE</version>
</dependency>
spring:
cloud:
gateway:
# 路由集合
routes:
#payment_route路由的ID,没有固定规则但要求唯一,建议配合服务名
- id: powerrecord-server
#匹配后提供服务的路由地址
uri: lb://powerrecord
# 断言,路径相匹配的进行路由
predicates:
- Path=/server/**
#- After=2020-02-21T15:51:37.485+08:00[Asia/Shanghai]
#- Cookie=username,zzyy
#- Header=X-Request-Id, \d+ # 请求头要有X-Request-Id属性并且值为整数的正则表达式
filters:
- name: RequestRateLimiter
args:
# 令牌桶每秒填充平均速率
redis-rate-limiter.replenishRate: 1
# 令牌桶的上限
redis-rate-limiter.burstCapacity: 1
# 使用SpEL表达式从Spring容器中获取Bean对象
key-resolver: '#{@ipKeyResolver}'
redis:
host: xx.xx.xx.xx
port: 6388
password: ENC(+I5Y1D9DQ1kXC+9yg8QQdga8vV4QGLsTiVxtiizoXapzMHu9L42OcTkE6v3x6BAP)
@Configuration
public class LimitConfiguration {
/**
* 按ip限流
*/
@Bean(value = "ipKeyResolver")
public KeyResolver ipKeyResolver() {
System.out.println("##############ipKeyResolver########################");
return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
}
}
@SpringBootApplication
@EnableDiscoveryClient
public class PRGatewayApplication {
public static void main(String[] args) {
System.setProperty("jasypt.encryptor.password", ConstantsInterface.JASYPT_ENCRYPTOR_PASSWORD);
SpringApplication.run(PRGatewayApplication.class, args);
}
}
运行结果及报错内容
无报错,连续点击发送请求,限流不生效
我的解答思路和尝试过的方法
更换spring-cloud-starter-gateway版本,使用其他自定义限流方式,目前用过的方法都无效
我想要达到的结果
可以正常起到限流作用