Move rate limiting examples
This commit is contained in:
-38
@@ -1,38 +0,0 @@
|
||||
package com.baeldung.springcloudgateway.ipaddress;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter;
|
||||
import org.springframework.cloud.gateway.route.RouteLocator;
|
||||
import org.springframework.cloud.gateway.route.builder.RouteLocatorBuilder;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.PropertySource;
|
||||
|
||||
@SpringBootApplication()
|
||||
@PropertySource("classpath:ipaddress-application.properties")
|
||||
public class IpAddressApplication {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(IpAddressApplication.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RouteLocator myRoutes(RouteLocatorBuilder builder) {
|
||||
return builder.routes()
|
||||
.route("requestratelimiter_route", p -> p
|
||||
.path("/example")
|
||||
.filters(f -> f.requestRateLimiter(r -> r.setRateLimiter(redisRateLimiter())))
|
||||
.uri("http://example.org"))
|
||||
.route("ipaddress_route", p -> p
|
||||
.path("/example2")
|
||||
.filters(f -> f.requestRateLimiter(r -> r.setRateLimiter(redisRateLimiter())
|
||||
.setDenyEmptyKey(false)
|
||||
.setKeyResolver(new SimpleClientAddressResolver())))
|
||||
.uri("http://example.org"))
|
||||
.build();
|
||||
}
|
||||
|
||||
@Bean
|
||||
public RedisRateLimiter redisRateLimiter() {
|
||||
return new RedisRateLimiter(1, 1, 1);
|
||||
}
|
||||
}
|
||||
-21
@@ -1,21 +0,0 @@
|
||||
package com.baeldung.springcloudgateway.ipaddress;
|
||||
|
||||
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
|
||||
import org.springframework.cloud.gateway.support.ipresolver.XForwardedRemoteAddressResolver;
|
||||
import org.springframework.context.annotation.Primary;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.InetSocketAddress;
|
||||
|
||||
@Primary
|
||||
@Component
|
||||
public class ProxiedClientAddressResolver implements KeyResolver {
|
||||
@Override
|
||||
public Mono<String> resolve(ServerWebExchange exchange) {
|
||||
XForwardedRemoteAddressResolver resolver = XForwardedRemoteAddressResolver.maxTrustedIndex(1);
|
||||
InetSocketAddress inetSocketAddress = resolver.resolve(exchange);
|
||||
return Mono.just(inetSocketAddress.getAddress().getHostAddress());
|
||||
}
|
||||
}
|
||||
-23
@@ -1,23 +0,0 @@
|
||||
package com.baeldung.springcloudgateway.ipaddress;
|
||||
|
||||
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver;
|
||||
import org.springframework.stereotype.Component;
|
||||
import org.springframework.web.server.ServerWebExchange;
|
||||
import reactor.core.publisher.Mono;
|
||||
|
||||
import java.net.InetAddress;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.util.Optional;
|
||||
|
||||
@Component
|
||||
public class SimpleClientAddressResolver implements KeyResolver {
|
||||
@Override
|
||||
public Mono<String> resolve(ServerWebExchange exchange) {
|
||||
return Optional.ofNullable(exchange.getRequest().getRemoteAddress())
|
||||
.map(InetSocketAddress::getAddress)
|
||||
.map(InetAddress::getHostAddress)
|
||||
.map(Mono::just)
|
||||
.orElse(Mono.empty());
|
||||
}
|
||||
}
|
||||
|
||||
-6
@@ -1,6 +0,0 @@
|
||||
server.port=8081
|
||||
spring.redis.database=0
|
||||
spring.redis.host=localhost
|
||||
spring.redis.port=16379
|
||||
spring.redis.password=mypass
|
||||
spring.redis.timeout=60000
|
||||
Reference in New Issue
Block a user