1. ํ์ ์ฌ์ ์๊ฑด & ์์คํ ์๊ตฌ์ฌ์ (Prerequisites)
๐ป ์์คํ ์ค์น ๋ฐ ์คํ ์๊ตฌ์ฌ์ (System Requirements)
| ํญ๋ชฉ | ์ต์ ์ฌ์ (Minimum) | ๊ถ์ฅ ์ฌ์ (Recommended) | ์ฐธ๊ณ ๋ฐ ๋น๊ณ |
|---|---|---|---|
| ์ด์์ฒด์ (OS) | Linux, macOS, Windows 10/11 (WSL2 ๊ถ์ฅ) | Ubuntu 22.04/24.04 LTS, macOS Apple Silicon, Windows 11 | Java 17/21 ์ง์ ๋ชจ๋ OS ํ๊ฒฝ ํธํ |
| CPU | 2 ์ฝ์ด ์ด์ | 4 ์ฝ์ด ~ 8 ์ฝ์ด ์ด์ | ArchUnit ๋ชจ๋ ์ ์ ๋ถ์ ๋ฐ ํ ์คํธ ์คํ ์๋ ํฅ์ |
| ๋ฉ๋ชจ๋ฆฌ (RAM) | 4 GB ์ด์ | 8 GB ~ 16 GB ์ด์ | ๋ก์ปฌ DB(PostgreSQL/MySQL), Docker ๋ฐ IDE ๋์ ๊ตฌ๋ ์ |
| ๋์คํฌ (Storage) | 5 GB ์ด์์ ์ฌ์ ๊ณต๊ฐ | 15 GB ์ด์์ ๊ณ ์ NVMe SSD | Gradle/Maven ์บ์ ๋ฐ PlantUML ๋ค์ด์ด๊ทธ๋จ ์์ฑ ๊ณต๊ฐ |
| JDK ๋ฐํ์ | OpenJDK 17 / 21 LTS ์ด์ | OpenJDK 21 LTS ๋๋ 25 LTS | Record, Pattern Matching, Virtual Threads ์ต์ ํ |
| Spring Boot | Spring Boot 3.3.x ์ด์ | Spring Boot 3.4.x ๋๋ 4.x ์ต์ ๋ฒ์ | Spring Modulith 1.3+ / 1.4+ ํธํ |
| ๋น๋ ๋๊ตฌ | Gradle 8.5+ ๋๋ Maven 3.9+ | Gradle 8.8+ (Kotlin/Groovy DSL) | ๋ฉํฐ ํ๋ก์ ํธ๊ฐ ์๋ ๋จ์ผ ํ๋ก์ ํธ ๊ตฌ์ฑ ๊ถ์ฅ |
๐ฆ ๋น๋ ์ค์ & ์์กด์ฑ ์ถ๊ฐ (Dependencies)
Spring Modulith๋ BOM(Bill of Materials)์ ํตํด ๊ด๋ จ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ ๋ฒ์ ์ ์ผ๊ด๋๊ฒ ๊ด๋ฆฌํฉ๋๋ค.
Gradle (build.gradle)
plugins {
id 'java'
id 'org.springframework.boot' version '3.3.5'
id 'io.spring.dependency-management' version '1.1.6'
}
java {
toolchain {
languageVersion = JavaLanguageVersion.of(21)
}
}
dependencyManagement {
imports {
// Spring Modulith BOM ์ฐ๋ (๋ฒ์ 1.3.x ์ด์)
mavenBom 'org.springframework.modulith:spring-modulith-bom:1.3.2'
}
}
dependencies {
// 1. Spring Boot ๊ธฐ๋ณธ ์คํํฐ
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
implementation 'org.springframework.boot:spring-boot-starter-validation'
// 2. Spring Modulith ํต์ฌ ๋ชจ๋
implementation 'org.springframework.modulith:spring-modulith-starter-core'
// 3. Event Publication Registry (JDBC / JPA ๊ธฐ๋ฐ ์์ํ: ์ด๋ฒคํธ ์ ์ค ๋ฐฉ์ง)
implementation 'org.springframework.modulith:spring-modulith-starter-jdbc'
// PostgreSQL / MySQL / H2 ๋๋ผ์ด๋ฒ (์ฌ์ฉ ํ๊ฒฝ์ ๋ง๊ฒ ์ถ๊ฐ)
runtimeOnly 'org.postgresql:postgresql'
testImplementation 'com.h2database:h2'
// 4. ์ก์ถ์์ดํฐ & ๊ด์ธก ๊ฐ๋ฅ์ฑ (๋ชจ๋ ๋ฉํธ๋ฆญ ๋ชจ๋ํฐ๋ง)
implementation 'org.springframework.modulith:spring-modulith-actuator'
// 5. ํ
์คํธ ๋ฐ ์ํคํ
์ฒ ์๋ ๋ฌธ์ํ (PlantUML, C4, ์บ๋ฒ์ค)
testImplementation 'org.springframework.boot:spring-boot-starter-test'
testImplementation 'org.springframework.modulith:spring-modulith-starter-test'
testImplementation 'org.springframework.modulith:spring-modulith-docs'
}
Maven (pom.xml)
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-bom</artifactId>
<version>1.3.2</version>
<type>pom</type>
<scope>import</scope>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<!-- Core & Starter -->
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-starter-core</artifactId>
</dependency>
<!-- JDBC Event Publication Registry -->
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-starter-jdbc</artifactId>
</dependency>
<!-- Testing & Documentation -->
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.modulith</groupId>
<artifactId>spring-modulith-docs</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
2. ์ง๊ด์ ์ธ ๋ฉํ ๋ชจ๋ธ (Mental Model & Architecture)
Spring Modulith๋ **โ๋ชจ๋๋ฆฌ์ค์ ๊ฐ๋ฐยท๋ฐฐํฌ ๋จ์ํจโ**๊ณผ **โ๋ง์ดํฌ๋ก์๋น์ค(MSA)์ ๋ช ํํ ๋๋ฉ์ธ ๊ฒฝ๊ณโ**๋ผ๋ ๋ ๋ง๋ฆฌ ํ ๋ผ๋ฅผ ๋จ์ผ ์คํ๋ง ๋ถํธ ์ ํ๋ฆฌ์ผ์ด์ ์์์ ์ก์ ์ ์๊ฒ ํด์ฃผ๋ ํ๋ ์์ํฌ์ ๋๋ค.
๐๏ธ ์ ํต์ ์ํคํ ์ฒ vs Modular Monolith ๋น๊ต
[1. ์ ํต์ ๊ณ์ธตํ ๋ชจ๋๋ฆฌ์ค (Layered Spaghetti Monolith)]
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
โ Controllers (OrderController, InventoryController...) โ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค
โ Services (OrderService โโโบ InventoryService โโโบ ...) โ โ ํจํค์ง ๊ฐ์์ฑ ํต์ ๋ถ๊ฐ
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโค โ ๋ชจ๋ ๋น์ด ์๋ก ์ฐธ์กฐ ๊ฐ๋ฅ
โ Repositories(OrderRepo, InventoryRepo, UserRepo...) โ โ ์ํจ ์์กด์ฑ(์คํ๊ฒํฐ)
โโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโโ
[2. Spring Modulith ๋ชจ๋ํ ๋ชจ๋๋ฆฌ์ค (Modular Monolith)]
Root: com.example.shop
โโโ order (๋ชจ๋ A)
โ โโโ OrderService (Public API) โโโ (๋์จํ ๋๋ฉ์ธ ์ด๋ฒคํธ ๋ฐํ)
โ โโโ OrderCompletedEvent โ
โ โโโ internal/ (ํจํค์ง ํ๋ผ์ด๋น) โ
โ โโโ OrderEntity โผ
โโโ inventory (๋ชจ๋ B) โโโโโโโโโโโโโโโโ (OrderCompletedEvent ์์ )
โ โโโ InventoryService (Public API)
โ โโโ internal/
โ โโโ InventoryRepository (์ธ๋ถ ๋ชจ๋์์ ์ง์ ์ ๊ทผ ์ ๋ ๋ถ๊ฐ!)
โโโ notification (๋ชจ๋ C)
โโโ NotificationListener (@ApplicationModuleListener)
๐ ๋ชจ๋ ์บก์ํ ๊ท์น (Encapsulation Rules)
- **๋ฃจํธ ํจํค์ง์ 1๋จ๊ณ ํ์ ํจํค์ง๊ฐ ๊ฐ๊ฐ ๋
๋ฆฝ์ ์ธ ๋๋ฉ์ธ ๋ชจ๋(Application Module)**์ด ๋ฉ๋๋ค.
- ์:
com.example.shop.order,com.example.shop.inventory
- ์:
- ๋ชจ๋ ๋ฃจํธ์ ํด๋์ค(
public)๋ง ์ธ๋ถ ๋ชจ๋์ ๋ ธ์ถ๋๋ ๊ณต์ API์ ๋๋ค. - ๋ชจ๋ ๋ด๋ถ์ ํ์ ํจํค์ง(์:
internal,infra,domain)๋ ์ธ๋ถ ๋ชจ๋์์ ์ง์ ํธ์ถํ ์ ์์ผ๋ฉฐ ์ปดํ์ผ/ํ ์คํธ ๋จ๊ณ์์ ์๊ฒฉํ ์ฐจ๋จ๋ฉ๋๋ค. - ๋ชจ๋ ๊ฐ์ ํ๋ ฅ์ ๊ณต๊ฐ API ์ธํฐํ์ด์ค ํธ์ถ ๋๋ **๋น๋๊ธฐ ๋๋ฉ์ธ ์ด๋ฒคํธ(Application Event)**๋ฅผ ํตํด ์ด๋ฃจ์ด์ง๋๋ค.
3. [์ด๋ณด์ 1๋จ๊ณ] ํฌ๋ก์๋ / ์ต์ ๋์ ์์ (Minimal Runnable Example)
๊ฐ์ฅ ๊ฐ๋จํ ์ฃผ๋ฌธ(order) ๋ชจ๋๊ณผ ์ฌ๊ณ (inventory) ๋ชจ๋์ ๊ตฌ์ถํ๊ณ , ๋ชจ๋ ๊ฒฝ๊ณ๊ฐ ์ฌ๋ฐ๋ฅธ์ง ๊ฒ์ฆํ๋ ์ต์ ๋์ ์์ ์
๋๋ค.
๐ 1. ๊ถ์ฅ ๋๋ ํ ๋ฆฌ ํจํค์ง ๊ตฌ์กฐ
src/main/java/com/example/shop
โโโ ShopApplication.java // @SpringBootApplication (๋ฃจํธ)
โโโ order/ // [Order ๋ชจ๋]
โ โโโ OrderService.java // ์ธ๋ถ ๊ณต๊ฐ API (Public Service)
โ โโโ OrderCompletedEvent.java // ์ธ๋ถ ๊ณต๊ฐ ์ด๋ฒคํธ (Record)
โ โโโ internal/ // ๐ ์ธ๋ถ ์ ๊ทผ ์ฐจ๋จ ๋ด๋ถ ํจํค์ง
โ โโโ OrderEntity.java // ๋ด๋ถ ๋๋ฉ์ธ ์ํฐํฐ
โโโ inventory/ // [Inventory ๋ชจ๋]
โโโ InventoryService.java // ์ธ๋ถ ๊ณต๊ฐ API
โโโ internal/
โโโ InventoryEventListener.java // OrderCompletedEvent ๋น๋๊ธฐ ๋ฆฌ์ค๋
โ 2. ๋ฉ์ธ ์ ํ๋ฆฌ์ผ์ด์
(ShopApplication.java)
package com.example.shop;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class ShopApplication {
public static void main(String[] args) {
SpringApplication.run(ShopApplication.class, args);
}
}
๐ฆ 3. ์ฃผ๋ฌธ ๋ชจ๋ ๋ฐ ๋๋ฉ์ธ ์ด๋ฒคํธ (order)
package com.example.shop.order;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.UUID;
// 1. ์ธ๋ถ ๋ชจ๋์ ์๋ฆด ๋ถ๋ณ ๋๋ฉ์ธ ์ด๋ฒคํธ (Java Record ๊ถ์ฅ)
public record OrderCompletedEvent(String orderId, String productId, int quantity) {}
// 2. ์ฃผ๋ฌธ ๋ชจ๋์ ์ธ๋ถ ๊ณต๊ฐ ์๋น์ค
@Service
public class OrderService {
private final ApplicationEventPublisher eventPublisher;
public OrderService(ApplicationEventPublisher eventPublisher) {
this.eventPublisher = eventPublisher;
}
@Transactional
public String createOrder(String productId, int quantity) {
String orderId = "ORD-" + UUID.randomUUID().toString().substring(0, 8);
System.out.printf("[Order ๋ชจ๋] ์ฃผ๋ฌธ ์์ฑ ์๋ฃ: %s (์ํ: %s, ์๋: %d)%n", orderId, productId, quantity);
// ๋ค๋ฅธ ๋ชจ๋์ ์ง์ ์ฃผ์
๋ฐ์ง ์๊ณ , ๋๋ฉ์ธ ์ด๋ฒคํธ ๋ฐํ!
eventPublisher.publishEvent(new OrderCompletedEvent(orderId, productId, quantity));
return orderId;
}
}
๐ฆ 4. ์ฌ๊ณ ๋ชจ๋ ๋ฐ ์ด๋ฒคํธ ๋ฆฌ์ค๋ (inventory)
package com.example.shop.inventory.internal;
import com.example.shop.order.OrderCompletedEvent;
import org.springframework.modulith.events.ApplicationModuleListener;
import org.springframework.stereotype.Component;
// ๐ internal ํจํค์ง์ ์์นํ์ฌ order ๋ชจ๋ ๋ฑ ์ธ๋ถ์ ๋
ธ์ถ๋์ง ์์
@Component
public class InventoryEventListener {
// Spring Modulith ์ ์ฉ ๋ฆฌ์ค๋ (ํธ๋์ญ์
์ปค๋ฐ ํ ๋น๋๊ธฐ ์์ ์คํ)
@ApplicationModuleListener
void onOrderCompleted(OrderCompletedEvent event) {
System.out.printf("[Inventory ๋ชจ๋] ์ฃผ๋ฌธ ์ด๋ฒคํธ ์์ โ ์ฌ๊ณ ์ฐจ๊ฐ ์ฒ๋ฆฌ: ์ฃผ๋ฌธ๋ฒํธ=%s, ์ฐจ๊ฐ์๋=%d%n",
event.orderId(), event.quantity());
}
}
๐งช 5. ๋ชจ๋ ๊ตฌ์กฐ ๊ฒ์ฆ ํ
์คํธ (ModularityTests.java)
package com.example.shop;
import org.junit.jupiter.api.Test;
import org.springframework.modulith.core.ApplicationModules;
import org.springframework.modulith.docs.Documenter;
class ModularityTests {
// ๋ฃจํธ ์ ํ๋ฆฌ์ผ์ด์
๊ธฐ์ค์ผ๋ก ์ ์ฒด ๋ชจ๋ ๊ตฌ์กฐ ์ค์บ
ApplicationModules modules = ApplicationModules.of(ShopApplication.class);
@Test
void verifiesModularArchitecture() {
// 1. ๋ชจ๋ ๊ฒฝ๊ณ, ๊ฐ์์ฑ ์๋ฐ, ์ํ ์์กด์ฑ ์ฌ๋ถ๋ฅผ ์ฆ์ ๊ฒ์ฆ!
modules.verify();
System.out.println("โ
Spring Modulith: ๋ชจ๋ ๋๋ฉ์ธ ๋ชจ๋ ๊ฒฝ๊ณ ๊ท์น์ด ์ ์ ์ค์๋์์ต๋๋ค.");
}
@Test
void createModuleDocumentation() {
// 2. build/spring-modulith-docs ๋๋ ํ ๋ฆฌ์ PlantUML ๋ฐ Markdown ์บ๋ฒ์ค ์์ฑ
new Documenter(modules)
.writeDocumentation()
.writeModulesAsPlantUml();
System.out.println("๐ Spring Modulith: ๋ชจ๋ ์ํคํ
์ฒ ๋ค์ด์ด๊ทธ๋จ ๋ฐ ๋ฌธ์๊ฐ ์์ฑ๋์์ต๋๋ค.");
}
}
์คํ ์ ์ฝ์ ์์ ์ถ๋ ฅ (Console Output):
โ
Spring Modulith: ๋ชจ๋ ๋๋ฉ์ธ ๋ชจ๋ ๊ฒฝ๊ณ ๊ท์น์ด ์ ์ ์ค์๋์์ต๋๋ค.
[Order ๋ชจ๋] ์ฃผ๋ฌธ ์์ฑ ์๋ฃ: ORD-7f41a8b9 (์ํ: PROD-100, ์๋: 2)
[Inventory ๋ชจ๋] ์ฃผ๋ฌธ ์ด๋ฒคํธ ์์ โ ์ฌ๊ณ ์ฐจ๊ฐ ์ฒ๋ฆฌ: ์ฃผ๋ฌธ๋ฒํธ=ORD-7f41a8b9, ์ฐจ๊ฐ์๋=2
4. [2๋จ๊ณ] ํต์ฌ ๊ธฐ๋ฅ 1: ๋ชจ๋ ๊ฒฝ๊ณ ๊ท์น๊ณผ ํจํค์ง ์๋ (Enforcing Boundaries)
Spring Modulith๋ ์ ์ ์ฝ๋ ๋ถ์ ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ธ ArchUnit์ ๊ธฐ๋ฐ์ผ๋ก ๋น๋/ํ ์คํธ ์์ ์ ์ํคํ ์ฒ ๊ท์น์ ๊ฐ์ ํฉ๋๋ค.
๐ซ ์๋ชป๋ ์ฐธ์กฐ ์์ (์ปดํ์ผ ๋๋ ํ ์คํธ ์คํจ)
๋ง์ฝ order ๋ชจ๋์ ์๋น์ค๊ฐ inventory ๋ชจ๋์ internal ํจํค์ง์ ์๋ ๋ฆฌํฌ์งํ ๋ฆฌ๋ ํด๋์ค๋ฅผ ์ง์ importํ์ฌ ์ฃผ์
๋ฐ์ผ๋ ค๊ณ ํ๋ฉด:
package com.example.shop.order;
// โ ์๋ฐ: ๋ค๋ฅธ ๋ชจ๋์ internal ํจํค์ง๋ฅผ ์ง์ import ์๋!
import com.example.shop.inventory.internal.InventoryRepository;
@Service
public class BrokenOrderService {
// private final InventoryRepository repo; -> modules.verify() ์คํ ์ ์ฆ์ ์์ธ ๋ฐ์!
}
modules.verify() ์คํจ ์ ์ถ๋ ฅ๋๋ ์๋ฌ ๋ฉ์์ง:
org.springframework.modulith.core.Violations:
- Module 'order' depends on non-exposed type com.example.shop.inventory.internal.InventoryRepository in module 'inventory'!
- Method <com.example.shop.order.BrokenOrderService> depends on non-public API!
๐ท๏ธ 1. @NamedInterface๋ก ๋ช
์์ ๋ชจ๋ API ๊ณต๊ฐํ๊ธฐ
๋ชจ๋ ๋ฃจํธ ํจํค์ง ์ธ์ ํ์ ํจํค์ง์ ํน์ ์ธํฐํ์ด์ค๋ DTO๋ง ์ธ๋ถ๋ก ๊ณต๊ฐํ๊ณ ์ถ๋ค๋ฉด package-info.java์ @NamedInterface๋ฅผ ์ฌ์ฉํฉ๋๋ค.
// ํ์ผ ์์น: src/main/java/com/example/shop/inventory/client/package-info.java
@org.springframework.modulith.NamedInterface("client-api")
package com.example.shop.inventory.client;
์ธ๋ถ ๋ชจ๋์์๋ @ApplicationModule(allowedDependencies = "inventory::client-api")์ ๊ฐ์ด ํน์ ๋ช
๋ช
๋ ์ธํฐํ์ด์ค์๋ง ์์กดํ๋๋ก ์ ๋ฐ ์ ์ด๊ฐ ๊ฐ๋ฅํฉ๋๋ค.
๐ 2. ์ํ ์ฐธ์กฐ(Cyclic Dependencies) ์์ฒ ์ฐจ๋จ
Order๋ชจ๋์ดPayment๋ชจ๋์ ์ฐธ์กฐํ๊ณ ,Payment๋ชจ๋์ด ๋ค์Order๋ชจ๋์ ์ฐธ์กฐํ๋ **์ํ ์์กด์ฑ(Cycle)**์ด ๊ฐ์ง๋๋ฉด ํ ์คํธ๊ฐ ์ฆ๊ฐ ์คํจํฉ๋๋ค.- ํด๊ฒฐ์ฑ
: ๋๊ธฐ ๋ฉ์๋ ์ง์ ํธ์ถ ๋์ **์คํ๋ง ์ ํ๋ฆฌ์ผ์ด์
์ด๋ฒคํธ(
publishEvent)**๋ฅผ ๋ฐํํ์ฌ ์์กด ๋ฐฉํฅ์ ๋จ๋ฐฉํฅ์ผ๋ก ํด์ํฉ๋๋ค.
5. [2๋จ๊ณ] ํต์ฌ ๊ธฐ๋ฅ 2: ๋ชจ๋ ๊ฐ ์ด๋ฒคํธ & Event Publication Registry
Spring Modulith์ ๊ฐ์ฅ ๊ฐ๋ ฅํ ๊ธฐ๋ฅ ์ค ํ๋๋ **โ๋ชจ๋ ๊ฐ ํธ๋์ญ์ ๋ถ๋ฆฌโ**์ **โ์ด๋ฒคํธ ์ ์ค ๋ฐฉ์ง(Outbox ํจํด ๋ด์ฅ)โ**์ ๋๋ค.
โก @ApplicationModuleListener ๋์ ์๋ฆฌ
์ ํต์ ์ธ @EventListener๋ ๋ฐํ์์ ํธ๋์ญ์
์์์ ๋๊ธฐ๋ก ์คํ๋์ด, ๊ตฌ๋
์์ ์์ธ๊ฐ ๋ฐํ์์ ๋กค๋ฐฑ์ ์ ๋ฐํ ์ํ์ด ์์ต๋๋ค.
๋ฐ๋ฉด @ApplicationModuleListener๋ ๋ค์ 3๊ฐ์ง ์ด๋
ธํ
์ด์
์ ํฉ์ฑ์ฒด์
๋๋ค:
@Async: ๋ณ๋ ์ค๋ ๋์์ ๋น๋๊ธฐ ์ฒ๋ฆฌ@Transactional(propagation = REQUIRES_NEW): ์๋ก์ด ๋ ๋ฆฝ ํธ๋์ญ์ ๋ณด์ฅ@TransactionalEventListener(phase = AFTER_COMMIT): ๋ฐํ์ ํธ๋์ญ์ ์ด ์ฑ๊ณต์ ์ผ๋ก DB์ ์ปค๋ฐ๋ ์งํ์๋ง ์คํ
package com.example.shop.notification.internal;
import com.example.shop.order.OrderCompletedEvent;
import org.springframework.modulith.events.ApplicationModuleListener;
import org.springframework.stereotype.Component;
@Component
public class NotificationService {
@ApplicationModuleListener
public void sendOrderNotification(OrderCompletedEvent event) {
// ์ฃผ๋ฌธ DB ์ ์ฅ์ด 100% ์๋ฃ๋ ํ์๋ง ์นด์นด์ค ์๋ฆผํก/์ด๋ฉ์ผ ๋ฐ์ก
System.out.printf("๐ฑ [์๋ฆผ ๋ฐ์ก] ์ฃผ๋ฌธ ๋ฒํธ %s ๊ณ ๊ฐ๋๊ป ๊ตฌ๋งค ํ์ธ ๋ฉ์์ง ์ ์ก%n", event.orderId());
}
}
๐ก๏ธ Event Publication Registry (ํธ๋์ญ์ ์์๋ฐ์ค)
๋ง์ฝ ์ด๋ฒคํธ๋ฅผ ๋ฐํํ๊ณ ์ปค๋ฐ๋์๋๋ฐ, ๋น๋๊ธฐ ๋ฆฌ์ค๋ ์คํ ์ง์ ์ ์๋ฒ๊ฐ ์ฌ์์๋๊ฑฐ๋ ์ธ๋ถ ๋คํธ์ํฌ ์ค๋ฅ๊ฐ ๋ฐ์ํ๋ฉด ์ด๋ฒคํธ๊ฐ ์ ์ค๋ ๊น์?
Spring Modulith๋ EVENT_PUBLICATION ํ
์ด๋ธ์ ํ์ฉํ์ฌ ์ด๋ฒคํธ ์ ์ค์ 100% ๋ฐฉ์งํฉ๋๋ค.
[1. ์ฃผ๋ฌธ ํธ๋์ญ์
์์]
โโโ Order DB Insert
โโโ EVENT_PUBLICATION ํ
์ด๋ธ์ ์ด๋ฒคํธ ์ง๋ ฌํ(JSON) ๋ฐ ์ํ(PENDING) ๊ธฐ๋ก (๋์ผ ํธ๋์ญ์
)
[2. ์ฃผ๋ฌธ ํธ๋์ญ์
์ปค๋ฐ (COMMIT)]
[3. @ApplicationModuleListener ๋น๋๊ธฐ ์คํ]
โโโ ์ฑ๊ณต ์ โ EVENT_PUBLICATION ์ํ๋ฅผ COMPLETED๋ก ๊ฐฑ์ (๋๋ ์๋ฃ์ผ์ ๊ธฐ๋ก)
โโโ ์๋ฒ ๋ค์ด ๋๋ ์์ธ ๋ฐ์ ์ โ PENDING ์ํ ์ ์ง โ ์ฌ์๋ ์ ์๋ ์ฌ์ฒ๋ฆฌ!
DDL ์คํค๋ง ์๋ ์์ฑ (PostgreSQL / H2 ๊ธฐ์ค):
CREATE TABLE IF NOT EXISTS event_publication (
id UUID PRIMARY KEY,
listener_id VARCHAR(512) NOT NULL,
event_type VARCHAR(512) NOT NULL,
serialized_event TEXT NOT NULL,
publication_date TIMESTAMP WITH TIME ZONE NOT NULL,
completion_date TIMESTAMP WITH TIME ZONE
);
๋ฏธ์๋ฃ ์ด๋ฒคํธ ์ฌ์๋ ์ค์ผ์ค๋ฌ (IncompleteEventPublications)
package com.example.shop;
import org.springframework.modulith.events.IncompleteEventPublications;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import java.time.Duration;
@Component
public class EventResubmissionScheduler {
private final IncompleteEventPublications incompleteEvents;
public EventResubmissionScheduler(IncompleteEventPublications incompleteEvents) {
this.incompleteEvents = incompleteEvents;
}
// 10๋ถ๋ง๋ค 5๋ถ ์ด์ ์๋ฃ๋์ง ์์ ๋ฏธ์๋ฃ ์ด๋ฒคํธ๋ฅผ ์ฌ์๋
@Scheduled(fixedDelay = 600000)
public void resubmitFailedEvents() {
incompleteEvents.resubmitIncompletePublicationsOlderThan(Duration.ofMinutes(5));
}
}
6. [3๋จ๊ณ] ์ค๋ฌด ์์ฉ 1: ์ํคํ ์ฒ ๋ค์ด์ด๊ทธ๋จ & ์บ๋ฒ์ค ์๋ํ (Documenter)
์ฝ๋๊ฐ ๋ณ๊ฒฝ๋ ๋๋ง๋ค ์ํคํ
์ฒ ๋ฌธ์๋ฅผ ์๋์ผ๋ก ์์ ํ ํ์๊ฐ ์์ต๋๋ค. Spring Modulith์ Documenter๋ ์ค์ ํจํค์ง ๊ตฌ์กฐ์ ์ด๋ฒคํธ ํ๋ฆ์ ๋ถ์ํ์ฌ ๋ค์ด์ด๊ทธ๋จ์ ์๋ ์์ฑํฉ๋๋ค.
package com.example.shop;
import org.junit.jupiter.api.Test;
import org.springframework.modulith.core.ApplicationModules;
import org.springframework.modulith.docs.Documenter;
import org.springframework.modulith.docs.Documenter.CanvasOptions;
class ArchitectureDocumentationTests {
ApplicationModules modules = ApplicationModules.of(ShopApplication.class);
@Test
void generateArchitectureDocumentation() {
new Documenter(modules)
// 1. ์ปดํฌ๋ํธ ๊ฐ ์์กด์ฑ ๋ค์ด์ด๊ทธ๋จ (PlantUML / C4 Model)
.writeModulesAsPlantUml()
.writeIndividualFilesAsPlantUml()
// 2. ๋ชจ๋ ์บ๋ฒ์ค (Asciidoc / Markdown ํ์ ์์ฝ ๋ณด๊ณ ์)
.writeModuleCanvases(CanvasOptions.defaults());
}
}
์์ฑ๋๋ ๊ฒฐ๊ณผ๋ฌผ (build/spring-modulith-docs/):
components.puml: ๋ชจ๋ ๊ฐ ์ ์ฒด ์ปดํฌ๋ํธ ๋ค์ด์ด๊ทธ๋จmodule-order.puml: order ๋ชจ๋ ๊ด์ ์ ์์กด์ฑ ๋ค์ด์ด๊ทธ๋จmodule-inventory.puml: inventory ๋ชจ๋ ๊ด์ ์ ์์กด์ฑ ๋ค์ด์ด๊ทธ๋จcanvas.adoc: ๋ชจ๋๋ณ ๊ณต๊ฐ API, ์์ /๋ฐํ ์ด๋ฒคํธ ๋ชฉ๋ก ์์ฝ ์บ๋ฒ์ค
7. [3๋จ๊ณ] ์ค๋ฌด ์์ฉ 2: ๋ชจ๋ ์ฌ๋ผ์ด์ค ํ
์คํธ (@ApplicationModuleTest)
์ ์ฒด ์คํ๋ง ์ปจํ
์คํธ(@SpringBootTest)๋ ๋ชจ๋์ด ๋ง์์ง์๋ก ๊ธฐ๋ ์๋๊ฐ ๋๋ ค์ง๋๋ค.
@ApplicationModuleTest๋ฅผ ์ฌ์ฉํ๋ฉด ํ
์คํธ ๋์ ๋ชจ๋๊ณผ ํด๋น ๋ชจ๋์ด ํ์๋ก ํ๋ ๋น๋ง ๊ฒฉ๋ฆฌ ๋ก๋ฉํ์ฌ ์ด๊ณ ์ ๋จ์ ํตํฉ ํ
์คํธ๋ฅผ ์ํํ ์ ์์ต๋๋ค.
package com.example.shop.order;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.modulith.test.ApplicationModuleTest;
import org.springframework.modulith.test.Scenario;
// Order ๋ชจ๋๋ง ๋จ๋
๊ฒฉ๋ฆฌํ์ฌ ์คํ๋ง ์ปจํ
์คํธ ๋ก๋ฉ!
@ApplicationModuleTest
class OrderModuleIsolationTest {
@Autowired
OrderService orderService;
@Test
void orderPlacementPublishesEvent(Scenario scenario) {
// Scenario API๋ฅผ ํตํ ์ด๋ฒคํธ ๋ฐํ ๋จ์ธ (Assertion)
scenario.stimulate(() -> orderService.createOrder("PROD-555", 3))
.andWaitForEventOfType(OrderCompletedEvent.class)
.toArriveAndVerify(event -> {
assert event.productId().equals("PROD-555");
assert event.quantity() == 3;
});
}
}
8. [3๋จ๊ณ] ์ค๋ฌด ์์ฉ 3: ๋ชจ๋ํ ๋ชจ๋๋ฆฌ์ค์์ MSA๋ก์ ์ ์ง์ ๋ถ๋ฆฌ
์๋น์ค ํธ๋ํฝ์ด ๊ธ์ฆํ์ฌ ํน์ ๋ชจ๋(์: inventory)์ ๋
๋ฆฝ๋ ๋ง์ดํฌ๋ก์๋น์ค(MSA)๋ก ๋ถ๋ฆฌํด์ผ ํ ๋, Spring Modulith๋ ์ต์์ ๋ง์ด๊ทธ๋ ์ด์
๊ธฐ๋ฐ์ ์ ๊ณตํฉ๋๋ค.
๐ฃ๏ธ MSA ์ ํ 4๋จ๊ณ ๋ก๋๋งต
- 1๋จ๊ณ (๊ธฐ๋ฐ ํ๋ณด): ๋ชจ๋ ๋ชจ๋ ๊ฐ ์ง์ ์ฐธ์กฐ๋ฅผ ์ ๊ฑฐํ๊ณ
@ApplicationModuleListener๊ธฐ๋ฐ ๋๋ฉ์ธ ์ด๋ฒคํธ๋ก ์ ํ (์ด๋ฏธ ์๋ฃ๋จ). - 2๋จ๊ณ (์ธ๋ถ ๋ฉ์์ง ๋ธ๋ก์ปค ์ฐ๋):
- ์ธ๋ฉ๋ชจ๋ฆฌ ์ด๋ฒคํธ ๋์
spring-modulith-events-kafka๋๋spring-modulith-events-amqp์คํํฐ ์ถ๊ฐ. - ์ค์ ํ ์ค๋ก ๋๋ฉ์ธ ์ด๋ฒคํธ๋ฅผ Apache Kafka ํ ํฝ์ด๋ RabbitMQ ์ต์ค์ฒด์ธ์ง๋ก ์๋ ๋ผ์ฐํ .
- ์ธ๋ฉ๋ชจ๋ฆฌ ์ด๋ฒคํธ ๋์
- 3๋จ๊ณ (DB ์คํค๋ง ๋ถ๋ฆฌ):
- ๋จ์ผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ด์์ ๋ชจ๋๋ณ ํ
์ด๋ธ ์คํค๋ง ๋ถ๋ฆฌ (
order_schema.*,inventory_schema.*).
- ๋จ์ผ ๋ฐ์ดํฐ๋ฒ ์ด์ค ๋ด์์ ๋ชจ๋๋ณ ํ
์ด๋ธ ์คํค๋ง ๋ถ๋ฆฌ (
- 4๋จ๊ณ (๋
๋ฆฝ ๋ฆฌํฌ์งํ ๋ฆฌ ๋ถ๋ฆฌ):
inventoryํจํค์ง๋ฅผ ๋ณ๋ ๋ ๋ฆฝ Spring Boot ํ๋ก์ ํธ๋ก ๋ณต์ฌ ๋ฐ ๋ฐฐํฌ. ์ด๋ฏธ ์ด๋ฒคํธ ๊ธฐ๋ฐ์ผ๋ก ํต์ ํ๊ณ ์์ผ๋ฏ๋ก ์ฝ๋ ๋ณ๊ฒฝ ์ต์ํ!
// Kafka๋ก ๋๋ฉ์ธ ์ด๋ฒคํธ๋ฅผ ์๋ ๋ธ๋ก๋์บ์คํ
ํ๋ ์คํํฐ
dependencies {
implementation 'org.springframework.modulith:spring-modulith-events-kafka'
}
9. ์ค๋ฌด ์ฃผ์์ฌํญ & ํ๋ก๋์ ์ฒดํฌ๋ฆฌ์คํธ (Best Practices)
โ ๏ธ ํํ ๊ฒช๋ ์ค์ & ํธ๋ฌ๋ธ์ํ
- ํ
์คํธ์์
modules.verify()์คํ ์ ์ํ ์ฐธ์กฐ ์๋ฌ ๋ฐ์:- ์์ธ: A ๋ชจ๋์ด B ๋ชจ๋์ ํด๋์ค๋ฅผ ์ฐ๊ณ , B ๋ชจ๋๋ A ๋ชจ๋์ DTO๋ ํด๋์ค๋ฅผ ์ฐธ์กฐ.
- ์กฐ์น: ๊ณตํต ํ์
์
shared๋๋common๋ฃจํธ ๋ชจ๋๋ก ์ด๋์ํค๊ฑฐ๋, ๋ชจ๋ ๊ฐ ์๋ต์ ์์ฒญ-์๋ต์ด ์๋ ์ด๋ฒคํธ ๋ฐํ ๋ฐฉ์์ผ๋ก ๋ณ๊ฒฝ.
@ApplicationModuleListener์คํ ์ค ๋กค๋ฐฑ ๋ฐ์ ์:- ๋ฆฌ์ค๋ ๋ด๋ถ์์ ์์ธ๊ฐ ๋ฐ์ํ๋๋ผ๋ ๋ฐํ์์ ์๋ ์ฃผ๋ฌธ ํธ๋์ญ์
์ ์ด๋ฏธ ์ฑ๊ณต์ ์ผ๋ก ์ปค๋ฐ๋ ์ํ(
AFTER_COMMIT)์ด๋ฏ๋ก ์ฃผ๋ฌธ ๋ฐ์ดํฐ๋ ์์ ํ๊ฒ ๋ณด์กด๋ฉ๋๋ค. - ๋ฆฌ์ค๋ ์คํจ ๊ฑด์
EVENT_PUBLICATIONํ ์ด๋ธ์ ๋ฏธ์๋ฃ ์ํ๋ก ๋จ์ ๊ด๋ฆฌ์ ์๋ฆผ ๋๋ ์ฌ์๋ ์ค์ผ์ค๋ฌ๋ก ์์ ํ๊ฒ ๋ณต๊ตฌํ ์ ์์ต๋๋ค.
- ๋ฆฌ์ค๋ ๋ด๋ถ์์ ์์ธ๊ฐ ๋ฐ์ํ๋๋ผ๋ ๋ฐํ์์ ์๋ ์ฃผ๋ฌธ ํธ๋์ญ์
์ ์ด๋ฏธ ์ฑ๊ณต์ ์ผ๋ก ์ปค๋ฐ๋ ์ํ(
- Event Publication Registry ํ
์ด๋ธ ๋ฏธ์์ฑ ์ค๋ฅ:
spring.modulith.events.jdbc.schema-initialization.enabled=true์ต์ ์ ์ค์ ํ๊ฑฐ๋, Flyway/Liquibase ๋ง์ด๊ทธ๋ ์ด์ ์คํฌ๋ฆฝํธ์ ๊ณต์ DDL์ ๋ฏธ๋ฆฌ ๋ฑ๋กํด์ผ ํฉ๋๋ค.
โ ํ๋ก๋์ ๋ฐฐํฌ ์ฒดํฌ๋ฆฌ์คํธ
- CI ํ์ดํ๋ผ์ธ์์
modules.verify()๋จ์ ํ ์คํธ๊ฐ ํญ์ ํต๊ณผํ๋์ง ํ์ธ. - ์ด์ RDBMS์
event_publicationํ ์ด๋ธ ์์ฑ ๋ฐ ์ธ๋ฑ์ค(publication_date,completion_date) ์ค์ ์๋ฃ. - ๋ฏธ์๋ฃ ์ด๋ฒคํธ ๋ชจ๋ํฐ๋ง ๋ฐ ์ฌ์๋ ์ค์ผ์ค๋ฌ(
IncompleteEventPublications) ๋น ๋ฑ๋ก. - Actuator ์๋ํฌ์ธํธ(
/actuator/modulith) ์ ๊ทผ ๊ถํ ๋ฐ ๋ชจ๋ ๋ฉํธ๋ฆญ ์์ง ์ค์ . - ์ต์ Java 21 Record์ ์์ฑ์ ๊ธฐ๋ฐ ๋ถ๋ณ ์ด๋ฒคํธ ๊ฐ์ฒด ์ค๊ณ ์ค์.
์๊ฒฌ ๋ฐ ์ง๋ฌธ
0์์ง ๋ฑ๋ก๋ ์๊ฒฌ์ด ์์ต๋๋ค. ์ฒซ ๋ฒ์งธ ๋๊ธ์ ๋จ๊ฒจ๋ณด์ธ์!
๋๊ธ ์์
๋๊ธ ์ญ์