====== Spring Cloud Gateway ====== * [[https://spring.io/projects/spring-cloud-gateway|Spring Cloud Gateway]] * Spring Boot / WebFlux / Reactor 기반의 Non Blocking API Gateway. * [[java:zuul|Zuul]] 에 대응. Java/Spring 기반 개발자들에게는 Spring Cloud Gateway 가 더 나아 보인다. * 이 페이지에 API Gateway 설계에 대한 내용을 많이 정리해뒀음. * 결론, 특별한 경우가 아니면 API Gatway 사용하지 말라. * Java Code 기반 Routing 설정 가능 ====== Routings ====== * ''localhost:8080/get'' -> ''http://httpbin.org:80/get'' 으로 요청 라우팅 .route(p -> p.path("/get") .filters(f -> f.addRequestHeader("Hello", "World")) .uri("http://httpbin.org:80")) * URL을 중간에 바꿔치기해서 라우팅 ''localhost:8080/api/v1/get'' -> ''http://httpbin.org:80/get'' 으로 요청 라우팅 .route(p -> p.path("/api/v1/get") .filters(f -> f.setPath("/get")) .uri("http://httpbin.org:80")) * PATH 앞에 떼어내기 ''localhost:8080/api/httpbin/get'' -> ''http://httpbin.org:80/get'' route("httpbin", p -> p.path("/api/httpbin/**") .filters(f -> f.stripPrefix(2)) .uri("http://httpbin.org:80)) // '/api' - 1, '/httpbin' - 2 해서 2 path 건너뛰기 * PATH Variable 정규표현식 ''localhost:8080/api/httpbin/get/123'' -> ''http://httpbin.org:80/get/123'', 단 ''/get/abc'' 는 거부됨 route("httpbin", p -> p.path("/api/httpbin/get/{num:[0-9]+}") .filters(f -> f.stripPrefix(2)) .uri("http://httpbin.org:80)) * Path Rewrite : [[https://stackoverflow.com/questions/48632054/spring-cloud-gateway-2-0-forward-path-variable|java - Spring Cloud Gateway 2.0 forward path variable - Stack Overflow]] route("userById", t -> t.path("/users/**") .filters(rw -> rw.rewritePath("/users/(?.*)", "/users/${segment}")) .uri("http://localhost:8080/users/")) ====== Actuator ====== ''implementation 'org.springframework.boot:spring-boot-starter-actuator''' 의존성 추가 management.endpoint.gateway.enabled=true # default value management.endpoints.web.exposure.include=gateway ===== 참조 ===== * [[https://spring.io/guides/gs/gateway/|Getting Started · Building a Gateway]] * [[https://github.com/spring-cloud-samples/spring-cloud-gateway-sample|Spring Cloud Gateway Sample]] * [[https://www.baeldung.com/spring-cloud-gateway|Exploring the New Spring Cloud Gateway | Baeldung]] * [[https://aboullaite.me/spring-cloud-gateway/|A look into Spring Cloud Gateway!]] * [[https://dzone.com/articles/spring-cloud-gateway-configuring-a-simple-route|Spring Cloud Gateway - Configuring a Simple Route - DZone Microservices]] * [[https://springboot.cloud/26|Spring Cloud Gateway 2.1.0RELEASE 레퍼런스]] * [[https://www.baeldung.com/spring-cloud-gateway-routing-predicate-factories|Spring Cloud Gateway Routing Predicate Factories | Baeldung]] * [[https://www.popit.kr/spring-cloud-gateway/|Spring Cloud Gateway | Popit]]