socket.io 的可扩展架构 [英] Scalable architecture for socket.io

查看:49
本文介绍了socket.io 的可扩展架构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 socket.io 和 Node JS 的新手,我正在尝试构建一个具有大量并发套接字连接(10,000+)的可扩展应用程序.

I am new to socket.io and Node JS and I am trying to build a scalable application with a high number of simultaneous socket connections (10,000+).

目前,我开始使用我的服务器创建子进程的模型,每个子进程都侦听一个附加了sicket.io 实例的特定端口.客户端连接后,他将被重定向到特定端口.

Currently, I started on a model where my server creates child process, and every child process listens a specific port with a sicket.io instance attached. Once a client connects, he is redirected on a specific port.

最大的问题是:在多个端口上拥有多个 socket.io 实例是否会增加可能的连接数?

这是我的代码,以防万一:

Here is my code, just in case :

服务器

var server = http.createServer(app);

server.childList = [];
for (var i = 0; i < app.portList.length; i++) {
  server.childList[i] = require('child_process').fork('child.js');
}

server.listen(3443, () => {
  for (var i = 0; i < app.portList.length; i++) {
    server.childList[i].send({ message: 'createServer', port: app.portList[i] });;
  }
});

child.js :

var app = require('./app');
var http = require('http');
var socket_io        = require( "socket.io" );

process.on('message', (m) => {
    if (m.message === 'createServer') {

        var childServ = http.createServer(app);

        childServ.listen(m.port, () => {
            console.log("childServ listening on port "+m.port);
        });

        var io = socket_io();
        io.attach( childServ );

        io.sockets.on('connection', function (socket) {
            console.log("A client just connected to my socket_io server on port "+m.port);
        });
    }
});

如果我在那里做了什么可怕的事情,请随意释放海妖

Feel free to release the kraken if I did something horrible there

推荐答案

首先,您需要优化的内容取决于您的 socket.io 连接的繁忙程度,以及 Activity 主要是异步 I/O 操作还是 CPU- 密集的东西.您可能已经知道,node.js 对于异步 I/O 的东西已经很好地扩展了,但是它需要多个进程才能很好地扩展到 CPU 密集型的东西.此外,在某些情况下,垃圾收集器会变得太忙(正在处理大量的小请求),因此您还需要转到多个进程.

First off, what you need to optimize depends on how busy your socket.io connections are and whether the activity is mostly asynchronous I/O operations or whether it's CPU-intensive stuff. As you may already know, node.js scales really well already for asynchronous I/O stuff, but it needs multiple processes to scale well for CPU-intensive stuff. Further, there are some situations where the garbage collector gets too busy (lots and lots of small requests being served) and you also need to go to multiple processes for that reason.

更多服务器实例(至少达到您在服务器中拥有的 CPU 数量)将为您提供更多 CPU 处理能力(如果这是您的需要).如果它们中的大多数都处于空闲状态,它不一定会增加您可以在一个盒子上支持的最大连接数.为此,您必须自定义调整您的服务器 支持大量连接.

More server instances (up to at least the number of CPUs you have in the server) will give you more CPU processing power (if that's what you need). It won't necessarily increase the number of max connections you can support on a box if most of them are idle. For that, you have to custom tune your server to support lots and lots of connections.

通常,您不希望 N 个 socket.io 服务器都侦听不同的端口.这给客户端带来了以某种方式选择端口的负担,并且客户端必须确切地知道要选择哪些端口(例如,您拥有多少个服务器实例).

Usually, you would NOT want N socket.io servers each listening on a different port. That puts the burden on the clients to somehow select a port and the client has to know exactly what ports to choose from (e.g. how many server instances you have).

通常,您不会这样做.通常,您有 N 个进程都在同一个端口上侦听,并且您使用某种负载均衡器在它们之间分配负载.这使得服务器基础设施对客户端透明,这意味着您可以在不改变客户端行为的情况下扩展或缩小服务器.事实上,您甚至可以添加多个物理服务器盒并通过这种方式进一步增加容量.

Usually, you don't do it this way. Usually, you have N processes all listening on the same port and you use some sort of loadbalancer to distribute the load among them. This makes the server infrastructure transparent to the clients which means you can scale the servers up or down without changing the client behavior at all. In fact, you can even add more than one physical server box and increase capacity even further that way.

这是 socket.io 文档中关于使用多个节点和负载均衡器来增加容量的文章:Socket.io - 使用多个节点.redis 还明确支持多个 socket.io 实例的组合 和 redis,因此您无论进程如何,都可以与任何 socket.io 实例通信.

Here's an article from the socket.io doc on using multiple nodes with a load balancer to increase capacity: Socket.io - using multiple nodes. There's also explicit support by redis for a combination of multiple socket.io instances and redis so you can communicate with any socket.io instance regardless of process.

这篇关于socket.io 的可扩展架构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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