diff --git a/api-client/common-client/src/main/java/woowa/tema4/bff/api/client/caller/WebClientCaller.java b/api-client/common-client/src/main/java/woowa/tema4/bff/api/client/caller/WebClientCaller.java index f3c2c29f..743b7443 100644 --- a/api-client/common-client/src/main/java/woowa/tema4/bff/api/client/caller/WebClientCaller.java +++ b/api-client/common-client/src/main/java/woowa/tema4/bff/api/client/caller/WebClientCaller.java @@ -1,6 +1,7 @@ package woowa.tema4.bff.api.client.caller; import io.github.resilience4j.circuitbreaker.CircuitBreaker; +import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; import io.github.resilience4j.reactor.circuitbreaker.operator.CircuitBreakerOperator; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; @@ -14,11 +15,14 @@ public class WebClientCaller { private final WebClient webClient; - private final CircuitBreaker circuitBreaker; + private final CircuitBreakerRegistry circuitBreakerRegistry; public Mono post(String url, T requestBody, ParameterizedTypeReference responseType) { + // CircuitBreakerRegistry 는 주어진 이름에 CircuitBreaker 객체가 없으면 생성해 캐싱한다, 있다면 캐싱해둔 객체를 반환한다 + CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker(getCircuitBreakerName(url)); + return webClient.post() .uri(url) .contentType(MediaType.APPLICATION_JSON) @@ -40,4 +44,11 @@ public R postBlocking(String url, T requestBody, ParameterizedTypeReference responseType) { return post(url, requestBody, responseType).block(); } + + private String getCircuitBreakerName(String url) { + // URL을 기반으로 CircuitBreaker 이름 생성 + // 알파벳, 숫자 외 다른 문자를 "_" 로 대체 + // 예: "http://api.example.com/users" -> "circuit_breaker_api_example_com_users" + return "circuit_breaker_" + url.replaceAll("[^a-zA-Z0-9]", "_"); + } } diff --git a/api-client/common-client/src/main/java/woowa/tema4/bff/api/client/config/ApiClientConfiguration.java b/api-client/common-client/src/main/java/woowa/tema4/bff/api/client/config/ApiClientConfiguration.java index 551d6669..8aef9601 100644 --- a/api-client/common-client/src/main/java/woowa/tema4/bff/api/client/config/ApiClientConfiguration.java +++ b/api-client/common-client/src/main/java/woowa/tema4/bff/api/client/config/ApiClientConfiguration.java @@ -1,6 +1,5 @@ package woowa.tema4.bff.api.client.config; -import io.github.resilience4j.circuitbreaker.CircuitBreaker; import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig; import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig.SlidingWindowType; import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; @@ -59,12 +58,7 @@ public CircuitBreakerRegistry circuitBreakerRegistry() { } @Bean - public CircuitBreaker circuitBreaker(CircuitBreakerRegistry circuitBreakerRegistry) { - return circuitBreakerRegistry.circuitBreaker("bff"); - } - - @Bean - public WebClientCaller webClientCaller(WebClient webClient, CircuitBreaker circuitBreaker) { - return new WebClientCaller(webClient, circuitBreaker); + public WebClientCaller webClientCaller(WebClient webClient, CircuitBreakerRegistry circuitBreakerRegistry) { + return new WebClientCaller(webClient, circuitBreakerRegistry); } }