首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将redis-sentinel和socket.io在node.js上集成?

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

Stack Overflow用户
提问于 2013-07-15 20:17:59
回答 1查看 1.7K关注 0票数 2

如何将socket.io和主从Redis配置与自动故障转移集成在一起?

EN

回答 1

Stack Overflow用户

发布于 2013-07-15 20:17:59

我使用了以下配置:

  1. 创建一个主redis实例。
  2. 创建一个由redis -哨兵-客户端进程组成的三个进程,并将它们指向主redis实例。您只需要在下面的配置中引用其中之一,因为客户端将填写其余部分。
  3. 创建一个从redis实例,并将其指向主服务器。

使用以下方法使用RedisStore配置socket.io:

代码语言:javascript
复制
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
  })
);
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17663122

复制
相关文章

相似问题

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