Spring Cloud Gateway Predicate Multi Paths

송형근·2024년 9월 16일
0

TIL

목록 보기
38/43
post-thumbnail

프로젝트에서 하나의 서버에 1개 이상의 Path를 사용중이라 Spring Cloud Gateway에서 설정이 필요

Application.yaml 작성 (최초 작성) - 문제 발생

  • Predicates 아래 여러개의 Path 작성
    #########
    spring:
      main:
        web-application-type: reactive
        allow-bean-definition-overriding: true
      application:
        name: gateway-server
      cloud:
        gateway:
          routes:
            - id: auth-server
              uri: lb://auth-server
              predicates:
                - Path=/api/users/**
            - id: company-server
              uri: lb://company-server
              predicates:
                - Path=/api/companies/**
                - Path=/api/products/**
          discovery:
            locator:
              enabled: true
    #########
  • 결과
    • auth-server는 문제가 없지만, company-server는 gateway를 통한 요청 시 404 발생

원인

  • Multi Paths를 사용할 때 작성법이 잘못되어서 해당 에러가 발생

해결 ( Application.yaml 수정 )

  • Path 작성 부분에서 ,로 구분해서 작성

    #########
    spring:
      main:
        web-application-type: reactive
        allow-bean-definition-overriding: true
      application:
        name: gateway-server
      cloud:
        gateway:
          routes:
            - id: auth-server
              uri: lb://auth-server
              predicates:
                - Path=/api/users/**
            - id: company-server
              uri: lb://company-server
              predicates:
                - Path=/api/companies/**, /api/products/**
          discovery:
            locator:
              enabled: true
    #########
  • 결과

    • 정상 작동 확인


P.S.

profile
기록을 남겨보자

0개의 댓글