Skip to content

Commit

Permalink
Merge branch 'dev' into refactor/124-cache-service-async
Browse files Browse the repository at this point in the history
  • Loading branch information
huiseung authored Aug 25, 2024
2 parents 15183ec + 0fff5f9 commit 1b5f8c9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,11 +15,14 @@
public class WebClientCaller {

private final WebClient webClient;
private final CircuitBreaker circuitBreaker;
private final CircuitBreakerRegistry circuitBreakerRegistry;


public <T, R> Mono<R> post(String url, T requestBody,
ParameterizedTypeReference<R> responseType) {
// CircuitBreakerRegistry 는 주어진 이름에 CircuitBreaker 객체가 없으면 생성해 캐싱한다, 있다면 캐싱해둔 객체를 반환한다
CircuitBreaker circuitBreaker = circuitBreakerRegistry.circuitBreaker(getCircuitBreakerName(url));

return webClient.post()
.uri(url)
.contentType(MediaType.APPLICATION_JSON)
Expand All @@ -40,4 +44,11 @@ public <T, R> R postBlocking(String url, T requestBody,
ParameterizedTypeReference<R> 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]", "_");
}
}
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
}

0 comments on commit 1b5f8c9

Please sign in to comment.