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
@@ -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");
}
}
@@ -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}"
@@ -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();
}
}
@@ -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>