Add RedisRateLimiter to webfilter package

This commit is contained in:
Cavero Barca 2020-04-27 01:22:33 +02:00
parent 44485bcc48
commit ec8d2244a3
5 changed files with 123 additions and 11 deletions

View File

@ -1,7 +1,7 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>spring-cloud-gateway</artifactId>
<name>spring-cloud-gateway</name>
@ -54,7 +54,21 @@
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
</dependency>
<!-- Redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis-reactive</artifactId>
</dependency>
<!-- Embedded Redis -->
<dependency>
<groupId>it.ozimov</groupId>
<artifactId>embedded-redis</artifactId>
<version>${redis.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator-cdi</artifactId>
@ -75,8 +89,8 @@
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
</dependencies>
@ -93,6 +107,7 @@
<spring-cloud-dependencies.version>Hoxton.SR3</spring-cloud-dependencies.version>
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
<hibernate-validator.version>6.0.2.Final</hibernate-validator.version>
<redis.version>0.7.2</redis.version>
</properties>
</project>

View File

@ -0,0 +1,16 @@
package com.baeldung.springcloudgateway.webfilters.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;
@Configuration
public class RequestRateLimiterResolverConfig {
@Bean
KeyResolver userKeyResolver() {
return exchange -> Mono.just("1");
}
}

View File

@ -1,4 +1,12 @@
logging:
level:
org.springframework.cloud.gateway: INFO
reactor.netty.http.client: INFO
spring:
redis:
host: localhost
port: 6379
cloud:
gateway:
routes:
@ -81,13 +89,14 @@ spring:
factor: 2
basedOnPreviousValue: false
- id: circuitbreaker_route
- id: request_rate_limiter
uri: https://httpbin.org
predicates:
- Path=/status/504
- Path=/redis/get/**
filters:
- name: CircuitBreaker
- StripPrefix=1
- name: RequestRateLimiter
args:
name: myCircuitBreaker
fallbackUri: forward:/anything
- RewritePath=/status/504, /anything
redis-rate-limiter.replenishRate: 10
redis-rate-limiter.burstCapacity: 5
key-resolver: "#{@userKeyResolver}"

View File

@ -0,0 +1,64 @@
package com.baeldung.springcloudgateway.webfilters;
import org.junit.After;
import org.junit.Before;
import org.junit.jupiter.api.RepeatedTest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.ActiveProfiles;
import redis.embedded.RedisServer;
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT)
@ActiveProfiles("webfilters")
@TestConfiguration
public class RedisWebFilterFactoriesLiveTest {
private static final Logger LOGGER = LoggerFactory.getLogger(RedisWebFilterFactoriesLiveTest.class);
private RedisServer redisServer;
public RedisWebFilterFactoriesLiveTest() {
}
@Before
public void postConstruct() {
this.redisServer = new RedisServer(6379);
redisServer.start();
}
@LocalServerPort
String port;
@Autowired
private TestRestTemplate restTemplate;
@Autowired
TestRestTemplate template;
@RepeatedTest(25)
public void whenCallRedisGetThroughGateway_thenOKStatusOrIsReceived() {
String url = "http://localhost:" + port + "/redis/get";
ResponseEntity<String> r = restTemplate.getForEntity(url, String.class);
// assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
LOGGER.info("Received: status->{}, reason->{}, remaining->{}",
r.getStatusCodeValue(), r.getStatusCode().getReasonPhrase(),
r.getHeaders().get("X-RateLimit-Remaining"));
}
@After
public void preDestroy() {
redisServer.stop();
}
}

View File

@ -3,7 +3,15 @@
<appender name="LISTAPPENDER"
class="com.baeldung.springcloudgateway.customfilters.gatewayapp.utils.LoggerListAppender">
</appender>
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern>
</encoder>
</appender>
<root level="info">
<appender-ref ref="LISTAPPENDER" />
</root>
<root level="info">
<appender-ref ref="STDOUT" />
</root>
</configuration>