๐MSA ๊ตฌ์กฐ์์ ์ํท ๋ธ๋ ์ด์ปค์ ์ญํ
์ํท ๋ธ๋ ์ด์ปค(Circuit Breaker)๋ ๋ถ์ฐ ์์คํ ์์ ๋ฐ์ํ ์ ์๋ ์ฅ์ ์ ๋์ํ๊ธฐ ์ํ ๋์์ธ ํจํด ์ค ํ๋๋ก, ํนํ MSA(Microservices Architecture) ๊ตฌ์กฐ์์ ์ค์ํ ์ญํ ์ ํ๋ค. ์ํท ๋ธ๋ ์ด์ปค๋ ๋คํธ์ํฌ๋ ์๋น์ค ๊ฐ์ ํต์ ์์ ๋ฐ์ํ ์ ์๋ ์ฅ์ ๋ ์ง์ฐ์ผ๋ก ์ธํ ์ ์ฒด ์์คํ ์ ์ฑ๋ฅ ์ ํ๋ฅผ ๋ฐฉ์งํ๊ณ ์์ ์ฑ์ ์ ์งํ๋ ๋ฐ ์ฌ์ฉ๋๋ค.
์ํท๋ธ๋ ์ด์ปค์ ์ฃผ์ ๊ธฐ๋ฅ์ ๋ค์๊ณผ ๊ฐ๋ค.
- ์ฅ์ ํํผ (Fault Tolerance): ์ํท ๋ธ๋ ์ด์ปค๋ ํน์ ์๋น์ค๋ ๋คํธ์ํฌ ํธ์ถ์ ์ฅ์ ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ, ํด๋น ํธ์ถ์ ํํผํ๊ณ ์ฆ์ ์๋ตํ๋ ๋ฐ ์ฌ์ฉ๋๋ค. ์ด๋ฅผ ํตํด ์ฅ์ ๊ฐ ์ ์ฒด ์์คํ ์ ์ ํ๋๋ ๊ฒ์ ๋ง์ ์์ ์ฑ์ ํ๋ณดํ๋ค.
- Timeout ์ค์ : ์ํท ๋ธ๋ ์ด์ปค๋ ํธ์ถ๋ ์๋น์ค์ ์๋ต ์๊ฐ์ ๋ชจ๋ํฐ๋งํ๊ณ , ์ง์ ๋ ์๊ฐ ๋ด์ ์๋ต์ด ์์ผ๋ฉด ํ๋ก๋ฅผ ์ด์ด ๋ค์ ์์ฒญ์ ์ฐจ๋จํ๋ค.
- Fallback ๊ธฐ๋ฅ: ์ํท ๋ธ๋ ์ด์ปค๊ฐ ํ๋ก๋ฅผ ์ด์ด ์์ฒญ์ ์ฐจ๋จํ ๊ฒฝ์ฐ, ๋์ฒด ๋ก์ง ๋๋ ๊ธฐ๋ณธ๊ฐ์ ์ ๊ณตํ์ฌ ์ ์ฒด ์์คํ ์ ๊ธฐ๋ฅ์ ์ ์งํ ์ ์๋๋ก ํฉ๋๋ค. ์ด๋ฅผ ํตํด ์ฌ์ฉ์ ๊ฒฝํ์ด๋ ์์คํ ์ฑ๋ฅ์ ์ ์งํ ์ ์๋ค.
- ์ค์๊ฐ ๋ชจ๋ํฐ๋ง ๋ฐ ๊ฒฝ๊ณ : ์ํท ๋ธ๋ ์ด์ปค๋ ํธ์ถ๋ ์๋น์ค์ ์ํ๋ฅผ ๋ชจ๋ํฐ๋งํ๊ณ , ์ฑ๋ฅ์ด๋ ์ฅ์ ๊ฐ ๋ฐ์ํ ๊ฒฝ์ฐ ๊ด๋ฆฌ์์๊ฒ ๊ฒฝ๊ณ ๋ฅผ ๋ณด๋ด๊ฑฐ๋ ๋ชจ๋ํฐ๋ง ๋์๋ณด๋๋ฅผ ํตํด ์ค์๊ฐ์ผ๋ก ํ์ธํ ์ ์๋ ๊ธฐ๋ฅ์ ์ ๊ณตํ๋ค.
์ํท ๋ธ๋ ์ด์ปค์ 3๊ฐ์ง ์ํ
Open/Closed/ํํฅ๋ ์ํ (Open/Closed/ Half-Open States)
- Open(์ด๋ฆผ) ์ํ: ์ผ์ ์๊ฐ ๋์ ์ง์์ ์ผ๋ก ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ํ๋ก๋ฅผ ์ด์ด์ ํฅํ ์์ฒญ์ ์ฐจ๋จํ๋ค.
- Closed(๋ซํ) ์ํ: ์๋น์ค ํธ์ถ์ด ์ ์์ ์ผ๋ก ์ด๋ฃจ์ด์ง๋ฉด ํ๋ก๋ฅผ ๋ซ์์ ์์ฒญ์ ํ์ฉํ๋ค.
- Half-Open(ํํฅ๋) ์ํ: ์ฃผ๊ธฐ์ ์ผ๋ก ์ผ๋ถ ์์ฒญ์ ์๋ํ์ฌ ์๋น์ค์ ์ํ๋ฅผ ํ์ธํ๊ณ , ๋ฌธ์ ๊ฐ ์๋ค๋ฉด ๋ค์ Closed ์ํ๋ก ์ ํ๋๋ค.
๊ทธ๋ ๋ค๋ฉด ํ๋ก์ ํธ์ ์ํท๋ธ๋ ์ด์ปค๋ฅผ ์ ์ฉํ ๋ถ๋ถ์ ์ด๋์ผ๊น?
- ์ธ๋ถ ์๋น์ค(๋ง์ดํฌ๋ก ์๋น์ค๋ API) ํธ์ถ
- ์ธ๋ถ ์๋น์ค๊ฐ ์๋ตํ์ง ์๊ฑฐ๋ ์๋ต ์๊ฐ์ด ์ง์ฐ๋๋ ๊ฒฝ์ฐ, ์ํท ๋ธ๋ ์ด์ปค๋ ํด๋น ์๋น์ค ํธ์ถ์ ์ค๋จํ๊ณ ๋น ๋ฅด๊ฒ ์คํจ ์๋ต์ ๋ฐํํ์ฌ ์ ์ฒด ์์คํ ์ ์์ ์ฑ์ ์ ์งํ๋ค.
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ํธ์ถ
- ๋ฐ์ดํฐ๋ฒ ์ด์ค ์๋ฒ์ ๋ํ ์ฐ๊ฒฐ์ด ์คํจํ๊ฑฐ๋ ์๋ต์ด ์ง์ฐ๋ ๋ ์ํท ๋ธ๋ ์ด์ปค๊ฐ ํด๋น ์ฐ๊ฒฐ์ ๋๊ณ ๋์ฒด ๋ก์ง์ด๋ ๊ธฐ๋ณธ๊ฐ์ ์ฌ์ฉํ์ฌ ์์คํ ์ ์ผ๊ด์ฑ์ ์ ์งํ ์ ์๋ค.
- ์ธ๋ถ ๋ฆฌ์์ค ์ฌ์ฉ (ํ์ผ ์์คํ , ๋ฉ์์ง ํ, ์ธ๋ถ ์ ์ฅ์ ๋ฑ)
์ํท๋ธ๋ ์ด์ปค๋ ํต์ฌ๊ณ ๋ ์ํคํ ์ฒ ์ค ์ด๋์ ์ ์ฉ์์ผ์ผํ ๊น?
Adapter/Application ์ด๋์๋ ์ ์ฉ๋์ด๋ ๊ด์ฐฎ์ง๋ง ์ผ๋ฐ์ ์ผ๋ก๋ ์ธ๋ถ ์๋น์ค ํธ์ถ์ด๋ ๋ฐ์ดํฐ ์์ค์์ ํตํฉ ์ง์ ์์ ์ํท ๋ธ๋ ์ด์ปค๋ฅผ ์ ์ฉํ๋ ๊ฒ์ด ์ผ๋ฐ์ ์ด๋ค.
๐Resilience4j - ์ํท ๋ธ๋ ์ด์ปค ์ ์ฉ
spring cloud hystrix์์ resilience4j ๋ฅผ ๋ ๋ง์ด ์ฐ๊ธฐ ์์ํ๋ค. ๋ค์ ๊ณผ์ ์ Resilience4j๋ฅผ ํ๋ก์ ํธ์ ์ ์ฉํด๋ณด๋ ์ฝ๋์ด๋ค.
1. pom.xml์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ ์ถ๊ฐ
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-circuitbreaker-resilience4j</artifactId>
<version>2.1.7</version>
</dependency>
2. ์์กด์ฑ ์ฃผ์
private final CircuitBreaker
3. ์ํท ๋ธ๋ ์ด์ปค๋ฅผ ์ฌ์ฉํ ์ฝ๋์ ๊ฐ์ธ์ค๋ค.
์ํท ๋ธ๋ ์ด์ปค ์ ์ฉ ์
@WebAdapter
@RequiredArgsConstructor
public class GetProductImageServiceAdapter implements GetProductImagePort {
private final OpenFeignGetProductImages openFeignGetProductImages;
@Override
public List<ProductImage> getProductImagePort(Long id) {
List<ProductImage> productImages = openFeignGetProductImages.call(id);
return productImages;
}
}
์ํท ๋ธ๋ ์ด์ปค ์ ์ฉ ํ
@WebAdapter
@RequiredArgsConstructor
public class GetProductImageServiceAdapter implements GetProductImagePort {
private final OpenFeignGetProductImages openFeignGetProductImages;
private final CircuitBreakerFactory circuitBreakerFactory;
@Override
public List<ProductImage> getProductImagePort(Long id) {
CircuitBreaker circuitBreaker = circuitBreakerFactory.create("getProductImage");
List<ProductImage> productImages = circuitBreaker.run(() ->
openFeignGetProductImages.call(id)
, throwable -> null
);
return productImages;
}
}
๊ฐ์ผ ์ฝ๋๊ฐ ์๋ฌ๊ฐ ๋ฐ์ํ๋ฉด ์ํท๋ธ๋ ์ด์ปค๋ฅผ ์คํํด์ ๋ง๋๋ก ์๋ํ๋ค.