【问题标题】:How to integrate redis-sentinel-client and socket.io on node.js?如何在 node.js 上集成 redis-sentinel-client 和 socket.io?
【发布时间】:2013-07-13 20:12:10
【问题描述】:

如何集成 socket.io 和具有自动故障转移的主从 Redis 配置?

【问题讨论】:

    标签: node.js redis socket.io redis-sentinel


    【解决方案1】:

    我使用了以下配置:

    1. 创建一个主 redis 实例。
    2. 创建三个 redis-sentinel-client 进程并将它们指向主 redis 实例。您只需在下面的配置中引用其中一个,因为其余的将由客户端填写。
    3. 创建一个从 Redis 实例,并将其指向主实例。

    使用以下内容通过 socket.io 配置您的 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
      })
    );
    

    【讨论】:

      猜你喜欢
      • 2014-11-25
      • 1970-01-01
      • 2012-05-22
      • 2012-06-19
      • 1970-01-01
      • 1970-01-01
      • 2023-03-30
      • 1970-01-01
      • 2015-07-16
      相关资源
      最近更新 更多