将 KrakenD 与本地 Nodejs 服务器结合使用
我的本地计算机上有一台正在运行的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
后端
内,负载均衡器中有两个主机
。 KrakenD 将以循环方式尝试其中之一。如果您按照消息中所写的那样启动了 KrakenD,则这两个名称都不可用。
localhost
是 KrakenD 本身(不是启动 KrakenD 的主机)。 KrakenD 没有任何端口3001
,因此预计它无法连接。你应该写你的主机IP。cotacts:3001
是一些外部服务。如果您需要按名称访问此服务,则需要通过 docker compose 使用它。您遇到的问题是 Docker 连接性,与 KrakenD 无关。 KrakenD 只是抱怨它无法连接到这些服务。
最后,
"default_hosts"
是KrakenD中不存在的东西,它对配置没有影响,你可以删除该行。如果您想拥有一个默认主机而不需要在每个后端中声明它,只需使用host
。总之,您的配置应如下所示:并将
1.2.3.4
替换为运行 Node.js 的计算机的 IP。Inside the
backend
you have twohosts
in the load balancer. KrakenD will try one and the other in a round-robin fashion.If you have started KrakenD as you have written in your message, then neither of the names are available.
localhost
is KrakenD itself (not the host machine starting KrakenD). KrakenD does not have any port3001
so it's expected that it cannot connect. You should write your host IP.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 justhost
. In summary, your configuration should look like:And replace
1.2.3.4
with the IP of the machine running the Node.