forked from kookmin-sw/cap-template
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Feature/be/#159 API Rate Limiter 적용
- Loading branch information
Showing
12 changed files
with
222 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
## Test Version | ||
|
||
version: '3' | ||
|
||
services: | ||
redis: | ||
image: redis:7.2.0-alpine | ||
container_name: redis | ||
hostname: redis | ||
restart: unless-stopped | ||
environment: | ||
TZ: "Asia/Seoul" | ||
ports: | ||
- 6379:6379 | ||
healthcheck: | ||
test: ["CMD-SHELL", "redis-cli ping | grep PONG"] | ||
interval: 5s | ||
timeout: 3s | ||
retries: 10 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
36 changes: 36 additions & 0 deletions
36
back-gateway/src/main/java/com/gateway/backgateway/config/RedisConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.gateway.backgateway.config; | ||
|
||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.data.redis.connection.ReactiveRedisConnectionFactory; | ||
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory; | ||
import org.springframework.data.redis.core.ReactiveRedisTemplate; | ||
import org.springframework.data.redis.serializer.RedisSerializationContext; | ||
import org.springframework.data.redis.serializer.StringRedisSerializer; | ||
|
||
@Configuration | ||
public class RedisConfig { | ||
|
||
@Value("${spring.data.redis.host}") | ||
String redishost; | ||
|
||
@Bean | ||
@Primary | ||
public ReactiveRedisConnectionFactory reactiveRedisConnectionFactory() { | ||
return new LettuceConnectionFactory(redishost, 6379); | ||
} | ||
|
||
@Bean | ||
@Primary | ||
public ReactiveRedisTemplate<String, Object> reactiveRedisTemplate(ReactiveRedisConnectionFactory factory) { | ||
RedisSerializationContext<String, Object> serializationContext = RedisSerializationContext | ||
.<String, Object>newSerializationContext(new StringRedisSerializer()) | ||
.hashKey(new StringRedisSerializer()) | ||
.hashValue(new StringRedisSerializer()) | ||
.build(); | ||
|
||
return new ReactiveRedisTemplate<>(factory, serializationContext); | ||
} | ||
} |
20 changes: 20 additions & 0 deletions
20
back-gateway/src/main/java/com/gateway/backgateway/config/UserIdKeyResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package com.gateway.backgateway.config; | ||
|
||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.context.annotation.Primary; | ||
import org.springframework.web.server.ServerWebExchange; | ||
import reactor.core.publisher.Mono; | ||
|
||
@Configuration("user-key-resolver") | ||
@Primary | ||
@Slf4j | ||
public class UserIdKeyResolver implements KeyResolver { | ||
@Override | ||
public Mono<String> resolve(ServerWebExchange exchange) { | ||
String userId = exchange.getRequest().getHeaders().getFirst("X-USER-ID"); | ||
log.info("The user id is {}", userId); | ||
return Mono.just(userId); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
back-gateway/src/main/java/com/gateway/backgateway/exception/TooManyRequestException.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
package com.gateway.backgateway.exception; | ||
|
||
public class TooManyRequestException extends BusinessException { | ||
|
||
// 자주 발생할 수 있는 Exception이라 싱글톤화 하는게 좋다고 합니다. | ||
public static final TooManyRequestException INSTANCE = new TooManyRequestException(); | ||
|
||
private TooManyRequestException() { | ||
super(ErrorCode.TOO_MANY_REQUESTS); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
back-gateway/src/main/java/com/gateway/backgateway/filter/RequestRateLimitFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
package com.gateway.backgateway.filter; | ||
|
||
import com.gateway.backgateway.exception.TooManyRequestException; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.springframework.cloud.gateway.filter.GatewayFilter; | ||
import org.springframework.cloud.gateway.filter.factory.AbstractGatewayFilterFactory; | ||
import org.springframework.cloud.gateway.filter.ratelimit.KeyResolver; | ||
import org.springframework.cloud.gateway.filter.ratelimit.RedisRateLimiter; | ||
import org.springframework.cloud.gateway.support.HasRouteId; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@Slf4j | ||
public class RequestRateLimitFilter extends AbstractGatewayFilterFactory<RequestRateLimitFilter.Config> { | ||
|
||
private final KeyResolver defaultKeyResolver; | ||
private final RedisRateLimiter defaultRateLimiter; | ||
|
||
public RequestRateLimitFilter( | ||
KeyResolver defaultKeyResolver, | ||
RedisRateLimiter redisRateLimiter) { | ||
super(Config.class); | ||
this.defaultKeyResolver = defaultKeyResolver; | ||
this.defaultRateLimiter = redisRateLimiter; | ||
} | ||
|
||
@Override | ||
public GatewayFilter apply(Config config) { | ||
log.info("여기 필터 지나는지 확인 1111"); | ||
GatewayFilter filter = (exchange, chain) -> { | ||
KeyResolver keyResolver = getOrDefault(config.keyResolver, defaultKeyResolver); | ||
RedisRateLimiter rateLimiter = getOrDefault(config.rateLimiter, defaultRateLimiter); | ||
String routeId = config.getRouteId(); | ||
log.info("여기 필터 지나는지 확인 2222222"); | ||
|
||
return keyResolver.resolve(exchange) | ||
.flatMap(key -> rateLimiter.isAllowed(routeId, key)) | ||
.flatMap(rateLimitResponse -> { | ||
if (rateLimitResponse.isAllowed()) { | ||
return chain.filter(exchange); | ||
} else { | ||
throw TooManyRequestException.INSTANCE; | ||
} | ||
}); | ||
}; | ||
|
||
return filter; | ||
} | ||
|
||
private <T> T getOrDefault(T configValue, T defaultValue) { | ||
if (configValue != null) return configValue; | ||
else return defaultValue; | ||
} | ||
|
||
@Getter | ||
@Setter | ||
public static class Config implements HasRouteId { | ||
private KeyResolver keyResolver; | ||
private RedisRateLimiter rateLimiter; | ||
private String routeId; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,22 @@ | ||
spring: | ||
application: | ||
name: back-gateway | ||
data: | ||
redis: | ||
host: ${REDIS_HOST} | ||
port: ${REDIS_PORT} | ||
data: 0 | ||
|
||
|
||
jwt: | ||
secret: | ||
key: ${JWT_SECRET} | ||
|
||
server: | ||
port: 8081 | ||
chatbot-url: ${CHATBOT_URL} | ||
chatbot-url: ${CHATBOT_URL} | ||
|
||
logging: | ||
level: | ||
root: debug | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters