☀️ bucket4j 의존성 추가
implementation 'com.bucket4j:bucket4j-core:8.10.1'
🐒 1분에 최대 3번의 요청을 받을 수 있도록 설정
@Configuration
public class BucketConfig {
@Bean
public Bucket bucket() {
final Refill refill = Refill.intervally(3, Duration.ofSeconds(60));
final Bandwidth limit = Bandwidth.classic(3, refill);
return Bucket.builder().addLimit(limit).build();
}
}
🍔 API 호출 시 1씩 차감
- 제한된 요청 횟수를 넘기면 429 에러가 뜨도록 했다.
@PostMapping("/auth")
public ResponseEntity<?> authenticate(@Valid @RequestBody final OTPRequest otpRequest) {
if (!bucket.tryConsume(1)) {
return ResponseEntity.status(HttpStatus.TOO_MANY_REQUESTS).build();
}
return ResponseEntity.ok().body(watchService.authenticate(otpRequest));
}
반응형
'Framework > Spring Boot' 카테고리의 다른 글
ClientAbortException (0) | 2024.05.16 |
---|---|
[Error] 'org.springframework.batch.core.configuration.annotation.JobBuilderFactory' that could not be found. (0) | 2024.05.06 |
prometheus와 SpringBoot 연동 (0) | 2024.05.02 |
[Error] No Feign Client for loadBalancing defined. Did you forget to include spring-cloud-starter-loadbalancer? (0) | 2024.04.28 |
[Error] Request failed with status code 403 (0) | 2024.04.04 |