Backend/spring cloud (MSA)

Eureka๋ž€?

dddzr 2025. 2. 16. 21:57

๐Ÿ“Œ 1. Eureka๋ž€?

Eureka๋Š” Netflix์—์„œ ๊ฐœ๋ฐœํ•œ ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ ๋ฐ ๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ์„ ์ œ๊ณตํ•˜๋Š” ๋„๊ตฌ 

 

์ฃผ๋กœ Spring Cloud์—์„œ ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ ์†”๋ฃจ์…˜์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š”๋ฐ, ์ด ๊ฒฝ์šฐ ์„œ๋น„์Šค๋“ค์ด ๋™์ ์œผ๋กœ ๋“ฑ๋ก๋˜๊ณ  ์„œ๋น„์Šค ์œ„์น˜ ์ •๋ณด๋ฅผ ์ค‘์•™์—์„œ ๊ด€๋ฆฌํ•˜๊ธฐ ๋•Œ๋ฌธ์— Gateway์—์„œ ๊ฐ ์„œ๋น„์Šค์˜ uri๋ฅผ ์ง์ ‘ ์„ค์ •ํ•  ํ•„์š”์—†๋‹ค.

 

โœ… Eureka์˜ ์ฃผ์š” ๊ฐœ๋…

1๏ธโƒฃ Eureka Server

  • Eureka Server๋Š” ์—ฌ๋Ÿฌ ๋งˆ์ดํฌ๋กœ์„œ๋น„์Šค๋“ค์ด ์„œ๋กœ๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋„๋ก ์„œ๋น„์Šค ์ •๋ณด๋ฅผ ์ €์žฅํ•˜๊ณ  ์ œ๊ณต.
  • ์—ฌ๋Ÿฌ ๊ฐœ์˜ Eureka ์„œ๋ฒ„๋ฅผ ํด๋Ÿฌ์Šคํ„ฐ๋งํ•˜์—ฌ ๊ณ ๊ฐ€์šฉ์„ฑ์„ ๋ณด์žฅํ•  ์ˆ˜ ์žˆ๋‹ค.

 

2๏ธโƒฃ Eureka Client ( ์„œ๋น„์Šค ๋“ฑ๋ก & ์„œ๋น„์Šค ๊ฒ€์ƒ‰ )

  • ์„œ๋น„์Šค๊ฐ€ Eureka Server์— ์ž์‹ ์„ ๋“ฑ๋กํ•˜๊ณ , ๋‹ค๋ฅธ ์„œ๋น„์Šค๋ฅผ ์ฐพ๊ธฐ ์œ„ํ•ด Eureka Client๋ฅผ ์‚ฌ์šฉ.
  • ์ฃผ๊ธฐ์ ์œผ๋กœ ์ž์‹ ์ด ์‹คํ–‰๋˜๊ณ  ์žˆ๋Š” ํ˜ธ์ŠคํŠธ์™€ ํฌํŠธ๋ฅผ Eureka Server์— ์•Œ๋ฆฐ๋‹ค.
  • ํด๋ผ์ด์–ธํŠธ๋Š” Eureka Server์—์„œ ์ œ๊ณตํ•˜๋Š” ์„œ๋น„์Šค ๋ชฉ๋ก์„ ์กฐํšŒํ•˜๊ณ , ์„œ๋น„์Šค ์ธ์Šคํ„ด์Šค๋ฅผ ์„ ํƒํ•˜์—ฌ ํ†ต์‹ ํ•  ์ˆ˜ ์žˆ๋‹ค.

โœ… Eureka์˜ ์ฃผ์š” ์—ญํ• 

1๏ธโƒฃ ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ (Service Discovery)

  • ํด๋ผ์ด์–ธํŠธ๋‚˜ ๋‹ค๋ฅธ ์„œ๋น„์Šค๊ฐ€ ์„œ๋น„์Šค๋ฅผ ์ฐพ์„ ๋•Œ, Eureka Server์—์„œ ์„œ๋น„์Šค ๋ชฉ๋ก์„ ์กฐํšŒํ•˜์—ฌ ํ•„์š”ํ•œ ์„œ๋น„์Šค์˜ ์œ„์น˜๋ฅผ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.
  • ์ด ๋ฐฉ์‹์œผ๋กœ ์„œ๋น„์Šค๊ฐ€ ๋™์ ์œผ๋กœ ํ™•์žฅ๋˜๊ฑฐ๋‚˜ ์ถ•์†Œ๋˜์–ด๋„ ํด๋ผ์ด์–ธํŠธ๋Š” ์„œ๋น„์Šค์˜ ์‹ค์ œ ์œ„์น˜๋ฅผ ์•Œ ํ•„์š” ์—†์ด Eureka๋ฅผ ํ†ตํ•ด ์ž๋™์œผ๋กœ ์ฐพ์„ ์ˆ˜ ์žˆ๋‹ค.

 

2๏ธโƒฃ ๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ ๋ฐ Fault Tolerance

  • Eureka๋Š” ์—ฌ๋Ÿฌ ์ธ์Šคํ„ด์Šค๊ฐ€ ๋“ฑ๋ก๋œ ๊ฒฝ์šฐ ํด๋ผ์ด์–ธํŠธ์—์„œ ๋ผ์šด๋“œ ๋กœ๋นˆ(Round-Robin) ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ์„ ๋ถ„๋ฐฐํ•  ์ˆ˜ ์žˆ๋„๋ก ์ง€์›ํ•ฉ๋‹ˆ๋‹ค.
  • ํŠน์ • ์„œ๋น„์Šค ์ธ์Šคํ„ด์Šค๊ฐ€ ์‘๋‹ตํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ, Eureka๋Š” ์ด๋ฅผ ์ œ๊ฑฐํ•˜๊ณ  ๋‹ค๋ฅธ ์ธ์Šคํ„ด์Šค๋ฅผ ์‚ฌ์šฉํ•˜๋„๋ก ํ•ฉ๋‹ˆ๋‹ค.

 

3๏ธโƒฃ ์ƒํƒœ ๊ฐฑ์‹ (heartbeat ์ฒดํฌ) & ์ž์ฒด ๋ณต๊ตฌ ๊ธฐ๋Šฅ (Self-healing)

  • Eureka Client๋Š” ์ผ์ • ์ฃผ๊ธฐ๋กœ Heartbeat ์‹ ํ˜ธ๋ฅผ Eureka Server์— ๋ณด๋‚ด ์„œ๋น„์Šค ์ƒํƒœ๋ฅผ ๊ฐฑ์‹ ํ•œ๋‹ค.
  • Heartbeat ์‹ ํ˜ธ๋ฅผ ๋ฐ›์ง€ ๋ชปํ•œ ๊ฒฝ์šฐ Eureka Server๋Š” ํ•ด๋‹น ์„œ๋น„์Šค๋ฅผ DOWN ์ƒํƒœ๋กœ ํ‘œ์‹œ or ๋ชฉ๋ก์—์„œ ์ œ๊ฑฐ.
  • ์„œ๋น„์Šค๊ฐ€ ๋‹ค์‹œ ์‹œ์ž‘๋˜๋ฉด Eureka์— ๋‹ค์‹œ ๋“ฑ๋ก๋˜์–ด ์ž๋™์œผ๋กœ ๋ณต๊ตฌ๋œ๋‹ค.

 

โญ ์ธ์Šคํ„ด์Šค ์ƒํƒœ

  • ํ—ฌ์Šค์ฒดํฌ ์‹คํŒจ ์‹œ : status Down์œผ๋กœ ๋ณ€๊ฒฝ
  • ํ—ฌ์Šค์ฒดํฌ ์ผ์ •๊ธฐ๊ฐ„(leaseExpirationDurationInSeconds, ๊ธฐ๋ณธ 90์ดˆ) ๋™์•ˆ ์‹คํŒจ: ๋“ฑ๋ก ํ•ด์ œ
  • ๋น„ํ™œ์„ฑํ™”(๊ด€๋ฆฌ์ž ๋˜๋Š” ์‹œ์Šคํ…œ์ด ์„ค์ •): ์œ ์ง€๋ณด์ˆ˜ ๋“ฑ์˜ ์ด์œ ๋กœ ์ผ์‹œ์ ์œผ๋กœ ์ œ์™ธํ•˜๋Š” ๊ฒƒ, ์„œ๋น„์Šค๋Š” ์‹คํ–‰ ์ค‘ ์ด์ง€๋งŒ ํด๋ผ์ด์–ธํŠธ ์š”์ฒญ์„ ๋ฐ›์ง€ ์•Š๋„๋ก ์„ค์ •๋จ.
์ƒํƒœ ์„ค๋ช… Eureka์— ๋“ฑ๋ก ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ ์ž๋™ ๋ณต๊ตฌ
OUT_OF_SERVICE ์„œ๋น„์Šค๊ฐ€ ์ผ์‹œ์ ์œผ๋กœ ์‚ฌ์šฉ ์ค‘์ง€๋จ (๋ณดํ†ต ์ˆ˜๋™ ์„ค์ •) โœ… (๋“ฑ๋ก๋จ) โŒ (์กฐํšŒ ๋ถˆ๊ฐ€) โœ… (UP์œผ๋กœ ๋ณ€๊ฒฝํ•˜๋ฉด ์ฆ‰์‹œ ๋ณต๊ตฌ๋จ)
DOWN ์„œ๋น„์Šค๊ฐ€ ์žฅ์• ๋กœ ์ธํ•ด ๋™์ž‘ํ•˜์ง€ ์•Š์Œ โœ… (๋“ฑ๋ก๋จ) โŒ (์กฐํšŒ ๋ถˆ๊ฐ€) โœ… (UP์œผ๋กœ ๋ณ€๊ฒฝํ•˜๊ฑฐ๋‚˜ ํ—ฌ์Šค ์ฒดํฌ ๋ณต๊ตฌ ์‹œ ์ž๋™ ๋ณต๊ตฌ)
DEREGISTERED ์„œ๋น„์Šค๊ฐ€ ์™„์ „ํžˆ ์‚ญ์ œ๋จ โŒ (๋“ฑ๋ก ํ•ด์ œ๋จ) โŒ (์กฐํšŒ ๋ถˆ๊ฐ€) โŒ (์ƒˆ๋กญ๊ฒŒ ์žฌ๋“ฑ๋กํ•ด์•ผ ํ•จ)

 

๐Ÿ“Œ 2. ์„œ๋ฒ„ ์„ค์ • ๋ฐฉ๋ฒ•

โœ… 2-1. ์˜์กด์„ฑ ์ถ”๊ฐ€

๐Ÿ“– build.gradle

implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
dependencyManagement {
    imports {
        mavenBom "org.springframework.cloud:spring-cloud-dependencies:2024.0.0"
    }
}

*๋ฒ„์ „ ์ฐธ๊ณ 

https://mvnrepository.com/artifact/org.springframework.cloud/spring-cloud-dependencies

โœ… 2-2. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ •

๐Ÿ“– application.properties

spring.application.name=eureka

# Eureka ์„œ๋ฒ„ ํฌํŠธ ์„ค์ •
server.port=8761

# Eureka ์„œ๋ฒ„ ์„ค์ •
 # ์ด ์„œ๋ฒ„๋Š” ๋‹ค๋ฅธ ์„œ๋ฒ„์— ๋“ฑ๋กํ•˜์ง€ ์•Š์Œ
eureka.client.registerWithEureka=false
 # ์ด ์„œ๋ฒ„๋Š” ๋‹ค๋ฅธ ์„œ๋ฒ„์˜ ์ •๋ณด๋ฅผ ๊ฐ€์ ธ์˜ค์ง€ ์•Š์Œ
eureka.client.fetchRegistry=false
# ์„œ๋ฒ„ ์ž๊ธฐ ๋ณดํ˜ธ ๋ชจ๋“œ ๋น„ํ™œ์„ฑํ™”
eureka.server.enableSelfPreservation=false
# ์ด ์„ค์ •์„ ํ†ตํ•ด ๊ธฐ๋ณธ UI๋ฅผ ํ™œ์„ฑํ™”ํ•  ์ˆ˜ ์žˆ์Œ
management.endpoints.web.exposure.include=*

 

โœ… 2-3. @EnableEurekaServer ์ถ”๊ฐ€

๐Ÿ“– main class

package com.example.eureka;

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

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

 

๐Ÿ“Œ 3. Client ์„ค์ • ๋ฐฉ๋ฒ•

โœ… 3-1. ์˜์กด์„ฑ ์ถ”๊ฐ€

๐Ÿ“– build.gradle

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

 

โœ… 3-2. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค์ •

๐Ÿ“– application.properties

spring.application.name=gateway

# Spring Cloud Gateway ์„ค์ •

# ์„œ๋ฒ„ ํฌํŠธ ์„ค์ •
server.port=8080

# ๋ผ์šฐํŠธ ์„ค์ •
# Eureka ์„œ๋น„์Šค ๋””์Šค์ปค๋ฒ„๋ฆฌ์™€ ํ†ตํ•ฉ
# ๋“ฑ๋ก๋œ ์„œ๋น„์Šค ์ด๋ฆ„์„ ์‚ฌ์šฉํ•˜์—ฌ ์—ฌ๋Ÿฌ ์ธ์Šคํ„ด์Šค์— ๋Œ€ํ•ด ๋กœ๋“œ ๋ฐธ๋Ÿฐ์‹ฑ์„ ์ˆ˜ํ–‰.
spring.cloud.gateway.routes[0].id=to_service1
spring.cloud.gateway.routes[0].uri=lb://SERVICE-1
spring.cloud.gateway.routes[0].predicates[0]=Path=/service1/**
spring.cloud.gateway.routes[0].filters[0]=AddRequestHeader=X-Gateway-Header, Gateway-Value

# Eureka ์„œ๋ฒ„ URL (Eureka์™€ ์—ฐ๋™ ์‹œ)
eureka.client.service-url.defaultZone=http://localhost:8761/eureka/

# Eureka ์„œ๋น„์Šค ๋“ฑ๋ก์„ ํ†ตํ•œ ๋™์  ๋ผ์šฐํŒ… ํ™œ์„ฑํ™”
spring.cloud.gateway.discovery.locator.enabled=true
# eureka server registry์— ํ˜„์žฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ž์‹ ์˜ ์ •๋ณด๋ฅผ ๋“ฑ๋กํ• ์ง€ (๋‹ค๋ฅธ ์„œ๋น„์Šค๋“ค์ด name์œผ๋กœ ์ ‘๊ทผ๊ฐ€๋Šฅ)
eureka.client.registerWithEureka=true
# registry์— ์žˆ๋Š” ์ •๋ณด(๋“ฑ๋ก๋œ ์„œ๋น„์Šค ๋ชฉ๋ก)๋“ค์„ ๊ฐ€์ ธ์˜ฌ์ง€ ์—ฌ๋ถ€
eureka.client.fetchRegistry=true
#Actuator ์—”๋“œํฌ์ธํŠธ ํ™œ์„ฑํ™” -> ์•ˆ ํ•ด๋„ ๋˜๋Š”๋ฐ ์œ ๋ ˆ์นด ์„œ๋ฒ„์—์„œ ์กฐํšŒํ•˜๋Š”๊ฒŒ ์žˆ์Œ.
management.endpoints.web.exposure.include=*

 

โœ… 3-3. @EnableDiscoveryClient์ถ”๊ฐ€ (์ตœ์‹  ๋ฒ„์ „์—์„œ ์ƒ๋žต ๊ฐ€๋Šฅ)

๐Ÿ“– main class

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@SpringBootApplication
@EnableDiscoveryClient
public class GatewayApplication {
    public static void main(String[] args) {
        SpringApplication.run(GatewayApplication.class, args);
    }
}



๐Ÿ“Œ 4. ์„œ๋น„์Šค ํ™•์ธ

์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์‹คํ–‰ ํ›„ http://localhost:8761(์œ ๋ ˆ์นด ์„œ๋ฒ„ ํฌํŠธ) ๋กœ ์ ‘์†ํ•˜์—ฌ ๋“ฑ๋ก๋œ ์„œ๋น„์Šคํ™•์ธ ๊ฐ€๋Šฅ.

  • Instances ์—์„œ ๋“ฑ๋กํ•œ application์ด ์กฐํšŒ๋œ๋‹ค.
  • status๋งํฌ ํด๋ฆญ ์‹œ http://desktop-2lii5um:8080/actuator/info๋กœ ์ด๋™๋œ๋‹ค.

 

โŒ ์—๋Ÿฌ: Invalid host: lb://{service_name}

[139c33b5] There was an unexpected error (type=Internal Server Error, status=500).
Invalid host: lb://product_service

2025-01-08T13:03:38.745+09:00 INFO 14960 --- [gateway] [nio-8080-exec-5] o.apache.coyote.http11.Http11Processor : Error parsing HTTP request header Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level. java.lang.IllegalArgumentException: Invalid character found in method name [0x160x030x010x000xf70x010x000x000xf30x030x030x850x020xcc00x8f>6\0xb60xd2e0xc20xb10xe80xa40xe60x86js0x8fl%,0xa90xd10x1aU0xd0,"0xbb0xcc ]. HTTP method names must be tokens
//…
[GatewayFilterAdapter{delegate=org.springframework.cloud.gateway.filter.ForwardRoutingFilter@1dde45af}, order = 2147483647]] 2025-01-08T13:04:00.262+09:00 ERROR 14960 --- [gateway] [nio-8080-exec-6] a.w.r.e.AbstractErrorWebExceptionHandler : [6b9f950f] 500 Server Error for HTTP GET "/product/api/products/all" java.lang.IllegalStateException: Invalid host: lb://product_service at

 

๐Ÿ›  ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

1. ์„œ๋น„์Šค ๋ช… ๋Œ€์‹  port ์ง์ ‘ ์„ค์ •

์ด๋ ‡๊ฒŒ ํ•˜๋‹ˆ๊นŒ ์ ‘์†๋จ. -> ์œ ๋ ˆ์นด ๋ฌธ์ œ์ธ ๊ฒƒ!!

spring.cloud.gateway.routes[1].uri=http://localhost:8083
# lb://product_service

 

2. ์„œ๋น„์Šค ๋ช… ์ˆ˜์ •

Eureka์— ๋“ฑ๋กํ•˜๋Š” ์„œ๋น„์Šค์— _์žˆ์œผ๋ฉด ์•ˆ๋œ๋‹ค๊ณ  ํ•œ๋‹ค. -๋กœ ์ˆ˜์ •ํ•˜๋‹ˆ๊นŒ ์ ‘์†๋จ.

spring.application.name=product-service

 

'Backend > spring cloud (MSA)' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

์‹ค์‹œ๊ฐ„ ๋ฐ์ดํ„ฐ ์ „์†ก  (0) 2025.02.23
[Kafka] ๊ณ ๊ธ‰ ์„ค์ •  (0) 2025.02.23
Kafka๋ž€?  (0) 2025.02.23
GateWay๋ž€?  (0) 2025.02.16
Spring Cloud๋ž€?  (0) 2025.02.16