繁体   English   中英

Nodejs:如何连接多个redis?

[英]Nodejs : How to connect to multiple redis?

如何使用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);
});

几个月后,我发现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库。 此库支持连接到Redis 主从,无需配置群集或使用标记。

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

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

此外,这是Redis文档中建议的Node.js库

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM