A-์๋น์ค ์์ Feign Client๋ฅผ ์ด์ฉํ์ฌ B-์๋น์ค๋ฅผ ํธ์ถํ ๋,
๊ธฐ์กด A-์๋น์ค์ Service Class์์ ์ง์ ํธ์ถํ๋ ๊ฒฝ์ฐ๊ฐ ์๋ค.
์ด๋ฌํ ๊ฒฝ์ฐ Application ๊ณ์ธต์์ Infrastructure ๊ณ์ธต์ ์ง์ ์ฌ์ฉํ๋ ๊ฒ์ด๋ฏ๋ก SRP์ DIP๊ฐ ์ ์ง์ผ์ง์ง ์๋๋ค.
๋ฐ๋ผ์ ์ด๋ฌํ ๊ฒฝ์ฐ B์๋น์ค๋ฅผ ํธ์ถํ๋ Service Class ๋ฅผ ํ์ฌ๋ํ์ฌ ๋ฐ๋ก ์์ฑํด์ฃผ๋๊ฒ ์ข๋ค.
์ฆ, Feign Client ํธ์ถ์ ์ํ Service Class๋ฅผ ์์ฑํ๋ ๊ฒ์ด๋ค.
@Service
@Slf4j(topic = "feign client : call product-service")
@RequiredArgsConstructor
public class ProductClientService {
private final ProductFeignClient productFeignClient;
public ProductInfo getProduct(UUID productId) {
return ProductInfo.from(productFeignClient.getProduct(productId));
}
}
๋ํ productFeignClient์์ ํธ์ถํ์ฌ ๋ด์ Response Dto๋ฅผ ๋ฐํ๊ฐ์ผ๋ก ์ฌ์ฉํ์ง ์๊ณ ,
ProductInfo ๋ผ๋ Dto๋ฅผ ์์ฑํ์ฌ ๋ด๋ถ์ฉ Dto๋ฅผ ๋ง๋ ๋ค.
์ด๋ ๊ฒ ํ๊ฒ๋๋ฉด getProduct์ ์๋ต๊ฐ์ด ๋ด๋ถ์ฉ Dto์ด๋ฏ๋ก ๊ธฐ์กด Application ๊ณ์ธต์ Service์์๋ Infrastructure๊ณ์ธต์ ๊ทธ ๋ฌด์๋ ์ ํ์๊ฐ ์์ด์ง๋ค.
์ฆ, ์บก์ํ๊ฐ ์งํ๋๊ณ SRP๊ฐ ์ ์ง์ผ์ง๊ฒ ๋๋ค. (+์ธํ๋ผ ์๋)
๋ง์ง๋ง์ผ๋ก ProductInfo๋ฅผ ๊ตฌํํ ์์์ด๋ค.
public record ProductInfo(
UUID productId,
String title,
String description,
BigDecimal price
) {
public static ProductInfo from(ProductReadResponse response) {
return new ProductInfo(response.getProductId(), response.getTitle(),
response.getDescription(), response.getPrice());
}
}
ํด๋น ProductInfo๋ Infrastructure ๊ณ์ธต์ด ์๋, application ๊ณ์ธต์ ์กด์ฌํด์ผํ๋ค.
'๋ฐฑ์๋ > Spring boot' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Entity ์์ ์กฐ๊ฑด์ ์๋์ผ๋ก ๋ถ์ฌ์ฃผ๋ ๋ฐฉ๋ฒ๋ค (@SQLRestriction, @Where, @Filter) (0) | 2025.04.14 |
---|---|
Feign Client ํธ์ถ ์๋ต Dto์ ํ๋๋ช ์ด ๋ค๋ฅผ๋ (0) | 2025.04.14 |
Spring Boot์์ Bean ์๋ช ์ฃผ๊ธฐ์ ์ด๊ธฐํ ๋ฐ ์๋ฉธ ๊ณผ์ (0) | 2025.03.10 |
API Gateway ์ Kafka (0) | 2025.02.26 |
Spring Boot ๋ฉํฐ ๋ชจ๋ ํ๋ก์ ํธ ์ค๊ณ (0) | 2025.02.25 |