首页 > 解决方案 > Krakend api-gateway的连接被拒绝错误?

问题描述

我想看看并尝试api-gateway我正在使用的具体工作方式Krakend。从技术上讲,我有一项服务,它只接收请求并回复。api-gateway问题是服务是一个非常简单的 http-server在相关端点被触发时没有接收到请求api-gateway。但是,当我通过浏览器发出获取请求并写入确切的 urlhttp://localhost:8000/api/v1/users时,我得到了响应。基本上,我想要的是,当我向localhost:8099/users哪个是api-gateway我希望 api-gateway 向用户 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')})

因此,当我向该地址发出 get 请求时,http://localhost:8099/users日志如下:

[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.jstcpmicroservicesapi-gatewaykrakend

解决方案


对于每个服务,在 中声明的容器docker-compose.yml名称应与文件中写入的名称匹配krakend.json。例如,user-service也应该像在字段中一样在krakend.json文件中声明。http://user-service:$PORThost


推荐阅读