[Project] netflix- eureka
Categories: Project
๐ ๊ฐ์ธ์ ์ธ ๊ณต๊ฐ์ผ๋ก ๊ณต๋ถ๋ฅผ ๊ธฐ๋กํ๊ณ ๋ณต์ตํ๊ธฐ ์ํด ์ฌ์ฉํ๋ ๋ธ๋ก๊ทธ์
๋๋ค.
์ ํํ์ง ์์ ์ ๋ณด๊ฐ ์์ ์ ์์ผ๋ ์ฐธ๊ณ ๋ฐ๋๋๋ค :๐ธ
[ํ๋ฆฐ ๋ด์ฉ์ ๋๊ธ๋ก ๋จ๊ฒจ์ฃผ์๋ฉด ๋ณต๋ฐ์ผ์ค๊ฑฐ์์]
netflix- eureka
์ค๋ MSA ์ ์ฉํ๊ธฐ๋ก ๊ฒฐ์ ํ๊ณ ํ์ ํ์ ๊ฐ์ ์ฃผ๋ง ๋์ MSA์ ๋ํด ๊ณต๋ถํด์ค๊ธฐ๋ก ํ๋ค.
์ง๊ธ ํ์ฐธ ์ด๋ ฅ์๋ ํฌํธํด๋ฆฌ์ค ๋ธ๋ค๊ณ ๋ค๋ค ๋ฐ๋น ์ ํ๋ก์ ํธ์ ์คํ๋ ค ์ง์ค ๋ชปํ๊ณ ์๋๋ฐ
๊ทธ๋๋ ๋ง์ง๋ง ํ๋ก์ ํธ ์์ฒญ ์ค์ํ๋๊น, ํ๋ก์ ํธ ์ ๋์์ ๋ค๋ค ๋์์ด ๋๋ฉด ์ข์ผ๋๊น ๊ณต๋ถํ๊ณ ๋ก์ปฌ์์ ํ๊ฒฝ ๊ตฌ์ฑํด๋ณด๊ธฐ๋ก ํ๋ค.
Netflix Eureka
- Netflix Eureka๋?
- Netflix Eureka๋ ์๋น์ค ๋ ์ง์คํธ๋ฆฌ(Server-side Discovery)๋ก ๋์ํ๋ ์์คํ
- ๊ฐ ๋ง์ดํฌ๋ก์๋น์ค๋ ์์ ์ Eureka Server์ ๋ฑ๋กํ๊ณ , ๋ค๋ฅธ ์๋น์ค๊ฐ Eureka Client๋ก์ ํด๋น ์๋น์ค์ ์์น ์ ๋ณด๋ฅผ ์กฐํํ ์ ์๋ค.
- ๊ธฐ๋ณธ์ ์ผ๋ก Eureka๋ Eureka Server์ Eureka Client๋ก ๋๋์ด ๋์ํ๋ฉฐ, Eureka Server๋ ์๋น์ค์ ๋ฑ๋ก๊ณผ ์กฐํ๋ฅผ ๊ด๋ฆฌํ๊ณ , Eureka Client๋ ๋ค๋ฅธ ์๋น์ค์ ๋ํ ์ ๋ณด๋ฅผ ์ป๊ณ , ํต์ ํ ์ ์๊ฒ ํด์ค๋ค.
- Eureka์ ๊ตฌ์ฑ ์์
- Eureka Server
- Eureka Server๋ ๋ชจ๋ ๋ง์ดํฌ๋ก์๋น์ค๊ฐ ๋ฑ๋ก๋๋ ์ค์ ์๋น์ค ๋ ์ง์คํธ๋ฆฌ ์ญํ ์ gํ๋ค.
- ๋ง์ดํฌ๋ก์๋น์ค๋ ์์ํ ๋ Eureka Server์ ์์ ์ ์์น(IP, ํฌํธ ๋ฑ)๋ฅผ ๋ฑ๋กํ๊ณ , ์ฃผ๊ธฐ์ ์ผ๋ก ํํธ๋นํธ(Heartbeat)๋ฅผ ๋ณด๋ด์ ์ด์ ์์์ ํ์ธ
- Eureka Server๋ ๊ฐ ์๋น์ค์ ์ธ์คํด์ค ์ ๋ณด๋ฅผ ์ ์ฅํ๊ณ , ์๋น์ค๊ฐ ์ค๋จ๋๋ฉด ์ด ์ ๋ณด๋ฅผ ์ ๊ฑฐ
- Eureka Client
- Eureka Client๋ ๋ค๋ฅธ ์๋น์ค์ ํต์ ํ๊ธฐ ์ํด Eureka Server๋ก๋ถํฐ ํ์ํ ์๋น์ค๋ฅผ ์กฐํํด.
- ์๋น์ค ํด๋ผ์ด์ธํธ๋ ์์ฒญ์ ๋ณด๋ด๊ธฐ ์ ์ Eureka Server์์ ์๋น์ค๋ฅผ ์ฐพ์ ํด๋น ์๋น์ค์ IP ์ฃผ์์ ํฌํธ๋ฅผ ๊ฐ์ ธ์ค๊ณ , ์ด๋ฅผ ์ฌ์ฉํด ํต์ ์ ํ ์ ์๋ค.
- ํด๋ผ์ด์ธํธ๋ ๋ํ Eureka Server์ ์์ ์ ๋ฑ๋กํ ์๋ ์๋ค.
- Eureka Server
- Eureka ๋์ ๋ฐฉ์
- ์๋น์ค ๋ฑ๋ก
- ์๋น์ค๊ฐ ์์๋๋ฉด Eureka Client๋ Eureka Server์ HTTP ์์ฒญ์ ํตํด ์์ ์ ์ ๋ณด(IP, ํฌํธ, ์ํ)๋ฅผ ๋ฑ๋ก
- ์ด๋ ์๋น์ค๋ ํํธ๋นํธ(Heartbeat)๋ฅผ ์ฃผ๊ธฐ์ ์ผ๋ก ๋ณด๋ด์ ์๋น์ค๊ฐ ์ฌ์ ํ ์ด์ ์์์ ํ์ธํ๋ค.
- ์๋น์ค ์กฐํ
- ์๋น์ค๊ฐ ๋ค๋ฅธ ์๋น์ค์ ์์ฒญ์ ๋ณด๋ด๊ธฐ ์ ์, Eureka Client๋ Eureka Server์์ ์์ฒญํ ์๋น์ค์ ์์น ์ ๋ณด๋ฅผ ์กฐํํ๋ค.
- Eureka Server๋ ๋ฑ๋ก๋ ์๋น์ค์ IP์ ํฌํธ ์ ๋ณด๋ฅผ ๋ฐํํ๊ณ , ํด๋ผ์ด์ธํธ๋ ํด๋น ์๋น์ค๋ก ์์ฒญ์ ๋ณด๋ผ ์ ์๋ค.
- ํํธ๋นํธ ๋ฐ ์๋น์ค ์ ๊ฑฐ
- Eureka Client๋ ์ฃผ๊ธฐ์ ์ผ๋ก ํํธ๋นํธ๋ฅผ ๋ณด๋ด ์๋น์ค๊ฐ ์ ์ ๋์ ์ค์์ Eureka Server์ ์๋ฆผ.
- Eureka Server๋ ์ผ์ ๊ธฐ๊ฐ ํํธ๋นํธ๊ฐ ์์ผ๋ฉด, ํด๋น ์๋น์ค๋ฅผ ๋ฑ๋ก ๋ชฉ๋ก์์ ์ ๊ฑฐํ๊ณ ๋ ์ด์ ์์ฒญ์ ๋ฐ์ ์ ์๊ฒ ํ๋ค.
- ์๋น์ค ๋ฑ๋ก
- Netflix Eureka์ ์ฃผ์ ๊ธฐ๋ฅ
- ์๋น์ค ๋ ์ง์คํธ๋ฆฌ ๊ด๋ฆฌ
- Eureka Server๋ ์ค์ ์ง์ค์ ์๋น์ค ๋ ์ง์คํธ๋ฆฌ ์ญํ ์ ํ๋ฉฐ, ๋ชจ๋ ์๋น์ค์ IP ์ฃผ์, ํฌํธ, ์ํ ์ ๋ณด๋ฅผ ๊ด๋ฆฌํ๋ค.
- ๋ง์ดํฌ๋ก์๋น์ค ํ๊ฒฝ์์๋ ์๋น์ค๊ฐ ๋์ ์ผ๋ก ์ถ๊ฐ๋๊ณ ์ ๊ฑฐ๋ ์ ์๊ธฐ ๋๋ฌธ์, Eureka๋ ์ด๋ฌํ ๋ณํ๋ฅผ ์ฆ์ ๋ฐ์ํ๋ค.
- ๊ณ ๊ฐ์ฉ์ฑ ์ง์
- Eureka๋ Failover ๊ธฐ๋ฅ์ ์ ๊ณตํ์ฌ, ๋ง์ฝ ํ ์๋น์ค ์ธ์คํด์ค๊ฐ ๋ค์ด๋๋๋ผ๋ ๋ค๋ฅธ ์ธ์คํด์ค๋ก ์๋์ผ๋ก ์์ฒญ์ ๋ณด๋ผ ์ ์๋ค.
- ์ด๋ฅผ ํตํด ๊ณ ๊ฐ์ฉ์ฑ(High Availability)์ ๋ณด์ฅํ๊ณ , ์๋น์ค ์ฅ์ ์์๋ ๋ค๋ฅธ ์ธ์คํด์ค๋ฅผ ํตํด ์์ฒญ์ ์ฒ๋ฆฌํ ์ ์๋ค.
- ๋ก๋ ๋ฐธ๋ฐ์ฑ
- Eureka๋ ์๋น์ค์ ์ฌ๋ฌ ์ธ์คํด์ค๋ฅผ ๊ด๋ฆฌํ๊ณ , ํด๋ผ์ด์ธํธ๊ฐ Eureka Server์์ ์ฌ๋ฌ ์ธ์คํด์ค ์ค ํ๋๋ฅผ ์ ํํด ๋ก๋ ๋ฐธ๋ฐ์ฑ์ ํ ์ ์๊ฒ ํด์ค๋ค.
- ํด๋ผ์ด์ธํธ๋ Ribbon ๊ฐ์ ๋ก๋ ๋ฐธ๋ฐ์๋ฅผ ์ฌ์ฉํด ๋ก๋๋ฅผ ๋ถ์ฐ์ํฌ ์ ์๋ค.
- Self-Preservation Mode
- Eureka๋ Self-Preservation Mode๋ผ๋ ๊ธฐ๋ฅ์ ์ ๊ณต
- ๋คํธ์ํฌ ์ฅ์ ๋ ์ฑ๋ฅ ์ด์๋ก ์ธํด ์ผ๋ถ ์๋น์ค์ ํํธ๋นํธ๊ฐ Eureka Server์ ๋๋ฌํ์ง ์๋๋ผ๋, ํด๋น ์๋น์ค๋ฅผ ๋ ์ง์คํธ๋ฆฌ์์ ๊ณง๋ฐ๋ก ์ ๊ฑฐํ์ง ์๊ณ , ์ผ์ ๊ธฐ๊ฐ ์ ์งํจ์ผ๋ก์จ ์์คํ
์ ์ ๋ขฐ์ฑ์ ๋์ธ๋ค.
- Netflix Eureka์ ์๋ ํ๋ฆ
- ์๋น์ค ๋ฑ๋ก
- ๋ง์ดํฌ๋ก์๋น์ค๊ฐ ์์๋๋ฉด Eureka Client๋ ์์ ์ IP์ ํฌํธ ์ ๋ณด๋ฅผ Eureka Server์ ๋ฑ๋ก.
- ์๋น์ค๋ ์ฃผ๊ธฐ์ ์ผ๋ก Eureka Server์ ํํธ๋นํธ๋ฅผ ๋ณด๋ด์, ์๋น์ค๊ฐ ์์ง ๋์ ์ค์์ ์๋ฆผ.
- ์๋น์ค ์กฐํ
- ์๋น์ค A๊ฐ ๋ค๋ฅธ ์๋น์ค B์ ์์ฒญ์ ๋ณด๋ด๋ ค๋ฉด, Eureka Client๋ ๋จผ์ Eureka Server์์ ์๋น์ค B์ ์์น ์ ๋ณด๋ฅผ ์กฐํ.
- Eureka Server๋ ์๋น์ค B์ IP์ ํฌํธ๋ฅผ ๋ฐํํ๊ณ , ์๋น์ค A๋ ๊ทธ ์ ๋ณด๋ฅผ ๋ฐํ์ผ๋ก ์๋น์ค B๋ก ์์ฒญ์ ๋ณด๋.
- Self-Preservation ๋ชจ๋
- ๋ง์ฝ ์๋น์ค B์ ํํธ๋นํธ๊ฐ ์ผ์ ์๊ฐ ๋์ ์์ ๋์ง ์์ผ๋ฉด, Eureka Server๋ Self-Preservation ๋ชจ๋๋ก ์ ํ
- ์ด ๋ชจ๋์์๋ ๋คํธ์ํฌ ๋ฌธ์ ๊ฐ ์ผ์์ ์ธ ์ฅ์ ์ผ ์ ์๊ธฐ ๋๋ฌธ์, ์๋น์ค B๋ฅผ ๊ณง๋ฐ๋ก ์ญ์ ํ์ง ์๊ณ ์ผ์ ์๊ฐ ๋์ ์ ํจ์ฑ์ ์ ์งํจ.
- ๋คํธ์ํฌ๊ฐ ๋ณต๊ตฌ๋๋ฉด, ์๋น์ค B๋ ํํธ๋นํธ๋ฅผ ์ฌ๊ฐํ๊ณ ๋ค์ ์ ์ ์ํ๋ก ๋ฑ๋ก๋ ์ ์๋ค.
Netflix Eureka ์ค์ต
-
https://start.spring.io/
์ฒ์์ https://start.spring.io/ ์ฌ๊ธฐ์ ์์ฑํด๋ ๋๊ณ
-
๊ธฐ์กด์ ํ๋ก์ ํธ์์ ์๋ ์ค์ ์ ์ถ๊ฐํด์ฃผ๋ฉด ๋๋ค.
๐Eureka Server
-
build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
plugins { id 'org.springframework.boot' version '2.7.0' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } javadoc.options.encoding = 'UTF-8' tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } //์ถ๊ฐํด ์ค ๋ถ๋ถ > ์คํ๋ง๋ถํธ ๋ฒ์ ์ ๋ฐ๋ผ ๋ค๋ฆ. ext { set('springCloudVersion', "2021.0.4") } configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.mapstruct:mapstruct:1.5.1.Final' annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.1.Final' // ์์กด์ฑ ์ถ๊ฐ : ๋ทํ๋ฆญ์ค ์ ๋ ์นด ์๋ฒ implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server' } tasks.named('javadoc') { source = sourceSets.main.allJava options.memberLevel = JavadocMemberLevel.PRIVATE destinationDir = file("build/docs/javadoc") } //์์กด์ฑ ๊ด๋ฆฌ ์ถ๊ฐ dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
-
์คํ๋ง ๋ถํธ์ ๋ฒ์ ์ด ๋ง์์ผ ํ๋ค. > ์ด๊ฑฐ ์ ๋ง์์ ์ฒ์์ ์๋ฌ ์์ฒญ ๋ฌ์๋คโฆใ ใ ใ
1 2 3 4
//์ถ๊ฐํด ์ค ๋ถ๋ถ > ์คํ๋ง๋ถํธ ๋ฒ์ ์ ๋ฐ๋ผ ๋ค๋ฆ. ext { set('springCloudVersion', "2021.0.4")
-
์์กด์ฑ ์ถ๊ฐ
1 2
// ์์กด์ฑ ์ถ๊ฐ : ๋ทํ๋ฆญ์ค ์ ๋ ์นด ์๋ฒ implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
-
Spring Cloud ์์กด์ฑ์ ๊ด๋ฆฌํ๊ธฐ ์ํ dependencyManagement ์น์ ์ ์ถ๊ฐํ๋ ์ฝ๋
1 2 3 4 5 6
// Spring Cloud์ ์ฌ๋ฌ ๋ชจ๋๋ค์ ํตํฉ ๊ด๋ฆฌํ ์ ์๋ค. dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
-
yaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
spring: h2: console: enabled: true path: /h2 datasource: url: jdbc:h2:mem:test jpa: hibernate: ddl-auto: create # (1) ์คํค๋ง ์๋ ์์ฑ show-sql: true # (2) SQL ์ฟผ๋ฆฌ ์ถ๋ ฅ # ์ถ๊ฐ server: port: 8761 eureka: client: register-with-eureka: false service-url: default-zone: http://${eureka.instance.hostname}:${server.port}/eureka fetch-registry: false instance: hostname: localhost
-
SpringStratApplication
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package com.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer; @SpringBootApplication @EnableEurekaServer //์ถ๊ฐ public class SpringStartApplication { public static void main(String[] args) { SpringApplication.run(SpringStartApplication.class, args); } }
-
localhost:8761 ๋ก ์ ์ํ๋ฉด ํ์ด์ง๊ฐ ๋ฌ๋ค.
๐ Eureka Client
-
build.gradle
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
plugins { id 'org.springframework.boot' version '2.7.0' id 'io.spring.dependency-management' version '1.0.11.RELEASE' id 'java' } javadoc.options.encoding = 'UTF-8' tasks.withType(JavaCompile) { options.encoding = 'UTF-8' } //์ถ๊ฐ ext { set('springCloudVersion', "2021.0.4") } configurations { compileOnly { extendsFrom annotationProcessor } } repositories { mavenCentral() } dependencies { implementation 'org.springframework.boot:spring-boot-starter-data-jpa' implementation 'org.springframework.boot:spring-boot-starter-validation' implementation 'org.springframework.boot:spring-boot-starter-web' compileOnly 'org.projectlombok:lombok' runtimeOnly 'com.h2database:h2' annotationProcessor 'org.projectlombok:lombok' testImplementation 'org.springframework.boot:spring-boot-starter-test' implementation 'org.mapstruct:mapstruct:1.5.1.Final' annotationProcessor 'org.mapstruct:mapstruct-processor:1.5.1.Final' //client ์ถ๊ฐ implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' } tasks.named('javadoc') { source = sourceSets.main.allJava options.memberLevel = JavadocMemberLevel.PRIVATE destinationDir = file("build/docs/javadoc") } dependencyManagement { imports { mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}" } }
- server์ ๋์ผํ์ง๋ง dependency๋ client ๋ก ์ถ๊ฐํด์ผํ๋ค.
-
yaml
1 2 3 4 5 6 7 8
server: port: 9001 eureka: client: register-with-eureka: true fetch-registry: true service-url: defaultZone: http://127.0.0.1:8761/eureka
-
SpringStratApplication
1 2 3 4 5 6 7 8 9 10 11 12 13 14
package com.springboot; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.cloud.client.discovery.EnableDiscoveryClient; @EnableDiscoveryClient //์ถ๊ฐ @SpringBootApplication public class SpringStartApplication { public static void main(String[] args) { SpringApplication.run(SpringStartApplication.class, args); } }
-
์ฃผ์์๋ฒ ์ํค๊ณ ํด๋ผ์ด์ธํธ ์คํํ๋ฉด ์๋์ ๊ฐ์ ์์ธ ๋ฐ์ ํ๋ค.
-
localhost:8761 ๋ก ์ ์ํ๋ฉด ์ธ์คํด์ค๊ฐ ์ถ๊ฐ ๋์ด ์๋ค (port : 9001)
์์ฒญ ๊ฐ๋จํด ๋ณด์ด์ง๋ง ์๊ฐ๋ณด๋ค ์ค๋ ๊ฑธ๋ ธ๋ค, ๊ฐ์๋ฅผ ์ฃผ๊ณ ์ฌ๋ ค๋๊น ๊ฝค ์์ด ๋ง๊ณ ์ธ์ ๋ค ๋ณผ ์ ์์์ง ๋ชจ๋ฅด๊ฒ ์ด์ ๊ทธ๋ฅ ๋ธ๋ก๊ทธ ๊ธ๋ง ์ฐธ๊ณ ํ๋๋ฐ ๋ค๋ค start.spring.io์์ ํ์ผ์ ๋ง์ด ๋ฐ์์ ์ฌ์ฉํด์ ์์ํ๋์ง build.gradle์ ์ฐธ๊ณ ํ ์ ๊ฐ์์๋คโฆ ๊ทธ๋์ ๊ธฐ์กด ํ์ผ์์ build.gradle ๋ง ์ถ๊ฐํ๋ ค๊ณ ํ๋ ์๋ฌ๊ฐ ๋ง์ด ๋ฐ์๋์๋๋ฐ ๊ทธ๋๋ ํ๋ค๋ณด๋ ํด๊ฒฐ! ๊ตฟ๊ตฟ
์ฌ๊ธฐ์ ์ด๋ป๊ฒ ๊ตฌํํ ๊ฒ์ธ์ง ๋ก์ปฌ๋ก ์ฌ๋ฌ ๋ฒ ํ ์คํธ ํด๋ณด๊ณ ์์ผ๊ฒ ๋ค.
Leave a comment