Apollo Gateway 在 docker-compose 内部不工作

Apollo Gateway not working inside docker-compose

我正在尝试使用 Docker 和 docker-compose 设置一个简单的联合 Apollo 网关,但似乎无法让网关连接到模式服务。

这是 docker-compose.yml 文件

version: "3.9"
services:
  gateway:
    build: ./api-gateway
    ports:
      - 4000:8080
    depends_on:
      - robots
      - sitemaps
    environment:
      APOLLO_KEY: ${APOLLO_KEY}
    volumes:
      - ../secrets/credentials.json:/tmp/key/credentials.json
      - ./api-gateway/index.js:/usr/src/app/index.js
  
  robots:
    build: ./api-robots
    ports:
      - 4001:8080
    environment:
      GOOGLE_APPLICATION_CREDENTIALS: /tmp/key/credentials.json
    volumes:
      - ../secrets/credentials.json:/tmp/key/credentials.json
      - ./api-robots/src:/usr/src/app/src


  sitemaps:
    build: ./api-sitemaps
    ports:
      - 4002:8080
    environment:
      GOOGLE_APPLICATION_CREDENTIALS: /tmp/key/credentials.json
    volumes:
      - ../secrets/credentials.json:/tmp/key/credentials.json
      - ./api-sitemaps/src:/usr/src/app/src

在网关代码中,我使用以下方法注册 robotssitemaps 服务:

const { ApolloServer } = require("apollo-server");
const { ApolloGateway } = require("@apollo/gateway");

// Initialize an ApolloGateway instance and pass it an array of
// the implementing service names and URLs
const gateway = new ApolloGateway({
  serviceList: [
    { name: "robots", url: "http://robots:4001" },
    { name: "sitemaps", url: "http://sitemaps:4002" },
  ],
});

const server = new ApolloServer({
  gateway,
  subscriptions: false,
});

server
  .listen(8080)
  .then(({ url }) => {
    console.log(` Gateway Server ready at ${url}`);
  })
  .catch((err) => {
    console.error("Failed to start Gateway");
  });

然而,当它运行时,我收到网关服务报告的以下错误:

gateway_1   | Error checking for changes to service definitions: Couldn't load service definitions for "robots" at http://robots:4001: request to http://robots:4001/ failed, reason: connect ECONNREFUSED 172.19.0.3:4001
gateway_1   | This data graph is missing a valid configuration. Couldn't load service definitions for "robots" at http://robots:4001: request to http://robots:4001/ failed, reason: connect ECONNREFUSED 172.19.0.3:4001

我知道这些服务工作正常,因为我能够分别从位于 http://localhost:4001http://localhost:4002 的主机连接到 robotssitemaps 服务;两者都没有任何问题。

我已经阅读了无数关于此的话题,我发现最常见的问题是其他人错误地尝试连接 localhost 而不是使用服务名称(例如 robotssitemaps) 作为域名。我没有犯那个错误。

这是我尝试过的其他一些方法,但也没有用...

我做错了什么?

您应该请求端口 8080 而不是 4001,仅此而已。应该是 http://robots:8080http://sitemaps:8080