Backend/Spring Cloud

Spring Cloud Gateway - Load Balancer

야뤼송 2022. 6. 21. 00:23
반응형

1. Spring Cloud Gateway Load Balancer 구성과 동작 순서

 

Eureka Server와 API Gateway Server 간 연동을 통해 Service간에 Load Balancer 기능을 구현한다.

 

Client에서 특정 Micro Service 요청시에 API Gateway Server와 Eureka Server, 그리고 Mircro Service 동작 순서는 다음과 같다.
① Client에서 Service A, welcome을 API Servier 요청
② API Server에서는 Eureka Server에 등록된 Service A에 대한 정보를 요청
    Eureka Server는 요청온 microservice를 위치를 검색 후 API Gateway server로 전달
③ API Gateway Server는 Eureka Server로 부터 응답받은 Service A의 정보를 통해 Service A로 포워딩 후 응답

 

2. Spring Cloud Gateway Load Balancer 구현 - Eureka Server 연동

 

Eureka 서버와의 연동을 위해 Eureka Client Library 추가해줘야 한다.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-cloud-starter-netflix-eureka-client</artifactId>
</dependency>

 

 

Dependency 추가 후 Eureka Server 정보 등록해줘야한다. eureka server 정보 등록을 위해 설정 정보 파일인, application.yml 파일에 Eureka Server 정보를 등록한다.

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka

 

 

eureka server와의 연동 관련하여 기본적으로 사용되는 속성 값은 아래와 같다. 자세한 eureka server 설정 관련 내용은 Spring Cloud Eureka Property에서 확인 가능하다.

Name Default Description
eureka.client.register-with-eureka  true 이 인스턴스가 다른 사용자가 검색 할 수 있도록 해당 정보를 eureka 서버에 등록해야하는지 여부를 나타냅니다.

경우에 따라 인스턴스가 검색되는 것을 원하지 않는 반면 다른 인스턴스를 검색하려는 경우도 있습니다.
eureka.client.fetch-registry true 이 클라이언트가 유레카 서버에서 유레카 레지스트리 정보를 가져와야하는지 여부를 나타냅니다.
eureka.client.service-url   유레카 서버와 통신하기위한 정규화 된 URL 목록에 대한 가용성 영역 맵 각 값은 단일 URL이거나 쉼표로 구분 된 대체 위치 목록 일 수 있습니다.

일반적으로 유레카 서버 URL은 프로토콜, 호스트, 포트, 컨텍스트 및 버전 정보를 전달합니다. 예 : http://ec2-256-156-243-129.compute-1.amazonaws.com:7001/eureka/

변경 사항은 eurekaServiceUrlPollIntervalSeconds에 지정된대로 다음 서비스 URL 새로 고침주기에 런타임에 적용됩니다.

 

application.yml 파일에 Micro Service 정보를 등록한다.  cloud.gateway.routes.uri는 기존과 다르게 lb://Eureka Server에 등록된 Micro Service Name으로 설정한다. cloud.gateway.routes.predicates에는 micro service path정보를 입력해준다. predicates에 입력된 Path와 client에서 요청 정보가 설정된 Path 맞을 경우 route.uri에 등록된 micro service로 포워딩 해준다.

spring:
  cloud:
    gateway:
      routes:
        - id: user-service
          uri: lb://USER-SERVICE
          predicates:
            - Path=/user-service/**



3. Spring Cloud Gateway Load Balancer 구현 - Eureka Server와 Micro Service 연동

 

Eureka Server와 Micro Service간에 연동을 위해 Micro Service Server 설정 파일에 Eureka Server 정보를 등록한다.

eureka:
  instance:
    instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://127.0.0.1:8761/eureka

 

4. 각각의 서버 기동 및 Load Balance 확인

 

Eureka Server를 먼저 기동하고 API Gateway Server, Micro Service Server를 각각 기동한다. Eureka Server에서 각각의 Application들이 정상적으로 보여지는지 인스턴스 List를 확인한다.

 

Client(Postman)을 통해  API Gateway Server로 Micro Service 호출 시 정상적으로 동작하는지 확인한다.

 

Micro Service Server 2개 기동 후 Eureka Server에 Micro Service가 2개 모두 등록되었는지 확인한다.

 

Client에서 Serivce 호출 시 API Gateway Server를 통해 정상적으로 Load Balance되는 지 확인한다.
아래 이미지와 같이 호출 시 Apigateway에서 라운드로빈 방식으로 2개의 Micro Service로 순차적으로 호출됨을 확인할 수 있다.

 

해당 내용은 인프런 이도원님의 "Spring Cloud로 개발하는 마이크로서비스 애플리케이션(MSA)"를 듣고 정리한 내용입니다.

 

 

 

반응형