负载均衡网络套接字 [英] Loadbalancing web sockets

查看:23
本文介绍了负载均衡网络套接字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个关于如何对网络套接字进行负载平衡的问题.

我有一个支持网络套接字的服务器.浏览器连接到我的网站,每个浏览器都会打开一个到 www.mydomain.com 的网络套接字.这样,我的社交网络应用就可以向客户端推送消息.

传统上,仅使用 HTTP 请求,我会通过在两个 Web 服务器前面添加第二个服务器和负载平衡器来扩展.

使用 web sockets,连接必须直接与 web 服务器,而不是负载平衡器,因为如果一台机器的物理限制是 64k 开放端口,并且客户端连接到负载平衡器,那么我可以'不支持超过 64k 的并发用户.

那我该怎么办-

  1. 让客户端在页面加载时直接连接到 Web 服务器(而不是负载均衡器)?我是否只是从一个节点加载 JavaScript,然后负载平衡器(或其他任何东西)在每次最初请求页面时随机修改脚本的 URL?

  2. 处理涟漪开始?当 Web 服务器关闭时,浏览器会注意到连接已关闭.我可以编写 JavaScript 代码来尝试重新打开连接,但该节点将消失一段时间.所以我想我必须回到负载均衡器来查询下一个要使用的节点的地址?

  3. 我确实想知道负载平衡器在初始请求上发送重定向,以便浏览器最初请求 www.mydomain.com 并被重定向到 www34.mydomain.com.这非常有效,直到节点出现故障 - 而像 Facebook 这样的网站不会这样做.他们是怎么做的?

解决方案

放置一个 L3 负载平衡器,根据源 IP 端口哈希将 IP 数据包分发到您的 WebSocket 服务器群.由于 L3 平衡器不保持任何状态(使用散列的源 IP 端口),它将扩展到低端硬件(例如 10GbE)上的线速.由于分布是确定性的(使用散列的源 IP 端口),它将适用于 TCP(因此也适用于 WebSocket).

另请注意,64k 硬限制仅适用于给定(源)IP 地址的传出 TCP/IP.它不适用于传入的 TCP/IP.我们已经测试了 Autobahn(高性能 WebSocket 服务器),在 2 核、4GB RAM 虚拟机上有 20 万个活动连接.>

另请注意,您可以在初始 WebSocket 握手期间宣布的 HTTP 路径上执行 L7 负载平衡.在这种情况下,负载均衡器必须保持状态(哪个源 IP 端口对将到达哪个后端节点).尽管如此,如果设置得当,它可能会扩展到数百万个连接.

免责声明:我是 Autobahn 的原作者,为 Tavendo 工作.

I have a question about how to load balance web sockets.

I have a server which supports web sockets. Browsers connect to my site and each one opens a web socket to www.mydomain.com. That way, my social network app can push messages to the clients.

Traditionally, using just HTTP requests, I would scale up by adding a second server and a load balancer in front of the two web servers.

With web sockets, the connection has to be directly with the web server, not the load balancers, because if a machine has a physical limit of say 64k open ports, and the clients were connecting to the load balancer, then I couldn't support more than 64k concurrent users.

So how do I -

  1. get the client to connect directly to the web server (rather than the load balancer) when the page loads? Do I simply load the JavaScript from a node, and the load balancers (or whatever) randomly modifies the URL for the script, every time the page is initially requested?

  2. handle a ripple start? The browser will notice that the connection is closed as the web server shuts down. I can write JavaScript code to attempt to reopen the connection, but the node will be gone for a while. So I guess I would have to go back to the load balancer to query the address of the next node to use?

  3. I did wonder about the load balancers sending a redirect on the initial request, so that the browser initially requests www.mydomain.com and gets redirected to www34.mydomain.com. That works quite well, until the node goes down - and sites like Facebook don't do that. How do they do it?

解决方案

Put a L3 load-balancer that distributes IP packets based on source-IP-port hash to your WebSocket server farm. Since the L3 balancer maintains no state (using hashed source-IP-port) it will scale to wire speed on low-end hardware (say 10GbE). Since the distribution is deterministic (using hashed source-IP-port), it will work with TCP (and hence WebSocket).

Also note that a 64k hard limit only applies to outgoing TCP/IP for a given (source) IP address. It does not apply to incoming TCP/IP. We have tested Autobahn (a high-performance WebSocket server) with 200k active connections on a 2 core, 4GB RAM VM.

Also note that you can do L7 load-balancing on the HTTP path announced during the initial WebSocket handshake. In that case the load balancer has to maintain state (which source IP-port pair is going to which backend node). It will probably scale to millions of connections nevertheless on decent setup.

Disclaimer: I am original author of Autobahn and work for Tavendo.

这篇关于负载均衡网络套接字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆