Backend/MSA
Service Discovery
chjysm
2024. 1. 9. 17:42
Service Discovery 란?
- 외부에서 MSA 서비스에 접근하기 위한 정보를 제공 ex) 전화부 책
- 키 밸류 형태로 등록
- 등록, 검색
여기서의 기술 스택은
Spring Cloud Netfilx Eureka 를 사용하겠다.
Spring Cloud Netfilx Eureka 사용 방법
Discovery 서버 생성
1. maven dependency 추가
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-server</artifactId>
</dependency>
2. main application 에 어노테이션 추가
@SpringBootApplication
@EnableEurekaServer
public class EcommerceDiscoveryServiceApplication {
public static void main(String[] args) {
SpringApplication.run(EcommerceDiscoveryServiceApplication.class, args);
}
}
3. 프로퍼티 추가
server:
port: 8761
spring:
application:
name: discoveryservice
eureka:
client: # 자신이 클라이언트가 아니라는 설정
register-with-eureka: false
fetch-registry: false
클라이언트 생성
1. maven dependency 추가
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>
2. main application 에 어노테이션 추가
@SpringBootApplication
@EnableDiscoveryClient
public class UserServiceApplication {
public static void main(String[] args) {
SpringApplication.run(UserServiceApplication.class, args);
}
}
3. 프로퍼티 추가
server:
port: 0 # 랜덤포트를 사용하겠다.
spring:
application:
name: user-service
eureka:
instance:
# 인스턴스 ID 값을 설정해야 랜덤포트 설정을 해도 다른 클라이언트로 인식한다.
# 인스턴스 ID 값을 설정 안하면 포트번호 0번인 클라이언트가 1개 존재한다고 인식한다.
instance-id: ${spring.cloud.client.hostname}:${spring.application.instance_id:${random.value}}
client:
register-with-eureka: true # eureka 서버에 등록하곘다.
fetch-registry: true # eureka 서버로 부터 정보를 주기적으로 가져오겠다.
service-url:
defaultZone: http://127.0.0.1:8761/eureka # eureka 서버의 주소