首页 > 解决方案 > Apollo Federation 无法在 docker-compose 中运行连接:无法加载服务定义...原因:连接 ECONNREFUSED 127.0.0.1:80

问题描述

我试图通过 docker-compose 运行两个应用程序但没有成功,联邦无法连接服务。两者都可以在 docker 中很好地工作。

下面关注项目:

码头工人-compose.yml

version: '3'
services:
  myservice:
    image: 'myservice:0.0.1-SNAPSHOT'
    container_name: myservice_container
    ports:
      - 8080:8080
    expose:
      - 8080
  apollo_federation:
    image: 'apollo-federation'
    build: '.'
    container_name: apollo_federation_container
    restart: always
    ports:
      - 4000:4000
    expose:
      - 4000
    environment:
      ENDPOINT: "http://myservice/graphql"
    depends_on:
      - myservice

我已经在端点 ex 中尝试了很多组合:http://myservice:8080/graphql、http://localhost:8080/graphql、http://myservice 等...

来自 Apollo 项目的 index.js

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

const gateway = new ApolloGateway({
  serviceList: [
    { name: "Service1", url: process.env.ENDPOINT || 'http://localhost:8080/graphql' },
  ]
});

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

server.listen().then(({ url }) => {
  console.log(` Server ready at ${url}`);
})

错误日志

 Error checking for changes to service definitions: Couldn't load service definitions for "Service1" at http://myservice/graphql: request to http://myservice/graphql failed, reason: connect ECONNREFUSED 172.18.0.3:80

如果我尝试从浏览中进行测试,我会得到 graphiql 的错误 500。

我已经尝试使用 nginx 作为反向代理,但没有成功

我正在使用项目中的最后一个库。

谢谢

标签: kotlindocker-composegraphqlapollo-serverapollo-federation

解决方案


在您的代码中,您正在使用

    { name: "Service1", url: process.env.ENDPOINT || 'http://localhost:8080/graphql' },

这是 pull process.env.ENDPOINT,它在您的 docker-compose 文件中定义为使用端口 80:

    environment:
      ENDPOINT: "http://myservice/graphql" # This is port 80

推荐阅读