๐Ÿ›๏ธ

Spring Modulith ์น˜ํŠธ์‹œํŠธ

Spring Modulith ์ตœ์‹  1.3+ / 1.4+ ๋ฐ Spring Boot 3.4+ / 4.x ๊ธฐ์ค€: DDD ๊ธฐ๋ฐ˜ ๋ชจ๋“ˆํ˜• ๋ชจ๋†€๋ฆฌ์Šค(Modular Monolith) ์•„ํ‚คํ…์ฒ˜ ์™„๋ฒฝ ๊ฐ€์ด๋“œ. ๋„๋ฉ”์ธ ๋ชจ๋“ˆ ๊ฒฝ๊ณ„ ๊ฒฉ๋ฆฌ ๋ฐ ๊ฒ€์ฆ(verify), ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ๋ชจ๋“ˆ ์ด๋ฒคํŠธ(@ApplicationModuleListener), ํŠธ๋žœ์žญ์…˜ ์ด๋ฒคํŠธ ๋ฐœํ–‰ ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ(Event Publication Registry), PlantUML/C4 ๋‹ค์ด์–ด๊ทธ๋žจ ๋ฐ ์บ”๋ฒ„์Šค ์ž๋™ ๋ฌธ์„œํ™”, ๋ชจ๋“ˆ ๋‹จ์œ„ ์Šฌ๋ผ์ด์Šค ํ…Œ์ŠคํŠธ(@ApplicationModuleTest) ๋ฐ MSA ์ ์ง„์  ๋ถ„๋ฆฌ ์‹ค์ „ ๊ฐ€์ด๋“œ

--views
๋‹จ์ˆ˜:
#spring-modulith#spring-boot#modular-monolith#ddd#domain-driven-design#event-publication-registry#archunit#application-modules#plantuml#c4-model#java#backend์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-10-24

1. ํ•„์ˆ˜ ์‚ฌ์ „ ์š”๊ฑด & ์‹œ์Šคํ…œ ์š”๊ตฌ์‚ฌ์–‘ (Prerequisites)

๐Ÿ’ป ์‹œ์Šคํ…œ ์„ค์น˜ ๋ฐ ์‹คํ–‰ ์š”๊ตฌ์‚ฌ์–‘ (System Requirements)

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (Minimum)๊ถŒ์žฅ ์‚ฌ์–‘ (Recommended)์ฐธ๊ณ  ๋ฐ ๋น„๊ณ 
์šด์˜์ฒด์ œ (OS)Linux, macOS, Windows 10/11 (WSL2 ๊ถŒ์žฅ)Ubuntu 22.04/24.04 LTS, macOS Apple Silicon, Windows 11Java 17/21 ์ง€์› ๋ชจ๋“  OS ํ™˜๊ฒฝ ํ˜ธํ™˜
CPU2 ์ฝ”์–ด ์ด์ƒ4 ์ฝ”์–ด ~ 8 ์ฝ”์–ด ์ด์ƒArchUnit ๋ชจ๋“ˆ ์ •์  ๋ถ„์„ ๋ฐ ํ…Œ์ŠคํŠธ ์‹คํ–‰ ์†๋„ ํ–ฅ์ƒ
๋ฉ”๋ชจ๋ฆฌ (RAM)4 GB ์ด์ƒ8 GB ~ 16 GB ์ด์ƒ๋กœ์ปฌ DB(PostgreSQL/MySQL), Docker ๋ฐ IDE ๋™์‹œ ๊ตฌ๋™ ์‹œ
๋””์Šคํฌ (Storage)5 GB ์ด์ƒ์˜ ์—ฌ์œ  ๊ณต๊ฐ„15 GB ์ด์ƒ์˜ ๊ณ ์† NVMe SSDGradle/Maven ์บ์‹œ ๋ฐ PlantUML ๋‹ค์ด์–ด๊ทธ๋žจ ์ƒ์„ฑ ๊ณต๊ฐ„
JDK ๋Ÿฐํƒ€์ž„OpenJDK 17 / 21 LTS ์ด์ƒOpenJDK 21 LTS ๋˜๋Š” 25 LTSRecord, Pattern Matching, Virtual Threads ์ตœ์ ํ™”
Spring BootSpring 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. **๋ฃจํŠธ ํŒจํ‚ค์ง€์˜ 1๋‹จ๊ณ„ ํ•˜์œ„ ํŒจํ‚ค์ง€๊ฐ€ ๊ฐ๊ฐ ๋…๋ฆฝ์ ์ธ ๋„๋ฉ”์ธ ๋ชจ๋“ˆ(Application Module)**์ด ๋ฉ๋‹ˆ๋‹ค.
    • ์˜ˆ: com.example.shop.order, com.example.shop.inventory
  2. ๋ชจ๋“ˆ ๋ฃจํŠธ์˜ ํด๋ž˜์Šค(public)๋งŒ ์™ธ๋ถ€ ๋ชจ๋“ˆ์— ๋…ธ์ถœ๋˜๋Š” ๊ณต์‹ API์ž…๋‹ˆ๋‹ค.
  3. ๋ชจ๋“ˆ ๋‚ด๋ถ€์˜ ํ•˜์œ„ ํŒจํ‚ค์ง€(์˜ˆ: internal, infra, domain)๋Š” ์™ธ๋ถ€ ๋ชจ๋“ˆ์—์„œ ์ง์ ‘ ํ˜ธ์ถœํ•  ์ˆ˜ ์—†์œผ๋ฉฐ ์ปดํŒŒ์ผ/ํ…Œ์ŠคํŠธ ๋‹จ๊ณ„์—์„œ ์—„๊ฒฉํžˆ ์ฐจ๋‹จ๋ฉ๋‹ˆ๋‹ค.
  4. ๋ชจ๋“ˆ ๊ฐ„์˜ ํ˜‘๋ ฅ์€ ๊ณต๊ฐœ 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๊ฐ€์ง€ ์–ด๋…ธํ…Œ์ด์…˜์˜ ํ•ฉ์„ฑ์ฒด์ž…๋‹ˆ๋‹ค:

  1. @Async: ๋ณ„๋„ ์Šค๋ ˆ๋“œ์—์„œ ๋น„๋™๊ธฐ ์ฒ˜๋ฆฌ
  2. @Transactional(propagation = REQUIRES_NEW): ์ƒˆ๋กœ์šด ๋…๋ฆฝ ํŠธ๋žœ์žญ์…˜ ๋ณด์žฅ
  3. @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. 1๋‹จ๊ณ„ (๊ธฐ๋ฐ˜ ํ™•๋ณด): ๋ชจ๋“  ๋ชจ๋“ˆ ๊ฐ„ ์ง์ ‘ ์ฐธ์กฐ๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  @ApplicationModuleListener ๊ธฐ๋ฐ˜ ๋„๋ฉ”์ธ ์ด๋ฒคํŠธ๋กœ ์ „ํ™˜ (์ด๋ฏธ ์™„๋ฃŒ๋จ).
  2. 2๋‹จ๊ณ„ (์™ธ๋ถ€ ๋ฉ”์‹œ์ง€ ๋ธŒ๋กœ์ปค ์—ฐ๋™):
    • ์ธ๋ฉ”๋ชจ๋ฆฌ ์ด๋ฒคํŠธ ๋Œ€์‹  spring-modulith-events-kafka ๋˜๋Š” spring-modulith-events-amqp ์Šคํƒ€ํ„ฐ ์ถ”๊ฐ€.
    • ์„ค์ • ํ•œ ์ค„๋กœ ๋„๋ฉ”์ธ ์ด๋ฒคํŠธ๋ฅผ Apache Kafka ํ† ํ”ฝ์ด๋‚˜ RabbitMQ ์ต์Šค์ฒด์ธ์ง€๋กœ ์ž๋™ ๋ผ์šฐํŒ….
  3. 3๋‹จ๊ณ„ (DB ์Šคํ‚ค๋งˆ ๋ถ„๋ฆฌ):
    • ๋‹จ์ผ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค ๋‚ด์—์„œ ๋ชจ๋“ˆ๋ณ„ ํ…Œ์ด๋ธ” ์Šคํ‚ค๋งˆ ๋ถ„๋ฆฌ (order_schema.*, inventory_schema.*).
  4. 4๋‹จ๊ณ„ (๋…๋ฆฝ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ ๋ถ„๋ฆฌ):
    • inventory ํŒจํ‚ค์ง€๋ฅผ ๋ณ„๋„ ๋…๋ฆฝ Spring Boot ํ”„๋กœ์ ํŠธ๋กœ ๋ณต์‚ฌ ๋ฐ ๋ฐฐํฌ. ์ด๋ฏธ ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜์œผ๋กœ ํ†ต์‹ ํ•˜๊ณ  ์žˆ์œผ๋ฏ€๋กœ ์ฝ”๋“œ ๋ณ€๊ฒฝ ์ตœ์†Œํ™”!
// Kafka๋กœ ๋„๋ฉ”์ธ ์ด๋ฒคํŠธ๋ฅผ ์ž๋™ ๋ธŒ๋กœ๋“œ์บ์ŠคํŒ…ํ•˜๋Š” ์Šคํƒ€ํ„ฐ
dependencies {
    implementation 'org.springframework.modulith:spring-modulith-events-kafka'
}

9. ์‹ค๋ฌด ์ฃผ์˜์‚ฌํ•ญ & ํ”„๋กœ๋•์…˜ ์ฒดํฌ๋ฆฌ์ŠคํŠธ (Best Practices)

โš ๏ธ ํ”ํžˆ ๊ฒช๋Š” ์‹ค์ˆ˜ & ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ…

  1. ํ…Œ์ŠคํŠธ์—์„œ modules.verify() ์‹คํ–‰ ์‹œ ์ˆœํ™˜ ์ฐธ์กฐ ์—๋Ÿฌ ๋ฐœ์ƒ:
    • ์›์ธ: A ๋ชจ๋“ˆ์ด B ๋ชจ๋“ˆ์˜ ํด๋ž˜์Šค๋ฅผ ์“ฐ๊ณ , B ๋ชจ๋“ˆ๋„ A ๋ชจ๋“ˆ์˜ DTO๋‚˜ ํด๋ž˜์Šค๋ฅผ ์ฐธ์กฐ.
    • ์กฐ์น˜: ๊ณตํ†ต ํƒ€์ž…์€ shared ๋˜๋Š” common ๋ฃจํŠธ ๋ชจ๋“ˆ๋กœ ์ด๋™์‹œํ‚ค๊ฑฐ๋‚˜, ๋ชจ๋“ˆ ๊ฐ„ ์‘๋‹ต์„ ์š”์ฒญ-์‘๋‹ต์ด ์•„๋‹Œ ์ด๋ฒคํŠธ ๋ฐœํ–‰ ๋ฐฉ์‹์œผ๋กœ ๋ณ€๊ฒฝ.
  2. @ApplicationModuleListener ์‹คํ–‰ ์ค‘ ๋กค๋ฐฑ ๋ฐœ์ƒ ์‹œ:
    • ๋ฆฌ์Šค๋„ˆ ๋‚ด๋ถ€์—์„œ ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋”๋ผ๋„ ๋ฐœํ–‰์ž์˜ ์›๋ž˜ ์ฃผ๋ฌธ ํŠธ๋žœ์žญ์…˜์€ ์ด๋ฏธ ์„ฑ๊ณต์ ์œผ๋กœ ์ปค๋ฐ‹๋œ ์ƒํƒœ(AFTER_COMMIT)์ด๋ฏ€๋กœ ์ฃผ๋ฌธ ๋ฐ์ดํ„ฐ๋Š” ์•ˆ์ „ํ•˜๊ฒŒ ๋ณด์กด๋ฉ๋‹ˆ๋‹ค.
    • ๋ฆฌ์Šค๋„ˆ ์‹คํŒจ ๊ฑด์€ EVENT_PUBLICATION ํ…Œ์ด๋ธ”์— ๋ฏธ์™„๋ฃŒ ์ƒํƒœ๋กœ ๋‚จ์•„ ๊ด€๋ฆฌ์ž ์•Œ๋ฆผ ๋˜๋Š” ์žฌ์‹œ๋„ ์Šค์ผ€์ค„๋Ÿฌ๋กœ ์•ˆ์ „ํ•˜๊ฒŒ ๋ณต๊ตฌํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
  3. 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
0 / 1500