我一直在尝试使用 Docker 做一个 NodeJS 微服务架构。
我目前有 2 项服务:Auth API 和用户 CRUD API。现在我的目标是使用 Express-Gateway 设置网关。
我按照网络上的许多教程尝试设置它,但是每当我尝试向网关发出请求(充当代理)时,它都会发送 502 bad gateway 响应。
我的 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