如何在node.js上集成redis-sentinel-client和socket.io?

如何将socket.io和主从Redisconfiguration与自动故障转移function集成?

我已经使用了以下configuration:

  1. 创build一个主redis实例。
  2. 创build一个redis-sentinel-client进程的三重奏,并将它们指向主redis实例。 你只需要在下面的configuration中引用其中的一个,因为客户端将填写其余的。
  3. 创build一个slave redis实例,并将其指向master。

使用以下命令以socket.ioconfiguration您的RedisStore:

var redisOptions = { host: 'localhost', // || redisSentinelHost, port: 26379, // Default sentinel port. masterName: 'mymaster' }; var RedisStore = require('socket.io/lib/stores/redis'), // The redis-sentinel-client requires a forked redis client // that is available here: redis = require('redis-sentinel-client/node_modules/redis'), // A sentinel client is required to back each of the redis // clients below. The sentinel clients handle the fail-overs. sentinel = require('redis-sentinel-client'), redisPubSentinel = sentinel.createClient(redisOptions), redisSubSentinel = sentinel.createClient(redisOptions), redisClientSentinel = sentinel.createClient(redisOptions), redisPub = redisClientSentinel.getMaster(), redisSub = redisSubSentinel.getMaster(), redisClient = redisPubSentinel.getMaster(); // We must be robust to connection errors in order to allow // for a reconnect. We therefore prevent redis client errors // from stopping the application. [ redisPubSentinel, redisSubSentinel, redisClientSentinel, redisPub, redisSub, redisClient ].forEach(function(c) { c.on('error', function(err) { logger.log(err); }); }); io.set('store', new RedisStore({ redis: redis, redisPub: redisPub, redisSub: redisSub, redisClient: redisClient }) );