春天云网关返回空响应

发布于 2025-02-03 10:08:24 字数 1729 浏览 2 评论 0原文

我正在尝试使用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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅语花开 2025-02-10 10:08:24

似乎您的应用程序具有/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。

这是可能对您有用的示例用法:

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

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}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文