Express-Gateway - 502 Bad Gateway with docker-compose Nodejs 微服务架构
Express-Gateway - 502 Bad Gateway with docker-compose Nodejs Microservices Architecture
我一直在尝试使用 Docker 来构建 NodeJS 微服务架构。
我目前有 2 项服务:Auth API 和 Users CRUD API。现在我的目标是使用 Express-Gateway 设置网关。
我按照网上的许多教程尝试设置它,但每当我尝试向网关发出请求(充当代理)时,它都会发送 502 错误的网关响应..
response error in PostMan
error in express-gateway logs
我的docker-compose.yml:
networks:
goodfood:
driver: bridge
services:
gateway:
container_name: gateway
image: 'node:17-alpine'
# env_file:
# - ./gateway/.env
working_dir: /usr/src/app
volumes:
- './gateway:/usr/src/app'
command: npm run dev
ports:
- '8080:8080'
networks:
- goodfood
auth:
container_name: auth
image: 'node:17-alpine'
# env_file:
# - ./auth/.env
working_dir: /usr/src/app
volumes:
- './auth:/usr/src/app'
command: npm run dev
ports:
- '3002:3000'
networks:
- goodfood
users:
container_name: users
image: 'node:17-alpine'
env_file:
- ./users/api/.env
working_dir: /usr/src/app
volumes:
- './users/api:/usr/src/app'
command: npm run dev
ports:
- '3001:3000'
networks:
- goodfood
depends_on:
- users-db
users-db:
container_name: users-db
image: postgres
restart: always
env_file:
- ./users/db/.env
volumes:
- './users/db/data:/var/lib/postgresql/data'
- './users/db/scripts/init.sql:/docker-entrypoint-initdb.d/init.sql'
ports:
- '5432:5432'
networks:
- goodfood
users-adminer:
container_name: users-adminer
restart: unless-stopped
image: adminer
ports:
- '8181:8080'
networks:
- goodfood
depends_on:
- users-db
还有我的 gateway.config.yml :
http:
port: 8080
admin:
port: 9876
host: localhost
apiEndpoints:
users:
path: ['/users', '/users/*']
auth:
path: ['/auth', '/auth/*']
serviceEndpoints:
users:
url: 'http://users:3001'
auth:
url: 'http://auth:3002'
policies:
- log
- proxy
# - jwt
# - request-transformer
pipelines:
authPipeline:
apiEndpoints:
- auth
policies:
- log:
action:
message: 'auth ${req.method}'
- proxy:
- action:
serviceEndpoint: auth
changeOrigin: true
usersPipeline:
apiEndpoints:
- users
policies:
- log:
action:
message: 'users ${req.method}'
- proxy:
- action:
serviceEndpoint: users
changeOrigin: true
# - jwt:
# action:
# secretOrPublicKey: 'goodfood'
# checkCredentialExistence: false
# - request-transformer:
# action:
# body:
# add:
# user: req.user
如果您需要更多详细信息,可以使用 github 存储库:https://github.com/KIVTVN/goodfood/tree/master
问题出在您的网关-config.xml文件中。它没有正确引用 docker-compose.xml.
中定义的端口
docker-compose.xml ports命令是HOST:CONTAINER,所以主机指的是用户容器的3001,是Docker中的端口3000。 Express Gateway 在 Docker 中是 运行,因此服务端点需要引用端口,因为它们出现在其他容器中(它们由 docker-[= 中定义的内部主机名区分) 20=] 文件而不是这个级别的端口):
serviceEndpoints:
users:
url: http://users:3000
auth:
url: http://auth:3000
如果您想从 Docker 外部访问这些 URL,您需要指定端口(3001、3002 等)。
我一直在尝试使用 Docker 来构建 NodeJS 微服务架构。
我目前有 2 项服务:Auth API 和 Users CRUD API。现在我的目标是使用 Express-Gateway 设置网关。
我按照网上的许多教程尝试设置它,但每当我尝试向网关发出请求(充当代理)时,它都会发送 502 错误的网关响应..
response error in PostMan
error in express-gateway logs
我的docker-compose.yml:
networks:
goodfood:
driver: bridge
services:
gateway:
container_name: gateway
image: 'node:17-alpine'
# env_file:
# - ./gateway/.env
working_dir: /usr/src/app
volumes:
- './gateway:/usr/src/app'
command: npm run dev
ports:
- '8080:8080'
networks:
- goodfood
auth:
container_name: auth
image: 'node:17-alpine'
# env_file:
# - ./auth/.env
working_dir: /usr/src/app
volumes:
- './auth:/usr/src/app'
command: npm run dev
ports:
- '3002:3000'
networks:
- goodfood
users:
container_name: users
image: 'node:17-alpine'
env_file:
- ./users/api/.env
working_dir: /usr/src/app
volumes:
- './users/api:/usr/src/app'
command: npm run dev
ports:
- '3001:3000'
networks:
- goodfood
depends_on:
- users-db
users-db:
container_name: users-db
image: postgres
restart: always
env_file:
- ./users/db/.env
volumes:
- './users/db/data:/var/lib/postgresql/data'
- './users/db/scripts/init.sql:/docker-entrypoint-initdb.d/init.sql'
ports:
- '5432:5432'
networks:
- goodfood
users-adminer:
container_name: users-adminer
restart: unless-stopped
image: adminer
ports:
- '8181:8080'
networks:
- goodfood
depends_on:
- users-db
还有我的 gateway.config.yml :
http:
port: 8080
admin:
port: 9876
host: localhost
apiEndpoints:
users:
path: ['/users', '/users/*']
auth:
path: ['/auth', '/auth/*']
serviceEndpoints:
users:
url: 'http://users:3001'
auth:
url: 'http://auth:3002'
policies:
- log
- proxy
# - jwt
# - request-transformer
pipelines:
authPipeline:
apiEndpoints:
- auth
policies:
- log:
action:
message: 'auth ${req.method}'
- proxy:
- action:
serviceEndpoint: auth
changeOrigin: true
usersPipeline:
apiEndpoints:
- users
policies:
- log:
action:
message: 'users ${req.method}'
- proxy:
- action:
serviceEndpoint: users
changeOrigin: true
# - jwt:
# action:
# secretOrPublicKey: 'goodfood'
# checkCredentialExistence: false
# - request-transformer:
# action:
# body:
# add:
# user: req.user
如果您需要更多详细信息,可以使用 github 存储库:https://github.com/KIVTVN/goodfood/tree/master
问题出在您的网关-config.xml文件中。它没有正确引用 docker-compose.xml.
中定义的端口docker-compose.xml ports命令是HOST:CONTAINER,所以主机指的是用户容器的3001,是Docker中的端口3000。 Express Gateway 在 Docker 中是 运行,因此服务端点需要引用端口,因为它们出现在其他容器中(它们由 docker-[= 中定义的内部主机名区分) 20=] 文件而不是这个级别的端口):
serviceEndpoints:
users:
url: http://users:3000
auth:
url: http://auth:3000
如果您想从 Docker 外部访问这些 URL,您需要指定端口(3001、3002 等)。