티스토리 뷰
Local 환경에서 RedisRateLimiter 를 적용한 과정.
1. docker 를 이용해서 redis 띄우기
docker run -d -p 6379:6379 --name rate_limiter_redis redis
- port 를 mapping 하지 않아서 spring cloud gateway 에서 redis 로 접속이 안되는 문제가 있었다.
2. build.gradle 에 spring-boot-starter-data-redis-reactive 의존성 추가
compile group: 'org.springframework.boot', name: 'spring-boot-starter-data-redis-reactive', version: '2.3.2.RELEASE'
3. bootstrap.yml 에 redis 접속을 위한 설정 추가
spring:
redis:
host: localhost
port: 6379
database: 0
4. bootstrap.yml 에 RequestRateLimiter 필터 설정
spring:
cloud:
gateway:
routes:
- id: return_delivery
uri: http://localhost:8082
predicates:
- Path=/limit/test
filters:
- name: RequestRateLimiter
args:
redis-rate-limiter.replenishRate: 1
redis-rate-limiter.burstCapacity: 5
key-resolver: "#{@myKeyResolver}"
++ Key 를 찾을 수 없을때에 대해서 block 할지 말지에 대한 설정은 아래 값을 변경해주면 된다. (true/false)
spring.cloud.gateway.filter.request-rate-limiter.deny-empty-key: true
위와 같이 설정했다면 설정한대로 RateLimit 이 잘 동작 하는지 확인!
KeyResolver 에서 모든 요청에 대해서 동일한 key 를 사용하도록 설정하였으므로 predicate 에 매칭된 모든 요청에 대해서 RateLimit 이 적용된다.
설정한 값보다 많은 요청이 들어올 경우에 429 로 응답이 오는것을 확인할 수 있다.
RedisRateLimiter 의 응답값은 log level 을 DEBUG 로 변경하면 요청마다 확인할 수 있다.
application.properties
logging.level.root=DEBUG
2020-08-04 15:21:09.575 DEBUG 88419 --- [ioEventLoop-5-1] o.s.c.g.f.ratelimit.RedisRateLimiter : response: Response{allowed=false, headers={X-RateLimit-Remaining=0, X-RateLimit-Burst-Capacity=5, X-RateLimit-Replenish-Rate=1}, tokensRemaining=-1}
++ Redis container 에 접속해서 현재 세팅된 key 의 value 값을 redis-cli 로 확인할 수 있다.
redis container 접속
docker exec -it rate_limiter_redis /bin/bash
redis-cli 실행
redis-cli
저장되어 있는 모든 키 확인
127.0.0.1:6379> keys *
key 의 value 확인
get request_rate_limiter.{WOOOONGS}.tokens
'Spring Framework > Spring Cloud' 카테고리의 다른 글
Spring Cloud Gateway dynamic configuration change (0) | 2020.11.29 |
---|---|
Spring Cloud Gateway mapping custom data to configuration - Route meta data to RoutePredication (0) | 2020.11.29 |
Spring Cloud Gateway mapping custom data to configuration - Meta Data 정의 (0) | 2020.11.29 |
Spring Cloud Gateway 프로젝트 세팅 (0) | 2020.08.04 |
Spring Cloud Gateway (0) | 2020.07.23 |
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
링크
TAG
- reactor
- ResultSet
- mariadb-connector-j
- AbstractMethodError
- custom config data convertion
- mariada-connector
- RoutePredication
- circurit breaker
- Lazy
- rate limit
- N+1
- MariaDB
- reative
- wait()
- DyanomoDB
- spring cloud gateway
- Flux
- GlobalFilter
- aurora
- notify()
- RouteDefinition
- ConcurrentHashMap
- msyql-connector-java
- Seperate Chaining
- dynamodb
- notifyAll()
- HashMap
- router
- getBoolean
- referencedColumnName
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | |||
5 | 6 | 7 | 8 | 9 | 10 | 11 |
12 | 13 | 14 | 15 | 16 | 17 | 18 |
19 | 20 | 21 | 22 | 23 | 24 | 25 |
26 | 27 | 28 | 29 | 30 | 31 |
글 보관함