Architecture/MSA

[JAVA/Spring/MSA] 1. 프로젝트 MSA 적용 여정: 유레카 클라이언트

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

유레카 클라이언트

유레카 서버가 관리할 클라이언트를 등록하는 과정을 소개합니다.

특정 애플리케이션에 유레카 클라이언트를 적용하기 전에 해당 애플리케이션에 Spring Boot Actuator*를 추가해야합니다.

 

* Spring Boot Actuator는 endpoint를 통해 애플리케이션 내부의 정보를 동적으로 생성해준다. 또한 설정을 실행 중에 동적으로 변경할 수 있는 기능을 제공해준다. 이 포스팅에서는 health check 측면에서의 역할만 다루고 있습니다.

 

Spring Boot Actuator 적용

의존성 추가

Eureka Client를 적용할 애플리케이션의 build.gradle에 아래 항목을 추가한다.

implementation 'org.springframework.boot:spring-boot-starter-actuator'

 

Actuator 관련 application.yml 설정 추가

management:
  endpoints:
    web:
      exposure:
        include: '*'
  health:
    readinessstate:
      enabled: true
    livenessstate:
      enabled: true
  endpoint:
    health:
      probes:
        enabled: true

endpoints와 enpoint를 잘 구분하자!

  • endpoints.web.exposure.include: '*'
    • actuator에서 제공하는 endpoint들을 모두 여는 설정
    • 개발 이후 단계에서는 필요한 설정만 white list로 관리 해야 한다.
  • helth.livenessstate.enabled: true 
    • 컨테이너나 애플리케이션이 살아있는지 확인한다.
    • 살아있으면 아무것도 하지 않는다.
    • 죽어있으면 해당 컨테이너, 애플리케이션을 재실행하여 회복을 시도한다.
  • helth.readinessstate.enabled: true
    • 네트워크 트래픽을 받을 수 있는 상태인지 확인할 수 있다.
    • 컨테이너가 시작되었더라도 네트워크 요청을 처리할 수 없는 상태일 수 있기 때문에 필요하다.
  • endpoint.health.probes.enabled: true
    • Liveness와 readness를 측정하기 위해 actuator/health endpoint 를 제공받기 위한 항목이다.
      • actuator/health/liveness
      • actuator/health/readiness

 

Eureka Client 적용

의존성 추가

Eureka Client를 적용할 애플리케이션의 build.gradle에 아래 항목을 추가한다.

implementation 'org.springframework.cloud:spring-cloud-starter-netflix-eureka-client'

 

Eureka Client 관련 application.yml 설정 추가

eureka:
  instance:
    prefer-ip-address: true
  client:
    fetch-registry: true
    register-with-eureka: true
    serviceUrl:
      defaultZone: http://localhost:8070/eureka/

info:
  app:
    name: ${spring.application.name}
    description: "PINTING Board Application"
    version: "1.0.0"

management:
  endpoints:
    web:
      exposure:
        include: '*'
  health:
    readinessstate:
      enabled: true
    livenessstate:
      enabled: true
  endpoint:
    health:
      probes:
        enabled: true
  info:
    env:
      enabled: true
  • eureka.instance.prefer-ip-address
    • hostname 대신 해당 마이크로서비스가 IP주소를 이용하여 유레카 서버에 등록되도록 IP주소를 선호를 true로 설정한다.
  • eureka.client.fetch-registry: true, eureka.client.register-with-eureka: true
    • 클라이언트 등록을 위한 설정
  • eureka.client.serviceUrl.defaultZone: http://localhost:8070/eureka/
    • eureka client가 eureka 서버와 연결하기 위한 url을 지정해준다.
    • docker container를 띄운 뒤에는 localhost 설정을 service 이름으로 변경해주어야 한다.
  • info
    • eureka dashboard에서 서비스의 상세 내용을 조회할 수 있도록 설정해준다.
  • management.info.enabled: true
    • info 에서 설정한 정보들을 환경변수로 참조하여 actuator에서 사용할 수 있도록 추가한다.

 

*actuator의 shutdown 관련 내용은 실무에 적용하지 않을 것이기 때문에 넘어가겠습니다. Actuator의 Endpoint에 대한 추가적인 설명은 아래의 공식문서를 참조하면 좋습니다.

 

53. Endpoints

Actuator endpoints let you monitor and interact with your application. Spring Boot includes a number of built-in endpoints and lets you add your own. For example, the health endpoint provides basic application health information. Each individual endpoint c

docs.spring.io

 

Application.java 변경

해당 애플리케이션을 유레카 서버에 클라이언트로 등록하고, 검색할 수 있도록 @EnableDiscoveryClient를 추가해준다.

import org.springframework.cloud.client.discovery.EnableDiscoveryClient;

@EnableDiscoveryClient

Eureka Client 적용 확인

* 몇 개월 전 작성했던 애플리케이션의 컴파일이 되지 않는 오류가 발생했습니다... 궁금하신 분은 아래의 포스팅을 참고해주세요..!

 

[Error] Could not find org.springframework.cloud:spring-cloud-starter-netflix-eureka-client

Eureka Client를 spring boot로 추가하면서 발생한 에러2024.10.25 - [Programming/Project] - [JAVA/Spring/MSA] 1. 프로젝트 MSA 적용 여정: 유레카 클라이언트 [JAVA/Spring/MSA] 1. 프로젝트 MSA 적용 여정: 유레카 클라이언

bitbit-merry-go-round.tistory.com

 

 

Eureka Server가 먼저 실행되고 있어야 한다.

애플리케이션을 build 하고 실행하면 아래와 같은 로그를 확인할 수 있다.

DiscoveryClient_BOARD/localhost:board:8082로 해당 애플리케이션이 성공적으로 등록되면 204 코드를 볼 수 있다.

이전에 지정해준 Eureka Server의 포트로 접속해서 잘 등록이 되어있는지 확인해보자.

 

http://loaclhost:8070으로 접속하면 아래와 같은 화면을 볼 수 있다.

* EMERGENCY! EUREKA MAY...

더보기

이 경고는 유레카 서버의 self-preservation(자체 보존) 모드때문에 발생한다.

간단히 말하자면 인스턴스가 1개라서 서비스의 응답이 없더라도 정상 동작 중이라고 표시할 것이라는 경고이다.

Actuator의 endpoint 확인

Dash board에 등록된 인스턴스가 링크로 제공되고 있다. 클릭하면 해당 애플리케이션의 정보를 볼 수 있는 info로 연결된다.

 

http://localhost:8082/actuator/info

application.yml의 management 설정에 따라 info에 명시한 정보를 json 데이터로 받아올 수 있다(feat. actuator).

 

http://localhost:8082/actuator/health

Liveness와 readiness를 제공하고 있음을 볼 수 있다.

 

각각을 항목별로 조회할 수도 있다.

http://localhost:8082/actuator/health/liveness

http://localhost:8082/actuator/readiness

 

Eureka Client 측이 아닌 Eureka Server 측에서 등록된 application 들을 조회할 수도 있다.

http://localhost:8070/eureka/apps

http://localhost:8070/eureka/apps/{APP_NAME}

앱 이름으로 각각의 서비스를 조회할 수도 있다.

 

Actuator의 활용성은 더욱 방대하지만 Eureka 서버와 클라이언트 사이에 Actuator가 정보를 제공해주고 있는 연결자로서의 역할을 다루어 보았습니다. 필요한 내용이 생길 때마다 하나씩 추가하도록 하겠습니다. :)

 

이후에는 여러 인스턴스 간의 로드 밸런싱, 인스턴스 사이의 통신을 Eureka Server를 통해 과정을 포스팅 해볼게요.

 

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