【发布时间】:2021-09-13 01:46:31
【问题描述】:
我想看看api-gateway 是如何工作的,我正在使用Krakend。从技术上讲,我有一项服务,它只接收请求并回复。问题是当通过api-gateway 触发相关端点时,作为一个非常简单的http-server 的服务没有收到来自api-gateway 的请求。但是,当我通过浏览器发出获取请求并写入确切的 url http://localhost:8000/api/v1/users 时,我得到了响应。基本上,我想要的是,当我向localhost:8099/users(即api-gateway)发出 get 请求时,我希望 api-gateway 向 users api 发出 get 请求并查看预期的日志.
这是我得到的错误。
错误 #01:获取“http://localhost:8000/api/v1/users”:拨打 tcp 127.0.0.1:8000:连接:连接被拒绝
这是我放置配置的krakend.json 文件。
{
"version": 2,
"host" : [
"http://localhost:8099"
],
"extra_config": {
"github_com/devopsfaith/krakend-cors": {
"allow_origins": [
"*"
],
"expose_headers": [
"Content-Length"
],
"max_age": "12h",
"allow_methods": [
"GET"
],
"allow_headers": [
"*"
]
}
},
"timeout": "3000ms",
"cache_ttl": "300s",
"output_encoding": "string",
"name": "api-gateway",
"port": 8099,
"read_timeout": "5s",
"idle_timeout": "5s",
"write_timeout": "5s",
"read_header_timeout": "5s",
"endpoints": [
{
"endpoint": "/users",
"method" : "GET",
"backend": [
{
"encoding" : "string",
"url_pattern": "/api/v1/users",
"method": "GET",
"host": [
"http://localhost:8000"
]
}
]
}
]
}
这是我的节点 js 服务器。
const express = require('express')
const app = express()
const cors = require('cors')
var corsOptions = {
origin: 'http://localhost:8099',
methods: "GET, PUT"
}
app.use(cors(corsOptions))
app.get('/api/v1/users', (req, res) => {
console.log('REQUEST => \n')
console.log(req.url)
console.log(req.hostname)
})
app.listen(8000, () => {console.log('server has started on 8000')})
所以,当我向地址http://localhost:8099/users 发出get 请求时,日志如下:
[GIN] 2021/06/30 - 22:44:05 | 500 | 4.8998ms | 172.17.0.1 | GET "/users"
Error #01: Get "http://localhost:8000/api/v1/users": dial tcp 127.0.0.1:8000: connect: connection refused
在此处的Krakend 文档中,它说当从后端返回高于 400 的状态代码时,“我猜”日志是用 500 代码编写的。
500 Internal Server Error 默认错误代码,一般情况下,当后端返回任何高于 400 的状态时
我确实玩过krakend.json 文件并更改了文件的不同部分,但仍然无法正常工作。
我知道这是一个tcp 错误,检查了导致该错误的原因。
1- 端口可能崩溃,这是不正确的,因为当我直接向端点http://localhost:8000/api/v1/users 发出获取请求时,我得到了日志。
【问题讨论】:
标签: node.js tcp microservices api-gateway krakend