Architecture/MSA

[JAVA/Spring/MSA] 0. 프로젝트 MSA 적용 여정: 유레카 서버

coco_daddy 2024. 10. 7. 19:00
※ Udemy 강의 中 【Master Microservices with SpringBoot,Docker,Kubernetes】의 내용을 기반으로 실제 프로젝트에 적용하는 과정을 기록하는 포스팅입니다.

Eureka Server란?

  • Netflix 팀에서 제공하는 Service Discovery Agent.
  • 그렇다면, Service Discovery란 무엇인가?

Service Discovery란?

동작하고 있는 서비스들의 인스턴스 정보를 추적하고 *Service Registry에 저장한다.

* Service Registry

더보기

* 서비스 레지스트리: **scale up/down 되었을 때 인스턴스의 정보가 해당 레지스트리에서 동적으로 추가/삭제 된다.

** scale up/down: 임의의 서비스의 인스턴스가 늘어나거나 줄어드는 것을 의미한다. scale out/in 이라는 개념과 차이가 있는데, scale out은 서비스에 대한 성능을 높이기 위해 시스템의 자원(CPU, memory 등)을 더 많이 할당하는 것이며, scale up은 서비스가 병렬적으로 늘어나는 것을 의미한다.

Client-side service discovery

  • 애플리케이션들은 자신의 정보를 서비스 레지스트리에 자신을 등록해야하는 책임이 있다.
    • 삭제 될 때는 제거해야하는 책임이 있다.
    • 주기적으로 heartbeat를 보내 자신이 healthy함(정상적으로 연결을 받을 수 있는 상태)을 알린다.

 

  • upstream 서비스(discovery)가 downstream 서비스(실제 서비스)에게 요청을 보내는 과정
    1. upstream 서비스는 downstream 서비스의 정보를 서비스 레지스트리에 질의(쿼리)한다.
    2. 서비스 레지스트리는 현재 등록된 서비스의 인스턴스 정보들을 반환한다.
    3. upstream 서비스는 로드밸런싱 전략에 따라 해당 서비스에 요청을 보낸다.
    4. 실제 서비스를 호출하는 책임이 클라이언트에게 있으므로 이 전략을 client-side service discovery라고 부른다.

* upstream service, downstream service

더보기

흐름: upstream service -> downstream service

  • upstream service: 의존성을 가지는 서비스
  • downstream service: 의존성을 주는 서비스, upstream의 데이터를 소비한다.

Client-side service discovery는 로드밸런싱 전략을 선택할 수 있다는 장점이 있다.

하지만 개발자에게 전략 선택의 자유가 주어진다는 것은 구현의 책임이 따라온다는 것이다.

 

Server-side service discovery도 있으나 이는 쿠버네티스 환경 안에서 마이크로서비스를 배포할 때마다 로드밸런서를 구축해야 한다는 단점이 있다. 이에 따라오는 단점은 아래와 같다.

  • 쿠버네티스 안에 로드밸런서를 구축해야 하기 때문에 쿠버네티스 전담 팀이 필요하다.
  • 비용이 많이 든다.

이러한 비용을 감당할 수 없다면 Client-side를 선택해야 한다.

+ Service Discovery의 구현체로 Eureka Server를 선택한다면 로드밸런싱 전략을 직접 구현하지 않을 수 있다.

서비스 디스커버리 레이어가 각 서비스들을 연결하는 과정

Eureka Server 프로젝트 적용

공식 레퍼런스: https://docs.spring.io/spring-cloud-netflix/reference/spring-cloud-netflix.html#spring-cloud-eureka-server

 

Spring Cloud Netflix Features :: Spring Cloud Netflix

Service Discovery is one of the key tenets of a microservice-based architecture. Trying to hand-configure each client or some form of convention can be difficult to do and can be brittle. Eureka is the Netflix Service Discovery Server and Client. The serve

docs.spring.io

build.gradle 의존성 추가

dependencies {
    implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    testRuntimeOnly 'org.junit.platform:junit-platform-launcher'
}

application.yml 설정 추가

eureka 서버 설정

server:
  port: 8070

eureka:
  instance:
    hostname: localhost
  client:
    fetchRegistry: false
    registerWithEureka: false
    serviceUrl:
      defaultZone: http://${eureka.instance.hostname}:${server.port}/eureka/

 

eureka.client.registerWithEureka: false

: eureka server 자신을 client로 등록할 필요가 없으므로 registerWithEureka를 비활성화 한다.

 

eureka.client.fetchRegistry: false

eureka server가 Microservice를 호출하지 않으므로 fetchRegistry를 비활성화 한다.

 

Eureka Server 실행하기

@SpringBootApplication
@EnableEurekaServer
public class EurekaserverApplication {

    public static void main(String[] args) {
        SpringApplication.run(EurekaserverApplication.class, args);
    }

}

 

애플리케이션을 실행시키면 설정한 포트(8070)로 접근하여 다음과 같은 페이지를 확인할 수 있다.

 

※ 질문, 개선점, 오류가 있다면 댓글로 남겨주세요 :)