node.js - 负载平衡 : Node. js - Socket.io - Redis

标签 node.js express redis socket.io

我有 3 个运行 NodeJ 的服务器,它们与 Redis 相互关联(1 个主服务器,2 个从服务器)。

我遇到的问题是,在单个服务器上运行系统工作正常,但是当我将它扩展到 3 个 NodeJS 服务器时,它开始丢失消息并且系统变得不稳定。

我的负载均衡器不接受粘性 session 。因此每次来自客户端的请求到达它时,它们都可以转到不同的服务器。

我将所有 NodeJS 服务器指向 Redis Master。

看起来 socket.io 正在每个服务器上存储信息,而不是使用 redis 进行分发。

我正在使用 socket.io V9,我怀疑我没有任何握手代码,这可能是原因吗?

我配置 socket.io 的代码是:

var express = require('express');
var io = require('socket.io');
var redis = require('socket.io/node_modules/redis');
var RedisStore = require('socket.io/lib/stores/redis');

var pub = redis.createClient("a port", "an ip");
var sub = redis.createClient("a port", "an ip");
var client = redis.createClient("a port", "an ip");
var events = require('./modules/eventHandler');

exports.createServer = function createServer() {
    var app = express();
    var server = app.listen(80);
    var socketIO = io.listen(server);

    socketIO.configure(function () {
        socketIO.set('store', new RedisStore({
            redisPub: pub,
            redisSub: sub,
            redisClient: client
        }));
        socketIO.set('resource', '/chat/socket.io');
        socketIO.set('log level', 0);
        socketIO.set('transports', [, 'htmlfile', 'xhr-polling', 'jsonp-polling']);
    });

    // attach event handlers
    events.attachHandlers(socketIO);

    // return server instance
    return server;
};

最佳答案

Redis 只从主服务器同步到从服务器。它从不从奴隶同步到主人。因此,如果您要写入所有 3 台机器,那么最终将在所有三台服务器上同步的唯一消息将是那些到达主服务器的消息。这就是为什么您看起来缺少消息的原因。

更多信息 here .

Read only slave

Since Redis 2.6 slaves support a read-only mode that is enabled by default. This behavior is controlled by the slave-read-only option in the redis.conf file, and can be enabled and disabled at runtime using CONFIG SET.

Read only slaves will reject all the write commands, so that it is not possible to write to a slave because of a mistake. This does not mean that the feature is conceived to expose a slave instance to the internet or more generally to a network where untrusted clients exist, because administrative commands like DEBUG or CONFIG are still enabled. However security of read-only instances can be improved disabling commands in redis.conf using the rename-command directive.

You may wonder why it is possible to revert the default and have slave instances that can be target of write operations. The reason is that while this writes will be discarded if the slave and the master will resynchronize, or if the slave is restarted, often there is ephemeral data that is unimportant that can be stored into slaves. For instance clients may take information about reachability of master in the slave instance to coordinate a fail over strategy.

关于node.js - 负载平衡 : Node. js - Socket.io - Redis,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17029262/

相关文章:

javascript - 如何使用 Sequelize 批量更新数组中的元素?

javascript - Nodejs 连接中间件函数重写

node.js - 在路由中访问 app.js 变量但没有全局 express

node.js - 偏执表上的级联删除 - Sequelize

node.js - 在 Google App Engine 上部署 Vuejs - webpack webapp

json - 使用 jQuery 发布 json 来表达

javascript - 将参数从 Controller 传递到 angularjs 中的资源工厂以在数据库中搜索

nginx - Ember CLI 通过 Lighting Strategy 部署

java - 无法从初始 URI [RedisURI [主机 ='127.0.0.1',端口 = 7001]] 检索初始集群分区

redis - 在管道中发送多个redis write命令时,是否需要读取返回值?