【问题标题】:Connection to Redis cluster failed连接 Redis 集群失败
【发布时间】: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


【解决方案1】:

好的,我认为这里有一个混乱。

Redis Cluster 部署与number of standard Redis instances protected by Sentinel 不同。两种截然不同的东西。

GCE 的点击部署选项部署了许多受 Sentinel 保护的标准 Redis 实例,而不是 Redis 集群。

ioredis 可以处理这两种部署,但您必须使用相应的 API。在这里,您尝试使用 Redis Cluster API,导致出现此错误(未针对标准 Redis 实例激活与集群相关的命令)。

根据 ioredis 文档,您应该连接到:

var redis = new Redis({
    sentinels: [{ host: hostMaster, port: 26379 },
                { host: hostSlab1, port: 26379 },
                { host: hostSlab2, port: 26379 } ],
    name: 'mymaster'
});

当然,检查主端口的哨兵端口和名称。 ioredis 会在 master 失败时自动管理切换到 slave 实例,sentinel 会确保 slave 在之前被提升为 master。

请注意,由于您使用 pub/sub,您将需要多个 redis 连接。

【讨论】:

  • 如何获取主名?
  • 假设您可以使用 redis-cli 连接到任何哨兵实例,命令 SENTINEL MASTERS 将返回已配置主服务器的列表。我希望这里只定义一个。
  • 这行不通。它说未知命令。我的redis版本是2.8.6。 prntscr.com/888wvy
  • 哟成功了!我之前尝试过那个哨兵代码,但我使用的是端口 6379,它不是哨兵端口。谢谢!!
  • 好的,所以主人的名字只是'主人' - 你应该能够从 node.js 连接。
猜你喜欢
  • 2019-08-03
  • 2020-08-23
  • 2022-10-24
  • 1970-01-01
  • 2020-06-16
  • 2015-10-13
  • 1970-01-01
  • 2020-12-23
相关资源
最近更新 更多