전공공부
[Spring Cloud Netflix Eureka] 기본 개념 정리 및 사용 방법 본문
1. Spring Cloud Netflix Eureka
Netflix에서 배포한 오픈 소스입니다. MSA 아키텍쳐를 구성 할 때 Client Side에서 각 서버의 Service를 연동하고 관리하는 것은 한 서버의 서비스 IP 또는, VM IP가 바뀌었을때 마다 대응이 힘들다고 판단하여서 Server 단에서 각 서비스의 IP를 관리하고 이를 사용하는 Client 단에서 각 서비스의 연동하는 name만 가져와서 사용 하게끔 합니다.
마치 K8S 내부의 Service와 같은 개념으로 각 Server를 관리하는데요. 이를 좀 더 유저 친화적으로 사용 할 수 있게끔 구현 한 것 입니다.
2. 서버 구현
Dependancy 등록
아래와 같이 의존성을 추가합니다.
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
application.yaml 등록
server:
port: 8787
# -- Eureka
eureka:
instance:
hostname: 127.0.0.1
client:
serviceUrl:
defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/
register-with-eureka: false
fetch-registry: false
위와 같은 형태로 등록하는데요.
우선 각 명령어에 대해서 설명드리겠습니다.
eureka.client.serveUrl.defaultZone : eureka는 여러 instance를 두고 여러 서버를 둘 수 있습니다. 이 부분의 역할은 이 여러 인스턴스를 연동해서 하나의 유레카 서버가 내려가더라도 다른 유레카 서버의 정보를 사용해서 각 client들의 기본 정보등을 불러 올 수 있게합니다.
eureka.client.register-with-eureka : client 단에서 사용하는 정보로 클라이언트 단으로 등록 할 지 여부를 묻는 것입니다. 당연히 Eureka Server 이니 false 입니다.
eureka.client. fetch-registry : 서버로 부터 registry 정보를 30초 마다 가져와서 캐싱하는 것인데요 이것 또한, client 단의 필요 요소이니 false 합니다. 이는 기본 값이 true입니다.
application 단
@EnableEurekaServer
@SpringBootApplication
public class EurekaApplication {
public static void main(String[] args) {
SpringApplication.run(EurekaApplication.class, args);
}
}
EnableEurekaServer를 등록하고 끝 입니다.
3. Client 구현
Eureka 등록을 원하는 application에 등록을 합니다. 보통 서로 연동이 필요한 application이므로 대게는 Spring Cloud Gateway 서버가 될 것 입니다.
Dependancy 단
implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
application.yaml 단
spring:
application:
name: apigateway-service
eureka:
client:
register-with-eureka: true
fetch-registry: true
service-url:
defaultZone: http://127.0.0.1:8787/eureka #유레카 서버에 등록합니다.
아까 지정한 셋팅과 반대로 지정하고 service-url 부분으로 부터 유레카 서버에 지정합니다. 만일 여러 인스턴스로 이루어진 유레카 서버라면 쉼표로 여러개를 작성해서 기입합니다.
spring.application.name 부분은 나중에 유레카 서버 상에서 연동해서 쓸 이름을 의미합니다. (필요한 이유는 만일, 제가 API gateway 서버를 유레카 서버와 연동해서 사용 중이라면 lb://USER-SERVICE 이런식으로 유레카 서버에 연동된 이름을 가져와서 사용 할 수 있습니다. 즉, 관리가 훨씬 수월해집니다.)
application 단
@EnableDiscoveryClient
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
다만, 위의 @EnableDiscoveryClient는 굳이 적지 않아도 기본적으로 지원 한다고 합니다. application.yaml 까지만 설정하셔도 좋습니다.
Cloud Native Applications (spring.io)
(참고 링크 발췌) Spring Cloud will provide both the blocking and reactive service discovery clients by default. 문맥상 이전 내용이 @EnableDiscoverytClient 였고 이를 Spring Cloud에서는 기본적으로 동기 방식의 client나 비동기방식의 client 모두 지원한다는 뜻 입니다.
만일, Spring Actuator를 연동해서 알맞게 application.yaml 부분에 셋팅을 해주었다면 healthz check 등을 확인 할 수 있는 창이 뜹니다.
[참고 자료]
Spring Cloud Netflix
Spring Cloud Netflix provides Netflix OSS integrations for Spring Boot apps through autoconfiguration and binding to the Spring Environment and other Spring programming model idioms. With a few simple annotations, you can quickly enable and configure the c
spring.io
Spring Cloud Netflix Eureka Server 사용해 보기
Eureka 소개 msa를 구성하면 각 application은 서비스 상황에 따라 트래픽이나 부하가 증가할 경우 인스턴스를 추가로 생성하거나, 반대로 트래픽이나 부하가 감소할 경우 인스턴스를 줄이는 등 microse
luvstudy.tistory.com
Spring Cloud Netflix Eureka - 개념 및 프로젝트 생성
해당 내용은 인프런 이도원님의 "Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA)"를 듣고 정리한 내용입니다. 1. Spring Cloud Netflix Eureka란? Netflix Eureka란? 넷플릭스에서 MSA를 위해 Spring Cloud
yarisong.tistory.com
Cloud Native Applications
Patterns such as service discovery, load balancing, and circuit breakers lend themselves to a common abstraction layer that can be consumed by all Spring Cloud clients, independent of the implementation (for example, discovery with Eureka or Consul). Sprin
cloud.spring.io
[SC04] Spring Cloud Eureka 란 ?
Eureka server 이해 WHY ? 한 마이크로서비스가 다른 마이크로서비스를 호출하려면, 대상 마이크로서비스의 IP 또는 FQDN(FQDN-Fully Qualified Domain Name)과 포트를 알아야 합니다. 대상 마이크로서비스는 증
happycloud-lee.tistory.com
'Study > Spring Boot' 카테고리의 다른 글
Spring Security with Async (0) | 2024.02.27 |
---|---|
[Spring Cloud Gateway] API Gateway를 활용한 실습 (0) | 2024.02.20 |
[Redis] Reactive Redis 연결 및 상세 설명 (0) | 2024.02.05 |
[Project 00] Webflux + DDD (1) | 2024.02.03 |
[Webflux] Mono의 사용에 따라서 달라지는 리턴 값 & Binary Input Data의 처리 (2) | 2024.01.30 |