Home > Mobile >  Spring cloud gateway returns empty response
Spring cloud gateway returns empty response

Time:06-04

I am trying to use Spring Cloud Gateway for routing my microservice, but even if the microservice is working expectedly gateway routing returns an empty response. My microservice is a simple application and it's running on port 5861. I routed my Gateway to it simply by predicting all cases to be sure it routed, after my routing trials with specific paths.

That's my Gateway configuration file:

spring:
  cloud:
    gateway:
      routes:
        - id: product_service
          uri: localhost:5861/
          predicates:
            - Path=/product-service/**

After running both service and hitting them, my microservice returns response properly:

$ curl -v http://localhost:5861/product/

*   Trying 127.0.0.1:5861...
* Connected to localhost (127.0.0.1) port 5861 (#0)
> GET /product/ HTTP/1.1
> Host: localhost:5861
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 
< Content-Type: application/json
< Transfer-Encoding: chunked
< Date: Wed, 01 Jun 2022 19:41:40 GMT
< 
* Connection #0 to host localhost left intact
[{"id":1,"name":"Apple","price":2},{"id":2,"name":"Apple","price":2},{"id":3,"name":"Apple","price":2}]

But if I try to do this from my API gateway it returns nothing, if I try to reach it from my browser a blank page occurs.

$ curl -v  http://localhost:5860/product-service/product

*   Trying 127.0.0.1:5860...
* Connected to localhost (127.0.0.1) port 5860 (#0)
> GET /product-service/product HTTP/1.1
> Host: localhost:5860
> User-Agent: curl/7.81.0
> Accept: */*
> 
* Mark bundle as not supporting multiuse
< HTTP/1.1 200 OK
< content-length: 0
< 
* Connection #0 to host localhost left intact

Can someone help me with what I am missing? I want to get the same response from the gateway.

CodePudding user response:

It seems like your application has /product/ endpoint exposed. From gateway, you are trying to redirect from /product-service/product to /product/ of service. By default, spring cloud gateway redirects to Uris as they are. So currently, I believe that http://localhost:5860/product-service/product is being redirected to http://localhost:5861/product-service/product.

If you need to redirect from product-service/** to /** of product-service then use RewritePath filter.

here is an example usage that may work for you:

spring:
  cloud:
    gateway:
      routes:
        - id: product_service
          uri: localhost:5861/
          predicates:
            - Path=/product-service/**
          filters:
            - RewritePath=/product-service/(?<segment>/?.*),$\{segment}

CodePudding user response:

Working update:

spring:
  cloud:
    gateway:
      routes:
        - id: product_service
          uri: http://localhost:5861/
          predicates:
            - Path=/product
  • Related