diff --git a/api-gateway/src/main/java/com/jmsoftware/maf/apigateway/universal/configuration/DiscoveryRouteConfiguration.java b/api-gateway/src/main/java/com/jmsoftware/maf/apigateway/universal/configuration/DiscoveryRouteConfiguration.java index 2c72d9a8..3f3053e4 100644 --- a/api-gateway/src/main/java/com/jmsoftware/maf/apigateway/universal/configuration/DiscoveryRouteConfiguration.java +++ b/api-gateway/src/main/java/com/jmsoftware/maf/apigateway/universal/configuration/DiscoveryRouteConfiguration.java @@ -1,12 +1,16 @@ package com.jmsoftware.maf.apigateway.universal.configuration; +import cn.hutool.core.util.ObjectUtil; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import lombok.val; import org.springframework.cloud.gateway.discovery.DiscoveryLocatorProperties; import org.springframework.cloud.gateway.filter.FilterDefinition; +import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver; import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter; +import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; +import reactor.core.publisher.Mono; import javax.annotation.PostConstruct; @@ -21,11 +25,20 @@ public class DiscoveryRouteConfiguration { private final DiscoveryLocatorProperties discoveryLocatorProperties; + /** + * Post construct. + * + * @see + * The Redis RateLimiter + */ @PostConstruct void postConstruct() { val filter = new FilterDefinition(); filter.setName("RequestRateLimiter"); // TODO: arguments should be passed from configuration dynamically + // setting replenishRate=1, requestedTokens=1 and burstCapacity=1 + // will result in a limit of 1 request per 1 second. + // setting replenishRate=1, requestedTokens=60 and burstCapacity=60 will result in a limit of 1 request/min. filter.addArg("redis-rate-limiter.replenishRate", "1"); filter.addArg("redis-rate-limiter.burstCapacity", "1"); filter.addArg("redis-rate-limiter.requestedTokens", "1"); @@ -33,4 +46,15 @@ void postConstruct() { log.info("Added filter[{}] for discovery services, filters: {}", RedisRateLimiter.class.getSimpleName(), discoveryLocatorProperties.getFilters()); } + + @Bean + public KeyResolver ipKeyResolver() { + return exchange -> { + val remoteAddress = exchange.getRequest().getRemoteAddress(); + if (ObjectUtil.isNotNull(remoteAddress)) { + return Mono.just(remoteAddress.getHostName()); + } + return Mono.just("unknown-address"); + }; + } }