我通过单击部署选项在Google 计算引擎中设置了 Redis 集群。现在我想使用'ioredis'从我的节点js代码连接到这个redis服务器这是我连接到单个redis实例的代码
var Redis = require("ioredis");
var store = new Redis(6379, 'redis-ob0g');//to store the keys
var pub = new Redis(6379, 'redis-ob0g');//to publish a message to all workers
var sub = new Redis(6379, 'redis-ob0g');//to subscribe a message
var onError = function (err) {
console.log('fail to connect to redis ',err);
};
store.on('error',onError);
pub.on('error',onError);
sub.on('error',onError);
它奏效了。现在我想作为集群连接到redis,所以我将代码更改为
/**
* list of server in replica set
* @type {{port: number, host: string}[]}
*/
var nodes =[
{ port: port, host: hostMaster},
{ port: port, host: hostSlab1},
{ port: port, host: hostSlab2}
];
var store = new Redis.Cluster(nodes);//to store the keys
var pub = new Redis.Cluster(nodes);//to publish a message to all workers
var sub = new Redis.Cluster(nodes);//to subscribe a message channel
这是我的谷歌计算控制台中的 Redis 集群: