我在本地机器上有一个启动和运行的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我在这里找到了一个相关的connection refused error with Krakend api-gateway?
但是,在我的情况下,我没有得到什么改变。
发布于 2022-03-14 18:27:01
在backend中,负载均衡器中有两个hosts。KrakenD将以一种循环往复的方式尝试另一种.
"host": [
"http://localhost:3001",
"http://cotacts:3001"
],如果您已经按照您在邮件中所写的方式启动了KrakenD,那么这两个名称都不可用。
localhost本身就是KrakenD (不是启动KrakenD的主机)。KrakenD没有任何端口3001,因此预期它无法连接。你应该写你的主机IP。,
cotacts:3001是一些外部服务。如果您需要以名称访问此服务,则需要通过坞撰写器.使用它。
您遇到的问题是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替换为运行节点的机器的IP。
https://stackoverflow.com/questions/71467741
复制相似问题