首页 > 解决方案 > 当我使用 docker-compose 运行时,Express 网关总是返回 Bad Gateway

问题描述

我尝试使用 Express Gateway 和 Docker 作为每个微服务的容器。到目前为止,它在本地测试方面表现非常出色。通过实现 EG 使用 Docker 时出现问题。我不知道问题出在哪里。LOG_LEVEL = debug 处没有完整的描述。以前我使用没有 EG 的 docker,然后与其他微服务通信就好了。请帮忙。

网关.config.yml

http:
  port: 8080
admin:
  port: 9876
apiEndpoints:
  authAPI:
    host: '*' # i tried with egateway, localhost, 127.0.0.1 but still not working
    paths:
      - '/users/signin'
      - '/users/signup'
      - '/users/oauth/facebook'
      - '/users/auth/google'
      - '/users/auth/google/callback'
      - '/users/secret'
      - '/users/get-user-google'
      - '/dashboard'
      - '/test'
  crudAPI:
    host: '*'
    paths:
      - '/users/get-user-data'
      - '/users/delete-user-data'
      - '/users/add-user-data'
      - '/users/get-one-user-data/*'
      - '/users/update-user-data'
      - '/users/update-pass-user-data'
serviceEndpoints:
  authService:
    url: 'http://localhost:3003'
  crudService:
    url: 'http://localhost:3004'
policies:
  - log
  - proxy
  - jwt
  - cors
  - jwt-custom1
pipelines:
  crud:
    apiEndpoints:
      - crudAPI
    policies:
      - cors:
          - action:
              origin: '*'
              methods: 'GET, POST, PUT, DELETE'
              allowedHeaders: 'X-Requested-With,content-type,Authorization'
              credentials: true
      - proxy:
          - action:
              serviceEndpoint: crudService
  auth:
    apiEndpoints:
      - authAPI
    policies:
      - proxy:
          - action:
              serviceEndpoint: authService


码头工人-compose.yml

version: '3'

services:

  egateway:
    hostname: egateway
    container_name: egateway
    build: './test-eg2'
    ports: 
      - "8080:8080"
    depends_on: 
      - mongo
    environment: 
      - LOG_LEVEL=debug

  api-auth:
    container_name: api-auth
    build: './api-auth'
    ports: 
      - "3003:3003"
    links: 
      - mongo
      - egateway

  api-crud:
    container_name: api-crud
    build: './api-crud'
    ports: 
      - "3004:3004"
    links: 
      - mongo
      - egateway

  mongo:
    container_name: mongo-dbx
    image: mongo
    ports: 
      - "27017:27017"

日志级别的结果 = 调试 在此处输入图像描述

我需要你的帮助。感谢您查看此问题。

标签: dockerdocker-composeexpress-gateway

解决方案


您的 auth-service 和 crud-service 的 URL 不应该是 localhost。它应该是:

serviceEndpoints:
  authService:
    url: 'http://api-auth:3003'
  crudService:
    url: 'http://api-crud:3004'

api-auth并且api-crud将是 docker-compose 网络中这两个容器的主机名。


推荐阅读