【问题标题】:Nodejs : How to connect to multiple redis?Nodejs:如何连接到多个redis?
【发布时间】:2017-03-29 13:54:50
【问题描述】:

如何用nodejs设置两个redis服务器(主/从)?
我用node_redis,已经尝试redis://host:port,host2:port2?db=10&password=bar

var connectionString = 'redis://host:port,host2:port2?db=10&password=bar'
var client = redis.createClient(connectionString);
client.set('key','value',function(err,reply){
   console.log(err);  //the db option is added twice and does not match
   console.log(reply);
});

【问题讨论】:

  • 您是否在配置中设置了复制?
  • @DavidMakogon 怎么设置?
  • 您应该查找redis replication - 已发布大量文档。您不能仅通过连接字符串进行复制。

标签: node.js redis node-redis


【解决方案1】:

几个月后,我发现 ioredis 可以帮助使用 nodejs 集群作业!

var Redis = require('ioredis');

var cluster = new Redis.Cluster([{
  port: 6380,
  host: '127.0.0.1'
}, {
  port: 6381,
  host: '127.0.0.1'
}]);

cluster.set('foo', 'bar');
cluster.get('foo', function (err, res) {
  // res === 'bar'
});

【讨论】:

    【解决方案2】:

    如果您不想将 Redis 服务器配置为在集群模式下运行,您可以使用 thunk-redis 库。该库支持连接到 Redis master-slave,而无需配置集群或使用哨兵。

    您只需向客户端添加多个 IP 地址:

    const client = redis.createClient(['127.0.0.1:6379', '127.0.0.1:6380'], {onlyMaster: false});
    

    另外,这是 Redis documentation 中建议的 Node.js 库

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-01-25
      • 2011-05-10
      • 2022-06-24
      • 1970-01-01
      • 2012-08-15
      • 2020-10-03
      • 2021-06-25
      • 1970-01-01
      相关资源
      最近更新 更多