Spring Cloud Gateway route to itself
Asked Answered
P

2

9

I have a scenario in an implementation of spring cloud gateway (Just for illustration call that service custom api gateway). In this 'Custom API Gateway' I want to define a route, filters, predicate and forward the request to a custom endpoint in the 'custom api gateway'? Is there a way to achieve that? Everything works except forwarding to a URI in the 'custom api gateway'

Here is an example

id: UpdateUser
uri:  SHOULD NOT GO DOWNSTREAM (endpoint is in 'custom api gateway')
predicates:
 - path=/api/users
filters:
 - ValidateSession

Thanks Segi

Postlude answered 26/2, 2021 at 2:27 Comment(1)
Hi Mohit Did you found the solution of it?Transpolar
F
4

You can define some controllers in spring cloud gateway and write routes to route requests to these controllers, but you should add uri changing filters like prefixPath, stripPrefix to change uri part to different one.

If you don't change the uri, requests will cause a 413 request entity too large error code, if you open debug level log, you'll see gateway is passing this request over and over again to itself.

Frugal answered 31/3, 2022 at 7:13 Comment(0)
F
0

You simply need to map the uri with "forward" -> uri: forward:///local-endpoint. More details here https://docs.spring.io/spring-cloud-gateway/reference/spring-cloud-gateway/global-filters.html#forward-routing-filter

gateway: 
  routes:
   - id: path_route
     uri: forward:///local-endpoint
     predicates:
     - Path=/somepath
Forefinger answered 4/10 at 14:38 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.