Eureka-Server / Eureka-Client

XingXi·2024년 2월 16일
0

SpringBoot

목록 보기
4/4

참고한 블로그!!.

1. Eureka Server

1. Spec

  • java version : 17
  • springboot : 3.2.2

2. dependency

  • netflix-eureka-server
  • lombok
dependencies {
	implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-server'
	compileOnly 'org.projectlombok:lombok'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

3. Discription

1. Application.java 에 Annotation 추가

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.netflix.eureka.server.EnableEurekaServer;

@EnableEurekaServer
@SpringBootApplication
public class ServerApplication {

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

}

@EnableEurekaServer 이거 추가함
해당 Application 이 Eureka Server 역할임을 선언

2. application.yml ( application.properties 파일을 변경 )

# 해당 Application Port 를 8761 로 설정 
server:
  port: 8761

# 해당 application 이름을 다음과 같이 변경 
spring:
  application:
    name: playground-eureka-server

eureka:
  client:
    register-with-eureka: false
    fetch-registry: false
  server:
    enable-self-preservation: false 
    response-cache-update-interval-ms: 5000
    eviction-interval-timer-in-ms: 10000  

    
# register-with-eureka -> Eureka 서버에 자신을 등록할지, 즉 서비스 레지스트리에 등록할 지 정하는 옵션 
#                         클라이언트 옵션인데 굳이 Eureka 서버 설정 파일에서 찾아서 조지는 이유는 
#                         default 값이 True 라서 



# fetch-registry -> Eureka 서버에 등록 된 서비스 목록을 가져올지에 대한 여부 설정  
#                         클라이언트 옵션인데 굳이 Eureka 서버 설정 파일에서 찾아서 조지는 이유는 
#                         default 값이 True 라서 
#                         활성화 하면 30 초 마다 Eureka Client 가 Eureka 레지스트리 변경 사항 여부를 재확인한다
#                         검색할 때마다 Eureka Server 를 호출하는 대신 레지스트리가 로컬로 캐싱됨


# enable-self-preservation -> 일시적 네트워크 장애로 서비스 해제를 막기 위함 
#                             활성화 되면 HEART BEAT 가 오지 않는 경우에도 해당 인스턴스를 정상으로 간주하고 유지한다 
#                             기본적으로 활성화 되어 있어서 , 해당 서비스가 일시적으로 멈췄을 때 인스턴스를 유지하는데 도움이 된다
#                             그래서 운영환경에서 true 로 설정되어 있는 것을 권장한다 --> 그래서 내가 client 닫으면 계속 남아 있었구나!                        
#                             그렇다고 얘를 false 로 설정한다고해서 문제가 있는 서비스가 바로 해지되지는 않는다 
#                             client 의 lease-renewal-interval-in-seconds 값을 기본 속성으로 두었을 때, 30 초 간격으로 client 가 server 로 하트비트를 전송 
#                             lease-expiration-duration-in-seconds 설정 때문에 하트비트를 받지 못하면 기본 설정 시 90 초 이후에 서비스 등록이 해지되기 때문이다. 


# response-cache-update-interval-ms -> Eureka 서버의 캐싱 업데이트 주기를 설정 

# eviction-interval-timer-in-ms -> 클라이언트로 부터 하트비트가 수신 점검 주기를 설정하기 위한 옵션 

갑자기 생겨난 의문에 대한 답 ( 의문 : 왜 Eureka Server 는 8761 을 사용할까 )

eviction-interval-timer-in-ms 설정을 10 초마다 한 것의 결과

3. 결과

2. Eureka Client

1. Spec

  • java version : 17
  • springboot : 3.2.2

2. dependency

  • netflix-eureka-client
  • lombok
  • spring-web ( 서비스니까 ~)
  • devtools
dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'
	compileOnly 'org.projectlombok:lombok'
	developmentOnly 'org.springframework.boot:spring-boot-devtools'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
}

3. Discription

1. Application.java 에 @EnableDiscoveryClient 추가

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;


@EnableDiscoveryClient
@SpringBootApplication
public class ClientApplication {

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

}

해당 application 이 Eureka Service 임을 선언
eureka, consul, zookeeper 의 implements 를 모두 포함한다.

2. application.yml ( application.properties 파일을 변경 )

server:
  port: 0 # 동적으로 어플리케이션을 생성하기 위해서 0 번 포트 사용 

spring:
  application:
    name: playground-eureka-client


eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    registry-fetch-interval-seconds: 5000
    disable-delta: true
    service-url:
      defaultZone: http://localhost:8761/eureka # Eureka Server URI 정보를 설정 
  instance:
    #lease-expiration-duration-in-seconds
    #lease-renewal-interval-in-seconds
    instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}} # instance-id 를 설정    



# register-with-eureka -> Eureka 서버에 자신을 등록할지, 즉 서비스 레지스트리에 등록할 지 정하는 옵션 



# fetch-registry -> Eureka 서버에 등록 된 서비스 목록을 가져올지에 대한 여부 설정  
#                         활성화 하면 30 초 마다 Eureka Client 가 Eureka 레지스트리 변경 사항 여부를 재확인한다
#                         검색할 때마다 Eureka Server 를 호출하는 대신 레지스트리가 로컬로 캐싱됨


# registry-fetch-interval-seconds -> 서비스 레지스트리 목록을 캐싱하는 주기를 설정하는 옵션

# disable-delta -> 서비스 레지스트리 목록을 캐싱할 때 변경된 부분만 업데이트 할 것인지 여부 설정 
#                   default 가 false 임으로 true 로 설정하여 대역폭 낭비를 방지 


# lease-renewal-interval-in-seconds -> Eureka 서버로 HeartBeat 보내는 시간 주기 설정  ( 기본 값 30초 )
#                                     Eureka Server 의  enable-self-preservation 를 비 활성화 상태에서 해서 문제가 있는 서비스들을 서비스 레지스트리 목록에서 제거하는 시간 조절 가능 
#                                     그러나 내부적 로직에 영향을 미칠 수 있기 때문에 가급적이면 건드리는 것을 권장

# lease-expiration-duration-in-seconds -> 마지막 hearbeat 로 부터 서비스 해제 되기 전까지 걸리는 시간을 설정
#                                         내부 로직에 영향을 미칠 수 있기 때문에 가급적이면 건드리지 않는 것을 권장 
#                                         당연한 얘기겠지만 lease-renewal-interval-in-seconds 이거 보다 값이 커야한다.  만 lease-renewal-interval-in-seconds 이거 보다 값이 커야한다.  

3. Eureka 서버가 실행 되고 있을 때 client 실행 시키기

서비스가 레지스트리에 등록 되는 것을 확인할 수 있다.

Eureka 서버 로그이다. Clinet 기동 시 서버에서 해당 서비스가 레지스트리에 들어간 것을 확인할 수 있다.

4. application 을 두개 실행 시켰을 때

서버에서 레지스트리에 등록되는 것을 볼 수 있으며
Eureka 에서 서비스 2개가 등록 된 것을 확인할 수 있다.

5. 서비스 하나 종료하기

바로 종료해도 아직 남아 있는 것을 확인할 수 있다.
서버에서 캐싱 정보를 5초마다 업데이트하고 서비스의 하트비트 점검을 10초 마다 한다
서비스가 종료된 것을 감지하더라도 서비스가 서버에게 하트비트를 보내주는 시간 ( 30 초 ) 만큼 기다리고
그 이후 90 초가 지나야 서비스를 완전히 레지스트리에서 제거한다.

0개의 댓글