본 글에서는 Spring Cloud Gateway에서 기본으로 제공하는 Route Predicate Factory들에 대해서 설명합니다. 본 글은 공식 홈페이지의 내용을 기능별로 정리한 내용입니다.
Route Predicate Factories
Route Prediates는 어떤 HTTP request를 match 시킬지 정하는 역할을 수행합니다. Spring Cloud Gateway는 이미 기본적으로 제공하는 Route Predicate Factories를 가지고 있으며 본 글에서 다룰 대상입니다. 만약 여러 Predicates를 동시에 사용하고 싶으시다면 yml 파일에 다음과 같이 여러 predicates를 같이 적으면 and 조건으로 들어가게 됩니다. 참고로 일반적으로 가장 많이 사용하는 Predicate는 URI 매칭을 하는 Path Predicate Factory 입니다.
spring:
cloud:
gateway:
routes:
- id: [Route ID]
uri: [Route를 보낼 곧]
predicates:
- [Predicate 1]
- [Predicate 2]
...
요청 시간 관련
시간 관련 Facotries는 요청 시간과 관련된 Factories 입니다.
Factory 이름 | 설명 | Parameter |
---|---|---|
After | 특정 시간 이후 요청을 Match 시킵니다. | datetime |
Before | 특정 시간 이전 요청을 Match 시킵니다. | datetime |
Between | 특정 시작 및 종료 시간 요청을 Match 시킵니다. | datetime1, datetime2 |
application-before.yml
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
application-after.yml
spring:
cloud:
gateway:
routes:
- id: after_route
uri: https://example.org
predicates:
- After=2017-01-20T17:42:47.789-07:00[America/Denver]
application-between.yml
spring:
cloud:
gateway:
routes:
- id: before_route
uri: https://example.org
predicates:
- Before=2017-01-20T17:42:47.789-07:00[America/Denver]
HTTP 요청 Header 관련
HTTP 요청 Header 필드 관련 factories 입니다.
Factory 이름 | 설명 | Parameter |
---|---|---|
Cookie | Cookie에 있는 특정 Name의 Field 값이 Regexp가 매칭되는지 확인합니다. | name, regexp |
Header | 특정 Header Field가 Regexp에 매칭되는 값을 가지고 있는지 확인합니다. | header, regexp |
Host | Host가 Pattern 중 매칭 되는지 확인합니다. | patterns |
XForwardedRemoteAddr | X-Forwarded-For 헤더에 | sources 리스트(최소 하나) |
application-cookie.yml
spring:
cloud:
gateway:
routes:
- id: cookie_route
uri: https://example.org
predicates:
- Cookie=chocolate, ch.p
application-header.yml
spring:
cloud:
gateway:
routes:
- id: header_route
uri: https://example.org
predicates:
- Header=X-Request-Id, \d+
application-host.yml
spring:
cloud:
gateway:
routes:
- id: host_route
uri: https://example.org
predicates:
- Host=**.somehost.org,**.anotherhost.org
application-Xforwardedremoteaddr.yml
spring:
cloud:
gateway:
routes:
- id: xforwarded_remoteaddr_route
uri: https://example.org
predicates:
- XForwardedRemoteAddr=192.168.1.1/24
Method / URI 관련
HTTP 요청의 Method와 URI 관련 Predicates 입니다.
Factory 이름 | 설명 | Parameter |
---|---|---|
Method | 요청이 Parameter Methods에 포함되는지 확인합니다. | methods |
Path | Patterns에 매칭되는 URI Path인지 확인합니다. matchTrailingSlash의 경우 URI 마지막에 /를 자동으로 포함하여 검사할지 여부입니다. | patterns, matchTrailingSlash(default true) |
Query | 특정 쿼리 param이 regexp에 매칭되는지 확인합니다. | param, regexp |
application-method.yml
spring:
cloud:
gateway:
routes:
- id: method_route
uri: https://example.org
predicates:
- Method=GET,POST
application-path.yml
spring:
cloud:
gateway:
routes:
- id: path_route
uri: https://example.org
predicates:
- Path=/red/{segment},/blue/{segment}
application-query.yml
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=green
기타
Factory 이름 | 설명 | Parameter |
---|---|---|
RemoteAddr | 요청한 IP 주소가 sources에 있는 CIDER-notation에 매칭되는지 확인합니다. | sources |
Weight | group 별로 weighted routing 할 수 있는 기능입니다. 같은 group name에 weight 퍼센트별로 라우팅 합니다. | group, weight |
application-remoteAddr.yml
spring:
cloud:
gateway:
routes:
- id: query_route
uri: https://example.org
predicates:
- Query=red, gree.
application-weight.yml
spring:
cloud:
gateway:
routes:
- id: remoteaddr_route
uri: https://example.org
predicates:
- RemoteAddr=192.168.1.1/24
마치며
이번 글에서는 기본 제공 Route Predicates Factories에 대해서 알아보았습니다. 추가적으로 기본 제공 기능 뿐만아니라 Custom Predicate을 만들 수 있으며, yml 파일 형태가 아닌 java 코드로 predicate를 설정할 수 있는데 해당 내용은 추후에 다뤄보도록 하겠습니다.
'코딩공부 > MSA' 카테고리의 다른 글
[Spring Cloud] Spring Cloud 프로젝트 개요 (1) | 2024.09.10 |
---|---|
[Spring Cloud] Spring Cloud Gateway - 설정 및 동작 원리 (0) | 2024.09.02 |
[마이크로서비스] 마이크로서비스의 원칙 (0) | 2024.04.01 |