【问题标题】:How to configure API gateway in microservice architecture inside docker containers如何在 docker 容器内的微服务架构中配置 API 网关
【发布时间】:2021-09-14 05:42:37
【问题描述】:

我是新手,正在使用 nodejs 构建微服务。我的问题是,当我使用 docker compose 启动容器时,我的 API 网关抛出错误。我可以直接向我的服务容器发送请求,并且 API 正在响应。但是当我通过 API 网关转发它们时,我遇到了问题。 强调文字

以下是我在 apiGateway 目录中的文件: 泊坞窗文件:

FROM devopsfaith/krakend

COPY krakend.json /etc/krakend/

这是一个专门为此网关构建并放置在 github 上的图像。

{
  "version": 2,
  "extra_config": {
    "github_com/devopsfaith/krakend-cors": {
      "allow_origins": ["*"],
      "allow_headers": ["*"],
      "expose_headers": ["Content-Length"],
      "max_age": "12h",
      "allow_methods": ["GET", "HEAD", "POST"]
    }
  },
  "timeout": "3000ms",
  "cache_ttl": "300s",
  "output_encoding": "json",
  "name": "Gateway",
  "port": 3001,
  "endpoints": [
    {
      "endpoint": "/auth/signup-customer",
      "method": "POST",
      "output_encoding": "json",
      "extra_config": {},
      "backend": [
        {
          "url_pattern": "/auth/signup-customer",
          "host": ["http://auth-service:4000"],
          "method": "POST"
        }
      ]
    },
    {
      "endpoint": "/auth/signup-restaurant",
      "method": "POST",
      "output_encoding": "json",
      "extra_config": {},
      "backend": [
        {
          "url_pattern": "/auth/signup-restaurant",
          "host": ["http://auth-service:4000"],
          "method": "POST"
        }
      ]
    },
    {
      "endpoint": "/auth/login",
      "method": "POST",
      "output_encoding": "json",
      "extra_config": {},
      "backend": [
        {
          "url_pattern": "/auth/login",
          "host": ["http://auth-service:4000"],
          "method": "POST"
        }
      ]
    },
    {
      "endpoint": "/auth/check",
      "method": "GET",
      "output_encoding": "json",
      "extra_config": {},
      "backend": [
        {
          "url_pattern": "/auth/check",
          "host": ["http://auth-service:4000"],
          "method": "GET"
        }
      ]
    },
    {
      "endpoint": "/products/_search",
      "method": "POST",
      "output_encoding": "json",
      "extra_config": {},
      "backend": [
        {
          "url_pattern": "/products/_search",
          "host": ["http://elasticsearch:9200"],
          "method": "POST"
        }
      ]
    },
    {
      "endpoint": "/health",
      "method": "GET",
      "output_encoding": "json",
      "extra_config": {
        "github.com/devopsfaith/krakend/proxy": {
          "static": {
            "data": {
              "status": "OK"
            },
            "strategy": "always"
          }
        }
      },
      "backend": [
        {
          "url_pattern": "/fake",
          "encoding": "json",
          "sd": "static",
          "method": "GET",
          "extra_config": {}
        }
      ]
    }
  ]
}

上面的代码是一个带有我的端点的 json 文件。目前我只是测试身份验证 api 端点。

身份验证服务 泊坞窗文件:

FROM node:14

# Create app directory
WORKDIR /app

# Install app dependencies
# A wildcard is used to ensure both package.json AND package-lock.json are copied
# where available (npm@5+)
COPY package*.json ./

RUN npm install
# If you are building your code for production
# RUN npm ci --only=production

# Bundle app source
COPY . .

EXPOSE 4000
CMD ["npm","start"]

Docker 撰写文件

version: "3"
services:
  api-gateway:
    build: "./gateway"
    ports:
      - 3001:3001

  auth-service:
    build: "./authService"
    ports:
      - 4000:4000
    environment:
      - DB_URL = mongodb://db/MagicMeal
    volumes:
      - ./authService:/app

  item-service:
    build: "./itemService"
    ports:
      - 5000:5000
    environment:
      - DB_URL = mongodb://db/MagicMeal
    volumes:
      - ./itemService:/app

  user-service:
    build: "./userService"
    ports:
      - 7000:7000
    environment:
      - DB_URL = mongodb://db/MagicMeal
    volumes:
      - ./userService:/app
#   db:
#     image: mongo:latest
#     ports:
#       - 27017:27017
#     volumes:
#       - ./MagicMeal:/data/db

# volumes:
#   MagicMeal:

下面是错误图片 在图像中,您可以看到一个错误,指出连接拒绝。现在,当我使用以下 url 调用我的 api 时出现此错误:“http://localhost:3001/auth/check”,因为这是我的 api 网关地址,它将请求重新路由到“localhost:4000/auth/check”。我的网关在端口 3001 上,我的 aut 服务在端口 4000 上。但如果我只是点击“localhost:4000/auth/check”,我会得到响应。但是通过门发起的相同请求会出错 click here to open the error image

【问题讨论】:

    标签: node.js docker-compose


    【解决方案1】:

    您应该将服务名称和服务公开端口(均在docker-comose.yml 中定义)放在 api 网关端点配置中。例如,auth-service:4000 而不是 localhost:4000

    当您成功连接到localhost:4000 时,您将连接到您的 docker 主机(您的机器,vm),它将请求直接路由到容器。

    【讨论】:

    • 还是同样的问题
    • 请将更新后的 api 网关配置添加到问题中,以便您的问题可以重现
    • 我更新了配置文件。请看一看。
    • 我能够解决这个问题。根据您告诉我的相同命名约定重新配置 docker compose 文件后,错误仍然存​​在,因为由于某种原因,当我运行 docker compose 时,它​​没有重建图像并使用旧图像。所以,我删除了当前图像并重新构建它并开始撰写。它就像一个魅力。谢谢。
    • 很高兴您解决了这个问题。要在运行的容器中使用 node.js 代码的实时重新加载,请参阅this 答案
    猜你喜欢
    • 2017-03-19
    • 1970-01-01
    • 2021-01-01
    • 2019-04-05
    • 2019-08-13
    • 1970-01-01
    • 2017-10-11
    • 2018-06-26
    • 2017-04-06
    相关资源
    最近更新 更多