将 KrakenD 与本地 Nodejs 服务器结合使用

发布于 2025-01-14 03:23:39 字数 1479 浏览 5 评论 0原文

我的本地计算机上有一台正在运行的 Nodejs 服务器(带有一个 API)。

我已经使用以下命令为 krakend 创建了新的 docker 容器

docker run -p 8080:8080 -v $PWD:/etc/krakend/ devopsfaith/krakend run --config /etc/krakend/krakend.json 

,但我必须对上述命令进行一些更改,因为我正在 Windows 上工作。

我创建了一个 krakend.json 文件,它的内容是

{
  "version": 3,
  "timeout": "3s",
  "cache_ttl": "300s",
  "port": 8080,
  "default_hosts": ["http://localhost:3001"],
  "endpoints": [
    {
      "endpoint": "/contacts",
      "output_encoding": "json",
      "extra_config": {
        "qos/ratelimit/router": {
          "max_rate": 5000
        }
      },
      "backend": [
        {
          "host": [
          "http://localhost:3001", 
          "http://cotacts:3001"
          ],
          "url_pattern": "/contacts",
          "is_collection": "true",
          "encoding": "json",
          "extra_config": {
            "backend/http": {
              "return_error_details": "backend_alias"
            }
          }
        }
      ]
    }
  ]
}

但是当我使用邮递员点击 url http://localhost:8080/contacts 时,我发现

[KRAKEND] 2022/03/14 - 07:26:30.305 ▶ ERROR [ENDPOINT: /contacts] Get "http://localhost:3001/contacts": dial tcp 127.0.0.1:3001: connect: connection refused

我在这里找到了一个相关的文件 Krakend api-gateway 出现连接拒绝错误?

但是,我不是了解我的情况需要改变什么

I have a up and running nodejs server (with one API) on my local machine .

I have created the new docker container for krakend using

docker run -p 8080:8080 -v $PWD:/etc/krakend/ devopsfaith/krakend run --config /etc/krakend/krakend.json 

Although, I have to make some changes in above command because I am working on windows.

I have created a krakend.json file and it's content are

{
  "version": 3,
  "timeout": "3s",
  "cache_ttl": "300s",
  "port": 8080,
  "default_hosts": ["http://localhost:3001"],
  "endpoints": [
    {
      "endpoint": "/contacts",
      "output_encoding": "json",
      "extra_config": {
        "qos/ratelimit/router": {
          "max_rate": 5000
        }
      },
      "backend": [
        {
          "host": [
          "http://localhost:3001", 
          "http://cotacts:3001"
          ],
          "url_pattern": "/contacts",
          "is_collection": "true",
          "encoding": "json",
          "extra_config": {
            "backend/http": {
              "return_error_details": "backend_alias"
            }
          }
        }
      ]
    }
  ]
}

But when I am hitting the url http://localhost:8080/contacts using postman I am getting

[KRAKEND] 2022/03/14 - 07:26:30.305 ▶ ERROR [ENDPOINT: /contacts] Get "http://localhost:3001/contacts": dial tcp 127.0.0.1:3001: connect: connection refused

I found a relevant one over here
connection refused error with Krakend api-gateway?

but, I am not getting what to change in my case

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

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

发布评论

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

评论(1

昔日梦未散 2025-01-21 03:23:39

后端内,负载均衡器中有两个主机。 KrakenD 将以循环方式尝试其中之一。

"host": [
          "http://localhost:3001", 
          "http://cotacts:3001"
          ],

如果您按照消息中所写的那样启动了 KrakenD,则这两个名称都不可用。

  1. localhost 是 KrakenD 本身(不是启动 KrakenD 的主机)。 KrakenD 没有任何端口 3001,因此预计它无法连接。你应该写你的主机IP。
  2. 我猜 cotacts:3001 是一些外部服务。如果您需要按名称访问此服务,则需要通过 docker compose 使用它。

您遇到的问题是 Docker 连接性,与 KrakenD 无关。 KrakenD 只是抱怨它无法连接到这些服务。

最后,"default_hosts"是KrakenD中不存在的东西,它对配置没有影响,你可以删除该行。如果您想拥有一个默认主机而不需要在每个后端中声明它,只需使用host。总之,您的配置应如下所示:

{
    "$schema": "https://www.krakend.io/schema/v3.json",
    "version": 3,
    "timeout": "3s",
    "cache_ttl": "300s",
    "port": 8080,
    "host": [
        "http://1.2.3.4:3001"
    ],
    "endpoints": [
        {
            "endpoint": "/contacts",
            "extra_config": {
                "qos/ratelimit/router": {
                    "max_rate": 5000
                }
            },
            "backend": [
                {
                    "url_pattern": "/contacts",
                    "is_collection": "true",
                    "extra_config": {
                        "backend/http": {
                            "return_error_details": "backend_alias"
                        }
                    }
                }
            ]
        }
    ]
}

并将 1.2.3.4 替换为运行 Node.js 的计算机的 IP。

Inside the backend you have two hosts in the load balancer. KrakenD will try one and the other in a round-robin fashion.

"host": [
          "http://localhost:3001", 
          "http://cotacts:3001"
          ],

If you have started KrakenD as you have written in your message, then neither of the names are available.

  1. localhost is KrakenD itself (not the host machine starting KrakenD). KrakenD does not have any port 3001 so it's expected that it cannot connect. You should write your host IP.
  2. I am guessing cotacts:3001 is some outside service. If you need to access this service by name you need to use it through a docker compose.

The problem you have is Docker connectivity, and is not related to KrakenD. KrakenD is just complaining that it cannot connect to those services.

Finally, the "default_hosts" is something that it does not exist in KrakenD, it makes no effect in the configuration, you can delete that line. If you want to have a default host without needing to declare it in every backend use just host. In summary, your configuration should look like:

{
    "$schema": "https://www.krakend.io/schema/v3.json",
    "version": 3,
    "timeout": "3s",
    "cache_ttl": "300s",
    "port": 8080,
    "host": [
        "http://1.2.3.4:3001"
    ],
    "endpoints": [
        {
            "endpoint": "/contacts",
            "extra_config": {
                "qos/ratelimit/router": {
                    "max_rate": 5000
                }
            },
            "backend": [
                {
                    "url_pattern": "/contacts",
                    "is_collection": "true",
                    "extra_config": {
                        "backend/http": {
                            "return_error_details": "backend_alias"
                        }
                    }
                }
            ]
        }
    ]
}

And replace 1.2.3.4 with the IP of the machine running the Node.

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