简体   繁体   English

Express-Gateway - 带有 docker-compose Nodejs 微服务架构的 502 错误网关

[英]Express-Gateway - 502 Bad Gateway with docker-compose Nodejs Microservices Architecture

I've been trying to do a NodeJS Microservices Architecture using Docker.我一直在尝试使用 Docker 做一个 NodeJS 微服务架构。

I currently have 2 services: Auth API et Users CRUD API.我目前有 2 项服务:Auth API 和用户 CRUD API。 Now my goal is to setup a Gateway using Express-Gateway.现在我的目标是使用 Express-Gateway 设置网关。

I followed many tutorials on the web to try to set it up but whenever I try to make a request to the gateway (acting like a proxy) it sends a 502 bad gateway response..我按照 web 上的许多教程尝试设置它,但是每当我尝试向网关发出请求(充当代理)时,它都会发送 502 错误网关响应。

response error in PostMan PostMan 中的响应错误

error in express-gateway logs快速网关日志中的错误

My docker-compose.yml:我的 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

And my gateway.config.yml:还有我的 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

If you need further details there's a github repo: https://github.com/KIVTVN/goodfood/tree/master如果您需要更多详细信息,请参阅 github 回购: https://github.com/KIVTVN/goodfood/tree/master

The problem lies in your gateway-config.xml file.问题出在您的 gateway-config.xml 文件中。 It is not referring correctly to the ports defined in docker-compose.xml.它没有正确引用 docker-compose.xml 中定义的端口。

The docker-compose.xml ports command is HOST:CONTAINER, so what the host refers to as 3001 for the users container, is port 3000 within the Docker. docker-compose.xml 端口命令是 HOST:CONTAINER,因此对于用户容器,主机所指的 3001 是 ZC5FD214CDD0D2B3B4272E73B02BA2 中的端口 3000。 Express Gateway is running in Docker, so the service endpoints need to refer to the ports as they appear to other containers (they are distinguished by internal hostname as defined in the docker-compose.xml file rather than by port at this level): Express Gateway 在 Docker 中运行,因此服务端点需要引用端口,因为它们出现在其他容器中(它们由 docker-compose.Z0F635D0E0F3874FFF8B581C132E6CA7 中定义的内部主机名区分,而不是在此文件级别:

serviceEndpoints:
  users:
    url: http://users:3000
  auth:
    url: http://auth:3000

If you want to hit these URLs from outside Docker, you'll need to specify the port (3001, 3002, etc).如果您想从 Docker 外部访问这些 URL,您需要指定端口(3001、3002 等)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM