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

huangapple go评论120阅读模式
英文:

Krakend http: invalid Read on closed Body

问题

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

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

期望的行为

GET localhost:8000

响应

  1. {
  2. "user-id": 1,
  3. "payments-id": 1,
  4. "loans-id": 1
  5. }

实际的行为

GET localhost:8000

响应

  1. {
  2. "payment-id": 1
  3. }

Krakend日志

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

服务1

  1. type Payment struct {
  2. Id int32 `json:"payment-id"`
  3. }
  4. var payments = []Payment{
  5. {
  6. Id: 0,
  7. },
  8. {
  9. Id: 1,
  10. },
  11. }
  12. func main() {
  13. app := fiber.New()
  14. app.Get("/payments", func(c *fiber.Ctx) error {
  15. return c.JSON(payments[1])
  16. })
  17. app.Listen(":7000")
  18. }

服务2

  1. func main() {
  2. app := fiber.New()
  3. app.Get("/loans", func(c *fiber.Ctx) error {
  4. return c.JSON(loans[1])
  5. })
  6. app.Listen(":6000")
  7. }

服务3

  1. func main() {
  2. app := fiber.New()
  3. app.Get("/users", func(c *fiber.Ctx) error {
  4. return c.JSON(users[1])
  5. })
  6. app.Listen(":5000")
  7. }

Krakend.json

  1. {
  2. "version": 2,
  3. "timeout": "3000ms",
  4. "cache_ttl": "300s",
  5. "output_encoding": "json",
  6. "name": "users",
  7. "port": 8000,
  8. "read_timeout": "2s",
  9. "write_timeout": "2s",
  10. "idle_timeout": "2s",
  11. "read_header_timeout": "2s",
  12. "endpoints": [
  13. {
  14. "endpoint": "/",
  15. "method": "GET",
  16. "output_encoding": "json",
  17. "backend": [
  18. {
  19. "url_pattern": "/users",
  20. "encoding": "json",
  21. "method": "GET",
  22. "host": [
  23. "http://localhost:5000"
  24. ]
  25. },
  26. {
  27. "url_pattern": "/loans",
  28. "encoding": "json",
  29. "method": "GET",
  30. "host": [
  31. "http://localhost:6000"
  32. ]
  33. },
  34. {
  35. "url_pattern": "/payments",
  36. "encoding": "json",
  37. "method": "GET",
  38. "host": [
  39. "http://localhost:7000"
  40. ]
  41. }
  42. ]
  43. }
  44. ]
  45. }
英文:

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

  1. {
  2. "user-id": 1,
  3. "payments-id": 1,
  4. "loans-id": 1,
  5. }

Actual behavior

GET localhost:8000

response

  1. {
  2. "payment-id": 1
  3. }

Krakend log

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

Service 1

  1. type Payment struct {
  2. Id int32 `json:"payment-id"`
  3. }
  4. var payments = []Payment{
  5. {
  6. Id: 0,
  7. },
  8. {
  9. Id: 1,
  10. }
  11. }
  12. func main() {
  13. app := fiber.New()
  14. app.Get("/payments", func(c *fiber.Ctx) error {
  15. return c.JSON(payments[1])
  16. })
  17. app.Listen(":7000")
  18. }

Service 2

  1. func main() {
  2. app := fiber.New()
  3. app.Get("/loans", func(c *fiber.Ctx) error {
  4. return c.JSON(loans[1])
  5. })
  6. app.Listen(":6000")
  7. }

Service 3

  1. func main() {
  2. app := fiber.New()
  3. app.Get("/users", func(c *fiber.Ctx) error {
  4. return c.JSON(users[1])
  5. })
  6. app.Listen(":5000")
  7. }

Krakend.json

  1. {
  2. "version": 2,
  3. "timeout": "3000ms",
  4. "cache_ttl": "300s",
  5. "output_encoding": "json",
  6. "name": "users",
  7. "port": 8000,
  8. "read_timeout": "2s",
  9. "write_timeout": "2s",
  10. "idle_timeout": "2s",
  11. "read_header_timeout": "2s",
  12. "endpoints": [
  13. {
  14. "endpoint": "/",
  15. "method": "GET",
  16. "output_encoding": "json",
  17. "backend": [
  18. {
  19. "url_pattern": "/users",
  20. "encoding": "json",
  21. "method": "GET",
  22. "host": [
  23. "http://localhost:5000"
  24. ]
  25. },
  26. {
  27. "url_pattern": "/loans",
  28. "encoding": "json",
  29. "method": "GET",
  30. "host": [
  31. "http://localhost:6000"
  32. ]
  33. },
  34. {
  35. "url_pattern": "/payments",
  36. "encoding": "json",
  37. "method": "GET",
  38. "host": [
  39. "http://localhost:7000"
  40. ]
  41. }
  42. ]
  43. }
  44. ]
  45. }

答案1

得分: 0

我在发送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

huangapple
  • 本文由 发表于 2022年3月1日 21:32:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/71309260.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定