@GetMapping("/userinfo")
public String userInfo(@RequestParam("username") String username) {
return Constants.HELLO_PREFIX + " " + username + ", " + dateStr();
}
<?xml version="1.0" encoding="UTF-8"?>
spring-cloud-tutorials
com.bolingcavalry
1.0-SNAPSHOT
4.0.0
gateway-requestratelimiter
com.bolingcavalry
common
${project.version}
org.springframework.cloud
spring-cloud-starter-gateway
org.springframework.boot
spring-boot-starter-data-redis-reactive
server:
#服务端口
port: 8081
spring:
application:
name: circuitbreaker-gateway
# redis配置
redis:
host: 192.168.50.43
port: 6379
cloud:
gateway:
routes:
- id: path_route
uri: http://127.0.0.1:8082
predicates:
- Path=/hello/**
filters:
- name: RequestRateLimiter
args:
# 令牌入桶的速度为每秒100个,相当于QPS
redis-rate-limiter.replenishRate: 100
# 桶内能装200个令牌,相当于峰值,要注意的是:第一秒从桶内能去200个,但是第二秒只能取到100个了,因为入桶速度是每秒100个
redis-rate-limiter.burstCapacity: 200
# 每个请求需要的令牌数
redis-rate-limiter.requestedTokens: 1
server:
#服务端口
port: 8081
spring:
application:
name: circuitbreaker-gateway
# redis配置
redis:
host: 192.168.50.43
port: 6379
cloud:
gateway:
routes:
- id: path_route
uri: http://127.0.0.1:8082
predicates:
- Path=/hello/**
filters:
- name: RequestRateLimiter
args:
# 令牌入桶的速度为每秒100个,相当于QPS
redis-rate-limiter.replenishRate: 100
# 桶内能装200个令牌,相当于峰值,要注意的是:第一秒从桶内能去200个,但是第二秒只能取到100个了,因为入桶速度是每秒100个
redis-rate-limiter.burstCapacity: 200
# 每个请求需要的令牌数
redis-rate-limiter.requestedTokens: 1
package com.bolingcavalry.gateway.config;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import reactor.core.publisher.Mono;
import java.util.Objects;
@Configuration
public class CustomizeConfig {
@Bean
KeyResolver userKeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("username"));
}
}
package com.bolingcavalry.gateway;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class RequestRateLimiterApplication {
public static void main(String[] args) {
SpringApplication.run(RequestRateLimiterApplication.class,args);
}
}
ab -n 10000 -c 2 http://localhost:8081/hello/userinfo?username=Tom
ab -n 10000 -c 2 http://localhost:8081/hello/userinfo?username=Tom
页面更新:2024-04-30
本站资料均由网友自行发布提供,仅用于学习交流。如有版权问题,请与我联系,QQ:4156828
© CopyRight 2008-2024 All Rights Reserved. Powered By bs178.com 闽ICP备11008920号-3
闽公网安备35020302034844号