전체 글
-
7장 - 아키텍처 요소 테스트하기클린 코드(Clean Code)/만들면서 배우는 클린 아키텍처 요약 2023. 3. 1. 00:01
이번장에서는 육각형 아키텍처의 각 요소들을 테스트할 수 있는 테스트 유형에 대해 논의합니다. 단위 테스트로 도메인 엔티티 테스트하기 Account의 상태를 특정 시점의 계좌 잔고와 그 이후 입출금 내역으로 구성되어 있습니다. withdraw() 메서드가 기대한 대로 동작하는지 검증하려면 단지 도메인 엔티티인 Account를 인스턴스화 하고 메서드를 호출하여 출금이 성공하는지 검증하고 객체의 상태에 기대되는 부수효과들이 잘 일어났는지 확인하는 단순한 단위 테스트를 수행할 수 있습니다. 도메인 엔티티는 다른 클래스에 거의 의존하지 않기 때문에 다른 종류의 테스트는 필요하지 않습니다. 단위 테스트로 유스케이스 테스트하기 테스트 가독성을 높이기 위해 given/when/then 섹션으로 나누어 작성하였습니다. ..
-
서킷 브레이커 구성하기프로젝트/선착순 쿠폰 발급 시스템 2023. 2. 28. 00:01
개요 Client 모듈을 생성 외부 서버를 호출하는 테스트를 수행해 보았습니다. 하지만 외부 서비스에 장애가 발생하면 어떻게 될까요? 내부 서비스까지 같이 장애가 전파되어 버릴 수 있습니다. 이를 막는 방법중 하나가 서킷 브레이커입니다. 서킷 브레이커란? 장애가 특정 임계값에 도달하면 오류와 함께 반환하는 간단한 아이디어입니다. 메서드 호출이 실패할 때 장애가 연쇄적으로 발생하는 것을 방지하고 실패한 서비스를 복구할 수 있는 시간을 제공합니다. 서킷 브레이커 적용하기 1. build.gradle.kts 수정 //Circuit Breaker implementation("io.github.resilience4j:resilience4j-spring-boot2:1.7.0") //kotlin-logging imp..
-
Client 모듈 구성하기프로젝트/선착순 쿠폰 발급 시스템 2023. 2. 27. 00:01
Client 모듈이란? 우리가 서버를 구성하다 보면 또 다른 서버로부터 데이터를 요청받고 처리해야 할 수 있습니다. 이때 Rest 방식으로 API를 호출하기 위해서 RestTemplate, WebClient, FeignClient 등이 있습니다. 이렇게 외부 서비스를 호출하기 위해서 Client 모듈을 두고 해당 모듈에 다른 서버로부터 데이터를 요청받고 처리하는 코드를 작성하고자 합니다. 이렇게 되면 외부 호출건에 대한 수정은 Client 모듈에서 이루어지게 됩니다. Client 모듈 구성하기 1. settings.gradle.kts 수정 //추가 include("client") 2. 프로젝트 root에 client 디렉터리 생성 및 gradle refresh 3. buidl.gradle.kts에 의존성..
-
6장 - 영속성 어댑터 구현하기클린 코드(Clean Code)/만들면서 배우는 클린 아키텍처 요약 2023. 2. 26. 00:01
애플리케이션 서비스에서는 영속성 기능을 사용하기 위해 포트 인터페이스를 호출합니다. 이 포트는 실제로 영속성 작업을 수행하고 데이터베이스와 통신할 책임을 가진 영속성 클래스에 의해 구현됩니다. 영속성 어댑터는 주도되는 어댑터이며 애플리케이션에 의해 호출될 뿐, 애플리케이션을 호출하지 않습니다. 영속성 어댑터의 책임 - 입력을 받는다 - 입력을 데이터베이스 포맷으로 매핑한다 - 입력을 데이터베이스로 보낸다 - 데이터베이스 출력을 애플리케이션 포맷으로 매핑한다 - 출력을 반환한다 포트 인터페이스 분리하기 ISP에 의거하여 하나의 비대한 포트를 갖는 것보다 서비스가 실제로 필요한 메서드만 사용하는 것이 좋습니다. 영속성 어댑터 분리하기 영속성 어댑터 클래스도 기능에 따라 경계를 나눌 수 있습니다. 스프링 데이..
-
5장 - 웹 어댑터 구현하기클린 코드(Clean Code)/만들면서 배우는 클린 아키텍처 요약 2023. 2. 25. 00:01
웹 어댑터는 주도하는 인커밍 어댑터입니다. 외부로부터 요청을 받아 애플리케이션 코어를 호출하고 무슨 일을 해야 할지 알려줍니다. 이때 제어 흐름은 웹 어뎁터에 있는 컨트롤러에서 애플리케이션 계층에 있는 서비스로 흐릅니다. 어댑터의 책임 - HTTP 요청을 자바 객체로 매핑 - 권한 검사 - 입력 유효성 검증 - 입력을 유스케이스의 입력 모델로 매핑 - 유스케이스 호출 - 유스케이스의 호출을 HTTP로 매핑 - HTTP 응답을 반환 HTTP와 관련된 것은 애플리케이션 계층으로 침투해서는 안됩니다. 컨트롤러 나누기 컨트롤러는 너무 적은 것보다는 너무 많은 게 낫습니다. 클래스 코드가 많아지게 되면 파악하는데 난이도가 높아지게 됩니다. 가급적 메서드와 클래스명은 유스케이스를 최대한 반영해 지어내고 별도의 컨트..
-
4장 - 유스케이스 구현하기클린 코드(Clean Code)/만들면서 배우는 클린 아키텍처 요약 2023. 2. 24. 00:01
도메인 모델 구현하기 한 계좌에서 다른 계좌로 송금하는 유스케이스를 구현합니다. 객체지향적인 방식으로 모델링하기 위해 입금과 출금을 할 수 있는 Account 엔티티를 만들고 출금 계좌에서 돈을 출금해서 입금계좌로 돈을 입금해야 합니다. 해당 엔티티는 withdraw()라는 출금 메서드를 가지고, deposit()이라는 입금 메서드를 통해 입출금을 수행할 수 있습니다. 또한 출금하지 전에는 잔고를 초과하는 금액을 출금할 수 없도록 하는 비즈니스 규칙을 검사합니다. 유스케이스 둘러보기 일반적으로 유스케이스는 다음과 같은 단계를 따릅니다. - 입력 받기 - 비즈니스 규칙 검증 - 모델 상태 조작 - 출력 반환 유스케이스는 인커밍 어댑터로부터 입력을 받으며 이 단계에서 유효성 검증은 이루어지지 않습니다. 유스..
-
코틀린 멀티모듈 프로젝트 구성하기프로젝트/선착순 쿠폰 발급 시스템 2023. 2. 23. 00:01
목표 Kotlin DSL Gradle을 사용하여 멀티 모듈을 구성하는 프로젝트를 만들어보고자 합니다. 멀티 모듈이란? 멀티모듈에 대해서는 다음글에서 다룹니다. - 우아한 멀티모듈 by 권용근님 - 실전! 멀티 모듈 프로젝트 구조와 설계 - 김대성님 간단하게 하나의 Spring 프로젝트에서 여러 디렉터리 구조를 가지고 서버를 실행시킬 수 있도록 구성할 수 있습니다. 개인적인 공부용으로 사용하는 프로젝트이며 envers, executor등은 각각 실행될 수 있는 하나의 SpringApplication으로 구성되어 있고, core같은 모듈은 공통적으로 사용합니다. Spring Initializr를 통해 프로젝트 만들기 1. Spring Initializr 접속 2. 프로젝트 세팅 Gradle Project K..
-
3장 - 코드 구성하기클린 코드(Clean Code)/만들면서 배우는 클린 아키텍처 요약 2023. 2. 22. 00:01
계층으로 구성하기 웹, 도메인, 영속성 계층으로 패키지를 나눈 모습입니다. DIP를 통해 domain 패키지에 있는 코드만을 가리키도록 했습니다. 하지만 애플리케이션의 기능 조각이나 특성을 구분 짓는 패키지 경계가 없습니다. 관리자 기능을 추가하기 위해서는 web 패키지에 UserController를 추가하고 domain 패키지에 UserService, UserRepository, User를 추가하고, persistence 패키지에 UserRepositoryImpl을 추가해야 합니다. 이렇게 되면 서로 연관되지 않은 기능들끼리 예상하지 못한 부수효과를 일으킬 수 있게 됩니다. 또한 특정 기능을 찾기 위해 어떤 서비스가 이를 구현했는지 추측하고, 해당 서비스 내의 어떤 메서드가 그에 대한 책임을 수행하는지..