본문 바로가기

Spring

Spring Cloud Gateway predicates 테스트

728x90
반응형

Predicates

Webflux handler Mapping을 사용하여 Route하는데 다양한 Predicates factory가 이미 정의되어 있다. 

 

 

After & Before

spring:
  cloud:
    gateway:
      routes:
      - id: after_route
        uri: https://example.org
        predicates:
        - After=2017-01-20T17:42:47.789-07:00[America/Denver]
spring:
  cloud:
    gateway:
      routes:
      - id: before_route
        uri: https://example.org
        predicates:
        - Before=2017-01-20T17:42:47.789-07:00[America/Denver]

after는 해당 날짜가 지난 후에 route가 가능하고 before는 해당날짜 전에 route가 가능하다.

zoneddatetime 으로 설정해야 작동한다. 

 

Cookie

spring:
  cloud:
    gateway:
      routes:
      - id: cookie_route
        uri: https://example.org
        predicates:
        - Cookie=chocolate, ch.p

Cookie는 Cookie로 넘어온 값이 일치하면 Route한다.

첫 번째 인자는 Cookie명, 두 번째 인자는 일치하는 값 혹은 정규식으로 설정이 가능한다.

 

Header

spring:
  cloud:
    gateway:
      routes:
      - id: header_route
        uri: https://example.org
        predicates:
        - Header=X-Request-Id, \d+

header에 값을 넣어서 라우팅할 수 있다.

첫 번째 인자는 header name이고 두번째 인자는 일치하는 값 혹은 정규식으로 설정이 가능하다.

 

Path

spring:
  cloud:
    gateway:
      routes:
      - id: path_route
        uri: https://example.org
        predicates:
        - Path=/red/{segment},/blue/{segment}

Path는 URL 패턴이 일치하면 routing한다.

가장 자주 쓰는 Predicates가 아닐까 싶다. 

 

그밖에도 굉장히 많다.

Method, host, query, remoteAddr 등등 

필요한 걸 그 때 그 때 찾아서 확인하면 될 것같다. 

 

application.properties에서 설정하는법

spring.cloud.gateway.routes[0].id=routeapi1
spring.cloud.gateway.routes[0].uri=http://localhost:8081
spring.cloud.gateway.routes[0].predicates[0]=Path=/routeapi1/**
spring.cloud.gateway.routes[0].predicates[1]=Before=2020-12-30T10:04:53.313602+09:00[Asia/Seoul]
spring.cloud.gateway.routes[0].predicates[2]=Cookie=Cookie_1,value
spring.cloud.gateway.routes[0].predicates[3]=Header=X-Request-Id,\d+

routes와 predicates에 숫자를 붙여주면 여러 개 사용이 가능하다.

728x90
반응형

'Spring' 카테고리의 다른 글

SpringBootServletInitializer 란 무엇일까? 왜쓰지?  (0) 2021.01.07
Spring Cloud Gateway GatewayFilter  (0) 2020.12.29
Spring Cloud Gateway Actuator enabled 방법  (0) 2020.12.15
JAR 파일 구조  (0) 2020.12.03
SpringBoot logback 설정  (0) 2020.11.24