728x90
CircuitBreaker
- 해당 MSA에서의 문제가 아닌 이를 호출하는 곳에서 에러가 난다면 해당 Service에서는 문제가 있는 것이 아니므로 정상적으로 동작하는 것으로 보여주어야 한다.
- 장애가 발생하는 서비스에 반복적인 호출이 되지 못하게 차단
- 특정 서비스가 정상적으로 동작하지 않을 경우 다른 기능으로 대체 수행(장애 회피)
- 2가지 상태가 존재 한다.
- Closed : 하나의 서비스를 동작하는 동안 문제가 없는 상태
- Open : 클라이언트 요청을 더 이상 MicroService에 전송하지 않고 CircuitBreaker가 처리하는 상태(장애가 있는 상태)
- https://martinfowler.com/bliki/CircuitBreaker.html
bliki: CircuitBreaker
You use software circuit breakers on connections to remote services. These breakers trip when the supplier becomes unresponsive, once tripped the breaker no longer calls the supplier until reset.
martinfowler.com
- 현재는 더이상 해당 서비스를 제공하지 않고 Netfilx에서 Resilience4 J로 대체해 제공한다.
Resilience4J
GitHub - resilience4j/resilience4j: Resilience4j is a fault tolerance library designed for Java8 and functional programming
Resilience4j is a fault tolerance library designed for Java8 and functional programming - GitHub - resilience4j/resilience4j: Resilience4j is a fault tolerance library designed for Java8 and functi...
github.com
- 의존성 추가

UserService의 Pom.xml 추가
- Service 로직 수정
- 주입

UserService.UserServiceImpl CircuitBreakerFactory 주입
- CircuitBreaker 사용
-
@Override public UserDto getUserByUserId(String userId) { UserEntity userEntity = userRepository.findByUserId(userId); if (userEntity == null) { throw new UsernameNotFoundException("User not found"); //없을떄 오류 로그 } UserDto userDto = new ModelMapper().map(userEntity, UserDto.class); //List<ResponseOrder> orders = orderServiceClient.getOrders(userId); CircuitBreaker circuitbreaker = circuitBreakerFactory.create("circuitbreaker"); List<ResponseOrder> orders = circuitbreaker .run(() -> orderServiceClient.getOrders(userId), //성공시 throwable -> new ArrayList<>());//실패시 userDto.setOrders(orders); return userDto; }
-
- 주입
- CirCuitBreaker 커스텀 사용하기
- Resilience4 JConfiguration 생성

UserService의 ResilienceJConfig 생성 - CircuitBreakerConfig : CircuitBreaker설정
- failureRateTreshould : CircuitBreaker를 열지 경정하는 확률 지정(기본은 50)
- waitDurationInOpenState : CircuitBreaker를 open 한 상태를 유지하는 지속시간(기본 60초)
- slidingWindowType : CircuitBreaker가 닫힐 때 통화 결과를 기록하는 데 사용되는 슬라이딩 창의 유형을 구성
- slidingWindowSize : CircuitBreaker가 닫힐때 통화 결과를 기록하는데 사용되는 슬라이딩 창의 크기를 지정
- TimeLimiterConfig : 시간 관련 설정
- timeoutDuration : future supplier의 time limit를 정할 수 있다(기본 1초)
- Resilience4 JConfiguration 생성
728x90
'Spring Cloud로 개발하는 MSA > 장애 처리와 Microservice 분산 추적' 카테고리의 다른 글
| 분산 추적의 개요 Zipkin 서버 설치 (0) | 2022.02.10 |
|---|