【发布时间】: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
【问题讨论】: