Today
-
Yesterday
-
Total
-
  • spring boot : eureka + gateway + random port 사용으로 load balancer 확인
    Spring Boot 🍃 2023. 12. 18. 00:01

    보고 배운 곳 : https://inf.run/GHeRm


    Spring Cloud Netflix Eureka , Spring Cloud Gateway , random port를 사용, 하나의 어플리케이션을 여러개 실행하여 간단하게 load balancing을 경험해본다.


    0.

    실제 서비스를 제공하는 application에서 제공 할 end point에 port 번호를 logging하도록 작성한다.

    FirstService Application -> FirstServiceController.java

    @Slf4j
    @RequiredArgsConstructor
    @RestController
    @RequestMapping("/")
    public class FirstServiceController {
        private final Environment 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. - PORT : %s"
                    , env.getProperty("local.server.port"));
        }
    
    }

    1.

    실제 서비스를 제공하는 application의 실행 port 를 random port로 설정하고, eureka에 등록 될 instance id 값을 지정해준다.

    -> random port는 겉으로 드러나기로는 0번으로 표시되기 때문에, 하나의 어플리케이션을 여러개 실행할 때 eureka의 instance id 값을 변경하지 않는다면 eureka에 단 하나의 어플리케이션 인스턴스만 등록되고 나머지 인스턴스들은 등록되지 않는다.

    FirstService Application -> application.yml

    server:
      port: 0
      servlet:
        context-path: /first-service
    
    spring:
      application:
        name: my-first-service
    
    eureka:
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
          defaultZone: http://localhost:8761/eureka
      instance:
        instance-id: ${spring.application.name}:${spring.application.instance_id:${random.value}}

    2.

    spring cloud gateway를 통해 실제 서비스를 제공하는 application들을 라우팅한다.

    -> 이때 , protocol을 lb 를 사용하여 서비스를 찾을 때 port 번호가 아니라 eureka에 등록된 application name으로 검색할 수 있도록 한다.

    ApiGatewayService Application -> application.yml

    server:
      port: 8000
      
    eureka:
      client:
        register-with-eureka: true
        fetch-registry: true
        service-url:
          defaultZone: http://localhost:8761/eureka
    
    spring:
      application:
        name: apigateway-service
        
      cloud:
        gateway:
          default-filters:
            - name: GlobalFilter
              args:
                baseMessage: Spring Cloud Gateway Filter
                preLogger: true
                postLogger: true
                
          routes:
            - id: first-service
              uri: lb://my-first-service
              predicates:
                - Path=/first-service/**
              filters:
                - CustomFilter
            - id: second-service
              uri: lb://my-second-service
              predicates:
                - Path=/second-service/**
              filters:
                - name: CustomFilter
                - name: LoggingFilter
                  args:
                    baseMessage: Hi, there.
                    preLogger: true
                    postLogger: true

    3.

    spring cloud netflix eureka 서버가 될 어플리케이션을 먼저 실행 하고 gateway 어플리케이션과 실제 서비스를 제공할 어플리케이션들을 순차적으로 실행해준다.

    1. EurekaService Application 실행

    2. ApiGatewayService Application 실행

    3. FirstService Application 2개 실행

    3. SecondService Application 2개 실행


    4.

    gateway를 통해 서비스에 접근하여 로깅되는 port 번호를 확인한다.

    -> spring boot는 Round Robin 기술을 사용하여 순차적으로 어플리케이션을 실행한다.

    ex)
        1회 접근 - A1앱 실행 됨
        2회 접근 - A2앱 실행 됨
        3회 접근 - A1앱 실행 됨
        4회 접근 - A2앱 실행 됨
    

Designed by Tistory / Custom by 얼거스