Add RedisRateLimiter to webfilter package
This commit is contained in:
parent
44485bcc48
commit
ec8d2244a3
@ -1,7 +1,7 @@
|
|||||||
<?xml version="1.0" encoding="UTF-8"?>
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
xmlns="http://maven.apache.org/POM/4.0.0"
|
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">
|
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>
|
<modelVersion>4.0.0</modelVersion>
|
||||||
<artifactId>spring-cloud-gateway</artifactId>
|
<artifactId>spring-cloud-gateway</artifactId>
|
||||||
<name>spring-cloud-gateway</name>
|
<name>spring-cloud-gateway</name>
|
||||||
@ -54,7 +54,21 @@
|
|||||||
<groupId>org.springframework.cloud</groupId>
|
<groupId>org.springframework.cloud</groupId>
|
||||||
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
|
<artifactId>spring-cloud-starter-circuitbreaker-reactor-resilience4j</artifactId>
|
||||||
</dependency>
|
</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>
|
<dependency>
|
||||||
<groupId>org.hibernate</groupId>
|
<groupId>org.hibernate</groupId>
|
||||||
<artifactId>hibernate-validator-cdi</artifactId>
|
<artifactId>hibernate-validator-cdi</artifactId>
|
||||||
@ -75,8 +89,8 @@
|
|||||||
<scope>test</scope>
|
<scope>test</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>org.springframework.boot</groupId>
|
<groupId>org.springframework.boot</groupId>
|
||||||
<artifactId>spring-boot-devtools</artifactId>
|
<artifactId>spring-boot-devtools</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
@ -93,6 +107,7 @@
|
|||||||
<spring-cloud-dependencies.version>Hoxton.SR3</spring-cloud-dependencies.version>
|
<spring-cloud-dependencies.version>Hoxton.SR3</spring-cloud-dependencies.version>
|
||||||
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
|
<spring-boot.version>2.2.6.RELEASE</spring-boot.version>
|
||||||
<hibernate-validator.version>6.0.2.Final</hibernate-validator.version>
|
<hibernate-validator.version>6.0.2.Final</hibernate-validator.version>
|
||||||
|
<redis.version>0.7.2</redis.version>
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</project>
|
||||||
|
@ -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:
|
spring:
|
||||||
|
redis:
|
||||||
|
host: localhost
|
||||||
|
port: 6379
|
||||||
cloud:
|
cloud:
|
||||||
gateway:
|
gateway:
|
||||||
routes:
|
routes:
|
||||||
@ -81,13 +89,14 @@ spring:
|
|||||||
factor: 2
|
factor: 2
|
||||||
basedOnPreviousValue: false
|
basedOnPreviousValue: false
|
||||||
|
|
||||||
- id: circuitbreaker_route
|
- id: request_rate_limiter
|
||||||
uri: https://httpbin.org
|
uri: https://httpbin.org
|
||||||
predicates:
|
predicates:
|
||||||
- Path=/status/504
|
- Path=/redis/get/**
|
||||||
filters:
|
filters:
|
||||||
- name: CircuitBreaker
|
- StripPrefix=1
|
||||||
|
- name: RequestRateLimiter
|
||||||
args:
|
args:
|
||||||
name: myCircuitBreaker
|
redis-rate-limiter.replenishRate: 10
|
||||||
fallbackUri: forward:/anything
|
redis-rate-limiter.burstCapacity: 5
|
||||||
- RewritePath=/status/504, /anything
|
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"
|
<appender name="LISTAPPENDER"
|
||||||
class="com.baeldung.springcloudgateway.customfilters.gatewayapp.utils.LoggerListAppender">
|
class="com.baeldung.springcloudgateway.customfilters.gatewayapp.utils.LoggerListAppender">
|
||||||
</appender>
|
</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">
|
<root level="info">
|
||||||
<appender-ref ref="LISTAPPENDER" />
|
<appender-ref ref="LISTAPPENDER" />
|
||||||
</root>
|
</root>
|
||||||
|
<root level="info">
|
||||||
|
<appender-ref ref="STDOUT" />
|
||||||
|
</root>
|
||||||
</configuration>
|
</configuration>
|
Loading…
x
Reference in New Issue
Block a user