【发布时间】:2015-11-12 07:57:39
【问题描述】:
我已经通过click to deploy 选项在Google compute Engine 中设置了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 集群:
【问题讨论】:
-
你安装的是什么版本的ioredis?
-
@Michelem 这是最新的 1.7.5
-
你运行的是什么版本的Redis(应该是3+),你的Redis集群配置正确了吗?
-
@ItamarHaber Haber 它看起来像是旧的 2.8.6。那么我需要更新它还是继续使用它?我单击以在谷歌计算中进行部署,他们进行集群设置。 prntscr.com/86ff7b
-
对不起,我的错误,您可能不需要升级,正在考虑其他集群。无论如何,IIUC,您不需要使用 ioredis.Cluster 东西 - 只需使用主节点的端点进行连接(子节点也可以连接到任何一个从节点)。
标签: node.js redis cluster-computing node-redis google-cloud-console