我有3个运行NodeJ的服务器,它们与Redis(1个主服务器,2个从服务器)相互关联.
我遇到的问题是在单个服务器上运行系统工作正常,但是当我将其扩展到3个NodeJS服务器时,它会启动丢失的消息并且系统变得不稳定.
我的负载均衡器不接受粘性会话.因此,每当来自客户端的请求到达它时,他们就可以转到不同的服务器.
我将所有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.