[Spring Cloud] - 6. Eureka๋ฅผ ์ด์šฉํ•œ ์„œ๋น„์Šค ๊ฒ€์ƒ‰

๋ฐ˜์‘ํ˜•

MSA๋กœ ์„ค๊ณ„๋œ ์›น ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ๊ทธ ์ธ์Šคํ„ด์Šค๊ฐ€ ์„œ๋น„์Šค๋ณ„๋กœ ๋‚˜๋ˆ„์–ด์ ธ ์žˆ๊ณ , ๋งŒ์•ฝ ์ด๋ฅผ ๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ์„ ์ด์šฉํ•ด ์—ฌ๋Ÿฌ ์„œ๋น„์Šค๋ฅผ ๋ถ„์‚ฐ ์ฒ˜๋ฆฌํ•˜๊ฒŒ ๋œ๋‹ค๋ฉด, ์„œ๋กœ ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค๊ฐ€ ์—ฌ๋Ÿฌ ๊ฐœ ์ƒ๊ธฐ๊ฒŒ ๋˜๋ฉฐ, ๊ทธ๋ ‡๊ฒŒ ๋˜๋ฉด API ์š”์ฒญ๋งˆ๋‹ค ์ ์ ˆํ•œ ์ธ์Šคํ„ด์Šค์˜ Endpoint๋ฅผ ๋งค๊ฒจ์ค˜์•ผ ํ•ฉ๋‹ˆ๋‹ค. 

Service Discovery๋Š” ์ด๋Ÿฌํ•œ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๊ณ ์ž, Server-Client ๋ฐฉ์‹๊ณผ ์œ ์‚ฌํ•œ Self-Registration Pattern์„ ์ด์šฉํ•ด ์ž์‹ ์˜ ์„œ๋น„์Šค๋ฅผ ์ค‘์•™์˜ Registry์— ๋“ฑ๋กํ•˜๊ณ , ์ด๋ฅผ ์š”์ฒญํ•  ๋•Œ๋งˆ๋‹ค ์•Œ๋งž๋Š” Endpoint๋ฅผ ๋งค์นญํ•ด์ค€๋‹ค๋Š” ๊ฒƒ์„ ์•Œ์•˜์Šต๋‹ˆ๋‹ค.

 

Eureka

Spring Cloud ์Šคํƒ์—์„œ ์ด๋Ÿฌํ•œ ์—ญํ• ์„ ํ•˜๋Š” ๋Œ€ํ‘œ์ ์ธ ๋ชจ๋“ˆ๋กœ Eureka๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค. Eureka๋Š” Netflix OSS ์ค‘ ํ•˜๋‚˜๋กœ Spring Boot์™€ ํ˜ธํ™˜๋˜์–ด ๋™์ž‘ํ•ฉ๋‹ˆ๋‹ค.

Eureka๋Š” ๊ธฐ๋ณธ์ ์œผ๋กœ REST ๊ธฐ๋ฐ˜์˜ ์„œ๋น„์Šค์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์„œ๋น„์Šค ์ธ์Šคํ„ด์Šค๋“ค์€ ์ •ํ•ด์ง„ Eureka Server์˜ Endpoint๋ฅผ ์ด์šฉํ•˜์—ฌ ์ž์‹ ์„ ๋“ฑ๋กํ•˜๊ณ , ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์›ํ•˜๋Š” ์„œ๋น„์Šค API์— ๋งž๊ฒŒ ์„œ๋ฒ„์—์„œ ์ธ์Šคํ„ด์Šค๋ฅผ ๋งค์นญ์‹œ์ผœ์ฃผ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. 

์ด ์™ธ์—๋„ Apache Zookeeper, Consul ๋“ฑ ๋‹ค์–‘ํ•œ Service Discovery๋ฅผ ์ง€์›ํ•˜๋Š” ๋ชจ๋“ˆ์ด ์žˆ์Šต๋‹ˆ๋‹ค.

 

Create Eureka Server

๊ทธ๋Ÿผ ์ด์ œ ๊ฐ„๋‹จํžˆ ์‚ฌ์šฉ ๋ฐฉ๋ฒ•์— ๋Œ€ํ•ด ์•Œ์•„๋ณด๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € Eureka ์„œ๋ฒ„๋ฅผ ํ•˜๋‚˜ ๋งŒ๋“ค์–ด๋ณด๋„๋ก ํ•˜์ฃ .

...

extra["springCloudVersion"] = "Hoxton.SR1"

dependencies {
    ...
    implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-server")
    ...
}

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

...

Gradle์— ์œ„์™€ ๊ฐ™์ด eureka-server dependency๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค. 

# bootstrap.yml
server:
  port: 8761

spring:
  application:
    name: nk-eureka

#  profiles:
#    active: local

eureka:
  client:
    register-with-eureka: true
    fetch-registry: false

๊ทธ๋Ÿฐ ๋‹ค์Œ, resources/bootstrap.yml ํŒŒ์ผ์„ ํ•˜๋‚˜ ์ƒ์„ฑํ•˜์—ฌ ์œ„์˜ ๋‚ด์šฉ์„ ์ž…๋ ฅํ•ด์ค๋‹ˆ๋‹ค. ์šฐ๋ฆฌ๋Š” 8761๋ฒˆ ํฌํŠธ๋ฅผ ์‚ฌ์šฉํ•  ๊ฒƒ์ด๊ณ , ์ž์‹ ์˜ ์„œ๋น„์Šค๋ฅผ Eureka ์„œ๋ฒ„๋กœ ๋“ฑ๋กํ•  ๊ฒƒ์ž…๋‹ˆ๋‹ค. 

๊ทธ๋ฆฌ๊ณ , Profiles์˜ active๋ฅผ ์ ์–ด๋„ฃ์€ ์ฝ”๋“œ๋ฅผ ์ฃผ์„ ์ฒ˜๋ฆฌ๋งŒ ํ•˜์˜€๋Š”๋ฐ, ์ด๋Š” Eureka Server์˜ ์„ค์ •์„ Configuration Server์™€ ๋™๊ธฐํ™” ํ–ˆ์„ ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์˜ต์…˜์ด์ง€๋งŒ, ํ˜„์žฌ๋Š” ์‚ฌ์šฉํ•˜์ง€ ์•Š์„ ๊ฒƒ์ด๊ณ , ๊ฐ€๋Šฅํ•˜๋ฉด ์ด๋ฅผ ๊ถŒ์žฅํ•˜๋Š” ๋ฐ”์ž…๋‹ˆ๋‹ค.

์™œ๋ƒํ•˜๋ฉด, Eureka ์„œ๋ฒ„์— Configuration Server์˜ ์ •๋ณด๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋˜๊ณ , ์ฐจํ›„์—๋Š” Configuration Server์— Eureka Client๊ฐ€ ๋“ค์–ด๊ฐ€๊ฒŒ ๋ฉ๋‹ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‚˜ ๋‘˜ ์ค‘ ์–ด๋Š ํ•œ ์ชฝ์˜ ์„œ๋ฒ„๊ฐ€ ๋จผ์ € ์‹คํ–‰๋์œผ๋ฉด ์‹คํ–‰๋˜์—ˆ์„์ง€ ์—ฐ์ •, ๋‘ ์„œ๋ฒ„๊ฐ€ ๋™์‹œ์— ์‹คํ–‰๋˜๋Š” ๊ฒฝ์šฐ๋Š” ์ ˆ๋Œ€ ์กด์žฌํ•˜์ง€ ์•Š์Šต๋‹ˆ๋‹ค.

๋”ฐ๋ผ์„œ ์ด๋ ‡๊ฒŒ ๋˜๋ฉด ๋‘ ์„œ๋ฒ„๊ฐ€ ์„œ๋กœ ๋‚ด ์ •๋ณด๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์„œ๋ฒ„๋ฅผ ์ฐพ์ง€ ๋ชปํ•ด ์‹คํ–‰ํ•˜์ง€ ๋ชปํ•˜๋Š” ์ƒํ™ฉ์ด ๋ฉ๋‹ˆ๋‹ค.

@EnableEurekaServer
@SpringBootApplication
...

๋งˆ์ง€๋ง‰์œผ๋กœ EnableEurekaServer๋ผ๋Š” ์–ด๋…ธํ…Œ์ด์…˜๋งŒ ๋ถ™์—ฌ์ฃผ๋ฉด Eureka ์„œ๋ฒ„์˜ ๊ธฐ๋ณธ ํ‹€์ด ์™„์„ฑ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿผ ํ•œ ๋ฒˆ ์‹คํ–‰ํ•ด๋ณด๋„๋ก ํ•˜์ฃ . 

์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•œ ๋’ค, http://localhost:8761์— ์ ‘์†ํ•˜๋ฉด, ์œ„์™€ ๊ฐ™์ด Eureka์˜ ๋ฉ”์ธ ํ™”๋ฉด์ด ๋‚˜ํƒ€๋‚ฉ๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , Eureka ์„œ๋ฒ„์— ๋“ฑ๋ก๋œ ํด๋ผ์ด์–ธํŠธ๋“ค์€ Instances currently registered with Eureka์— ํ‘œ์‹œ๋˜์ง€๋งŒ, ํ˜„์žฌ๋Š” ์•„์ง Client์—์„œ ์„œ๋ฒ„์— ๋“ฑ๋กํ•œ ์„œ๋น„์Šค ์ธ์Šคํ„ด์Šค๊ฐ€ ์—†๊ธฐ ๋•Œ๋ฌธ์— ํ•˜๋‚˜๋„ ๋‚˜์˜ค๊ณ  ์žˆ์ง€ ์•Š๋Š” ๋ชจ์Šต์ž…๋‹ˆ๋‹ค.

 

Register Instance

๊ทธ๋Ÿผ ์ด์ œ ์„œ๋ฒ„๊ฐ€ ๋งŒ๋“ค์–ด์กŒ์œผ๋‹ˆ, ์ธ์Šคํ„ด์Šค๋ฅผ ๋“ฑ๋กํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค. ๋จผ์ € ๊ธฐ์กด ํฌ์ŠคํŠธ์—์„œ ์ž‘์„ฑํ•œ Member Service๋ฅผ ์—ด์–ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

implementation("org.springframework.cloud:spring-cloud-starter-netflix-eureka-client")

Gradle ํŒŒ์ผ์„ ์—ด๊ณ , ๋””ํŽœ๋˜์‹œ์— ์œ„์˜ Eureka Client๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.

eureka:
  client:
    service-url:
      defaultZone: ${EUREKA_URI:http://localhost:8761/eureka}

  instance:
    prefer-ip-address: true

๊ทธ๋Ÿฐ ํ›„, Bootstrap.yml ํŒŒ์ผ์„ ์—ด๊ณ , ์œ„์™€ ๊ฐ™์ด Eureka ์„œ๋ฒ„์˜ URI ์ฃผ์†Œ๋ฅผ ๋„ฃ์–ด์ค๋‹ˆ๋‹ค. ๊ทธ๋ฆฌ๊ณ , ๊ฐ€๋Šฅํ•œ prefer-ip-address ์˜ต์…˜์„ true๋กœ ์„ค์ •ํ•˜์—ฌ, ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋„๋ฉ”์ธ ์ฃผ์†Œ๊ฐ€ ์•„๋‹Œ ๊ฐ€๊ธ‰์  IP ์ฃผ์†Œ๋กœ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋„๋ก ํ•ด์ฃผ๋Š” ๊ฑธ ๊ถŒ์žฅํ•ฉ๋‹ˆ๋‹ค.

@EnableDiscoveryClient
@SpringBootApplication
...

๊ทธ๋ฆฌ๊ณ , EnableDiscoveryClient ์–ด๋…ธํ…Œ์ด์…˜์„ ์ถ”๊ฐ€ํ•˜๊ณ , ์„œ๋ฒ„๋ฅผ ์‹คํ–‰ํ•˜๋ฉด, Eureka ์„œ๋ฒ„์— member service๊ฐ€ ๋“ฑ๋ก๋ฉ๋‹ˆ๋‹ค.

ํ™•์ธํ•˜๊ธฐ ์ „์— ์šฐ๋ฆฌ๊ฐ€ ํ•œ ๊ฐ€์ง€ ๋” ์„œ๋ฒ„์— ๋“ฑ๋กํ•ด์•ผ ํ•  ๊ฒƒ์ด ์žˆ์Šต๋‹ˆ๋‹ค. ๋ฐ”๋กœ API Gateway์ธ๋ฐ์š”. ์™œ API Gateway๋„ Eureka ์„œ๋ฒ„์— ๋“ฑ๋กํ•ด์•ผ ํ• ๊นŒ์š”?

API Gateway์˜ ์—ญํ• ์€ ์•Œ๋‹ค์‹œํ”ผ ํŠน์ • ์„œ๋น„์Šค๊ฐ€ ์žˆ๋Š” ์—”๋“œํฌ์ธํŠธ๋ฅผ ์„œ๋น„์Šค๋ณ„๋กœ ํ˜ธ์ถœํ•ด์ฃผ๋Š” ํ”„๋ก์‹œ ์—ญํ• ์ž…๋‹ˆ๋‹ค. ๋”ฐ๋ผ์„œ ์›ํ•˜๋Š” API ์—”๋“œํฌ์ธํŠธ๋ฅผ ๋ถ€๋ฅด๊ฒŒ ๋˜๋ฉด, ๊ฒŒ์ดํŠธ์›จ์ด๋Š” Eureka ์„œ๋ฒ„๋ฅผ ํ†ตํ•ด ํ•ด๋‹น ์—”๋“œํฌ์ธํŠธ๊ฐ€ ์–ด๋Š ์ฃผ์†Œ์— ์žˆ๋Š”์ง€๋ฅผ ์š”์ฒญํ•˜๊ฒŒ ๋˜๊ณ , Eurka ์„œ๋ฒ„๊ฐ€ ์ด๋ฅผ ์ œ๊ณตํ•  ๊ฒƒ์ด๊ธฐ ๋•Œ๋ฌธ์— Zuul Gateway์—์„œ ์ด๋ฅผ ์œ ์—ฐํ•˜๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ๊ฒŒ ๋ฉ๋‹ˆ๋‹ค.

๊ทธ๋Ÿฌ๋ฉด ์ด์ œ Zuul Gateway ํ”„๋กœ์ ํŠธ๋ฅผ ์—ด์–ด์„œ, ์œ„์™€ ๊ฐ™์ด ๋””ํŽœ๋˜์‹œ๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , Eureka ์„œ๋ฒ„๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ์ž‘์—…์„ ๋™์ผํ•˜๊ฒŒ ์ง„ํ–‰ํ•ฉ๋‹ˆ๋‹ค.

๊ทธ๋ฆฌ๊ณ  ๋‹ค์‹œ Eureka ๋ฉ”์ธ ํŽ˜์ด์ง€์— ์ ‘์†ํ•˜๋ฉด, ์œ„ ์ด๋ฏธ์ง€์ฒ˜๋Ÿผ ๊ฒŒ์ดํŠธ์›จ์ด์™€ Member Service๊ฐ€ Eureka ์„œ๋ฒ„์— ๋“ฑ๋ก๋ฉ๋‹ˆ๋‹ค.

 

๋งˆ์น˜๋ฉฐ...

์—ฌ๊ธฐ๊นŒ์ง€ Eureka๋ฅผ ์ด์šฉํ•œ Service Discovery์— ๋Œ€ํ•ด ์•Œ์•„๋ดค์Šต๋‹ˆ๋‹ค. ๊ฐ„๋‹จํ•œ ์ธ์Šคํ„ด์Šค๋งŒ์„ ๋‹ค๋ฃจ๋ฉฐ ํ…Œ์ŠคํŠธ๋งŒ ํ•ด๋ณด๋Š” ๊ฒƒ์„ ๋‹ค๋ค„๋ดค์Šต๋‹ˆ๋‹ค๋งŒ ์‹ค์ œ๋กœ ์˜ํ–ฅ ์žˆ๋Š” ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋ ค๋ฉด, Docker ๋“ฑ์„ ์ด์šฉํ•ด์„œ ์—ฌ๋Ÿฌ ๊ฐœ ์ธ์Šคํ„ด์Šค๋ฅผ ์ƒ์„ฑํ•˜๊ณ , Eureka์— ์ง์ ‘ ์ถ”๊ฐ€ํ•˜๊ณ , ๋ถ€ํ•˜ ํ…Œ์ŠคํŠธ๋ฅผ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด ๊ฐ€์žฅ ๋ฐ”๋žŒ์งํ•  ๊ฒƒ์ด๋ผ๊ณ  ๋ด…๋‹ˆ๋‹ค.

๊ธฐํšŒ๊ฐ€ ๋œ๋‹ค๋ฉด, ๋ถ€ํ•˜ ํ…Œ์ŠคํŠธ ํˆด์„ ์ด์šฉํ•˜์—ฌ ์ง์ ‘ ์—ฐ๊ณ„ ์„œ๋น„์Šค๋ฅผ ํ•œ ๋ฒˆ ํฌ์ŠคํŒ… ํ•ด๋ณด๋„๋ก ํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค.

๋ฐ˜์‘ํ˜•
TAGS.

Tistory Comments