๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๋ฐฑ์—”๋“œ/Spring boot

Feign Client๋ฅผ ๊ฐ์‹ธ๋Š” Facade Service Class

by sh119 2025. 4. 14.

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 ๊ณ„์ธต์— ์กด์žฌํ•ด์•ผํ•œ๋‹ค.