- <dependency>
- <groupId>org.springframework.bootgroupId>
- <artifactId>spring-boot-starter-data-redis-reactiveartifactId>
- dependency>
3.4 创建配置类 RequestRateLimiterConfig
- @Configuration
- public class RequestRateLimiterConfig {
- /**
- * IP 限流
- * 把用户的 IP 作为限流的 Key
- *
- * @return
- */
- @Bean
- @Primary
- public KeyResolver hostAddrKeyResolver() {
- return (exchange) -> Mono.just(exchange.getRequest().getRemoteAddress().getHostName());
- }
- /**
- * 用户 id 限流
- * 把用户 ID 作为限流的 key
- *
- * @return
- */
- @Bean
- public KeyResolver userKeyResolver() {
- return exchange -> Mono.just(exchange.getRequest().getQueryParams().getFirst("userId"));
- }
- /**
- * 请求接口限流
- * 把请求的路径作为限流 key
- *
- * @return
- */
- @Bean
- public KeyResolver apiKeyResolver() {
- return exchange -> Mono.just(exchange.getRequest().getPath().value());
- }
- }
查看 redis