SpringCloud:Spring Cloud Gateway中如何配置RedisRateLimiter
Spring Cloud Gateway中如何配置RedisRateLimiter
目录
一、安装和配置Redis
1、下载地址:
Linux版:
https://redis.io/download
Windows版:
https://github.com/microsoftarchive/redis/releases
2、配置
Linux版,可查看文档:
https://redis.io/documentation
Windows版,则修改redis.windows-service.conf文件,设置密码:
requirepass 12345678
在服务中可查找到Redis服务,其可执行文件路径:
"C:\Program Files\Redis\redis-server.exe" --service-run "C:\Program Files\Redis\redis.windows-service.conf"
启动此服务。
二、配置Maven
<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>2.2.1.RELEASE</spring-cloud.version>
</properties><parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath />
</parent><dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-webflux</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency><dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
<version>${spring-cloud.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-gateway</artifactId>
<version>${spring-cloud.version}</version>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-ribbon</artifactId>
<version>${spring-cloud.version}</version>
</dependency></dependencies>
spring-boot-starter-data-redis-reactive就是SpringBoot需要用到的Redis相关依赖。
三、配置application.properties文件
server.port=8080
spring.application.name=gateway_server# redis
spring.redis.password=12345678# default true
management.endpoint.gateway.enabled=true
management.endpoints.web.exposure.include=*logging.level.org.springframework.cloud.gateway=TRACE
# default true
ribbon.eureka.enabled=false# default true
spring.cloud.loadbalancer.ribbon.enabled=true
# default false
spring.cloud.gateway.actuator.verbose.enabled=true########################################################################
spring.cloud.gateway.routes[0].id=my_route
spring.cloud.gateway.routes[0].uri=lb://my-load-balanced-service
spring.cloud.gateway.routes[0].predicates[0]=Path=/test/**
spring.cloud.gateway.routes[0].filters[0].name=RequestRateLimiter
spring.cloud.gateway.routes[0].filters[0].args[redis-rate-limiter.replenishRate]=1
spring.cloud.gateway.routes[0].filters[0].args[redis-rate-limiter.burstCapacity]=2
spring.cloud.gateway.routes[0].filters[0].args[key-resolver]=#{@userkeyResolver}my-load-balanced-service.ribbon.listOfServers=localhost:9090, 127.0.0.1:9090
my-load-balanced-service.ribbon.NFLoadBalancerRuleClassName=com.netflix.loadbalancer.RoundRobinRule
spring.redis.password:设置Redis登录密码。
redis-rate-limiter.replenishRate:针对同一个key,允许的每秒请求数,不包括被抛弃的请求。这实际是令牌桶填充率。
redis-rate-limiter.burstCapacity:针对同一个key,一秒内允许的最大请求数。这实际是令牌桶可容纳的最大令牌数。若设为0,则拒绝所有请求。
以上两个参数都根据同一个key的,这个key如何解析出来,就需要用到key-resolver这个参数,它会去查找名称为userkeyResolver的Bean。
其中,Redis的相关配置参数可参考:
https://docs.spring.io/spring-boot/docs/2.2.4.RELEASE/reference/html/appendix-application-properties.html#common-application-properties
RedisRateLimiter的配置文档可参考:
https://cloud.spring.io/spring-cloud-static/spring-cloud-gateway/2.2.1.RELEASE/reference/html/#the-requestratelimiter-gatewayfilter-factory
四、Java代码实现
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;
@Configuration
@SpringBootApplication
public class AppBoot {
@Bean
public KeyResolver userkeyResolver() {
return exchange -> Mono.just(exchange.getRequest().getRemoteAddress().getAddress().getHostAddress());
}
public static void main(String[] args) {
SpringApplication.run(AppBoot.class, args);
}
}
通过上述代码,会生成一个名称为userkeyResolver的KeyResolver类型的Bean,这个Bean会解析HTTP请求的IP,这个IP就会作为Key使用。当然,也可以使用别的规则生成这个Key,提供给RedisRateLimiter使用。
这样通过访问如http://127.0.0.1:8080/test/testApp/TestServlet这种地址,就可以使用Redis做限流管理。
更多推荐
所有评论(0)