MSA

Eureka - Service Discovery

Beekei 2021. 9. 10. 17:46
반응형

Eureka란?

Middle-tier server(비지니스 로직이 위치한 애플리케이션 서버단)의 로드밸런스와 Failover를 위해 서비스를 배치해주는 REST 기반 서비스

주로 AWS Cloud에서 사용된다.

Eureka Client

Java 기반 클라이언트 요소도 있는데, 서버와 상호작용을 더 쉽게 해주는 것을 Eureka Client라 부른다.

Ribbon - Client LoadBalancer 방식을 기본으로 사용하는 로드밸런서를 내장하고 있다.

Netflix에서는 Eureka에서 기본 로드 밸런스 외에도 트래픽, 리소스 사용량, 에러 상황 요소에 따라 로드밸런스를 할 수 있도록 제공하고 있다.

Eureka Server의 필요성

AWS Cloud에서는 특성상 서버가 자주 꺼졌다 켜졌다 한다.

IP 주소와 hostname을 이용하는 기존 로드밸런스와 다르게, AWS에서는 로드밸런스는 좀 더 정교한 능력을 요구한다.

IP 주소는 수시로 변하기 때문에 로드밸런스가 서버를 등록하고 해지하는 작업을 유동적으로 할 수 있게 해야한다.

AWS에서는 아직 middle tier 로드밸런스를 제공하지 않기 때문에, Eureka에서는 이를 제공해준다.

DNS 기반 로드밸런싱을 사용하면 서버의 상태가 healthy든 unhealthy든 상관없이 보내기 때문에 문제가 생긴다.

Eureka in Spring Cloud

  • 서버 시작 시 Eureka Server(Registry)에 자동으로 자신의 상태를 등록(UP)
    eureka.client.register-with-eureka: true(default)
  • 주기적으로 Eureka Client의 상태를 HeartBeat로 Eureka Server에 자신이 살아 있음을 알림(로컬에 동기화)
    eureka.instance.lease-renewal-interval-in-seconds: 30(default)
  • 서버 종료 시 Eureka Server에 자신의 상태를 변경(DOWN) 혹은 자신의 목록 삭제
  • Eureka상에 등록된 이름은 'spring.application.name'

사용법

1. Eureka Dependency 추가

EurekaServer build.gradle

dependencies {
	...
	compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-server')
}

2. Main Class에 @EnableEurekaServer 어노테이션 추가

EurekaServer main class

@EnableEurekaServer
@SpringBootApplication
public class EurekaServerApplication {

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

}

3. 서버 실행 후 http://localhost:8761 확인

4. Eureka-Client 라이브러리 추가

ServiceA, ServiceB build.gradle

dependencies {
	...
	compile('org.springframework.cloud:spring-cloud-starter-netflix-eureka-client')
}

5. 각 서비스에 @EnableEurekaClient 어노테이션 추가

ServiceA, ServiceB main class

@EnableEurekaClient
@EnableCircuitBreaker // Hystrix
@SpringBootApplication
public class ServiceAApplication {

    @Bean
    @LoadBalanced
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }

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

}

6. application.yml 설정

  • ribbon.listOfServers를 사용하고 있다면 주석처리(Ribbon - Client LoadBalancer)
    서버 주소는 Eureka Server에서 가져옴
  • eureka.instance.prefer-ip-address: true(default)
    OS에서 제공하는 hostname 대신 자신의 ip address를 사용하여 등록

ServiceA, ServiceB application.yml

serviceB:
	ribbon:
		# listOfServers: localhost:8082, localhost:8083 # Load Balancing될 서버 목록
		# 기타 retry 기능 추가
		MaxAutoRetries: 0 # 실패시 재시도 하는 최대 횟수
		MaxAutoRetriesNextServer: 1 # 실패시 다른 서버로 재시도 하는 횟수

eureka:
  instance:
    prefer-ip-address: true # ip address를 사용
  • @EnableEurekaClient를 붙인 Application은 Eureka 서버로 부터 남의 주소를 가져오는 역활과 자신의 주소를 등록하는 역활을 둘다 수행 가능하다.
  • Eureka Client가 Eureka Server에 자신을 등록할 때 'spring.application.name'을 이름으로 사용

7. Eureka Server 주소 직접 명시(로컬에서는 불필요)

ServiceA, ServiceB application.yml

eureka:
  instance:
    prefer-ip-address: true # ip address를 사용
	client:
		server-url:
			defaultZone: http://127.0.0.1:8761/eureka # eureka server 설정
  • 로컬 테스트에서는 Eureka Server가 같은 서버(localhost)에 있기 때문에 설정해주지 않지만 실제 운용서버는 다른 서버에 Eureka Server가 존재하기 때문에 설정해주어야 한다.

정리

Eureka - Service Discovery에 활성화된 서버를 동기화 후 Ribbon - Client LoadBalancer을 통해 로드밸런싱


 

 

spring cloud - eureka 기본 설정!

https://github.com/Kouzie/sample-spring-cloud

kouzie.github.io

반응형