SpringBoot/SpringCloud
[springCloud]Spring Cloud Netflix Eureka 프로젝트 생성
유쾌한고등어
2023. 3. 27. 18:22
Netflix Eureka란?
- 넷플릭스에서 MSA를 위해 Spring Cloud에 기부한 오픈 소스
- MSA에서 회원, 상품, 주문 등등 각각의 서비스들에 대한 정보를 저장하여 외부에서 서비스 호출 시 그에 맞는 서비스 서버로 전달해주는 미들웨어
Sevice Discovery란?
- Client가 서비스를 호출할 때 서비스의 위치(즉, IP와 PORT 정보)를 알아야 호출할 수 있는데 이러한 정보들을 저장, 관리하는 것을 Service Discovery라고 한다.
넷플릭스 유레카(Eureka)를 사용하여 서비스 디스커버리를 구현해보기
- 스프링 부트 프로젝트 2개를 만들어서 하나는 디스커버리 서버, 다른 하나는 디스커버리 클라이언트 역할을 하도록 한다.
● 유레카 서버
- dependency 추가
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
- application.yml 설정
server:
port: 8761
spring:
application:
name: discovery-service
eureka:
client:
register-with-eureka: false # 기본설정 true(자신의 정보를 자신에게 등록함;)
fetch-registry: false
@EnableEurekaServer : 현재 스프링부트 프로젝트가 eEureka 서버역할을 하기 위해서,서버 자격으로 등록
@SpringBootApplication
@EnableEurekaServer
public class EcoomerceApplication {
public static void main(String[] args) {
SpringApplication.run(EcoomerceApplication.class, args);
}
}
● discovery service(Eureka)에 등록될 micro-service 생성
- user-service 프로젝트 생성 및 dependency추가
- eureka.client.fetch-registry=true ; EUREKA 서버로부터 인스턴스들의 정보를 주기적으로 가져올 것인지를 설정하는 속성. true로 설정하면, 갱신 된 정보를 받겠다는 설정
- service-url: 서버의 위치폴더 지정
server:
port: 9001
spring:
application:
name: user-service
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:8761/eureka
- @EnableEurekaServer 달아주기