๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Spring

๋„ทํ”Œ๋ฆญ์Šค ์œ ๋ ˆ์นด(Eureka)๋ฅผ ์‚ฌ์šฉํ•œ ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ ๊ตฌํ˜„

by Leica 2021. 8. 2.
๋ฐ˜์‘ํ˜•

๋„ทํ”Œ๋ฆญ์Šค ์œ ๋ ˆ์นด(Eureka)๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ๋ฅผ ๊ตฌํ˜„ํ•ด๋ณด์ž.

์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ 2๊ฐœ๋ฅผ ๋งŒ๋“ค์–ด์„œ ํ•˜๋‚˜๋Š” ๋””์Šค์ปค๋ฒ„๋ฆฌ ์„œ๋ฒ„, ๋‹ค๋ฅธ ํ•˜๋‚˜๋Š” ๋””์Šค์ปค๋ฒ„๋ฆฌ ํด๋ผ์ด์–ธํŠธ ์—ญํ• ์„ ํ•˜๋„๋ก ํ•  ๊ฒƒ์ด๋‹ค.

 

java 11

spring boot 2.4.9

gradle

 

๋””์Šค์ปค๋ฒ„๋ฆฌ ์„œ๋ฒ„ - ์œ ๋ ˆ์นด ์„œ๋ฒ„

์šฐ์„  ๋””์Šค์ปค๋ฒ„๋ฆฌ ์„œ๋ฒ„ ์—ญํ• ์„ ํ•  ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ธํŒ…ํ•œ๋‹ค.

 

Dependency

spring-cloud-starter-eureka-server ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

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

 

์œ ๋ ˆ์นด ์„œ๋ฒ„ ํ™œ์„ฑํ™”

์Šคํ”„๋ง ๋ถ€ํŠธ ๋ฉ”์ธ ํด๋ž˜์Šค์— @EnableEurekaServer ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ ์œ ๋ ˆ์นด ์„œ๋ฒ„๋ฅผ ํ™œ์„ฑํ™”ํ•œ๋‹ค.

@EnableEurekaServer
@SpringBootApplication
public class DiscoveryApplication {

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

}

 

์œ ๋ ˆ์นด ์„œ๋ฒ„ ์„ค์ •

๐Ÿ“„ application.yml

server:
  port: 8761

spring:
  application:
    name: eureka-server

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

์œ ๋ ˆ์นด ์„œ๋ฒ„๋Š” ์ผ์ข…์˜ ์›น์„œ๋น„์Šค์ด๋‹ค.

์Šคํ”„๋ง ๋ถ€ํŠธ ๋‚ด์žฅ ์„œ๋ฒ„๋ฅผ ํ†ตํ•ด ์„œ๋น„์Šค๋˜๋ฏ€๋กœ server.port๋กœ ์œ ๋ ˆ์นด ์„œ๋ฒ„์˜ ํฌํŠธ๋ฅผ ๋ณ€๊ฒฝํ•  ์ˆ˜ ์žˆ๋‹ค.

๋’ค์—์„œ ์‚ดํŽด๋ณผ ์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ์˜ ๊ธฐ๋ณธ ๋””์Šค์ปค๋ฒ„๋ฆฌ ์„œ๋ฒ„ ํฌํŠธ๊ฐ€ 8761์ด๋ผ ์ด ํฌํŠธ๋ฅผ ์‚ฌ์šฉํ•˜์˜€๋‹ค.

๋ณดํ†ต ์œ ๋ ˆ์นด ์„œ๋ฒ„๋Š” 8761์„ ๊ธฐ๋ณธ ํฌํŠธ๋กœ ์‚ฌ์šฉํ•œ๋‹ค.

 

๋‚˜๋จธ์ง€ ์„ค์ •์€ ๋‹ค์Œ์„ ์ฐธ๊ณ ํ•œ๋‹ค.

 

spring.application.name
MSA์—์„œ ์ด ์„œ๋น„์Šค๋ฅผ ์‹๋ณ„ํ•˜๋Š” id์ด๋‹ค.

eureka.client.register-with-eureka

์œ ๋ ˆ์นด ์„œ๋ฒ„์— ์ž๊ธฐ ์ž์‹ ์„ ํด๋ผ์ด์–ธํŠธ๋กœ ๋“ฑ๋กํ•˜์ง€ ์•Š๋„๋ก ํ•˜๋Š” ์„ค์ •์ด๋‹ค.
๋ณธ ํ”„๋กœ์ ํŠธ๋Š” ๋””์Šค์ปค๋ฒ„๋ฆฌ ์„œ๋ฒ„ ์—ญํ• ์„ ํ•˜๋Š” ์œ ๋ ˆ์นด ์„œ๋ฒ„์ด๋ฏ€๋กœ ์ž๊ธฐ ์ž์‹ ์„ ํด๋ผ์ด์–ธํŠธ๋กœ์จ ๋””์Šค์ปค๋ฒ„๋ฆฌ ์„œ๋ฒ„์— ๋“ฑ๋กํ•˜์ง€ ์•Š๋„๋ก false๋กœ ์„ค์ •ํ•œ๋‹ค.

eureka.client.fetch-registry
ํด๋ผ์ด์–ธํŠธ๋กœ์จ eureka ์„œ๋ฒ„์—์„œ eureka ๋ ˆ์ง€์ŠคํŠธ๋ฆฌ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ฌ์ง€ ์—ฌ๋ถ€๋ฅผ ์„ค์ •ํ•œ๋‹ค.
์œ„ ์„ค์ •๊ณผ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์•„๋‹ˆ๋ฏ€๋กœ false๋กœ ์„ค์ •ํ•œ๋‹ค.

 

์œ ๋ ˆ์นด ์„œ๋ฒ„ ์‹คํ–‰ ๋ฐ ํ™•์ธ

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜์—ฌ ๋‹ค์Œ url๋กœ ์„œ๋น„์Šค๋ฅผ ํ™•์ธํ•ด๋ณธ๋‹ค.

 

UI ๋Œ€์‹œ๋ณด๋“œ - http://localhost:8761

์œ ๋ ˆ์นด ์„œ๋ฒ„๊ฐ€ ์ œ๊ณตํ•˜๋Š” UI ๋Œ€์‹œ๋ณด๋“œ๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์„œ๋ฒ„ ๋ชฉ๋ก - http://localhost:8761/eureka/apps

์œ ๋ ˆ์นด ์„œ๋ฒ„์— ๋“ฑ๋ก๋œ ๋งˆ์ดํฌ๋กœ ์„œ๋น„์Šค์˜ ๋ชฉ๋ก์„ ์กฐํšŒํ•  ์ˆ˜ ์žˆ๋‹ค.

 

๋””์Šค์ปค๋ฒ„๋ฆฌ ํด๋ผ์ด์–ธํŠธ - ์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ

์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ๋Š” ๋‹ค์Œ์˜ ๋‘ ๊ฐ€์ง€ ์—ญํ• ์„ ์ˆ˜ํ–‰ํ•œ๋‹ค.

  • ์œ ๋ ˆ์นด ์„œ๋ฒ„์™€ ํ†ต์‹ ํ•˜์—ฌ ์ž์‹ ์„ ๋“ฑ๋กํ•˜๊ณ  ์„œ๋น„์Šค ์ •๋ณด๋ฅผ ๋ณด๋‚ธ๋‹ค.
  • ์„œ๋ฒ„๋กœ๋ถ€ํ„ฐ ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€์ ธ์™€ ์บ์‹ฑํ•˜๊ณ  ์ฃผ๊ธฐ์ ์œผ๋กœ ๋ณ€๊ฒฝ์‚ฌํ•ญ์„ ํ™•์ธํ•œ๋‹ค.

 

๋‹ค์Œ์œผ๋กœ ๋””์Šค์ปค๋ฒ„๋ฆฌ ํด๋ผ์ด์–ธํŠธ ์—ญํ• ์„ ํ•  ์Šคํ”„๋ง ๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ์ƒˆ๋กœ ๋งŒ๋“ ๋‹ค.

 

Dependency

spring-cloud-starter-netflix-eureka-client ์˜์กด์„ฑ์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'

 

์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ ํ™œ์„ฑํ™”

์œ ๋ ˆ์นด ์„œ๋ฒ„๋ฅผ ํ™œ์„ฑํ™”ํ• ๋•Œ @EnableEurekaServer ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ด๋“ฏ, ์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ๋„ ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์—ฌ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ๋‹ค.

์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ๋ฅผ ํ™œ์„ฑํ™”ํ•˜๊ธฐ ์œ„ํ•œ ์• ๋…ธํ…Œ์ด์…˜์€ ๋‹ค์Œ์˜ ๋‘ ๊ฐœ ์ค‘ ํ•˜๋‚˜๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ๋‹ค.

  • org.springframework.cloud.client.discovery.EnableDiscoveryClient
  • org.springframework.cloud.netflix.eureka.EnableEurekaClient

 

์šฐ์„  @EnableDiscoveryClient๋ฅผ ์‚ฌ์šฉํ•ด ๋ณด์ž.

@EnableDiscoveryClient
@SpringBootApplication
public class ClientApplication {

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

}

์Šคํ”„๋ง ๋ถ€ํŠธ ๋ฉ”์ธ ํด๋ž˜์Šค์— @EnableDiscoveryClient ์• ๋…ธํ…Œ์ด์…˜์„ ๋ถ™์ธ๋‹ค.

 

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ •

application.yml์— ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์„ค์ •๊ฐ’์„ ์ถ”๊ฐ€ํ•œ๋‹ค.

spring:
  application:
    name: client-service

eureka:
  client:
    serviceUrl:
      defaultZone: http://localhost:8761/eureka/

 

spring.application.name์€ ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ์— ๋“ฑ๋กํ•  ํด๋ผ์ด์–ธํŠธ ์„œ๋น„์Šค ์ด๋ฆ„, 

eureka.client.serviceUrl.defaultZone์€ ํด๋ผ์ด์–ธํŠธ์™€ ํ†ต์‹ ํ•  ๋””์Šค์ปค๋ฒ„๋ฆฌ ์„œ๋ฒ„ ์ฃผ์†Œ๋ฅผ ์„ค์ •ํ•œ๋‹ค.

 

์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ ์‹คํ–‰ ๋ฐ ํ™•์ธ

์•ž์„œ ๋งŒ๋“  ์œ ๋ ˆ์นด ์„œ๋ฒ„๊ฐ€ ๋„์›Œ์ ธ ์žˆ๋Š” ์ƒํƒœ์—์„œ, ์ด๋ฒˆ์— ๋งŒ๋“  ์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์‹คํ–‰ํ•˜์ž.

์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์€ ์„œ๋กœ ๋‹ค๋ฅธ ํฌํŠธ๋กœ ์ด 2๊ฐœ์˜ ์ธ์Šคํ„ด์Šค๋ฅผ ์‹คํ–‰ํ•  ๊ฒƒ์ด๋‹ค.

 

๐Ÿ–ฅ ๋นŒ๋“œ

./gradlew -x test clean build

 

๐Ÿ–ฅ ์‹คํ–‰

java -jar -Dserver.port=8081 study-eureka-client-0.0.1-SNAPSHOT.jar
java -jar -Dserver.port=8083 study-eureka-client-0.0.1-SNAPSHOT.jar

 

์œ ๋ ˆ์นด ์„œ๋ฒ„, ์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ๊ฐ€ ๋ชจ๋‘ ์ •์ƒ ์‹คํ–‰๋˜๊ณ  ๋‚˜๋ฉด ์œ ๋ ˆ์นด ์„œ๋ฒ„ UI ์ฝ˜์†” - http://localhost:8761์— ๋‹ค์‹œ ์ ‘์†ํ•ด๋ณด์ž.

 

Instances currently registered with Eureka ์˜์—ญ์— 8081, 8083 ํฌํŠธ๋กœ ๋“ฑ๋ก๋œ ๋‘ ๊ฐœ์˜ client-service ์ธ์Šคํ„ด์Šค๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

โœ๏ธ ์ฐธ๊ณ 
Application์— ๋ณด์ด๋Š” application name(์—ฌ๊ธฐ์„œ ์˜ˆ๋กœ ๋“ค๋ฉด CLIENT-SERVICE)๋Š” ๋Œ€์†Œ๋ฌธ์ž๋ฅผ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๋Š”๋‹ค.
spring.application.name์—๋Š” client-service๋กœ ์„ค์ •ํ–ˆ์ง€๋งŒ eureka ๋Œ€์‹œ๋ณด๋“œ์—์„œ๋Š” ์„œ๋น„์Šค ์ด๋ฆ„์ด ๋ชจ๋‘ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜๋˜์–ด CLIENT-SERVICE๋ผ๊ณ  ์ถœ๋ ฅ๋œ๋‹ค.

 

์ด๋ฒˆ์—” 8083 ํฌํŠธ๋กœ ๋„์šด ํด๋ผ์ด์–ธํŠธ ์ธ์Šคํ„ด์Šค๋ฅผ ์ข…๋ฃŒํ•œ ๋’ค ๋‹ค์‹œ ํ™•์ธํ•ด๋ณด์ž.

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ข…๋ฃŒ ์‹œ ๋‹ค์Œ๊ณผ ๊ฐ™์€ ๋กœ๊ทธ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

2021-08-02 22:44:09.979  INFO 10948 --- [extShutdownHook] com.netflix.discovery.DiscoveryClient    : Shutting down DiscoveryClient ...
2021-08-02 22:44:12.984  INFO 10948 --- [extShutdownHook] com.netflix.discovery.DiscoveryClient    : Unregistering ...
2021-08-02 22:44:12.990  INFO 10948 --- [extShutdownHook] com.netflix.discovery.DiscoveryClient    : DiscoveryClient_CLIENT-SERVICE/192.168.219.104:client-service:8083 - deregister  status: 200
2021-08-02 22:44:12.997  INFO 10948 --- [extShutdownHook] com.netflix.discovery.DiscoveryClient    : Completed shut down of DiscoveryClient

 

๊ทธ ๋‹ค์Œ ์œ ๋ ˆ์นด ์„œ๋ฒ„ ๋Œ€์‹œ๋ณด๋“œ๋ฅผ ๋ณด๋ฉด registered ๋ชฉ๋ก์—์„œ 8083 ํฌํŠธ์˜ ์ธ์Šคํ„ด์Šค๊ฐ€ ์ œ๊ฑฐ๋˜๊ณ , 8081 ํฌํŠธ์˜ ์ธ์Šคํ„ด์Šค๋งŒ ๋ณด์ด๋Š” ๊ฒƒ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์ถ”๊ฐ€๋กœ ์œ ๋ ˆ์นด ์„œ๋ฒ„ ๋Œ€์‹œ๋ณด๋“œ์˜ http://localhost:8761/lastn ์—์„œ๋Š” ์ƒˆ๋กญ๊ฒŒ ๋“ฑ๋ก๋˜๊ฑฐ๋‚˜ ๋“ฑ๋ก ์ทจ์†Œ๋œ ์„œ๋น„์Šค ์ด๋ ฅ์„ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค.

 

์œ ๋ ˆ์นด ํด๋ผ์ด์–ธํŠธ ์ธ์Šคํ„ด์Šค๋ฅผ ๋ชจ๋‘ ์ข…๋ฃŒ ํ›„, ํด๋ผ์ด์–ธํŠธ ํ™œ์„ฑํ™”๋ฅผ ์œ„ํ•ด ์ถ”๊ฐ€ํ–ˆ๋˜ @EnableDiscoveryClient ์• ๋…ธํ…Œ์ด์…˜์„ @EnableEurekaClient๋กœ ๋ณ€๊ฒฝ ํ›„ ์ธ์Šคํ„ด์Šค๋ฅผ ์žฌ์‹คํ–‰ํ•ด๋„ ๋™์ผํ•œ ๊ฒฐ๊ณผ๋ฅผ ๋ณผ ์ˆ˜ ์žˆ๋‹ค.

 

๐Ÿ“š ์ฐธ๊ณ  ์„œ์ 

๋งˆ์Šคํ„ฐ๋ง ์Šคํ”„๋ง ํด๋ผ์šฐ๋“œ

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€