![](/img/trans.png)
[英]how to communicate 2 instances inside the same VPC but different subnets (regions) using internal IP
[英]Route GraphQL requests to APIs in different regions serving different content but using same schema
我们在多个 AWS 区域用 GraphQL API 部署后端服务。 GraphQL 架构对于每个区域都是相同的,但服务提供区域特定数据。 Apollo federation 似乎没有解决这个话题,因为我们不想合并不同的模式。 我们的问题也不在于选择最近的区域。 客户端选择特定区域以访问该区域提供的数据。
使用 REST,反向代理可以根据路径参数将请求转发到目标区域。 然而,在 GraphQL 中,所有请求都针对同一个端点。
将 GraphQL 请求路由到使用相同架构但提供不同内容的不同区域的最佳做法是什么?
编辑:也许同样重要的是要注意我们也在使用 GraphQL 订阅。 如果可能的话,最好避免连接到客户端中的多个端点。
我们去了Apollo Links 。 每个后端都部署在不同的区域,提供自己的 GraphQL API 端点。
前端保留所有可用区域端点的列表并设置定向链接。 我们可以通过设置上下文变量 select 各自的链接。
import { ApolloLink, HttpLink } from '@apollo/client';
import { RetryLink } from '@apollo/client/link/retry';
const directionalLink = new RetryLink().split(
(operation) => operation.getContext().region === "us",
new HttpLink({ uri: "http://us-endpoint/v1/graphql" }),
new RetryLink().split(
(operation) => operation.getContext().region === "eu",
new HttpLink({ uri: "http://eu-endpoint/v1/graphql" }),
new HttpLink({ uri: "http://au-endpoint/v2/graphql" })
)
);
无论 REST 还是 GraphQL,我认为必须在请求到达区域服务之一之前完成全局路由。
So for example, with Route 53, you can create different subdomains us.example.com
and europe.example.com
where each directs to the corresponding regional GraphQL API.
如果你想在同一区域内跨 AZ 路由,那么你可以使用 ELB。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.