๐Ÿ“Œ Spring Cloud Eureka ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ ์ •๋ฆฌ

ghgh2505380ยท2026๋…„ 3์›” 16์ผ

MSA(Microservice Architecture)

๋ชฉ๋ก ๋ณด๊ธฐ
3/13

๐Ÿ“– ๊ฐœ์š”

Spring Cloud Eureka
๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ํ™˜๊ฒฝ์—์„œ ์„œ๋น„์Šค ๋“ฑ๋ก ๋ฐ ์œ„์น˜ ์กฐํšŒ๋ฅผ ๊ด€๋ฆฌํ•˜๋Š” ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ ์„œ๋ฒ„

์™œ ์‚ฌ์šฉํ•˜๋Š”์ง€

์„œ๋น„์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ๋กœ ๋ถ„๋ฆฌ๋œ ํ™˜๊ฒฝ์—์„œ๋Š”
๊ฐ ์„œ๋น„์Šค์˜ IP / Port๋ฅผ ์ง์ ‘ ๊ด€๋ฆฌํ•˜๊ธฐ ์–ด๋ ค์›€

์„œ๋น„์Šค๊ฐ€ ์‹คํ–‰๋  ๋•Œ ์ž๋™์œผ๋กœ Eureka Server์— ๋“ฑ๋ก๋˜๊ณ 
๋‹ค๋ฅธ ์„œ๋น„์Šค๊ฐ€ ํ•„์š”ํ•œ ์„œ๋น„์Šค์˜ ์œ„์น˜๋ฅผ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›

ํ•ด๊ฒฐํ•˜๋Š” ๋ฌธ์ œ

  • ์„œ๋น„์Šค ์œ„์น˜ ๊ด€๋ฆฌ ๋ฌธ์ œ ํ•ด๊ฒฐ
  • ์„œ๋น„์Šค ์ธ์Šคํ„ด์Šค ํ™•์žฅ ๊ด€๋ฆฌ
  • ์žฅ์•  ์„œ๋น„์Šค ์ž๋™ ์ œ์™ธ

ํ•ต์‹ฌ ํŠน์ง•

  • Service Registration (์„œ๋น„์Šค ๋“ฑ๋ก)
  • Service Discovery (์„œ๋น„์Šค ์กฐํšŒ)
  • ๋กœ๋“œ๋ฐธ๋Ÿฐ์‹ฑ ์ง€์›
  • Health Check ๊ธฐ๋ฐ˜ ์žฅ์•  ๊ฐ์ง€

Spring Cloud ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ
https://start.spring.io/


โš™๏ธ ํ•ต์‹ฌ ๊ฐœ๋…

1๏ธโƒฃ Service Registration (์„œ๋น„์Šค ๋“ฑ๋ก)

์„œ๋น„์Šค๊ฐ€ ์‹คํ–‰๋  ๋•Œ Eureka Server์— ์ž์‹ ์˜ ์ •๋ณด๋ฅผ ๋“ฑ๋ก

๋“ฑ๋ก ์ •๋ณด

  • ์„œ๋น„์Šค ์ด๋ฆ„
  • IP ์ฃผ์†Œ
  • Port
  • ์„œ๋น„์Šค ์ƒํƒœ

๊ตฌ์„ฑ

Eureka Server

  • ๋“ฑ๋ก๋œ ์„œ๋น„์Šค ๋ชฉ๋ก ๊ด€๋ฆฌ
  • ์„œ๋น„์Šค ์œ„์น˜ ์ •๋ณด ์ œ๊ณต

Eureka Client

  • ์ž์‹ ์˜ ์„œ๋น„์Šค ์ •๋ณด๋ฅผ Eureka Server์— ๋“ฑ๋ก
  • ํ•„์š”ํ•œ ์„œ๋น„์Šค ์œ„์น˜ ์กฐํšŒ

2๏ธโƒฃ Service Discovery (์„œ๋น„์Šค ์œ„์น˜ ์กฐํšŒ)

์„œ๋น„์Šค๊ฐ€ ๋‹ค๋ฅธ ์„œ๋น„์Šค๋ฅผ ํ˜ธ์ถœํ•  ๋•Œ
Eureka Server๋ฅผ ํ†ตํ•ด ์„œ๋น„์Šค ์œ„์น˜ ์กฐํšŒ

๊ธฐ์กด ๋ฐฉ์‹

Client โ†’ ์ง์ ‘ ์„œ๋น„์Šค ์ฃผ์†Œ ํ˜ธ์ถœ

๋ฌธ์ œ

  • ์„œ๋น„์Šค ์ฃผ์†Œ ๋ณ€๊ฒฝ ์‹œ ์ฝ”๋“œ ์ˆ˜์ • ํ•„์š”
  • ์ธ์Šคํ„ด์Šค ํ™•์žฅ ๊ด€๋ฆฌ ์–ด๋ ค์›€

Eureka ๋ฐฉ์‹

Client โ†’ Eureka Server โ†’ Service Instance

์žฅ์ 

  • ์„œ๋น„์Šค ์œ„์น˜ ์ž๋™ ๊ด€๋ฆฌ
  • ์ธ์Šคํ„ด์Šค ํ™•์žฅ ์ž๋™ ๋ฐ˜์˜
  • ๋™์  ์„œ๋น„์Šค ํ˜ธ์ถœ ๊ฐ€๋Šฅ

3๏ธโƒฃ ์„œ๋น„์Šค ํ˜ธ์ถœ ๋ฐฉ์‹

RestTemplate ๋ฐฉ์‹

์„œ๋น„์Šค ํ˜ธ์ถœ ์‹œ ๋กœ๋“œ๋ฐธ๋Ÿฐ์‹ฑ ์ ์šฉ ํ•„์š”

@LoadBalanced
RestTemplate

ํŠน์ง•

  • ์„œ๋น„์Šค ์ด๋ฆ„ ๊ธฐ๋ฐ˜ ํ˜ธ์ถœ ๊ฐ€๋Šฅ
  • Spring Cloud LoadBalancer ์‚ฌ์šฉ

ํ•˜์ง€๋งŒ ํ˜„์žฌ ํ”„๋กœ์ ํŠธ์—์„œ๋Š” RestTemplate ๋ฐฉ์‹ ์‚ฌ์šฉํ•˜์ง€ ์•Š์Œ


Feign Client ๋ฐฉ์‹ (ํ”„๋กœ์ ํŠธ ์‚ฌ์šฉ)

์ธํ„ฐํŽ˜์ด์Šค ๊ธฐ๋ฐ˜ HTTP Client

ํŠน์ง•

  • ์ธํ„ฐํŽ˜์ด์Šค ์„ ์–ธ๋งŒ์œผ๋กœ API ํ˜ธ์ถœ ๊ฐ€๋Šฅ
  • ์„œ๋น„์Šค ์ด๋ฆ„ ๊ธฐ๋ฐ˜ ํ˜ธ์ถœ
  • ์ฝ”๋“œ ๊ฐ„๊ฒฐ

๊ตฌ์กฐ

Client โ†’ Feign Client โ†’ Eureka โ†’ Target Service

์˜ˆ์‹œ ์ฝ”๋“œ

@FeignClient(name = "store-service")
public interface StoreClient {

    @GetMapping("/stores/{id}")
    StoreResponse getStore(@PathVariable Long id);
}

์„ค๋ช…

  • name ๊ฐ’์ด ์„œ๋น„์Šค ์ด๋ฆ„
  • Eureka์—์„œ ํ•ด๋‹น ์„œ๋น„์Šค ์œ„์น˜ ์กฐํšŒ ํ›„ ํ˜ธ์ถœ

4๏ธโƒฃ Health Check ๋ฐ ์žฅ์•  ์ฒ˜๋ฆฌ

Eureka๋Š” ์„œ๋น„์Šค ์ƒํƒœ๋ฅผ ํ—ฌ์Šค ์ฒดํฌ ๊ธฐ๋ฐ˜์œผ๋กœ ๊ด€๋ฆฌ

ํ—ฌ์Šค ์ฒดํฌ ์—”๋“œํฌ์ธํŠธ

/actuator/health

๋™์ž‘ ๊ตฌ์กฐ

Service โ†’ Eureka Server โ†’ ์ƒํƒœ ๋ณด๊ณ 

์ƒํƒœ ์˜ˆ์‹œ

  • UP
  • DOWN
  • OUT_OF_SERVICE

์žฅ์•  ๋ฐœ์ƒ ์‹œ

Eureka๊ฐ€ ํ•ด๋‹น ์ธ์Šคํ„ด์Šค๋ฅผ ์„œ๋น„์Šค ๋ชฉ๋ก์—์„œ ์ œ์™ธ

๋‹ค๋ฅธ ์ •์ƒ ์ธ์Šคํ„ด์Šค๋กœ ํŠธ๋ž˜ํ”ฝ ์ „๋‹ฌ

ํšจ๊ณผ

  • ์žฅ์•  ์„œ๋น„์Šค ์ž๋™ ์ฐจ๋‹จ
  • ๋ฐฑ์—… ์ธ์Šคํ„ด์Šค ์‚ฌ์šฉ ๊ฐ€๋Šฅ

๐Ÿ” ๋™์ž‘ ์›๋ฆฌ

์„œ๋น„์Šค ๋“ฑ๋ก ํ๋ฆ„

Service Instance โ†’ Eureka Server ๋“ฑ๋ก

์„œ๋น„์Šค ํ˜ธ์ถœ ํ๋ฆ„

Client Service
โ†“
Eureka Server (์„œ๋น„์Šค ์œ„์น˜ ์กฐํšŒ)
โ†“
Target Service Instance

Feign Client ์‚ฌ์šฉ ์‹œ

Client โ†’ Feign Client โ†’ Eureka โ†’ Target Service


๐Ÿ’ป ์ฝ”๋“œ ์˜ˆ์‹œ

Eureka Server ์„ค์ •

๋ฉ”์ธ ํด๋ž˜์Šค

@SpringBootApplication
@EnableEurekaServer
public class EurekaServerApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaServerApplication.class, args);
    }

}

application.properties

spring.application.name=eureka-server
server.port=19090

eureka.client.register-with-eureka=false
eureka.client.fetch-registry=false
eureka.client.service-url.defaultZone=http://localhost:19090/eureka/

์„ค๋ช…

Eureka Server๋Š” ์„œ๋น„์Šค ๋“ฑ๋ก ์„œ๋ฒ„ ์—ญํ• 
์ž๊ธฐ ์ž์‹ ์„ ๋“ฑ๋กํ•˜์ง€ ์•Š๊ธฐ ๋•Œ๋ฌธ์— false ์„ค์ • ์‚ฌ์šฉ


Eureka Client ์„ค์ •

๋ฉ”์ธ ํด๋ž˜์Šค

@SpringBootApplication
@EnableDiscoveryClient
public class FirstServiceApplication {

    public static void main(String[] args) {
        SpringApplication.run(FirstServiceApplication.class, args);
    }

}

application.properties

spring.application.name=first-service
server.port=19091

eureka.client.service-url.defaultZone=http://localhost:19090/eureka/

๋‘ ๋ฒˆ์งธ ์„œ๋น„์Šค ์˜ˆ์‹œ

spring.application.name=second-service
server.port=19092

eureka.client.service-url.defaultZone=http://localhost:19090/eureka/

๐Ÿš€ ์‚ฌ์šฉ ์‚ฌ๋ก€

๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ํ™˜๊ฒฝ์—์„œ ์„œ๋น„์Šค ํ˜ธ์ถœ ๊ด€๋ฆฌ

์˜ˆ์‹œ ๊ตฌ์กฐ

Client
โ†“
API Gateway
โ†“
Microservices

Microservices

  • User Service
  • Store Service
  • Order Service
  • Payment Service

์„œ๋น„์Šค ๋“ฑ๋ก ๊ตฌ์กฐ

๊ฐ ์„œ๋น„์Šค โ†’ Eureka Server ๋“ฑ๋ก

์„œ๋น„์Šค ํ˜ธ์ถœ

Service โ†’ Eureka โ†’ ๋‹ค๋ฅธ ์„œ๋น„์Šค ํ˜ธ์ถœ

์„œ๋น„์Šค IP ๋ณ€๊ฒฝ์ด๋‚˜ ์ธ์Šคํ„ด์Šค ์ถ”๊ฐ€ ์‹œ
Eureka๊ฐ€ ์ž๋™์œผ๋กœ ๊ด€๋ฆฌ


๐Ÿ”Ž Eureka Dashboard ํ™•์ธ

Eureka Server ์‹คํ–‰ ํ›„ ๋ธŒ๋ผ์šฐ์ € ์ ‘์†

http://localhost:19090/

๋˜๋Š”

http://localhost:19090/eureka/

ํ™•์ธ ํฌ์ธํŠธ

Instances currently registered with Eureka

์—ฌ๊ธฐ์— ๋‹ค์Œ ์„œ๋น„์Šค๊ฐ€ ๋ณด์ด๋ฉด ์ •์ƒ ๋“ฑ๋ก

FIRST-SERVICE
SECOND-SERVICE

์ฆ‰

  • first-service
  • second-service

์ธ์Šคํ„ด์Šค๊ฐ€ ํ‘œ์‹œ๋˜๋ฉด Eureka Client ๋“ฑ๋ก ์„ฑ๊ณต


๐Ÿง  TIL (Today I Learned)

  • Spring Cloud Eureka๋Š” ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค ํ™˜๊ฒฝ์—์„œ ์„œ๋น„์Šค ๋“ฑ๋ก ๋ฐ ์œ„์น˜ ์กฐํšŒ ๊ด€๋ฆฌ
  • ์„œ๋น„์Šค ์ธ์Šคํ„ด์Šค๋Š” Eureka Server์— ์ž๋™ ๋“ฑ๋ก
  • ์„œ๋น„์Šค ํ˜ธ์ถœ ์‹œ Eureka๋ฅผ ํ†ตํ•ด ์„œ๋น„์Šค ์œ„์น˜ ์กฐํšŒ ๊ฐ€๋Šฅ
  • RestTemplate ๋Œ€์‹  Feign Client ๊ธฐ๋ฐ˜ ์„œ๋น„์Šค ํ˜ธ์ถœ ๊ฐ€๋Šฅ
  • Health Check ๊ธฐ๋ฐ˜ ์žฅ์•  ์„œ๋น„์Šค ์ž๋™ ์ œ์™ธ ๊ฐ€๋Šฅ
profile

0๊ฐœ์˜ ๋Œ“๊ธ€