Krakend http:在关闭的 Body 上读取无效

发布于 2025-01-10 20:05:18 字数 2996 浏览 0 评论 0原文

我正在使用 Krakend 构建一个 API 网关来连接三个后端服务。网关始终从一个或两个后端服务返回,并且 X-Krakend-Completed 标头 始终设置为 false。

日志中出现 http: invalid Read on Closed Body 错误的原因可能是什么?

预期行为

GET localhost:8000

响应

{
    "user-id": 1,
    "payments-id": 1,
    "loans-id": 1,
}

实际行为

GET localhost:8000

响应

{
    "payment-id": 1
}

Krakend 日志

[GIN] 2022/03/01 - 16:29:41 | 200 |     801.319µs |             ::1 | GET      "/"
Error #01: Get "http://localhost:5000/users": http: invalid Read on closed Body
Get "http://localhost:6000/loans": http: invalid Read on closed Body
[GIN] 2022/03/01 - 16:29:55 | 200 |     851.735µs |             ::1 | GET      "/"
Error #01: Get "http://localhost:6000/loans": http: invalid Read on closed Body
Get "http://localhost:5000/users": http: invalid Read on closed Body

Service 1

type Payment struct {
    Id int32 `json:"payment-id"`
}

var payments = []Payment{
    {
        Id: 0,
    },
    {
        Id: 1,
    }
}

func main() {

    app := fiber.New()

    app.Get("/payments", func(c *fiber.Ctx) error {
        return c.JSON(payments[1])
    })

    app.Listen(":7000")

}

Service 2

func main() {

    app := fiber.New()

    app.Get("/loans", func(c *fiber.Ctx) error {
        return c.JSON(loans[1])
    })

    app.Listen(":6000")

}

Service 3

func main() {

    app := fiber.New()

    app.Get("/users", func(c *fiber.Ctx) error {
        return c.JSON(users[1])
    })

    app.Listen(":5000")


}

Krakend.json

{
    "version": 2,
    "timeout": "3000ms",
    "cache_ttl": "300s",
    "output_encoding": "json",
    "name": "users",
    "port": 8000,
    "read_timeout": "2s",
    "write_timeout": "2s",
    "idle_timeout": "2s",
    "read_header_timeout": "2s",
    "endpoints": [
      {
        "endpoint": "/",
        "method": "GET",
        "output_encoding": "json",
        "backend": [
          {
            "url_pattern": "/users",
            "encoding": "json",
            "method": "GET",
            "host": [
              "http://localhost:5000"
            ]
          },
          {
            "url_pattern": "/loans",
            "encoding": "json",
            "method": "GET",
            "host": [
              "http://localhost:6000"
            ]
          },
          {
            "url_pattern": "/payments",
            "encoding": "json",
            "method": "GET",
            "host": [
              "http://localhost:7000"
            ]
          }
        ]
      }
    ]
  }

I'm using Krakend to build an API gateway to connect three backend services. The gateway always returns from one or two of the backend services with the X-Krakend-Completed header always set to false.

What could be the cause of the http: invalid Read on closed Body error in the logs?

Expected behavior

GET localhost:8000

response

{
    "user-id": 1,
    "payments-id": 1,
    "loans-id": 1,
}

Actual behavior

GET localhost:8000

response

{
    "payment-id": 1
}

Krakend log

[GIN] 2022/03/01 - 16:29:41 | 200 |     801.319µs |             ::1 | GET      "/"
Error #01: Get "http://localhost:5000/users": http: invalid Read on closed Body
Get "http://localhost:6000/loans": http: invalid Read on closed Body
[GIN] 2022/03/01 - 16:29:55 | 200 |     851.735µs |             ::1 | GET      "/"
Error #01: Get "http://localhost:6000/loans": http: invalid Read on closed Body
Get "http://localhost:5000/users": http: invalid Read on closed Body

Service 1

type Payment struct {
    Id int32 `json:"payment-id"`
}

var payments = []Payment{
    {
        Id: 0,
    },
    {
        Id: 1,
    }
}

func main() {

    app := fiber.New()

    app.Get("/payments", func(c *fiber.Ctx) error {
        return c.JSON(payments[1])
    })

    app.Listen(":7000")

}

Service 2

func main() {

    app := fiber.New()

    app.Get("/loans", func(c *fiber.Ctx) error {
        return c.JSON(loans[1])
    })

    app.Listen(":6000")

}

Service 3

func main() {

    app := fiber.New()

    app.Get("/users", func(c *fiber.Ctx) error {
        return c.JSON(users[1])
    })

    app.Listen(":5000")


}

Krakend.json

{
    "version": 2,
    "timeout": "3000ms",
    "cache_ttl": "300s",
    "output_encoding": "json",
    "name": "users",
    "port": 8000,
    "read_timeout": "2s",
    "write_timeout": "2s",
    "idle_timeout": "2s",
    "read_header_timeout": "2s",
    "endpoints": [
      {
        "endpoint": "/",
        "method": "GET",
        "output_encoding": "json",
        "backend": [
          {
            "url_pattern": "/users",
            "encoding": "json",
            "method": "GET",
            "host": [
              "http://localhost:5000"
            ]
          },
          {
            "url_pattern": "/loans",
            "encoding": "json",
            "method": "GET",
            "host": [
              "http://localhost:6000"
            ]
          },
          {
            "url_pattern": "/payments",
            "encoding": "json",
            "method": "GET",
            "host": [
              "http://localhost:7000"
            ]
          }
        ]
      }
    ]
  }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

献世佛 2025-01-17 20:05:18

我无意中在 GET 请求上发送了正文,导致 Krakend http: invalid Read on Closed Body 错误

链接到 Github 问题

I was unknowningly sending a body on a GET request resulting in the Krakend http: invalid Read on closed Body error

Link to Github Issue

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