春天云网关返回空响应
我正在尝试使用Spring Cloud Gateway来路由我的微服务,但是即使微服务正常工作,Gateway路由仍会返回空空心响应。 我的微服务是一个简单的应用程序,它正在端口5861
上运行。我只需预测所有案例以确保其路由之后,我就可以通过特定的路径进行路由,以确保其路由。
那是我的网关配置文件:
spring:
cloud:
gateway:
routes:
- id: product_service
uri: localhost:5861/
predicates:
- Path=/product-service/**
运行服务并击中它们后,我的微服务返回响应正确:
$ 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}]
但是,如果我尝试从API网关中执行此操作,则该响应将无需返回,如果我尝试从浏览器到达浏览器,则会发生空白页。
$ 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
有人可以帮助我缺少的东西吗?我想从网关得到相同的回应。
编辑:
工作更新:
spring:
cloud:
gateway:
routes:
- id: product_service
uri: http://localhost:5861/
predicates:
- Path=/product
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.
Edit:
Working update:
spring:
cloud:
gateway:
routes:
- id: product_service
uri: http://localhost:5861/
predicates:
- Path=/product
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
似乎您的应用程序具有
/product/
端点公开。从Gateway中,您试图从
/product-Service/product
转换为服务的/product/code>。
默认情况下,Spring Cloud Gateway按原样将其重定向到URI。因此,目前,我相信
http:// localhost:5860/product-service/product
正在重定向到http:// localhost:5861/product-service/product/product
。如果您需要从
product-service/**
重定向到/**
产品服务的,请使用rewritepath
filter。这是可能对您有用的示例用法:
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 tohttp://localhost:5861/product-service/product
.If you need to redirect from
product-service/**
to/**
of product-service then useRewritePath
filter.here is an example usage that may work for you: