首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Socket.io和Redis的节点集群问题

使用Socket.io和Redis的节点集群问题
EN

Stack Overflow用户
提问于 2016-02-06 04:11:58
回答 1查看 813关注 0票数 0

好的,我有一个快速驱动的应用程序接口,其中我也运行了socket.io来接收/发送实时events...all工作。我需要集群我的应用程序。我根据下面的代码设置了所有内容。我启动了工人,他们得到了连接,一切都正常工作,除了我现在不能“爆炸”到所有的socket.io连接。下面是设置(取自this):

代码语言:javascript
复制
var express = require('express'),
    cluster = require('cluster'),
    net = require('net'),
    sio = require('socket.io'),
    sio_redis = require('socket.io-redis');

var port = 3000,
    num_processes = require('os').cpus().length;

if (cluster.isMaster) {
    // This stores our workers. We need to keep them to be able to reference
    // them based on source IP address. It's also useful for auto-restart,
    // for example.
    var workers = [];

    // Helper function for spawning worker at index 'i'.
    var spawn = function(i) {
        workers[i] = cluster.fork();

        // Optional: Restart worker on exit
        workers[i].on('exit', function(worker, code, signal) {
            console.log('respawning worker', i);
            spawn(i);
        });
    };

    // Spawn workers.
    for (var i = 0; i < num_processes; i++) {
        spawn(i);
    }

    // Helper function for getting a worker index based on IP address.
    // This is a hot path so it should be really fast. The way it works
    // is by converting the IP address to a number by removing the dots,
    // then compressing it to the number of slots we have.
    //
    // Compared against "real" hashing (from the sticky-session code) and
    // "real" IP number conversion, this function is on par in terms of
    // worker index distribution only much faster.
    var workerIndex = function (ip, len) {
    var _ip = ip.split(/['.'|':']/),
        arr = [];

    for (el in _ip) {
        if (_ip[el] == '') {
            arr.push(0);
        }
        else {
            arr.push(parseInt(_ip[el], 16));
        }
    }

    return Number(arr.join('')) % len;
}

    // Create the outside facing server listening on our port.
    var server = net.createServer({ pauseOnConnect: true }, function(connection) {
        // We received a connection and need to pass it to the appropriate
        // worker. Get the worker for this connection's source IP and pass
        // it the connection.
        var worker = workers[worker_index(connection.remoteAddress, num_processes)];
        worker.send('sticky-session:connection', connection);
    }).listen(port);
} else {
    // Note we don't use a port here because the master listens on it for us.
    var app = new express();

    // Here you might use middleware, attach routes, etc.

    // Don't expose our internal server to the outside.
    var server = app.listen(0, 'localhost'),
        io = sio(server);

    // Tell Socket.IO to use the redis adapter. By default, the redis
    // server is assumed to be on localhost:6379. You don't have to
    // specify them explicitly unless you want to change them.
    io.adapter(sio_redis({ host: 'localhost', port: 6379 }));

    // Here you might use Socket.IO middleware for authorization etc.

    // Listen to messages sent from the master. Ignore everything else.
    process.on('message', function(message, connection) {
        if (message !== 'sticky-session:connection') {
            return;
        }

        // Emulate a connection event on the server by emitting the
        // event with the connection the master sent us.
        server.emit('connection', connection);

        connection.resume();
    });
}

所以我从不同的机器连接来测试并发性,工作人员做了他们的事情,一切都很好,但是当我得到一个IO连接时,我记录了总的“连接”计数,它总是每个实例1。我需要一种方式来说

代码语言:javascript
复制
allClusterForks.emit(stuff)

我在正确的worker pid上获得了连接,但是"ALL CONNECTIONS“总是返回1。

代码语言:javascript
复制
io.on('connection', function(socket) {
    console.log('Connected to worker %s', process.pid);
    console.log("Adapter ROOMS %s ", io.sockets.adapter.rooms);
    console.log("Adapter SIDS %s ", io.sockets.adapter.sids);
    console.log("SOCKETS CONNECTED %s ", Object.keys(io.sockets.connected).length);
});

我可以看到订阅/取消订阅正在使用Redis监视器

代码语言:javascript
复制
1454701383.188231 [0 127.0.0.1:63150] "subscribe" "socket.io#/#gXJscUUuVQGzsYJfAAAA#"
1454701419.130100 [0 127.0.0.1:63167] "subscribe" "socket.io#/#geYSvYSd5zASi7egAAAA#"
1454701433.842727 [0 127.0.0.1:63167] "unsubscribe" "socket.io#/#geYSvYSd5zASi7egAAAA#"
1454701444.630427 [0 127.0.0.1:63150] "unsubscribe" "socket.io#/#gXJscUUuVQGzsYJfAAAA#"

这些是来自两台不同机器的连接,我期望通过使用socket io redis适配器,这些订阅将进入相同的redis连接,但它们是不同的。

我是不是完全漏掉了什么?令人惊讶的是,关于这方面的文档/文章还没有完全过时/错误/模棱两可。

编辑: Node v5.3.0 Redis v3.0.6 Socket.io v1.3.7

EN

回答 1

Stack Overflow用户

发布于 2016-02-06 06:38:15

因此,如果有人遇到这种情况,我认为实际上“查看”跨进程的连接套接字的数量不是一件事,但广播或发送给它们是一件事。所以我基本上一直在无缘无故地“测试”。所有操作都如预期的那样工作。我将重写socket.io-redis适配器,以允许跨进程检查计数。

几年前,有一个拉取请求,以实现对我正在尝试做的事情的支持。https://github.com/socketio/socket.io-redis/pull/15和我可能会试着清理一下,然后重新提交。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35232703

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档