[英]Express-Gateway - 502 Bad Gateway with docker-compose Nodejs Microservices Architecture
我一直在嘗試使用 Docker 做一個 NodeJS 微服務架構。
我目前有 2 項服務:Auth API 和用戶 CRUD API。 現在我的目標是使用 Express-Gateway 設置網關。
我按照 web 上的許多教程嘗試設置它,但是每當我嘗試向網關發出請求(充當代理)時,它都會發送 502 錯誤網關響應。
我的 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
問題出在您的 gateway-config.xml 文件中。 它沒有正確引用 docker-compose.xml 中定義的端口。
docker-compose.xml 端口命令是 HOST:CONTAINER,因此對於用戶容器,主機所指的 3001 是 ZC5FD214CDD0D2B3B4272E73B02BA2 中的端口 3000。 Express Gateway 在 Docker 中運行,因此服務端點需要引用端口,因為它們出現在其他容器中(它們由 docker-compose.Z0F635D0E0F3874FFF8B581C132E6CA7 中定義的內部主機名區分,而不是在此文件級別:
serviceEndpoints:
users:
url: http://users:3000
auth:
url: http://auth:3000
如果您想從 Docker 外部訪問這些 URL,您需要指定端口(3001、3002 等)。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.