โ˜๏ธ

Spring Cloud & MSA ์น˜ํŠธ์‹œํŠธ

Spring Boot 3.4+ / 4.x ๋ฐ Spring Cloud 2024.x/2025.x ์ตœ์‹  ์™„์ „ ๊ฐ€์ด๋“œ: Eureka ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ, Spring Cloud Gateway ๋™์  ๋ผ์šฐํŒ… ๋ฐ JWT ๊ธ€๋กœ๋ฒŒ ํ•„ํ„ฐ, OpenFeign ์„ ์–ธ์  ์„œ๋น„์Šค ๊ฐ„ ํ†ต์‹ , Resilience4j ์„œํ‚ท ๋ธŒ๋ ˆ์ด์ปค & Fallback, Spring Cloud Config ์ค‘์•™ ์ง‘์ค‘ ์„ค์ • ๊ด€๋ฆฌ, Micrometer Tracing/Zipkin ๋ถ„์‚ฐ ์ถ”์  ๋ฐ MSA ๋ถ„์‚ฐ ํŠธ๋žœ์žญ์…˜ Saga ํŒจํ„ด

--views
๋‹จ์ˆ˜:
#spring-cloud#msa#microservices#spring-boot#eureka#gateway#openfeign#resilience4j#config-server#distributed-tracing#backend#java์ตœ์ข… ์—…๋ฐ์ดํŠธ: 2026-10-10

1. ์‚ฌ์ „ ์š”๊ฑด & ํ”„๋กœ์ ํŠธ ๋นŒ๋“œ ๊ตฌ์„ฑ (Prerequisites & Setup)

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

ํ•ญ๋ชฉ์ตœ์†Œ ์‚ฌ์–‘ (Minimum)๊ถŒ์žฅ ์‚ฌ์–‘ (Recommended)์ฐธ๊ณ  ๋ฐ ๋น„๊ณ 
์šด์˜์ฒด์ œ (OS)Windows 10/11, macOS, LinuxUbuntu 22.04 LTS+, macOS Sonoma, Windows 11 (WSL2)Java ๊ฐ€์ƒ ๋จธ์‹ (JVM) ์ง€์› 64-bit OS
CPU4 ์ฝ”์–ด ์ด์ƒ (๋กœ์ปฌ ๋‹ค์ค‘ ์„œ๋น„์Šค ๊ตฌ๋™ ์‹œ)8 ์ฝ”์–ด ~ 16 ์ฝ”์–ด ์ด์ƒEureka, Gateway, ๋น„์ฆˆ๋‹ˆ์Šค ์„œ๋น„์Šค 2~3๊ฐœ ๋™์‹œ ์‹คํ–‰
๋ฉ”๋ชจ๋ฆฌ (RAM)4 GB ์ด์ƒ (๊ฐœ๋ฐœ/ํ…Œ์ŠคํŠธ)8 GB ~ 16 GB ์ด์ƒ์„œ๋น„์Šค ๋…ธ๋“œ 1๊ฐœ๋‹น JVM ํž™ ์•ฝ 512MB~1GB ํ•„์š”
๋””์Šคํฌ (Storage)2 GB ์ด์ƒ์˜ ์—ฌ์œ  ๊ณต๊ฐ„20 GB ์ด์ƒ์˜ ๊ณ ์† NVMe SSD๋นŒ๋“œ ์•„ํ‹ฐํŒฉํŠธ, ์˜์กด์„ฑ JAR ์บ์‹œ ๋ฐ Git Config ์Šคํ† ๋ฆฌ์ง€
๋„คํŠธ์›Œํฌ / ๋Ÿฐํƒ€์ž„Java 17 / 21 LTS / Spring Boot 3.3+ / Spring Cloud 2023.0+Java 21 / 25 LTS / Spring Boot 3.4+ / Spring Cloud 2024.0+ํฌํŠธ 8761(Eureka), 8080(Gateway), 8888(Config)

๐Ÿ“ฆ Spring Cloud BOM ๋ฐ ํ•ต์‹ฌ ์˜์กด์„ฑ ์„ค์ • (Dependencies)

Spring Cloud๋Š” Spring Boot ๋ฒ„์ „๊ณผ ์ •ํ™•ํžˆ ์ผ์น˜ํ•˜๋Š” **BOM(Bill of Materials)**์„ importํ•˜์—ฌ ์˜์กด์„ฑ ๊ฐ„์˜ ๋ฒ„์ „ ์ถฉ๋Œ์„ ์›์ฒœ ๋ฐฉ์ง€ํ•ฉ๋‹ˆ๋‹ค.

โ‘  Gradle (build.gradle)

plugins {
    id 'java'
    id 'org.springframework.boot' version '3.4.3'
    id 'io.spring.dependency-management' version '1.1.7'
}

ext {
    // Spring Boot 3.4.x ํ˜ธํ™˜ Spring Cloud 2024.0.x (Moorgate) BOM
    set('springCloudVersion', "2024.0.0")
}

dependencies {
    // [1] ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ๊ณตํ†ต ํด๋ผ์ด์–ธํŠธ (Eureka ๋“ฑ๋ก & ๋กœ๋“œ๋ฐธ๋Ÿฐ์‹ฑ)
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    
    // [2] ์„ ์–ธ์  HTTP ํด๋ผ์ด์–ธํŠธ (์„œ๋น„์Šค ๊ฐ„ ํ†ต์‹ )
    implementation 'org.springframework.cloud:spring-cloud-starter-openfeign'
    
    // [3] ์„œํ‚ท ๋ธŒ๋ ˆ์ด์ปค & ๋‚ด๊ฒฐํ•จ์„ฑ
    implementation 'org.springframework.cloud:spring-cloud-starter-circuitbreaker-resilience4j'
    
    // [4] ๊ด€์ธก์„ฑ & ๋ถ„์‚ฐ ์ถ”์  (Micrometer Tracing + Zipkin)
    implementation 'org.springframework.boot:spring-boot-starter-actuator'
    implementation 'io.micrometer:micrometer-tracing-bridge-brave'
    implementation 'io.zipkin.reporter2:zipkin-reporter-brave'

    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
    }
}

โ‘ก Maven (pom.xml)

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>org.springframework.cloud</groupId>
            <artifactId>spring-cloud-dependencies</artifactId>
            <version>2024.0.0</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

2. Spring Cloud MSA ํ•ต์‹ฌ ๋ฉ˜ํƒˆ ๋ชจ๋ธ & ์•„ํ‚คํ…์ฒ˜ (Architecture)

๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์•„ํ‚คํ…์ฒ˜(MSA)์—์„œ๋Š” ์ˆ˜์‹ญ~์ˆ˜๋ฐฑ ๊ฐœ์˜ ๋…๋ฆฝ ์„œ๋น„์Šค๊ฐ€ ๋™์ ์œผ๋กœ ํ™•์žฅ(Scale-out)๋˜๋ฏ€๋กœ, ๋‹จ์ผ ์ง„์ž…์ (Gateway), ๋™์  ์ฃผ์†Œ ๊ฒ€์ƒ‰(Eureka), ์žฅ์•  ๊ฒฉ๋ฆฌ(Resilience4j), **์ค‘์•™ ์„ค์ •(Config)**์ด ํ•„์ˆ˜์ ์ž…๋‹ˆ๋‹ค.

๐Ÿ“ MSA ํŒจํ‚ท ํ๋ฆ„ ๋ฐ ์ปดํฌ๋„ŒํŠธ ์•„ํ‚คํ…์ฒ˜ (ASCII Diagram)

[ Client Request ] (์™ธ๋ถ€ ๋ชจ๋ฐ”์ผ/์›น ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ)
       โ”‚
       โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚                    Spring Cloud Gateway (Port 8080)                         โ”‚
โ”‚                                                                             โ”‚
โ”‚  - ๋‹จ์ผ ์ง„์ž…์  (Single Entry Point) & SSL/TLS ์ข…๋‹จ                          โ”‚
โ”‚  - JWT ๊ธ€๋กœ๋ฒŒ ์ธ์ฆ ํ•„ํ„ฐ (Authentication Filter)                             โ”‚
โ”‚  - ๋กœ๋“œ๋ฐธ๋Ÿฐ์‹ฑ ๋ผ์šฐํŒ… (lb://ORDER-SERVICE, lb://PAYMENT-SERVICE)             โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
               โ”‚                                              โ”‚
               โ–ผ ์„œ๋น„์Šค ์ฃผ์†Œ ์งˆ์˜(Discovery)                   โ”‚ ๋ผ์šฐํŒ…(Routing)
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”                              โ”‚
โ”‚ Eureka Server (Port 8761)    โ”‚                              โ”‚
โ”‚  - ์„œ๋น„์Šค ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ         โ”‚                              โ”‚
โ”‚  - ํ•˜ํŠธ๋น„ํŠธ ์ƒํƒœ ๊ฐ์‹œ (UP)   โ”‚                              โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–ฒโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜                              โ”‚
               โ”‚                                              โ”‚
        โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ดโ”€โ”€โ”€โ”€โ”€โ”€โ”
        โ”‚ ์ฃผ๊ธฐ์  ๋“ฑ๋ก/ํ•˜ํŠธ๋น„ํŠธ        โ”‚ ์ฃผ๊ธฐ์  ๋“ฑ๋ก/ํ•˜ํŠธ๋น„ํŠธ         โ”‚
        โ–ผ                             โ–ผ                              โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”   OpenFeign ํ†ต์‹    โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Order Service (8081)    โ”‚ โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ–บ โ”‚ Payment Service (8082)  โ”‚
โ”‚  - ์ฃผ๋ฌธ ์ƒ์„ฑ            โ”‚   (๋™๊ธฐ HTTP ํ˜ธ์ถœ) โ”‚  - ๊ฒฐ์ œ ์Šน์ธ            โ”‚
โ”‚  - Resilience4j ์„œํ‚ท์ฐจ๋‹จโ”‚ โ—„โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ โ”‚  - ๊ฒฐ์ œ ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค    โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜   Fallback ์•ˆ์ „์‘๋‹ตโ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
            โ”‚                                               โ”‚
            โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”ฌโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                                    โ”‚ ๋ถ„์‚ฐ ํŠธ๋žœ์žญ์…˜ ์ด๋ฒคํŠธ ๋ฐœํ–‰
                                    โ–ผ
โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”
โ”‚ Kafka / RabbitMQ (๋น„๋™๊ธฐ ์ด๋ฒคํŠธ ๋ธŒ๋กœ์ปค - Saga Choreography ๋ถ„์‚ฐ ํŠธ๋žœ์žญ์…˜)   โ”‚
โ””โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜

๐Ÿงฉ 6๋Œ€ ํ•ต์‹ฌ ์ปดํฌ๋„ŒํŠธ ์—ญํ•  ๋น„๊ต

์ปดํฌ๋„ŒํŠธํ•ต์‹ฌ ์—ญํ• ์ฃผ์š” ์–ด๋…ธํ…Œ์ด์…˜ / ์„ค์ •
Eureka Server / Client๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ๋™์  ๋“ฑ๋ก(Registry) ๋ฐ IP/ํฌํŠธ ์ž๋™ ๊ฒ€์ƒ‰@EnableEurekaServer, @EnableDiscoveryClient
Spring Cloud GatewayAPI ๋‹จ์ผ ์ง„์ž…์ , URL ๊ฒฝ๋กœ ๋ผ์šฐํŒ…, ์ธ์ฆ ํ•„ํ„ฐ, Rate Limitingspring.cloud.gateway.routes, GlobalFilter
OpenFeign์ธํ„ฐํŽ˜์ด์Šค ์„ ์–ธ๋งŒ์œผ๋กœ ๋‹ค๋ฅธ ์„œ๋น„์Šค์˜ REST API๋ฅผ ํ˜ธ์ถœํ•˜๋Š” HTTP ํด๋ผ์ด์–ธํŠธ@EnableFeignClients, @FeignClient(name = "...")
Resilience4j์—ฐ์‡„ ์žฅ์•  ์ „ํŒŒ๋ฅผ ์ฐจ๋‹จํ•˜๋Š” ์„œํ‚ท ๋ธŒ๋ ˆ์ด์ปค, ํƒ€์ž„์•„์›ƒ, ์žฌ์‹œ๋„, ๋ฒŒํฌํ—ค๋“œ@CircuitBreaker, @Retry, fallbackMethod
Spring Cloud ConfigGit ๋˜๋Š” ๋ณผ๋ฅจ ๊ธฐ๋ฐ˜ ๋ถ„์‚ฐ ํ™˜๊ฒฝ์„ค์ • ์ค‘์•™ ์ง‘์ค‘ ๊ด€๋ฆฌ ๋ฐ ํ•ซ ๋ฆฌํ”„๋ ˆ์‹œ@EnableConfigServer, @RefreshScope, Actuator
Micrometer Tracing๋‹ค์ค‘ ์„œ๋น„์Šค๋ฅผ ๊ฐ€๋กœ์ง€๋ฅด๋Š” ์š”์ฒญ์— Trace ID/Span ID๋ฅผ ๋ถ€์—ฌํ•˜์—ฌ ๋ณ‘๋ชฉ ์ถ”์ io.micrometer:micrometer-tracing, Zipkin ๋Œ€์‹œ๋ณด๋“œ

3. [์ดˆ๋ณด์ž 1๋‹จ๊ณ„] 3๋ถ„ ํ—ฌ๋กœ์›”๋“œ: Eureka ์„œ๋น„์Šค ๋“ฑ๋ก & ๊ฒ€์ƒ‰ (Service Discovery)

๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ์ƒํƒœ๊ณ„์˜ ์ค‘์•™ ์ „ํ™”๋ฒˆํ˜ธ๋ถ€ ์—ญํ• ์„ ํ•˜๋Š” Eureka Server๋ฅผ ๋„์šฐ๊ณ , ์‹ค์ œ ๋น„์ฆˆ๋‹ˆ์Šค ์„œ๋น„์Šค(Order Service)๊ฐ€ ์ž๋™์œผ๋กœ ์ž์‹ ์„ ๋“ฑ๋กํ•˜๋Š” ์ตœ์†Œ ๋™์ž‘ ์˜ˆ์ œ์ž…๋‹ˆ๋‹ค.

โ‘  Eureka Server ํ”„๋กœ์ ํŠธ (EurekaServerApplication.java)

package com.example.eurekaserver;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@SpringBootApplication
@EnableEurekaServer // [1] Eureka ์„œ๋น„์Šค ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ ์„œ๋ฒ„ ํ™œ์„ฑํ™”
public class EurekaServerApplication {
    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }
}
# eureka-server/src/main/resources/application.yml
server:
  port: 8761

spring:
  application:
    name: eureka-server

eureka:
  instance:
    hostname: localhost
  client:
    # ์„œ๋ฒ„ ์ž์‹ ์ด ํด๋ผ์ด์–ธํŠธ๋กœ ๋“ฑ๋ก๋˜๊ฑฐ๋‚˜ ๋‹ค๋ฅธ ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ๋ฅผ ๊ธ์–ด์˜ฌ ํ•„์š”๊ฐ€ ์—†์Œ
    register-with-eureka: false
    fetch-registry: false
    service-url:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

โ‘ก ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ํด๋ผ์ด์–ธํŠธ (OrderServiceApplication.java)

package com.example.orderservice;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
@EnableDiscoveryClient // [2] Eureka ํด๋ผ์ด์–ธํŠธ ๋“ฑ๋ก ํ™œ์„ฑํ™”
public class OrderServiceApplication {
    public static void main(String[] args) {
        SpringApplication.run(OrderServiceApplication.class, args);
    }
}

@RestController
@RequestMapping("/orders")
class OrderController {
    @GetMapping("/hello")
    public String getHello() {
        return "์ฃผ๋ฌธ ์„œ๋น„์Šค๊ฐ€ ์ •์ƒ ๊ฐ€๋™ ์ค‘์ž…๋‹ˆ๋‹ค (Port: 8081)";
    }
}
# order-service/src/main/resources/application.yml
server:
  port: 8081

spring:
  application:
    name: order-service # Eureka์— ๋Œ€๋ฌธ์ž ORDER-SERVICE๋กœ ๋“ฑ๋ก๋จ

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/
  instance:
    prefer-ip-address: true
    instance-id: ${spring.application.name}:${server.port}:${random.value}

โ‘ข ์‹คํ–‰ ๋ฐ ๊ฒ€์ฆ ๊ฒฐ๊ณผ

  1. Eureka Server ์‹คํ–‰: http://localhost:8761 ์›น ๋ธŒ๋ผ์šฐ์ € ์ ‘์†
    • Instances currently registered with Eureka ํ‘œ์— ORDER-SERVICE๊ฐ€ ๋…น์ƒ‰ UP (1) - 192.168.x.x:order-service:8081:... ์ƒํƒœ๋กœ ๋“ฑ๋ก๋œ ๊ฒƒ์„ ํ™•์ธ.
  2. REST API ํ˜ธ์ถœ ๊ฒ€์ฆ:
curl http://localhost:8081/orders/hello
# ์ถœ๋ ฅ: ์ฃผ๋ฌธ ์„œ๋น„์Šค๊ฐ€ ์ •์ƒ ๊ฐ€๋™ ์ค‘์ž…๋‹ˆ๋‹ค (Port: 8081)

4. [2๋‹จ๊ณ„] ํ•ต์‹ฌ ๊ธฐ๋Šฅ: Spring Cloud Gateway ๋ผ์šฐํŒ… & JWT ๊ธ€๋กœ๋ฒŒ ํ•„ํ„ฐ (Gateway)

์™ธ๋ถ€์˜ ๋ชจ๋“  ํŠธ๋ž˜ํ”ฝ์„ ๋‹จ์ผ ํฌํŠธ(8080)๋กœ ์ˆ˜์‹ ํ•˜์—ฌ Eureka์— ๋“ฑ๋ก๋œ ์„œ๋น„์Šค๋ช…(lb://ORDER-SERVICE)์œผ๋กœ ์ž๋™ ๋ถ„์‚ฐ ๋ผ์šฐํŒ…ํ•˜๊ณ , JWT ํ† ํฐ์„ ์‚ฌ์ „ ๊ฒ€์ฆํ•˜๋Š” ํ•ต์‹ฌ ๊ด€๋ฌธ์ž…๋‹ˆ๋‹ค.

โš ๏ธ ์ฃผ์˜: Gateway๋Š” ๋…ผ๋ธ”๋กœํ‚น ๋น„๋™๊ธฐ์ธ Spring WebFlux(Netty) ๊ธฐ๋ฐ˜์ž…๋‹ˆ๋‹ค. spring-boot-starter-web(Tomcat)๊ณผ ์ ˆ๋Œ€ ํ•จ๊ป˜ ๋นŒ๋“œํ•˜์ง€ ๋งˆ์„ธ์š”!

โ‘  ์„ ์–ธ์  ๋ผ์šฐํŒ… ์„ค์ • (application.yml)

# gateway-service/src/main/resources/application.yml
server:
  port: 8080

spring:
  application:
    name: gateway-service
  cloud:
    gateway:
      discovery:
        locator:
          enabled: true      # Eureka ์„œ๋น„์Šค ๋ชฉ๋ก ๊ธฐ๋ฐ˜ ์ž๋™ ๋ผ์šฐํŒ… ํ™œ์„ฑํ™”
          lower-case-service-id: true
      routes:
        # [1] ์ฃผ๋ฌธ ์„œ๋น„์Šค ๋ผ์šฐํŠธ ๊ทœ์น™
        - id: order-service-route
          uri: lb://ORDER-SERVICE    # lb:// -> ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ ์—ฐ๋™ ์„œ๋น„์Šค๋ช…
          predicates:
            - Path=/api/orders/**    # ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ URL ํŒจํ„ด
          filters:
            - StripPrefix=1          # /api๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋ฐฑ์—”๋“œ /orders/**๋กœ ์ „๋‹ฌ

        # [2] ๊ฒฐ์ œ ์„œ๋น„์Šค ๋ผ์šฐํŠธ ๊ทœ์น™
        - id: payment-service-route
          uri: lb://PAYMENT-SERVICE
          predicates:
            - Path=/api/payments/**
          filters:
            - StripPrefix=1

eureka:
  client:
    service-url:
      defaultZone: http://localhost:8761/eureka/

โ‘ก JWT ๊ฒ€์ฆ ๊ธ€๋กœ๋ฒŒ ํ•„ํ„ฐ (JwtAuthenticationGlobalFilter.java)

package com.example.gatewayservice.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

@Component
public class JwtAuthenticationGlobalFilter implements GlobalFilter, Ordered {

    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        ServerHttpRequest request = exchange.getRequest();
        String path = request.getURI().getPath();

        // 1. ํšŒ์›๊ฐ€์ž… ๋ฐ ๋กœ๊ทธ์ธ ๋“ฑ ๊ณต๊ฐœ ์—”๋“œํฌ์ธํŠธ๋Š” ์ธ์ฆ ์ œ์™ธ
        if (path.contains("/auth/") || path.contains("/public/")) {
            return chain.filter(exchange);
        }

        // 2. Authorization ํ—ค๋” ์กด์žฌ ์—ฌ๋ถ€ ํ™•์ธ
        if (!request.getHeaders().containsKey(HttpHeaders.AUTHORIZATION)) {
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete(); // 401 ์ฆ‰์‹œ ์ข…๋ฃŒ
        }

        String authHeader = request.getHeaders().getFirst(HttpHeaders.AUTHORIZATION);
        if (authHeader == null || !authHeader.startsWith("Bearer ")) {
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }

        String token = authHeader.substring(7);

        // 3. ํ† ํฐ ๊ฒ€์ฆ ๋กœ์ง (์‹ค๋ฌด์—์„œ๋Š” JwtParser๋กœ ํŒŒ์‹ฑ)
        if (!isValidToken(token)) {
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }

        // 4. ๊ฒ€์ฆ ํ†ต๊ณผ ์‹œ ๋ฐฑ์—”๋“œ ์„œ๋น„์Šค๋กœ ์š”์ฒญ ์ธ๊ณ„
        return chain.filter(exchange);
    }

    private boolean isValidToken(String token) {
        return token != null && !token.isBlank(); // ์˜ˆ์ œ์šฉ ๊ฒ€์ฆ
    }

    @Override
    public int getOrder() {
        return -1; // ์ตœ์šฐ์„  ์‹คํ–‰
    }
}

5. [2๋‹จ๊ณ„] ํ•ต์‹ฌ ๊ธฐ๋Šฅ: OpenFeign ์„ ์–ธ์  ์„œ๋น„์Šค ๊ฐ„ ํ†ต์‹  (Declarative HTTP)

๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ๊ฐ„(A โž” B) ๋‚ด๋ถ€ ๋™๊ธฐ REST API ํ˜ธ์ถœ ์‹œ ๋ณต์žกํ•œ RestTemplate์ด๋‚˜ WebClient ๋Œ€์‹ , ์ž๋ฐ” ์ธํ„ฐํŽ˜์ด์Šค ์„ ์–ธ๋งŒ์œผ๋กœ ๋‹ค๋ฅธ ์„œ๋น„์Šค์™€ ํ†ต์‹ ํ•ฉ๋‹ˆ๋‹ค.

โ‘  ๋ฉ”์ธ ํด๋ž˜์Šค Feign ํ™œ์„ฑํ™”

@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients // [1] Feign ํด๋ผ์ด์–ธํŠธ ์Šค์บ” ํ™œ์„ฑํ™”
public class OrderServiceApplication { ... }

โ‘ก Feign ํด๋ผ์ด์–ธํŠธ ์ธํ„ฐํŽ˜์ด์Šค ์ •์˜ (PaymentClient.java)

package com.example.orderservice.client;

import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;

// [2] Eureka์— ๋“ฑ๋ก๋œ ์„œ๋น„์Šค ์ด๋ฆ„(payment-service) ์ง€์ •
@FeignClient(name = "payment-service")
public interface PaymentClient {

    // payment-service์˜ GET /payments/{orderId} ์—”๋“œํฌ์ธํŠธ ํ˜ธ์ถœ
    @GetMapping("/payments/{orderId}")
    PaymentResponse getPaymentInfo(@PathVariable("orderId") Long orderId);

    // payment-service์˜ POST /payments/process ์—”๋“œํฌ์ธํŠธ ํ˜ธ์ถœ
    @PostMapping("/payments/process")
    PaymentResponse processPayment(@RequestBody PaymentRequest request);
}

// DTO ์ •์˜
record PaymentRequest(Long orderId, Long amount, String paymentMethod) {}
record PaymentResponse(Long paymentId, String status, Long amount) {}

โ‘ข ๋น„์ฆˆ๋‹ˆ์Šค ์„œ๋น„์Šค์—์„œ ์ฃผ์ž… ๋ฐ ํ˜ธ์ถœ

@Service
public class OrderService {

    private final PaymentClient paymentClient;

    public OrderService(PaymentClient paymentClient) {
        this.paymentClient = paymentClient;
    }

    public void createOrder(Long orderId, Long amount) {
        // ์ผ๋ฐ˜ ๋กœ์ปฌ ์ž๋ฐ” ๋ฉ”์„œ๋“œ๋ฅผ ํ˜ธ์ถœํ•˜๋“ฏ ๋‹ค๋ฅธ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ํ˜ธ์ถœ!
        PaymentResponse response = paymentClient.processPayment(new PaymentRequest(orderId, amount, "CREDIT_CARD"));
        System.out.println("๊ฒฐ์ œ ์ฒ˜๋ฆฌ ๊ฒฐ๊ณผ: " + response.status());
    }
}

6. [2๋‹จ๊ณ„] ํ•ต์‹ฌ ๊ธฐ๋Šฅ: Resilience4j ์„œํ‚ท ๋ธŒ๋ ˆ์ด์ปค & Fallback (Fault Tolerance)

ํ˜ธ์ถœ ๋Œ€์ƒ ์„œ๋น„์Šค(Payment Service)๊ฐ€ ๊ณผ๋ถ€ํ•˜๋‚˜ ์žฅ์• ๋กœ ๋‹ค์šด๋˜์—ˆ์„ ๋•Œ, ์š”์ฒญ์ด ๊ณ„์† ์Œ“์—ฌ ํ˜ธ์ถœ์ž(Order Service)๊นŒ์ง€ ์—ฐ์‡„์ ์œผ๋กœ ๋ฉˆ์ถ”๋Š” **์บ์Šค์ผ€์ด๋”ฉ ์žฅ์• (Cascading Failure)**๋ฅผ ์›์ฒœ ์ฐจ๋‹จํ•ฉ๋‹ˆ๋‹ค.

๐Ÿ›ก๏ธ ์„œํ‚ท ๋ธŒ๋ ˆ์ด์ปค 3๋Œ€ ์ƒํƒœ ์ „์ด (State Machine)

  • CLOSED (์ •์ƒ): ๋ชจ๋“  ํŠธ๋ž˜ํ”ฝ์ด ๋ชฉ์ ์ง€๋กœ ํ†ต๊ณผ. ์‹คํŒจ์œจ์„ ๋ชจ๋‹ˆํ„ฐ๋ง.
  • OPEN (์ฐจ๋‹จ): ์‹คํŒจ์œจ์ด ์ž„๊ณ„์น˜(์˜ˆ: 50%)๋ฅผ ๋„˜์œผ๋ฉด ์ฆ‰์‹œ ํšŒ๋กœ๋ฅผ ์—ด๊ณ , ์›๊ฒฉ ํ˜ธ์ถœ ์—†์ด ์ฆ‰์‹œ Fallback ์•ˆ์ „ ์‘๋‹ต ๋ฐ˜ํ™˜.
  • HALF_OPEN (์‹œํ—˜): ์„ค์ •๋œ ์‹œ๊ฐ„(10์ดˆ) ๊ฒฝ๊ณผ ํ›„ ์ผ๋ถ€ ์š”์ฒญ๋งŒ ๋ณด๋‚ด ์„œ๋ฒ„๊ฐ€ ํšŒ๋ณต๋˜์—ˆ๋Š”์ง€ ํ…Œ์ŠคํŠธ.

โ‘  ์–ด๋…ธํ…Œ์ด์…˜ ๊ธฐ๋ฐ˜ ์„œํ‚ท ๋ธŒ๋ ˆ์ด์ปค ์ ์šฉ

package com.example.orderservice.service;

import com.example.orderservice.client.PaymentClient;
import io.github.resilience4j.circuitbreaker.annotation.CircuitBreaker;
import io.github.resilience4j.retry.annotation.Retry;
import org.springframework.stereotype.Service;

@Service
public class OrderPaymentService {

    private final PaymentClient paymentClient;

    public OrderPaymentService(PaymentClient paymentClient) {
        this.paymentClient = paymentClient;
    }

    // [1] ์„œํ‚ท ๋ธŒ๋ ˆ์ด์ปค ๋ฐ ์žฌ์‹œ๋„ ๋“ฑ๋ก
    @CircuitBreaker(name = "paymentCircuit", fallbackMethod = "fallbackPaymentProcess")
    @Retry(name = "paymentRetry")
    public String executePayment(Long orderId, Long amount) {
        return paymentClient.processPayment(new PaymentRequest(orderId, amount, "CARD")).status();
    }

    // [2] ์›๊ฒฉ ์„œ๋น„์Šค ์žฅ์•  ๋˜๋Š” ์„œํ‚ท OPEN ์‹œ ๋Œ€์‹  ํ˜ธ์ถœ๋˜๋Š” Fallback ๋ฉ”์„œ๋“œ
    // ์ค‘์š”: ์›๋ž˜ ๋ฉ”์„œ๋“œ์™€ ๋™์ผํ•œ ํŒŒ๋ผ๋ฏธํ„ฐ ๋ชฉ๋ก ๋’ค์— ๋ฐ˜๋“œ์‹œ Throwable ์ธ์ž๋ฅผ ์ถ”๊ฐ€ํ•ด์•ผ ํ•จ!
    public String fallbackPaymentProcess(Long orderId, Long amount, Throwable ex) {
        System.err.println("๊ฒฐ์ œ ์„œ๋น„์Šค ์žฅ์•  ๊ฐ์ง€! Fallback ์‹คํ–‰ ์‚ฌ์œ : " + ex.getMessage());
        return "PAYMENT_PENDING_OFFLINE"; // ๊ฒฐ์ œ ๋ณด๋ฅ˜ ์ƒํƒœ๋กœ ์•ˆ์ „ ๋ฐ˜ํ™˜
    }
}

โ‘ก application.yml ์„œํ‚ท ๋ธŒ๋ ˆ์ด์ปค ์ •๋ฐ€ ํŠœ๋‹

resilience4j:
  circuitbreaker:
    instances:
      paymentCircuit:
        sliding-window-type: COUNT_BASED       # ํ˜ธ์ถœ ํšŸ์ˆ˜ ๊ธฐ๋ฐ˜ ์Šฌ๋ผ์ด๋”ฉ ์œˆ๋„์šฐ
        sliding-window-size: 10                # ์ตœ๊ทผ 10๋ฒˆ์˜ ํ˜ธ์ถœ ํ†ต๊ณ„ ์ธก์ •
        failure-rate-threshold: 50             # ์‹คํŒจ์œจ 50% ์ด์ƒ ์‹œ ์„œํ‚ท OPEN
        wait-duration-in-open-state: 10s       # OPEN ํ›„ HALF_OPEN ์ „ํ™˜ ๋Œ€๊ธฐ ์‹œ๊ฐ„
        permitted-number-of-calls-in-half-open-state: 3 # HALF_OPEN ์‹œ ํ…Œ์ŠคํŠธ ํ˜ธ์ถœ ์ˆ˜
        automatic-transition-from-open-to-half-open-enabled: true

7. [3๋‹จ๊ณ„] ์‹ค๋ฌด ์‘์šฉ: ๋ถ„์‚ฐ ์„ค์ •(Config Server) & ํ•ซ ๋ฆฌํ”„๋ ˆ์‹œ (Config)

Git ์ €์žฅ์†Œ์— ๊ฐ ํ™˜๊ฒฝ(dev, prod)๋ณ„ YAML ์„ค์ •์„ ๋ชจ์•„๋‘๊ณ , ์„œ๋น„์Šค ์žฌ๊ธฐ๋™ ์—†์ด ์„ค์ •์„ ์‹ค์‹œ๊ฐ„ ๋ณ€๊ฒฝํ•ฉ๋‹ˆ๋‹ค.

โ‘  ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค์—์„œ @RefreshScope ์ ์šฉ

package com.example.orderservice.controller;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RefreshScope // [1] /actuator/refresh ํ˜ธ์ถœ ์‹œ ํ•ด๋‹น ๋นˆ์˜ ํ•„๋“œ๊ฐ’์„ ๋™์ ์œผ๋กœ ์žฌ์ฃผ์ž…
public class ConfigTestController {

    @Value("${order.discount-rate:0.1}")
    private double discountRate;

    @GetMapping("/orders/discount")
    public String getDiscountRate() {
        return "ํ˜„์žฌ ์ ์šฉ ํ• ์ธ์œจ: " + (discountRate * 100) + "%";
    }
}

โ‘ก ๋Ÿฐํƒ€์ž„ ๋ฌด์ค‘๋‹จ ์„ค์ • ๋ฆฌํ”„๋ ˆ์‹œ ํ˜ธ์ถœ

Git์—์„œ order.discount-rate: 0.25๋กœ ์ปค๋ฐ‹ ํ›„, ์žฌ๋ถ€ํŒ… ์—†์ด ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค์— HTTP POST ์š”์ฒญ ์ „์†ก:

curl -X POST http://localhost:8081/actuator/refresh
# ์‘๋‹ต: ["order.discount-rate"] (๋ณ€๊ฒฝ๋œ ์„ค์ • ํ‚ค ๋ชฉ๋ก ๋ฐ˜ํ™˜)

curl http://localhost:8081/orders/discount
# ์ถœ๋ ฅ: ํ˜„์žฌ ์ ์šฉ ํ• ์ธ์œจ: 25.0% (์ฆ‰์‹œ ๋ฐ˜์˜ ํ™•์ธ!)

8. [3๋‹จ๊ณ„] ์‹ค๋ฌด ์‘์šฉ: ๋ถ„์‚ฐ ์ถ”์ (Micrometer Tracing) & Saga ํŒจํ„ด (Tracing & Saga)

๐Ÿ•ต๏ธ ๋ถ„์‚ฐ ์ถ”์  (Distributed Tracing with Zipkin)

๋‹จ์ผ ์‚ฌ์šฉ์ž ํด๋ฆญ์ด Gateway โž” Order โž” Payment โž” Delivery ์„œ๋น„์Šค๋กœ ์ด์–ด์งˆ ๋•Œ, ์ „์ฒด ๊ฒฝ๋กœ๋ฅผ ํ•˜๋‚˜์˜ Trace ID๋กœ ๋ฌถ์–ด ์„ฑ๋Šฅ ๋ณ‘๋ชฉ ์ง€์ ์„ ์‹œ๊ฐํ™”ํ•ฉ๋‹ˆ๋‹ค.

# application.yml
management:
  tracing:
    sampling:
      probability: 1.0  # ๊ฐœ๋ฐœ ํ™˜๊ฒฝ: 100% ์ƒ˜ํ”Œ๋ง (์šด์˜ ํ™˜๊ฒฝ์€ 0.1 ๊ถŒ์žฅ)
  zipkin:
    tracing:
      endpoint: http://localhost:9411/api/v2/spans
logging:
  pattern:
    # ๋กœ๊ทธ ํฌ๋งท์— [์„œ๋น„์Šค๋ช…, TraceID, SpanID] ์ž๋™ ์ถœ๋ ฅ
    level: "%5p [${spring.application.name:},%X{traceId:-},%X{spanId:-}]"
# ์ฝ˜์†” ๋กœ๊ทธ ์ถœ๋ ฅ ์˜ˆ์‹œ: ๋™์ผํ•œ Trace ID(65a4c9b1d03e)๊ฐ€ ์ „ ์„œ๋น„์Šค์— ๊ฑธ์ณ ์œ ์ง€๋จ
INFO [order-service,65a4c9b1d03e,e450b281f012] : ์ฃผ๋ฌธ ์ƒ์„ฑ ์š”์ฒญ ์ˆ˜์‹ 
INFO [payment-service,65a4c9b1d03e,99a01f56a12b] : ๊ฒฐ์ œ ์Šน์ธ ์™„๋ฃŒ

๐Ÿ”„ ๋ถ„์‚ฐ ํŠธ๋žœ์žญ์…˜ Saga ํŒจํ„ด (Orchestration vs Choreography)

๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋Š” ์„œ๋กœ ๋‹ค๋ฅธ DB๋ฅผ ๊ฐ€์ง€๋ฏ€๋กœ ์ „ํ†ต์ ์ธ 2๋‹จ๊ณ„ ์ปค๋ฐ‹(2PC)์ด ๋ถˆ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค. ๊ฐ ์„œ๋น„์Šค๊ฐ€ ๋กœ์ปฌ ํŠธ๋žœ์žญ์…˜์„ ์‹คํ–‰ํ•˜๊ณ , ์‹คํŒจ ์‹œ **๋ณด์ƒ ํŠธ๋žœ์žญ์…˜(Compensating Transaction)**์„ ์‹คํ–‰ํ•˜์—ฌ ๋ฐ์ดํ„ฐ ์ •ํ•ฉ์„ฑ์„ ๋งž์ถฅ๋‹ˆ๋‹ค.

[ ์ •์ƒ ํ๋ฆ„ (Happy Path) ]
์ฃผ๋ฌธ ์„œ๋น„์Šค (์ฃผ๋ฌธ์ƒ์„ฑ) โ”€โ”€โ–บ ๊ฒฐ์ œ ์„œ๋น„์Šค (๊ฒฐ์ œ์™„๋ฃŒ) โ”€โ”€โ–บ ์žฌ๊ณ  ์„œ๋น„์Šค (์žฌ๊ณ ์ฐจ๊ฐ) โ”€โ”€โ–บ ์™„๋ฃŒ!

[ ์‹คํŒจ ์‹œ ๋ณด์ƒ ํŠธ๋žœ์žญ์…˜ (Compensating Flow) ]
์ฃผ๋ฌธ ์„œ๋น„์Šค (์ฃผ๋ฌธ์ƒ์„ฑ) โ”€โ”€โ–บ ๊ฒฐ์ œ ์„œ๋น„์Šค (๊ฒฐ์ œ์™„๋ฃŒ) โ”€โ”€โ–บ ์žฌ๊ณ  ์„œ๋น„์Šค (โŒ ์žฌ๊ณ ๋ถ€์กฑ ์—๋Ÿฌ!)
                                                          โ”‚
                      โ”Œโ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”˜
                      โ–ผ ๋ณด์ƒ ์ด๋ฒคํŠธ ๋ฐœํ–‰ (Kafka)
            ๊ฒฐ์ œ ์„œ๋น„์Šค (๊ฒฐ์ œ ์ทจ์†Œ/ํ™˜๋ถˆ)
                      โ”‚
                      โ–ผ ๋ณด์ƒ ์ด๋ฒคํŠธ ๋ฐœํ–‰
            ์ฃผ๋ฌธ ์„œ๋น„์Šค (์ฃผ๋ฌธ ์ทจ์†Œ ์ƒํƒœ ๊ฐฑ์‹ )

9. ์‹ค๋ฌด ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ… & ํ”„๋กœ๋•์…˜ ์šด์˜ ์ฒดํฌ๋ฆฌ์ŠคํŠธ (Best Practices)

โš ๏ธ ์ดˆ๋ณด์ž๊ฐ€ ๊ฐ€์žฅ ํ”ํžˆ ๊ฒช๋Š” 5๋Œ€ ์‹ค์ˆ˜ ๋ฐ ํ•ด๊ฒฐ์ฑ…

์ฆ์ƒ ๋ฐ ๋ฌธ์ œ์ ์›์ธ ๋ถ„์„ํ™•์‹คํ•œ ํ•ด๊ฒฐ ๊ฐ€์ด๋“œ
Gateway ์‹คํ–‰ ์‹œ Spring MVC found on classpath ์—๋ŸฌGateway๋Š” WebFlux ์ „์šฉ์ธ๋ฐ Tomcat ์˜์กด์„ฑ์ด ์„ž์ž„build.gradle์—์„œ spring-boot-starter-web์„ ์ œ๊ฑฐํ•˜๊ณ  spring-cloud-starter-gateway๋งŒ ๋‹จ๋… ์œ ์ง€
Eureka์— EMERGENCY! ... CLAIMING INSTANCES ARE UP ๊ฒฝ๊ณ Eureka ์ž๊ธฐ๋ณด์กด ๋ชจ๋“œ(Self-Preservation) ์ž‘๋™๋„คํŠธ์›Œํฌ ์ผ์‹œ ๋‹จ์ ˆ ์‹œ ๋ฌด๋ถ„๋ณ„ํ•œ ์ธ์Šคํ„ด์Šค ์ถ•์ถœ์„ ๋ฐฉ์ง€ํ•˜๋Š” ์ •์ƒ ๋ฐฉ์–ด ๊ธฐ์ œ. ๋กœ์ปฌ ํ…Œ์ŠคํŠธ ์‹œ enable-self-preservation: false๋กœ ๋„๊ธฐ ๊ฐ€๋Šฅ
OpenFeign ํ˜ธ์ถœ ์‹œ Read timed out 500 ์—๋Ÿฌ๊ธฐ๋ณธ HTTP ์†Œ์ผ“ ํƒ€์ž„์•„์›ƒ์ด 1000ms(1์ดˆ)๋กœ ๋งค์šฐ ์งง์Œfeign.client.config.default.read-timeout: 5000 ์„ค์ •์œผ๋กœ ํƒ€์ž„์•„์›ƒ ์ƒํ–ฅ ์กฐ์ •
์„œ๋น„์Šค ๊ฐ„ Feign ํ†ต์‹  ์‹œ ๋กœ๊ทธ์ธ JWT ํ† ํฐ ์œ ์‹คFeign์€ ๊ธฐ๋ณธ์ ์œผ๋กœ ์ธ๋ฐ”์šด๋“œ ํ—ค๋”๋ฅผ ์ „๋‹ฌํ•˜์ง€ ์•Š์ŒRequestInterceptor ๋นˆ์„ ๋“ฑ๋กํ•˜์—ฌ RequestContextHolder์—์„œ ํ† ํฐ์„ ์ถ”์ถœํ•ด Feign ์š”์ฒญ ํ—ค๋”์— ์ฃผ์ž…
OpenFeign ์ˆœํ™˜ ์˜์กด์„ฑ(Circular Dependency) ๋นŒ๋“œ ์—๋ŸฌA ์„œ๋น„์Šค์™€ B ์„œ๋น„์Šค๊ฐ€ ์„œ๋กœ๋ฅผ @FeignClient๋กœ ์ฐธ์กฐ๋น„๋™๊ธฐ ๋ฉ”์‹œ์ง€ ํ(Kafka/RabbitMQ) ์ด๋ฒคํŠธ ๊ธฐ๋ฐ˜ ํ†ต์‹ ์œผ๋กœ ๊ฒฐํ•ฉ๋„๋ฅผ ๋ถ„๋ฆฌ

๐Ÿš€ ํ”„๋กœ๋•์…˜ ์šด์˜ ์ฒดํฌ๋ฆฌ์ŠคํŠธ

  1. Eureka ํ•˜ํŠธ๋น„ํŠธ ์ฃผ๊ธฐ ํŠœ๋‹: ๊ธฐ๋ณธ๊ฐ’(30์ดˆ) ๋Œ€์‹  ์šด์˜ ํ™˜๊ฒฝ์—์„œ๋Š” lease-renewal-interval-in-seconds: 10, lease-expiration-duration-in-seconds: 30์œผ๋กœ ์กฐ์ •ํ•˜์—ฌ ๋‹ค์šด๋œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋น ๋ฅด๊ฒŒ ๊ฐ์ง€.
  2. Spring Cloud Gateway CORS ์„ค์ •: ๊ธ€๋กœ๋ฒŒ ํ•„ํ„ฐ ์•ž๋‹จ์—์„œ ๋ธŒ๋ผ์šฐ์ € Preflight(OPTIONS) ์š”์ฒญ์ด 401๋กœ ์ฐจ๋‹จ๋˜์ง€ ์•Š๋„๋ก Gateway ์„ค์ • ๋ ˆ๋ฒจ์—์„œ globalcors.cors-configurations๋ฅผ ๋ช…์‹œ.
  3. OpenFeign ์š”์ฒญ ์ธํ„ฐ์…‰ํ„ฐ(RequestInterceptor): ์ธ์ฆ ํ† ํฐ(Authorization: Bearer ...)์ด ์„œ๋น„์Šค ๊ฐ„ ํ™‰(Hop)์„ ๋„˜์–ด๊ฐˆ ๋•Œ ์œ ์‹ค๋˜์ง€ ์•Š๋„๋ก ๊ณตํ†ต ์ธํ„ฐ์…‰ํ„ฐ ๊ตฌ์„ฑ ํ•„์ˆ˜.
๐Ÿ’ฌ

์˜๊ฒฌ ๋ฐ ์งˆ๋ฌธ

0
0 / 1500