*Spring Boot 3.2.1, Spring Cloud 2023.0.0 ๋ฒ์ ์์ ํ ์คํธํ์์ต๋๋ค.
1. Eureka Server ์ค์
Micro Service๋ค์ ์ด์ฉํ๊ฒ ๋ Eureka Server์ ๋ํ ์ค์ ์ ์์๋ณด๋๋ก ํ๊ฒ ๋ค.
๋จผ์ , netflix eureka ์์กด์ฑ์ ์ฃผ์ ํด ์ฃผ์ด์ผ ํ๋ค.
...
ext {
set('springCloudVersion', "2023.0.0")
}
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 ๋ฒ์ ์ ์ ํํ๋ฉด ๋๋ค.
๋ฒ์ ์ ๋ค์ ๋งํฌ์์ ํ์ธํ ์ ์๋ค.
https://spring.io/projects/spring-cloud/
๋ค์์ผ๋ก๋, Eureka Server๋ก ํด๋น ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ฑ๋กํด์ผ ํ๋ค.
@SpringBootApplication
@EnableEurekaServer // Eureka Server ๋ฑ๋ก
public class SpringMsaApplication {
public static void main(String[] args) {
SpringApplication.run(SpringMsaApplication.class, args);
}
}
@EnableEurekaServer ์ด๋ ธํ ์ด์ ์ ํตํด ๋ฑ๋กํ ์ ์๋ค.
๋ค์์ผ๋ก๋, ์ค์ ํ์ผ์ ์์ ํด์ผ ํ๋ค.
- application.yml
spring:
application:
name: spring-msa # ๋ง์ดํฌ๋ก ์๋น์ค๊ฐ unique ID๋ก ์ฌ์ฉ๋จ
eureka:
client:
register-with-eureka: false # Eureka Server์ ์๊ธฐ ์์ ์ ๋ฑ๋กํ์ง ์์
fetch-registry: false # Eureka Server๋ก๋ถํฐ ์ธ์คํด์ค์ ์ ๋ณด๋ค์ ๊ฐ์ ธ์ด
- application์ name์ ์ค์ ํด์ค์ผ๋ก์จ, ๋ง์ดํฌ๋ก์๋น์ค๋ค ๊ฐ์ ์๋ณ์๋ก ์ฌ์ฉํ๊ฒ ๋๋ค.
- register-with-eureka : Eureka Server์ ๋ฑ๋กํ๋๋ ์ค์ ์ด๋ค. ์ด ์ ํ๋ฆฌ์ผ์ด์ ์ Eureka Server ์์ ์ด๋ฏ๋ก ์๊ธฐ ์์ ์ ๋ฑ๋กํ์ง ์๋๋ค.
- fetch-registry : Eureka Server๋ก๋ถํฐ ์ธ์คํด์ค์ ์ ๋ณด๋ค์ ์ฃผ๊ธฐ์ ์ผ๋ก ๊ฐ์ ธ์ค๋๋ ์ค์ ์ด๋ค. ๋ํ ์๊ธฐ ์์ ์ด๊ธฐ ๋๋ฌธ์ false๋ก ์ค์ ํ๋ค.
2. micro service ์ค์
Eureka Server์ ๋ฑ๋กํ ์๋น์ค๋ค์ ์ค์ ํด๋ณด์.
์์กด์ฑ ์ฃผ์ ์ ๋ค์๊ณผ ๊ฐ๋ค.
ext {
set('springCloudVersion', "2023.0.0")
}
dependencies {
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client' // Eureka Client
}
dependencyManagement {
imports {
mavenBom "org.springframework.cloud:spring-cloud-dependencies:${springCloudVersion}"
}
}
Eureka Server์ ๋ฌ๋ฆฌ Eureka client๋ฅผ ์ฌ์ฉํ๋ฉด ๋๋ค.
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
๋ํ, @EnableDiscoveryClient ์ด๋ ธํ ์ด์ ์ ํตํด Client๋ฅผ ํ์ฑํ์์ผ์ค๋ค.
- application.yml
spring:
application:
name: user-service # micro service unique ID
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone : http://127.0.0.1:8081/eureka #Eureka Server's address
ํด๋น ์๋น์ค์ ์ด๋ฆ์ user-serivce๋ก ๋ฑ๋กํ๋ค.
register-with-eureka์ fetch-registry๋ true๊ฐ ๊ธฐ๋ณธ๊ฐ์ด์ด์ ๊ตณ์ด ๋ฃ์ง ์์๋ ๋๋ค.
service-url์ defaultZone์๋ ๋ฐฉ๊ธ ์ค์ ํ๋ Eureka์๋ฒ์ ์ฃผ์๋ฅผ ๋ฃ์ด์ฃผ๋ฉด ๋๋ค.
3. ํ ์คํธ
์ด๋ ๊ฒ ์ค์ ํ๊ณ , ๋ ์ ํ๋ฆฌ์ผ์ด์ ์ ๋ชจ๋ ์คํํด ๋ณด์.
์คํ ํ, Eureka ์๋ฒ์ IP๋ก ๋ค์ด๊ฐ๋ณด์.
Eureka ์๋ฒ๋ 8081ํฌํธ, user-service ํฌํธ๋ 8082๋ก ์ค์ ํด ๋์๋ค.
Eureka ์๋ฒ์์ USER-SERVICE ์ธ์คํด์ค๊ฐ ๋ฑ๋ก๋์ด ์๋ค๋ ๊ฒ์ ๋ณผ ์ ์๋ค.
'BackEnd > Spring Cloud' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring Cloud] Spring Cloud Config Server ์ค์ ๊ฐ์ Private Repository ์์ ๊ฐ์ ธ์ค๊ธฐ (0) | 2024.02.12 |
---|---|
[Spring Cloud] Spring Cloud Bus (0) | 2024.02.05 |
[Spring Cloud] Actuator๋ฅผ ์ด์ฉํ Config Server ๊ธฐ๋ (0) | 2024.01.29 |
[Spring Cloud] API Gateway, Filter (1) | 2024.01.08 |
[Spring Cloud] Srping Cloud Services (0) | 2024.01.07 |