πŸ“Œ Spring Cloud CircuitBreaker + Resilience4j 정리

ghgh2505380Β·2026λ…„ 3μ›” 17일

MSA(Microservice Architecture)

λͺ©λ‘ 보기
5/13

πŸ“– κ°œμš”

  • Circuit Breaker
    λ§ˆμ΄ν¬λ‘œμ„œλΉ„μŠ€ κ°„ 호좜 μ‹€νŒ¨λ₯Ό κ°μ§€ν•˜κ³  μ‹œμŠ€ν…œ μž₯μ•  μ „νŒŒλ₯Ό μ°¨λ‹¨ν•˜λŠ” νŒ¨ν„΄

  • μ™œ μ‚¬μš©ν•˜λŠ”μ§€
    μ„œλΉ„μŠ€ κ°„ 호좜 μ‹€νŒ¨κ°€ μ—°μ‡„μ μœΌλ‘œ λ°œμƒν•˜λŠ” 문제 λ°©μ§€
    μ™ΈλΆ€ API μž₯μ• λ‘œ 전체 μ‹œμŠ€ν…œ λ‹€μš΄λ˜λŠ” 상황 λ°©μ§€

  • ν•΄κ²°ν•˜λŠ” 문제

  • μž₯μ•  μ „νŒŒ λ°©μ§€

  • λΉ λ₯Έ μ‹€νŒ¨ 처리

  • μ‹œμŠ€ν…œ μ•ˆμ •μ„± 확보

  • Resilience4j
    CircuitBreaker κ΅¬ν˜„ 라이브러리
    μ‹€νŒ¨μœ¨ 기반 μƒνƒœ 관리 및 fallback 제곡


βš™οΈ 핡심 κ°œλ…

1️⃣ CircuitBreaker 좔상화 vs κ΅¬ν˜„μ²΄

Spring Cloud CircuitBreaker

  • 좔상화 계측 제곡
  • λ‹€μ–‘ν•œ κ΅¬ν˜„μ²΄ 선택 κ°€λŠ₯

μ˜ˆμ‹œ

  • Resilience4j
  • Hystrix (deprecated)
  • Sentinel λ“±

❗ νŠΉμ§•

  • starter μ‚¬μš© μ‹œ κ΅¬ν˜„μ²΄ ν¬ν•¨λ˜μ§€ μ•ŠμŒ
  • λ‚΄λΆ€μ μœΌλ‘œ μΈν„°νŽ˜μ΄μŠ€λ§Œ 제곡

πŸ‘‰ μ‹€μ œ λ™μž‘ μœ„ν•΄ κ΅¬ν˜„μ²΄ ν•„μš”


2️⃣ Resilience4j 직접 μ‚¬μš©

ν˜„μž¬ ν”„λ‘œμ νŠΈ 방식

  • Spring Cloud starter μ‚¬μš© ❌
  • Resilience4j 라이브러리 직접 μΆ”κ°€
implementation 'io.github.resilience4j:resilience4j-spring-boot3:2.2.0'

πŸ“Œ 이유

  • 좔상화 없이 직접 μ œμ–΄ κ°€λŠ₯
  • μ„€μ • λͺ…ν™•
  • 디버깅 용이

3️⃣ CircuitBreaker μƒνƒœ 관리

  • Closed β†’ 정상 호좜
  • Open β†’ 호좜 차단
  • Half-Open β†’ 일뢀 μš”μ²­ ν…ŒμŠ€νŠΈ

πŸ“Œ μƒνƒœ 흐름

Closed β†’ Open β†’ Half-Open β†’ Closed

πŸ‘‰ μ‹€νŒ¨μœ¨ 기반 μžλ™ μ „ν™˜


4️⃣ Fallback

  • μ™ΈλΆ€ 호좜 μ‹€νŒ¨ μ‹œ λŒ€μ²΄ 둜직 μ‹€ν–‰
  • μ‚¬μš©μžμ—κ²Œ μ•ˆμ •μ μΈ 응닡 제곡
@CircuitBreaker(name = "productService", fallbackMethod = "fallback")
public String call() {
    return externalService.call();
}

public String fallback(Throwable t) {
    return "fallback";
}

πŸ‘‰ μž₯μ•  μƒν™©μ—μ„œλ„ μ„œλΉ„μŠ€ μœ μ§€ κ°€λŠ₯


5️⃣ LLM μ‚¬μš© μ‹œ 주의 (μ€‘μš”)

LLM (예: GPT)

  • ν™˜κ°(Hallucination) λ°œμƒ κ°€λŠ₯
  • 항상 μ •ν™•ν•œ 정보 보μž₯ λΆˆκ°€

πŸ“Œ λŒ€μ‘ 방법

  • 곡식 λ¬Έμ„œ 확인 ν•„μˆ˜
  • ꡬ글 검색 μž¬κ²€μ¦
  • μ½”λ“œ μ‹€ν–‰μœΌλ‘œ 검증

πŸ‘‰ LLM κ²°κ³Ό = 참고용
πŸ‘‰ μ‹€μ œ 적용 μ „ 검증 ν•„μˆ˜


πŸ” λ™μž‘ 원리

CircuitBreaker 흐름

Client β†’ Service β†’ External API

정상

Client β†’ Service β†’ External API β†’ Success

μž₯μ•  λ°œμƒ

Client β†’ Service β†’ External API β†’ Fail
β†’ μ‹€νŒ¨μœ¨ 증가
β†’ Circuit Open

이후

Client β†’ Service β†’ ❌ External 호좜 차단
β†’ Fallback μ‹€ν–‰


μƒνƒœ λ³€ν™” 흐름

μš”μ²­ β†’ μ‹€νŒ¨ λˆ„μ  β†’ μž„κ³„κ°’ 초과
β†’ Open μƒνƒœ μ „ν™˜
β†’ 일정 μ‹œκ°„ ν›„ Half-Open
β†’ 성곡 μ‹œ Closed 볡귀


πŸ’» μ½”λ“œ μ˜ˆμ‹œ

1️⃣ μ˜μ‘΄μ„± μΆ”κ°€

dependencies {
    implementation 'io.github.resilience4j:resilience4j-spring-boot3:2.2.0'
    implementation 'org.springframework.boot:spring-boot-starter-aop'
}

2️⃣ Service 적용

@Service
public class ProductService {

    @CircuitBreaker(name = "productService", fallbackMethod = "fallback")
    public String getProduct(String id) {
        if ("111".equals(id)) {
            throw new RuntimeException("error");
        }
        return "product " + id;
    }

    public String fallback(String id, Throwable t) {
        return "fallback product " + id;
    }
}

3️⃣ application.yml μ„€μ •

resilience4j:
  circuitbreaker:
    configs:
      default:
        slidingWindowType: COUNT_BASED
        slidingWindowSize: 5
        minimumNumberOfCalls: 5
        failureRateThreshold: 50
        waitDurationInOpenState: 20s

πŸš€ μ‚¬μš© 사둀

  • μ™ΈλΆ€ 결제 API 호좜 보호
  • μƒν’ˆ μ„œλΉ„μŠ€ β†’ 재고 μ„œλΉ„μŠ€ 호좜 보호
  • FeignClient 호좜 μ•ˆμ •μ„± 확보
  • μž₯μ•  λ°œμƒ μ‹œ fallback 데이터 제곡

🧠 TIL (Today I Learned)

  • Spring Cloud CircuitBreakerλŠ” 좔상화 계측 제곡
  • starter μ‚¬μš© μ‹œ κ΅¬ν˜„μ²΄ ν¬ν•¨λ˜μ§€ μ•ŠμŒ
  • Resilience4j 직접 μΆ”κ°€ν•˜μ—¬ μ‚¬μš© κ°€λŠ₯
  • CircuitBreakerλŠ” μž₯μ•  μ „νŒŒ λ°©μ§€ 핡심 νŒ¨ν„΄
  • LLM κ²°κ³ΌλŠ” λ°˜λ“œμ‹œ 검증 ν›„ μ‚¬μš© ν•„μš”
profile

0개의 λŒ“κΈ€