๐ก ์๋น์ค ๋์ค์ปค๋ฒ๋ฆฌ , ๋ ์ง์คํธ๋ฆฌ์ ๊ดํ ๋ด์ฉ์ ๋ฏธ๋ฆฌ ํ์ตํ๊ณ ์ฝ์ผ๋ฉด ์ข์ต๋๋ค.
Spring Cloud Eureka๋?
- ๋ทํ๋ฆญ์ค์์ ๋ง๋ ์๋น์ค
- ์๋ฒ๋ค์ ์ค์ ๋ ์ง์คํธ๋ฆฌ์ ๋ฑ๋กํ๊ณ ์ฅ์ ๊ฐ ๋ฐ์ํ ๋ ์๋์ผ๋ก ์ ์ธ
- ์ ์ธ์คํด์ค ์์์ Eureka ์๋ฒ์ IP, ํธ์คํธ ์ฃผ์, ํฌํธ ๋ฒํธ ๋ฑ์ ์ค์ค๋ก ์ ์ก.
- ์ด์์๋ค์ IP ์ ๋ณด๋ฅผ ์ ๊ฒฝ์ฐ์ง ์์๋ ๋จ.
- ๋ค๋ฅธ ์๋น์ค ํธ์ถ์ Eureka ์๋ฒ์ ๋ฑ๋ก๋ ์ธ์คํด์ค๋ฅผ ์กฐํ.
- ํด๋ผ์ด์ธํธ์ ์๋ฒ๋ก ๊ตฌ๋ถํ์ฌ ์ฌ์ฉ.
๊ตฌ์ฑ ์์
Service Discovery
- ๋ง์ดํฌ๋ก์๋น์ค์ ์์น๋ฅผ ์ฐพ๋ ์์
- Spring Cloud Netflix - Eureka Server ๋ฅผ ์ด์ฉ
Service Registry
- ๊ฐ๊ฐ์ ์๋น์ค๊ฐ ์์ ์ ์์น๋ฅผ ํน์ ์๋ฒ์ ๋ฑ๋กํ๋ ์์
- Spring Cloud Netflix - Eureka Client ๋ฅผ ์ฌ์ฉ
ํ๋ฆ
- Service Registry ๊ธฐ๋ฅ์ ํ Eureka Server ๊ฐ ์ต์ด์ ๊ธฐ๋
- Service Registry ์๋ฒ์ธ Eureka Server ์ ๋ฑ๋ก๋ ์๋น์ค๋ค์ด ๊ธฐ๋
- ์ฌ๊ธฐ์ ๋ฑ๋ก๋ ์๋น์ค๋ Eureka Client๋ผ๊ณ ๋ถ๋ฆ
- Eureka ์๋ฒ๋ ์์ ์๊ฒ ๋ฑ๋ก๋ Eureka Client ์๊ฒ 30์ด(๊ธฐ๋ณธ๊ฐ)๋ง๋ค Ping์ ๋ณด๋ด๋ฉฐ Health Checking์ ์ํ
- ๋ง์ฝ 30์ด๋ง๋ค ๋ณด๋ด๋ Heart Heat๊ฐ ์ผ์ ํ์ ์ด์์ผ๋ก ๋์๋์ง ์์ผ๋ฉด Eureka Server๋ ํด๋น Client๋ฅผ ์ญ์
Eureka๋ ๋จ์ง ์๋น์ค์ ์์น๋ง์ ํํํ๋ ์ญํ ๋ก ๋ณดํต Spring Cloud Gateway๋ Netflix Zuul ๊ณผ ๊ฐ์ Gateway ์๋น์ค๋ Ribbon ๊ณผ ๊ฐ์ ํด๋ผ์ด์ธํธ ์ฌ์ด๋ ๋ก๋๋ฐธ๋ฐ์์ ํจ๊ป ๋์ํฉ๋๋ค.
Eureka์ ํจ๊ป ์ฌ์ฉ๋๋ Zuul, Ribbon
Eureka Server ์ฌ์ฉ๋ฒ
build.gradle
ext {
set('springCloudVersion', "2021.0.4")
}
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 ๋ฒ์ ์ ํํ์ฌ ์ฌ์ฉํ์ฌ์ผ ํฉ๋๋ค.
- Spring initializr๋ฅผ ์ฌ์ฉํ๋ค๋ฉด ๋๋ถ๋ถ ์์์ ํด๊ฒฐํด์ค๋๋ค.
application.yml
server:
port: 8761 // Eureka ์๋ฒ์ ํฌํธ ์ง์ , ๋ณดํต 8761 ์ฌ์ฉ
spring:
application:
name: discovery-service // application ์ด๋ฆ ์ง์ , ์๋น์ค๋ฅผ ์๋ณํ ๋ ์ฌ์ฉ
eureka:
client:
register-with-eureka: false // ํด๋น ์๋ฒ๋ฅผ Client๋ก ๋์์ํฌ์ง๋ฅผ ์ง์ , ๊ธฐ๋ณธ๊ฐ์ด true์ด๋ฏ๋ก false ์ฒ๋ฆฌ
fetch-registry: false // ์์ ๋์ผ, ์ง์ ์ํด์ฃผ๋ฉด ์๊ธฐ์์ ์ ๋ฑ๋กํ๊ฒ๋๋ค.
@EnableEurekaServer ์ถ๊ฐ
@EnableEurekaServer
@SpringBootApplication
public class DiscoveryServerApplication {
public static void main(String[] args) {
SpringApplication.run(DiscoveryServerApplication.class, args);
}
}
Eureka Client ์ฌ์ฉ๋ฒ
build.gradle
ext {
set('springCloudVersion', "2021.0.4")
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
...
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
application.yml
server:
port: 8081 // ์๋ฒ ํฌํธ ์ค์
spring:
application:
name: item-service // app ์ด๋ฆ ์ค์
eureka:
client:
register-with-eureka: true // ํด๋น ์๋น์ค๋ฅผ ํด๋ผ์ด์ธํธ๋ก ์ธ์
fetch-registry: true // ์์ ๋์ผ
service-url:
defaultZone: http://localhost:8761/eureka // Eureka ์๋ฒ๊ฐ ์์นํ ๊ธฐ๋ณธ ์ฃผ์ ์
๋ ฅ
- ๋๋คํฌํธ ์ฌ์ฉ ์ ์ค์
- ์ธ์คํด์ค๊ฐ ๋ฑ๋ก๋ ๋ port ์ค์ ์ด 0์ผ๋ก ์กํ์์ด ์ฌ๋ฌ๊ฐ๋ฅผ ํค๋๋ผ๋ ํ๋๋ง ๋ณด์ฌ์ง๋ ํ์์ด ์๊ธฐ๊ธฐ ๋๋ฌธ์ Instance ID๋ฅผ ์ง์ ํด์ฃผ์ด์ผ ํฉ๋๋ค.
server:
port: 0
spring:
application:
name: user-service
eureka:
instance:
instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}} // ์ธ์คํด์ค id ์ถ๊ฐ
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:8761/eureka
@EnableEurekaClient ์ถ๊ฐ
@EnableEurekaClient
@SpringBootApplication
public class ItemServiceApplication {
public static void main(String[] args) {
SpringApplication.run(ItemServiceApplication.class, args);
}
}
์ ์ฉ ๋ชจ์ต
์ถ๊ฐ์ ์ธ ํ
- ์ค์ต์ ํ๋ค๋ณด๋ฉด ์ธ์คํด์ค๋ฅผ ์ฌ๋ฌ ๊ฐ ๋์์ผ ํ๋๋ฐ ๊ทธ๋๋ง๋ค Port ๋ฒํธ๋ฅผ ๋ฐ๊ฟ์ค์ผํ๋ ๋ฒ๊ฑฐ๋ก์์ด ์์ต๋๋ค.
๋๋ค ํฌํธ๋ก ์ง์ ํด์ฃผ๋ฉด ํธํ๊ฒ ์ค์ต์ ์งํํ ์ ์์ต๋๋ค.
- ์ด๊ณณ์์ ๋๋คํฌํธ ์ ์ฉํ๋ ๋ฒ์ ํ์ธํ ์ ์์ต๋๋ค.
์ฐธ๊ณ ์๋ฃ