๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’Š Java & Kotlin & Spring/- spring framework +

[Spring Cloud] Service Discover Server๋กœ Netflix Eureka ์ด์šฉํ•˜๊ธฐ

by Wonit 2021. 4. 24.

Spring Cloud Netflix

์Šคํ”„๋ง ํด๋ผ์šฐ๋“œ ๋„ทํ”Œ๋ฆญ์Šค๋Š” Spring Boot Application ์— ๋Œ€ํ•œ ํ†ตํ•ฉ ํ™˜๊ฒฝ ๊ตฌ์„ฑ์„ ์ง€์›ํ•˜๋Š” Netflix์˜ OSS ์„œ๋น„์Šค์ด๋‹ค.

Netflix๋Š” 2007๋…„ ์‹ฌ๊ฐํ•œ DB์†์ƒ์œผ๋กœ 3์ผ๊ฐ„ ์„œ๋น„์Šค ์žฅ์• ๋ฅผ ๊ฒผ์—ˆ๋Š”๋ฐ, ์ด์— ์šด์˜์ง„๋“ค๊ณผ ๊ธฐ์ˆ  ๋ฆฌ๋”๋“ค์€ ์‹ ๋ขฐ์„ฑ ๋†’๊ณ  ์ˆ˜ํ‰ํ™•์žฅ์ด ๊ฐ€๋Šฅํ•œ Cloud System์œผ๋กœ ์ด์ „์„ ํ•ด์•ผ ํ•จ์„ ๋Š๊ผˆ์—ˆ๋‹ค๊ณ  ํ•œ๋‹ค. ๊ทธ๋ฆฌ๊ณ  ๊ฒฐ๊ตญ MSA๋ฅผ 7๋…„๋งŒ์— ์™„๋ฒฝํ•˜๊ฒŒ ์ด์ „ํ•˜๊ฒŒ ๋˜์—ˆ๋Š”๋ฐ, ์ด ๋•Œ ์Œ“์€ ๊ฒฝํ—˜์น˜๋ฅผ ๋ฐ”ํƒ•์œผ๋กœ MSA ๊ธฐ์ˆ ์„ Spring์—๊ฒŒ ์˜คํ”ˆ์†Œ์Šค๋กœ ํ’€๊ฒŒ ๋˜์—ˆ๋‹ค.

Netflix OSS ์™€ Spring Cloud Netflix

Netflix OSS ์—๋Š” ๋‹ค์–‘ํ•œ ์„œ๋น„์Šค๋“ค์ด ์กด์žฌํ•œ๋‹ค.

  • Eureka : Discovery Server
    • ๊ฐ๊ฐ์˜ ์„œ๋น„์Šค ์ธ์Šคํ„ด์Šค๋“ค์ด ๋™์ ์œผ๋กœ ํ™•์žฅ, ์ถ•์†Œ ๋˜๋”๋ผ๋„ ์ธ์Šคํ„ด์Šค์˜ ์ƒํƒœ๋ฅผ ํ•˜๋‚˜์˜ ์„œ๋น„์Šค๋กœ ๊ด€๋ฆฌํ•  ์ˆ˜ ์žˆ๋Š” ์„œ๋น„์Šค
  • Ribbon : Client Side Load Balancer
    • ๋ถ€ํ•˜ ๋ถ„์‚ฐ์„ ์œ„ํ•œ ๋ชจ๋†€๋ฆฌ์Šค์˜ L4 ์Šค์œ„์น˜์™€ ๊ฐ™์ด ํŠธ๋ž˜ํ”ฝ์„ ๋ถ„์‚ฐ์‹œํ‚ค๋Š” ๊ธฐ๋Šฅ
  • Zuul : API Gateway
    • ๊ฐ๊ฐ์˜ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค์˜ ์ข…๋‹จ์ ์„ ์—ฐ๊ฒฐํ•˜๋Š” ๋ฆฌ๋ฒ„์Šค ํ”„๋ก์‹œ
  • Hystrix : Circuit Breaker
    • ํŠน์ • ์„œ๋น„์Šค๊ฐ€ ๊ณผ๋ถ€ํ•˜๊ฐ€ ๊ฑธ๋ ค ์„œ๋น„์Šค ์žฅ์• ๋ฅผ ์ „ํŒŒํ•˜๋Š” ํŠน์„ฑ์„ ํ•ด๊ฒฐํ•˜๋ ค๋Š” ๊ธฐ๋Šฅ
  • EvCache
  • Spectator
  • Archaius

์ด๋Ÿฐ ์„œ๋น„์Šค๋“ค์ด ๋ชจ๋‘ Spring Cloud ์—์„œ ์‚ฌ์šฉ๋˜๋Š” ๊ฒƒ์€ ์•„๋‹ˆ๋‹ค.

 

Spring ์€ Spring Cloud ์™€ ์ž˜ ๋งž๋Š” ๋ช‡๋ช‡ ์ œํ’ˆ๋“ค์„ ๊ฐ€์ ธ๋‹ค๊ฐ€ Spring Cloud์— ๋งž๊ฒŒ ์žฌ๊ตฌ์„ฑํ•˜์—ฌ ์ œ๊ณตํ•˜๋Š” ์„œ๋น„์Šค๋“ค์ด ์กด์žฌํ•œ๋‹ค.


์ตœ์ดˆ์—๋Š” ๋ชจ๋“  ์„œ๋น„์Šค๊ฐ€ Spring Cloud ์— ์˜ํ•ด์„œ ๊ด€๋ฆฌ๋˜์—ˆ์ง€๋ฉด ํ˜„์žฌ๋Š” ๋ช‡๋ช‡ ๊ธฐ์ˆ ๋“ค์„ ์žฌ๊ตฌ์„ฑ ํ˜น์€ ๋ฐœ์ „์‹œํ‚ค๊ฑฐ๋‚˜ ๋…๋ฆฝ์ ์ธ ์„œ๋น„์Šค๋กœ ์ง„ํ™”์‹œ์ผœ ๊ธฐ์กด์˜ ์„œ๋น„์Šค๋“ค์€ Maintenance ๋ชจ๋“œ๋กœ ๋“ค์–ด๊ฐ„ ๊ฒƒ๋“ค์ด ์กด์žฌํ•œ๋‹ค.

 

spring cloud netflix Maintenance Mode -> replacement

What is Maintenance Mode?

Placing a module in maintenance mode means that the Spring Cloud team will no longer be adding new features to the module. We will fix blocker bugs and security issues, and we will also consider and review small pull requests from the community.

๋ชจ๋“ˆ์„ ์œ ์ง€ ๋ณด์ˆ˜ ๋ชจ๋“œ๋กœ ์ „ํ™˜ํ•˜๋ฉด Spring Cloud ํŒ€์ด ๋” ์ด์ƒ ๋ชจ๋“ˆ์— ์ƒˆ ๊ธฐ๋Šฅ์„ ์ถ”๊ฐ€ํ•˜์ง€ ์•Š๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ์ฐจ๋‹จ ๋ฒ„๊ทธ์™€ ๋ณด์•ˆ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ , ์ปค๋ฎค๋‹ˆํ‹ฐ์˜ ์ž‘์€ ๊บผ๋‚ด๊ธฐ ์š”์ฒญ๋„ ๊ณ ๋ คํ•˜๊ณ  ๊ฒ€ํ† ํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค.

Spring Cloud Netflix - Eureka

์œ„์— ์žˆ๋Š” Maintenance Mode ๋กœ ๋“ค์–ด๊ฐ„ ํ”„๋กœ์ ํŠธ๋ฅผ ๋Œ€์ฒดํ•  ๋ช‡๋ช‡์˜ ํ”„๋กœ์ ํŠธ๋Š” ์ด๋ฒˆ ์ฃผ์ œ์—์„œ ์ด์•ผ๊ธฐํ•˜์ง€ ์•Š์œผ๋ ค ํ•œ๋‹ค.


๋งŽ์€ Netflix ์„œ๋น„์Šค๊ฐ€ Maintenance ๋˜์—ˆ์ง€๋งŒ ๊ทธ ์ค‘์—์„œ๋„ ๊ฐ€์žฅ ์ž์ฃผ ์• ์šฉํ•˜๋Š” Eureka์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์ž.

 

์šฐ์„  Eureka ์˜ ์—ญํ• ์— ๋Œ€ํ•ด์„œ ์•Œ๊ธฐ ์œ„ํ•ด์„œ๋Š” Service Discover Pattern์— ๋Œ€ํ•ด์„œ ์•Œ์•„์•ผ ํ•œ๋‹ค.
Service Discover ํŒจํ„ด์— ๋Œ€ํ•ด์„œ ์ž˜ ๋ชจ๋ฅธ๋‹ค๋ฉด MSA์˜ Service Mesh ํŒจํ„ด์„ ์ฐธ๊ณ ํ•˜๋ฉด ๋„์›€์ด ๋ ๋“ฏ ํ•˜๋‹ค.

Netflix-Eureka

๊ฐ„๋‹จํ•˜๊ฒŒ ๋งํ•˜์ž๋ฉด Service Discovery๋Š” MSA ํ•ต์‹ฌ ์š”์†Œ ์ค‘ ํ•˜๋‚˜๋‹ค.


๊ฐ ํด๋ผ์ด์–ธํŠธ์˜ ์„ค์ • ๋ฐ ์œ„์น˜ ์ •๋ณด (IP)๋ฅผ ์ˆ˜๋™์œผ๋กœ ๊ตฌ์„ฑํ•œ๋‹ค๋ฉด CI CD, ํ˜น์€ Scaling ์— ๋งค์šฐ ๋งŽ์€ ์ œ์•ฝ์‚ฌํ•ญ์ด ๋”ฐ๋ฅธ๋‹ค.


ํ•˜์ง€๋งŒ Netlfix- Eureka๋Š” ์„œ๋ฒ„๋ฅผ ๊ตฌ์„ฑ ๋ฐ ๋ฐฐํฌํ•˜์—ฌ ๊ฐ€์šฉ์„ฑ์ด ๋†’๋„๋ก ํ•  ์ˆ˜ ์žˆ์œผ๋ฉฐ, ๊ฐ ์„œ๋ฒ„๋Š” ๋“ฑ๋ก๋œ ์„œ๋น„์Šค์— ๋Œ€ํ•œ ์ƒํƒœ๋ฅผ ๋‹ค๋ฅธ ์„œ๋ฒ„๋กœ ๋ณต์ œํ•  ์ˆ˜ ์žˆ๊ฒŒ ํ•˜๋Š” ์—ญํ• ์„ ํ•œ๋‹ค.

 

์ด๋Ÿฌํ•œ Eureka์˜ ๊ฐœ๋…์—๋Š” 2๊ฐ€์ง€ ๊ตฌ์„ฑ ์š”์†Œ๋“ค์ด ์กด์žฌํ•˜๋Š”๋ฐ,

  • Service Discovery
    • ๊ฐ๊ฐ์˜ ์„œ๋น„์Šค์˜ ์œ„์น˜๊ฐ€ ๋“ฑ๋ก๋œ ์„œ๋ฒ„์—์„œ ํŠน์ • ์ž‘์—…์„ ์œ„ํ•œ ์„œ๋ฒ„์˜ ์œ„์น˜๋ฅผ ํŒŒ์•…ํ•˜๋Š” ์ž‘์—…์„ ๋œปํ•œ๋‹ค.
      ์šฐ๋ฆฌ๋Š” Service Discovery๋ฅผ ์œ„ํ•ด์„œ Spring Cloud Netflix - Eureka Server๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.
  • Service Registry
    • ๊ฐ๊ฐ์˜ ์„œ๋น„์Šค๊ฐ€ ์ž์‹ ์˜ ์œ„์น˜(IP) ์ •๋ณด๋ฅผ ํŠน์ • ์„œ๋ฒ„์— ๋“ฑ๋ก Registry ํ•˜๋Š” ์ž‘์—…์„ ๋งํ•œ๋‹ค.
      ์šฐ๋ฆฌ๋Š” Service Registry๋ฅผ ์œ„ํ•ด์„œ Spring Cloud Netflix - Eureka Client๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค.

 

์ด๋“ค์€ ๊ฐ๊ฐ ์–ด๋–ค ์ผ์„ ์ˆ˜ํ–‰ํ•˜๊ณ  ์–ด๋–ค ์ผ์„ ๊ฑฐ์น˜๋ฉด์„œ ๋™์ž‘ํ• ๊นŒ?

 

Service Discover ๊ณผ์ •

Spring Cloud Eureka๋Š” ์•„๋ž˜์™€ ๊ฐ™์€ ์ˆœ์„œ๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋œ๋‹ค.

 

  1. Service Registry ๊ธฐ๋Šฅ์„ ํ•  Eureka Server ๊ฐ€ ์ตœ์ดˆ์— ๊ธฐ๋™๋œ๋‹ค.
  2. Service Registry ์„œ๋ฒ„์ธ Eureka Server ์— ๋“ฑ๋ก๋  ์„œ๋น„์Šค๋“ค์ด ๊ธฐ๋™๋œ๋‹ค. ์—ฌ๊ธฐ์„œ ๋“ฑ๋ก๋œ ์„œ๋น„์Šค๋Š” Eureka Client ๋ผ๊ณ  ํ•œ๋‹ค.
  3. Eureka ์„œ๋ฒ„๋Š” ์ž์‹ ์—๊ฒŒ ๋“ฑ๋ก๋œ Eureka Client ์—๊ฒŒ 30์ดˆ๋งˆ๋‹ค Ping์„ ๋ณด๋‚ด๋ฉฐ Health Checking์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.
  4. ๋งŒ์•ฝ 30์ดˆ๋งˆ๋‹ค ๋ณด๋‚ด๋Š” Heart Heat๊ฐ€ ์ผ์ • ํšŸ์ˆ˜ ์ด์ƒ์œผ๋กœ ๋™์ž‘๋˜์ง€ ์•Š์œผ๋ฉด Eureka Server๋Š” ํ•ด๋‹น Client๋ฅผ ์‚ญ์ œํ•œ๋‹ค.

Eureka๋Š” ๋‹จ์ง€ ์„œ๋น„์Šค์˜ ์œ„์น˜๋งŒ์„ ํ‘œํ˜„ํ•˜๋Š” ์—ญํ• ๋กœ ๋ณดํ†ต Spring Cloud Gateway๋‚˜ Netflix Zuul ๊ณผ ๊ฐ™์€ Gateway ์„œ๋น„์Šค๋‚˜ Ribbon ๊ณผ ๊ฐ™์€ ํด๋ผ์ด์–ธํŠธ ์‚ฌ์ด๋“œ ๋กœ๋“œ๋ฐธ๋Ÿฐ์„œ์™€ ํ•จ๊ป˜ ๋™์ž‘ํ•œ๋‹ค.

 

์‹ค์Šตํ•˜๊ธฐ

 

์ด์ œ ๋Œ€์ถฉ ๊ฐœ๋…์— ๋Œ€ํ•œ ๋ถ€๋ถ„์€ ํŒŒ์•…ํ–ˆ์œผ๋‹ˆ ์‹ค์ œ Spring Boot๋ฅผ ์ด์šฉํ•ด์„œ Eureka ์„œ๋ฒ„์™€ ํด๋ผ์ด์–ธํŠธ๋ฅผ ์ด์šฉํ•œ ๊ตฌ์„ฑ์„ ํ•ด๋ณด์ž.

 

์šฐ๋ฆฌ๋Š” 2๊ฐœ์˜ ์„œ๋น„์Šค์™€ 1๊ฐœ์˜ Service Discovery ์„œ๋ฒ„๋ฅผ ๊ฐ€์šฉํ•  ๊ฒƒ์ด๋‹ค.

 

  • Service Discovery Server
    • Discovery Application
      • Spring Cloud Eureka-Server
  • Microservices
    • UserService
      • Spring Cloud Eureka-Client
    • OrderService
      • Spring Cloud Eureka-Client

Spring Cloud Eureka Server

  • ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ
  • ์˜์กด์„ฑ ์ถ”๊ฐ€
  • application.yml ์ž‘์„ฑ
  • DiscoveryApplication ์— @EnableEurekaServer ๋“ฑ๋ก
  • Eureka Dashboard

 

๊ธฐ๋ณธ์ ์œผ๋กœ ์‹ค์Šต IDE๋Š” IntelliJ๋ฅผ ์ด์šฉํ•  ๊ฒƒ์ด๋‹ค.

 

๋งŒ์•ฝ ๋ณธ์ธ์ด IntelliJ ๋ผ์ด์„ผ์Šค๊ฐ€ ์—†๊ณ  ํ•™์ƒ์ด๋‹ค๋ฉด 1๋…„ ๋ฌด๋ฃŒ ์ด์šฉ ๋ผ์ด์„ผ์Šค๊ฐ€ ์กด์žฌํ•˜๋‹ˆ ์ด์šฉํ•˜๋Š” ๊ฒƒ๋„ ๋‚˜์˜์ง€ ์•Š๋‹ค.

 

์‹ค์Šต ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

์šฐ์„  ์‹ค์Šต์„ ์œ„ํ•œ ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ•˜์ž.


๋‚˜๋Š” my-msa ๋ผ๋Š” ๋””๋ ‰ํ† ๋ฆฌ๋ฅผ ์ƒ์„ฑํ–ˆ๋‹ค.


๊ทธ๋ฆฌ๊ณ  ํ•ด๋‹น ๋””๋ ‰ํ† ๋ฆฌ ์•„๋ž˜์— Discovery-Server ๋ฅผ ์ƒ์„ฑํ•˜์ž.

 

์˜์กด์„ฑ ์ถ”๊ฐ€

 

ํ•„์š”ํ•œ ์˜์กด์„ฑ์€ Spring Cloud Eureka Server ์ด๋‹ค.

 

ext {
    set('springCloudVersion', "2020.0.2")
}

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
}

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

์—ฌ๊ธฐ์„œ Spring Boot์˜ ๋ฒ„์ „๊ณผ Spring Cloud ์˜ ๋ฒ„์ „์€ ๋งค์šฐ ์ค‘์š”ํ•˜๋‹ค. ๊ฐ๊ฐ ํ˜ธํ™˜์ด ๋˜๋Š” ๋ฒ„์ „์ด ๋”ฐ๋กœ ์žˆ์œผ๋‹ˆ ์ถฉ๋ถ„ํžˆ ์กฐ์‚ฌํ•ด๋ณด๋„๋ก ํ•˜์ž. ์ผ๋‹จ ํ˜„์žฌ 2021๋…„ 4์›” ๊ธฐ์ค€์œผ๋กœ๋Š” Spring Boot 2.4 ๋Œ€ ๋ฒ„์ „๊ณผ Spring Cloud 2020 ๋ฒ„์ „์ด ๊ถŒ์žฅ๋˜๊ณ  ์žˆ๋‹ค.

 

application.yml ์ž‘์„ฑ

Springboot ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒ์„ฑํ•˜๋ฉด ๊ธฐ๋ณธ์ ์œผ๋กœ ์„ค์ •์„ ์œ„ํ•œ ํŒŒ์ผ๋กœ application.properties ํŒŒ์ผ์ด ์ƒ์„ฑ๋˜๋Š”๋ฐ, ํ•ด๋‹น ํ”„๋กœํผํ‹ฐ ํŒŒ์ผ์€ ์ค‘๋ณต๋˜๋Š” ์ฝ”๋“œ๊ฐ€ ๋งŽ์ด ๋‚˜์˜ฌ ์ˆ˜ ์žˆ์œผ๋ฏ€๋กœ aaplication.yml ํŒŒ์ผ๋กœ ๋Œ€์ฒดํ•˜์—ฌ ์ž‘์„ฑํ•œ๋‹ค.

 

server:    
  port: 8761 # 1๋ฒˆ

spring: 
  application:
    name: discovery-service # 2๋ฒˆ

eureka:
  client:
    register-with-eureka: false # 3๋ฒˆ
    fetch-registry: false  # 4๋ฒˆ

 

application.yml ํŒŒ์ผ์ด ํ•ด๋‹น ํ”„๋กœ์ ํŠธ์—์„œ ๊ฐ€์žฅ ๊ฐ€์žฅ ๊ฐ€์žฅ ์ค‘์š”ํ•œ ํŒŒ์ผ์ด๋‹ค.

 

  1. server.port : ํ•ด๋‹น ์œ ๋ ˆ์นด ์„œ๋ฒ„๊ฐ€ ๋™์ž‘๋˜๋Š” ์„œ๋ฒ„์˜ ํฌํŠธ๋ฅผ ์ง€์ •ํ•œ๋‹ค. ๋ณดํ†ต 8761 ํฌํŠธ๋ฅผ ๋งŽ์ด ์‚ฌ์šฉํ•œ๋‹ค.
  2. spring.application.name : Application ์ด๋ฆ„์„ ์ง€์ •ํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ, ์•ž์œผ๋กœ ๋ชจ๋“  ์„œ๋น„์Šค๋ฅผ application.name ์œผ๋กœ ์‹๋ณ„ํ•œ๋‹ค.
  3. eureka.client.register-with-eureka : ํ•ด๋‹น ์„œ๋ฒ„๋ฅผ ํด๋ผ์ด์–ธํŠธ๋กœ ๋™์ž‘์‹œํ‚ค๊ฒ ๋ƒ๋Š” ์„ค์ •์„ false ๋กœ ์„ค์ •ํ•ด์•ผ ํ•œ๋‹ค.
  4. eureka.client.fetch-registry : ์œ„์™€ ๋™์ผํ•œ false๋กœ ์ง€์ •ํ•ด์•ผ ํ•˜๋Š”๋ฐ, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด ์ž์‹ ์„ ๋””์Šค์ปค๋ฒ„๋ฆฌ์— ๋“ฑ๋กํ•˜๊ฒŒ ๋œ๋‹ค.

DiscoveryApplication ์— @EnableEurekaServer ๋“ฑ๋ก

ํ”„๋กœ์ ํŠธ์˜ Application์ด ๋Œ์•„๊ฐ€๋Š” ๋ฃจํŠธ ๋””๋ ‰ํ† ๋ฆฌ๋กœ ๊ฐ€์„œ DiscoveryApplication.java๋ฅผ ํด๋ฆญํ•œ๋‹ค.


๊ทธ๋Ÿผ @SpringBootApplication ์ด๋ผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์–ด์žˆ๋Š”๋ฐ, ํ•ด๋‹น ์–ด๋…ธํ…Œ์ด์…˜ ์•„๋ž˜์— @EnableEurekaServer ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•ด์ค€๋‹ค.

 

๊ทธ๋Ÿผ ํ•ด๋‹น ์„œ๋ฒ„๋Š” EurekaServer ๋กœ ๋™์ž‘ํ•˜๊ฒŒ ๋˜๋Š” ๊ฒƒ์ด๋‹ค.

 

@SpringBootApplication
@EnableEurekaServer
public class EurekaApplication {

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

}

 

๊ทธ๋ฆฌ๊ณ  App์„ ์‹คํ–‰์‹œ์ผœ๋ณด์ž.

 

๊ทธ๋Ÿผ ์ •์ƒ์ ์œผ๋กœ ์ž˜ ์‹คํ–‰์ด ๋˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 

๊ทธ๋ฆฌ๊ณ  localhost:8761 ๋กœ ์ ‘์†ํ•˜๋ฉด Eureka Dashboard๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋Š”๋ฐ ์•ž์œผ๋กœ ์ž์ฃผ ๋ณด๊ฒŒ๋  ๋Œ€์‰ฌ๋ณด๋“œ์ด๋‹ค.

 

Spring Cloud Eureka Client

 

  • ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ
  • ์˜์กด์„ฑ ์ถ”๊ฐ€
  • application.yml ์ž‘์„ฑ
  • UserApplication์™€ OrderService ์— @EnableDiscoveryClient ์–ด๋…ธํ…Œ์ด์…˜ ์ถ”๊ฐ€
  • ๊ฐ๊ฐ ์„œ๋ฒ„์˜ Controller ์ƒ์„ฑ

์šฐ๋ฆฌ๋Š” ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋กœ 2๊ฐœ์˜ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋ฅผ ๊ตฌ์„ฑํ•œ๋‹ค๊ณ  ํ–ˆ๋‹ค.

 

  1. User ์„œ๋น„์Šค user-service
  2. Order ์„œ๋น„์Šค order-service

ํ”„๋กœ์ ํŠธ 2๊ฐœ๋ฅผ ๊ฐ๊ฐ ์ƒ์„ฑํ•˜๊ณ  ์ด๋ฆ„์„ ์œ„์™€ ๊ฐ™์ด ํ•ด๋ณด์ž.

 

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

ํ”„๋กœ์ ํŠธ๋ฅผ ๊ฐ๊ฐ user-service์™€ order-service ๋กœ ๊ฐ๊ฐ ์ƒ์„ฑํ•˜์ž

 

์˜์กด์„ฑ ์ถ”๊ฐ€

 

ํ”„๋กœ์ ํŠธ๋ฅผ ๊ฐ๊ฐ user-service์™€ order-service ๋กœ ๊ฐ๊ฐ ์ƒ์„ฑํ•˜๊ณ  ๋‹ค์Œ ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•ด์ฃผ์ž!

 

  • Lombok
  • Spring Web
  • Eureka Discovery Client

ํ•ด๋‹น ์„œ๋ฒ„๋Š” Springboot ์˜ ์ผ๋ฐ˜์ ์ธ ์„œ๋ฒ„๋กœ์„œ ๋™์ž‘ํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— Spring Web ์˜์กด์„ฑ๊ณผ Lombok ์˜์กด์„ฑ์„ ์ถ”๊ฐ€๋กœ ๋ฐ›์•„์•ผ ํ•œ๋‹ค.

 

plugins {
    id 'org.springframework.boot' version '2.4.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '11'

configurations {
    compileOnly {
        extendsFrom annotationProcessor
    }
}

repositories {
    mavenCentral()
}

ext {
    set('springCloudVersion', "2020.0.2")
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
    compileOnly 'org.projectlombok:lombok'
    annotationProcessor 'org.projectlombok:lombok'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

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

test {
    useJUnitPlatform()
}

application.yml ์ž‘์„ฑ

 

UserService

server:
  port: 64412

spring:
  application:
    name: user-service

eureka:
  instance:
    instance-id: user-microservice-instance

  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka

 

OrderService

server:
  port: 54412

spring:
  application:
    name: order-service

eureka:
  instance:
    instance-id: order-microservice-instance

  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka

 

๋ญ๊ฐ€ ๋งŽ์•„๋ณด์ธ๋‹ค. ํ•˜๋‚˜์”ฉ ์•Œ์•„๋ณด์ž.

 

  • server.port : ์„œ๋ฒ„์˜ ํฌํŠธ ๋ฒˆํ˜ธ, ์œ„์™€ ๋™์ผํ•˜๋‹ค
  • spring.application.name : ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„œ๋ฒ„์˜ ์ด๋ฆ„ , ์œ„์™€ ๋™์ผํ•˜๋‹ค
  • eureka.instance.instance-id ์œ ๋ ˆ์นด๊ฐ€ ์ธ์‹ํ•  instance id ์ธ๋ฐ ๋Œ€์‰ฌ๋ณด๋“œ์—์„œ ์‹๋ณ„ํ•˜๋Š” ์šฉ๋„๋กœ ์‚ฌ์šฉ๋œ๋‹ค.
  • eureka.client.register-with-eureka & fetch-registry ์ด๋ฒˆ์—๋Š” ํ•ด๋‹น ์„œ๋น„์Šค๊ฐ€ ํด๋ผ์ด์–ธํŠธ๋กœ ์ธ์‹๋˜์–ด์•ผ ํ•˜๋ฏ€๋กœ true๋กœ ํ•ด์ฃผ์ž
  • eureka.client.service-url.defaultZone : eureka server ๊ฐ€ ์œ„์น˜ํ•˜๊ณ  ์žˆ๋Š” ๊ธฐ๋ณธ ์ฃผ์†Œ๋ฅผ ์ ์–ด์ค„ ์ˆ˜ ์žˆ๋‹ค.

defaultZone ๊ฐ™์€ ๊ฒฝ์šฐ๋Š” ๊ผญ Camel Case ๋กœ ์ ์–ด์•ผ ํ•œ๋‹ค๊ณ  Spring Cloud Eureka์— ๋‚˜์™€์žˆ๋‹ค.

 

The defaultZone property is case sensitive and requires camel case because the serviceUrl property is a Map<String, String>. Therefore, the defaultZone property does not follow the normal Spring Boot snake-case convention of default-zone.

Application์— @EnableDiscoveryClient ์–ด๋…ธํ…Œ์ด์…˜ ์ถ”๊ฐ€ & Controller ์ถ”๊ฐ€

 

ํ•ด๋‹น ์„œ๋น„์Šค๊ฐ€ Eureka Client ์ž„์„ ์•Œ๋ ค์ฃผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜๊ณผ ๋”๋ถˆ์–ด ๊ฐ๊ฐ์˜ ์„œ๋น„์Šค๋ฅผ ์‹๋ณ„ํ•˜๊ธฐ ์œ„ํ•ด์„œ ์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ถ”๊ฐ€ํ•ด๋ณด์ž.

 

@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class UserServiceApplication {

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

    @GetMapping("/info")
    public String info(@Value("${}") String port) {
        return "User ์„œ๋น„์Šค์˜ ๊ธฐ๋ณธ ๋™์ž‘ Port: {" + port + "}";
    }

}


@SpringBootApplication
@EnableDiscoveryClient
@RestController
public class OrderServiceApplication {

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

    @GetMapping("/info")
    public String info(@Value("${server.port}") String port) {
        return "Order ์„œ๋น„์Šค์˜ ๊ธฐ๋ณธ ๋™์ž‘ Port: {" + port + "}";
    }

}

 

๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ ์œ ๋ ˆ์นด ๋Œ€์‰ฌ๋ณด๋“œ๋กœ ๊ฐ€๋ฉด ์•ž์„œ ๋“ฑ๋กํ–ˆ๋˜ 2๊ฐœ์˜ ์„œ๋น„์Šค๊ฐ€ ์˜ฌ๋ผ์˜จ ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๊ฐ๊ฐ URL์— ๋“ค์–ด๊ฐ€์„œ /info ์ฃผ์†Œ๋กœ ํ–ฅํ•œ๋‹ค๋ฉด ์ž˜ ๋™์ž‘ํ•˜๋Š” ๊ฒƒ์„ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

 


์ด๋ ‡๊ฒŒ ์˜ค๋Š˜์€ ์•„์ฃผ์•„์ฃผ ๊ธฐ๋ณธ์ ์ธ Spring Cloud Eureka์— ๋Œ€ํ•ด์„œ ์•Œ์•„๋ณด์•˜๋‹ค.

 

Spring Cloud Eureka๋Š” Default ์„ค์ •์ด ์•„์ฃผ ๊ธฐ๊ฐ€๋ง‰ํ˜€์„œ ๋”ฐ๋กœ ์„ค์ •์„ ํ•˜์ง€ ์•Š๋”๋ผ๋„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋Š” ์ˆ˜์ค€์ด๋ผ๊ณ  ํ•œ๋‹ค.


ํ•˜์ง€๋งŒ ์–ด์ฉ” ์ˆ˜ ์—†๋Š” ๋ช‡๋ช‡ ์ƒํ™ฉ์ด ์กด์žฌํ•˜๋Š”๋ฐ, ๊ทธ๋Ÿฐ ์ƒํ™ฉ์—์„œ ๋„์›€๋  ์ˆ˜ ์žˆ๋Š” ๋ช‡๊ฐ€์ง€ Eureka ๊ณ ๊ธ‰ ์„ค์ • ๊ธฐ๋ฒ•๋“ค์„ ๋‹ค์Œ์‹œ๊ฐ„์— ์•Œ์•„๋ณด๋„๋กํ•˜์ž.

๋Œ“๊ธ€