본문 바로가기
FrameWork/Spring Cloud

Spring Cloud Gateway - Load Balancer

by 태윤2 2021. 8. 25.

Eureka 연동

  • first-service, second-service, Spring Cloud Gateway
    • spring-cloud-starter-netflix-eureka-client 의존성 추가
    • eureka 서버 등록

  • application.yml - GatewayService
  • uri 변경

lb -> Load Balancer

First Service, Second Service를 각각 2개씩 기동

Intellij에서 포트를 변경해서 서버를 기동 하는법

  • VM Options -> -Dsever.port=[다른포트]
  • ./gradlew bootRun --args='--server.port=[다른포트]' # gradle
    mvn spring-boot:run -Dspring-boot.run.jvmArguments='-Dserver.port=[다른포트]' # mvn
  • ./gradlew clean build
    mvn clean compile package
    
    java -jar -Dserver.port=[다른포트] ./build/libs/user-service-0.0.1-SNAPSHOT.jar

 

  • RandomPort를 사용해서 서버를 기동
server:
  port: 0 # 랜덤포트 사용

spring:
  application:
    name: my-first-service

eureka:
  client:
    register-with-eureka: true
    fetch-registry: true
    service-url:
      defaultZone: http://localhost:8761/eureka
  # 랜덤포트 사용시 여러개의 서비스들이 0으로만 표시되는걸 나눠서 볼수 있게 인스턴스 아이디 부여
  instance:
    instance-id: ${spirng.application.name}:${spring.application.instance_id:${random.value}}
  • controller에 env 주입받기

  • env 주입받기

  • 포트 정보 가져오기
@GetMapping("/check")
    public String check(HttpServletRequest request) {
        log.info("Server port={}", request.getServerPort());
        return String.format("Hi, there. This is a message from First Service on PORT %s", env.getProperty("local.server.port"));
    }
  • Round Robbin 방식으로 게이트웨이가 번갈아가면서 요청을 보냄(호출함)

 

  • reference

SpringCloud로 개발하는 마이크로어플리케이션

'FrameWork > Spring Cloud' 카테고리의 다른 글

Spring Cloud Bus  (0) 2021.09.04
Configuration Service  (0) 2021.09.03
E-Commerce 애플리케이션  (0) 2021.08.26
API Gateway Service  (0) 2021.08.25
Service Discovery  (0) 2021.08.24