如何动态地将特定客户端(浏览器)分配给许多服务器之一?

发布于 2025-01-03 08:14:18 字数 307 浏览 1 评论 0原文

我正在构建一项服务,需要我在世界各地的许多位置动态启动和关闭服务器(例如使用 AWS)。当用户访问我的域时,需要将他们分配到延迟最低的本地服务器。

通过分配,我的意思是,例如客户端对 example.com/getData 进行 ajax 调用,它应该直接转到已分配的一台特定服务器。不同的服务器将执行不同的计算,因此仅拥有某种通用负载平衡是不够的。

什么通用机制/技术允许我 1) 评估特定客户端与我控制下的任何服务器之间的延迟? 2)将特定客户端分配给特定服务器?例如,我不能只使用 IP 地址,因为 javascript 有基于域名的限制。

谢谢

I am building a service which requires me to dynamically launch and close servers at many locations around the world, (for example using AWS). When a user visits my domain they need to be assigned to a local server with the lowest latency.

By assignment, I mean that for example the client makes an ajax call to example.com/getData, it should go directly to one particular server that is has been assigned to. Different servers will be doing different computation, so it is not sufficient to have some kind of general load balancing.

What general mechanisms/technology would allow me to 1) Assess the latency between a particular client and any server under my control? 2) Assign a particular client to a particular server? I cannot use just the IP addresses for example, since javascript has domain name based restrictions.

Thanks

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情话难免假 2025-01-10 08:14:18

注意:我没有足够的声誉来链接响应中的所有技术,因此有时您会看到以纯文本形式复制的链接。

1) 将用户分配到延迟最低的本地服务器并不总是可行。

有时,地理位置上距离用户最近的服务器意外地是延迟最高的服务器。

要找到(正在运行的)服务器和用户之间的最低延迟并不是一件容易的事。
客户端和服务器之间可能有许多不同的跃点(路由器),其中任何一个在任何时候都可能出现问题、路由更新、数据包拥塞等。
评估延迟的最快方法是 ping,但防火墙可能会阻止此操作。

所以实现这一点的最好方法是使用 anycast

所有主要的 CDN 提供商都实现了这种方法。有些使用 TCP 任播,这似乎不推荐,而其他则使用 UDP 任播。这是一场公开辩论。

无论如何,为了实现选播,您需要能够与 ISP 路由器进行对等,但通常这是不可能的。此外,还有好同伴和坏同伴。
最后,所有这些都需要对路由协议和 TCP/IP 堆栈有深入的了解。

一个快速但肮脏的解决方案可能是将 BIND 与 GEO-IP 补丁一起使用。
因此,您可以为每个国家/地区定义特定的 DNS 查询响应。
我的意思是,例如,如果您在英国有一台服务器,在美国有一台服务器,您可以配置 BIND 来响应来自欧洲的用户访问英国服务器和来自美国的用户访问美国服务器。

2) 要将特定客户端分配给特定服务器,您可以使用我在第 1 点中描述的技术,也可以使用代理和粘性会话。
HA-Proxy 是实现这一目标的一个很好的产品。 (网址:xy.1wt.eu)

3)如果使用第1点,就不会出现跨域ajax调用的问题。事实上,它对客户来说是完全透明的。例如,对于同一域 example.com,来自美国的用户会将其解析为 1.1.1.1,而来自德国的用户会将 example.com 解析为 2.2.2.2(IP 地址是假的,仅用作示例)。

顺便说一句,跨域 ajax 调用的解决方案是 JSON-P,但它有一些缺点,例如缺乏对 POST 的支持。

如果我是你,我会选择 BIND 和 GEO-IP,因为它可以一次性解决所有三个问题。 (延迟的一部分,因为并不总是地理位置最接近的服务器是延迟最低的服务器。)

Note: I do not have enough reputation to link all the technologies in the response, therefore sometimes you will see the links copied in plain text.

1) Assign users to a local server with the lowest latency is not always possible.

Sometimes the geographically closest server to a user is unexpectedly the one with the highest latency.

To find the lowest latency between your (running) servers and the users is not an easy task.
There might be many different hops (routers) between the client and the server, and any of them at any time can have problems, routes update, packet congestions and so on.
The quickest way to assess the latency is a ping, but it can be that the firewalls block this.

So the best way to achieve this is to use the anycast

All the major CDN providers implement this method. Some use the TCP anycast, which seems to be not recommended, and others UDP anycast. It is an open debate.

Anyway in order to implement anycast you need to be able to peer with the ISP routers, and normally this is not possible. Additionally there are good peers and bad peers.
Finally All this requires a deep knowledge of the routing protocols and the TCP/IP stack.

A quick and dirty solution could be to use BIND with the GEO-IP patch.
So you can define specific dns query responses per country.
What I mean is that, for instance, if you have a server in UK and one in US you can configure BIND to respond to users coming from europe to hit the UK server and users coming from US to hit the US server.

2) To assign a particular client to a particular server you can use the technique I described on the point 1 or you can use a proxy and sticky sessions.
HA-Proxy is a good product to achieve this. (the URL: xy.1wt.eu )

3) if you use the point 1, you will not have problems with cross domain ajax calls. In fact it is completely transparant for the client. For instance for the same domain example.com a user coming from US will resolve it to 1.1.1.1 whereas a user coming from Germany will resolve example.com to 2.2.2.2 (ip addresses are fake and used just as an example).

On a side note, a solution to do cross domain ajax call is JSON-P which has though some drawbacks, like the lack of support for POST.

If I were you I would go with the BIND and GEO-IP, because it would solve all three problems in once. (a part for the latency because is not always true that the geographically closest server is the one with the lowest latency.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文