在Apollo Federation中,我面临这样的问题:每当我们对服务列表中的任何联邦服务的模式进行更改时,网关都需要重新启动。据我所知,网关每次启动时都会收集所有模式并聚合数据图。但是,有没有一种方法可以在不重新启动网关的情况下自动处理这一问题,因为它也会关闭所有其他未受影响的GraphQL联合服务
阿波罗网关,@ Apollo / GraphQL
发布于 2019-10-24 21:13:23
您可以使用一个实验性的轮询间隔:
const gateway = new ApolloGateway({
serviceList: [
{ name: "products", url: "http://localhost:4002" },
{ name: "inventory", url: "http://localhost:4001" },
{ name: "accounts", url: "http://localhost:4000" }
],
debug: true,
experimental_pollInterval:3000
})上面的代码将每3秒拉取一次
发布于 2020-10-13 11:46:28
您可以使用express刷新网关的架构。ApolloGateway有一个load()函数,用于从实现服务中获取模式。如果需要自动操作,此HTTP调用可能会成为部署过程的一部分。我不会使用轮询或其他过于自动化的东西。一旦部署了实现的服务,模式就不会改变,直到它被更新和再次部署。
import { ApolloGateway } from '@apollo/gateway';
import { ApolloServer } from 'apollo-server-express';
import express from 'express';
const gateway = new ApolloGateway({ ...config });
const server = new ApolloServer({ gateway, subscriptions: false });
const app = express();
app.post('/refreshGateway', (request, response) => {
gateway.load();
response.sendStatus(200);
});
server.applyMiddleware({ app, path: '/' });
app.listen();更新:load()函数现在在重新加载模式之前检查phase === 'initialized'。一种解决方法可能是使用gateway.loadDynamic(false)或可能更改gateway.state.phase = 'initialized';。我推荐使用loadDyamic(),因为更改状态可能会导致以后出现问题。我还没有测试这些解决方案中的任何一个,因为在这次更新时我还没有与Apollo Federation合作。
发布于 2019-10-27 22:22:44
除了轮询之外,我不知道其他自动重新加载网关的方法。我做了一个可重用的docker image,如果出现了重新加载服务的新方法,我会不断更新它。现在,您可以使用POLL_INTERVAL环境变量定期检查更改。下面是一个使用docker-compose的示例:
version: '3'
services:
a:
build: ./a # one service implementing federation
b:
build: ./b
gateway:
image: xmorse/apollo-federation-gateway
ports:
- 8000:80
environment:
CACHE_MAX_AGE: '5' # seconds
POLL_INTERVAL: '30' # seconds
URL_0: "http://a"
URL_1: "http://b"https://stackoverflow.com/questions/58139504
复制相似问题