【发布时间】:2021-05-29 07:34:00
【问题描述】:
我正在尝试使用 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
在网关代码中,我使用以下代码注册 robots 和 sitemaps 服务:
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:4001 和http://localhost:4002 的主机连接到robots 和sitemaps 服务;并且两者都可以正常工作。
我已经阅读了无数关于此的主题,我发现最常见的问题是其他人错误地尝试连接localhost,而不是使用服务名称(例如robots 和sitemaps)作为域姓名。我没有犯这个错误。
这是我尝试过的其他一些事情,但也没有奏效......
- 创建自定义
networks定义并将其分配给每个服务 - 连接到
http://robots:8080和http://sitemaps:8080 - 连接到
http://localhost:4001和http://localhost:4002
我做错了什么?
【问题讨论】:
-
嗯,网络可能是最好的选择...与某些防火墙设置相关的问题...登录网关并检查对服务的 ping...可能正确解析了地址(您可以尝试@987654340 @ 如果没有),然后尝试 curl 一些请求......服务上不需要
/graphql端点(url)? -
如果容器内的进程正在侦听端口 8080,您需要在 URL 中使用该端口号。在容器之间建立连接时,不考虑(或要求)
ports:。 -
ra9r,你在 mac 上吗?如果您可以通过
http://localhost:4001连接,您为什么要尝试通过http://robots:4001连接?另一个想法 - 你的 docker-compose.yaml 中缺少 container names。所以真正的主机名(容器名)不会是robots,而是像folder_robots_1这样的随机名称(可通过docker ps检查)
标签: docker docker-compose graphql microservices apollo-server