Nodejs:如何连接到多个redis?

Nodejs : How to connect to multiple redis?

如何用nodejs架设两台redis服务器(master/slave)?
我使用 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);
});

几个月后我发现 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'
});

如果您不想在集群模式下将 Redis 服务器配置为 运行,您可以使用 thunk-redis library. This library supports connecting to Redis master-slave,而无需配置集群或使用 sentinel。

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

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

此外,这是 Redis documentation

中建议的 Node.js 库