与Redis集群的连接失败 [英] Connection to Redis cluster failed

查看:110
本文介绍了与Redis集群的连接失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在



这是我在Google计算控制台中的Redis集群:



解决方案

我认为



A Redis Cluster 部署是与受Sentinel保护的标准Redis实例的数量不同。两件事截然不同。



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



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



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

  var redis = new Redis({
sentinels:[{host:hostMaster,port :26379},
{主机:hostSlab1,端口:26379},
{主机:hostSlab2,端口:26379}],
名称:'mymaster'
});

当然,请检查前哨端口和主服务器的名称。当主服务器发生故障时,ioredis将自动管理切换到从属实例,并且哨兵将确保从属服务器在之前被提升为主服务器。



请注意,由于您使用pub /子,您将需要几个Redis连接。


I have setup Redis cluster in Google compute Engine by click to deploy option. Now i want to connect to this redis server from my node js code using 'ioredis' here is my code to connect to single instance of 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);

And it worked. Now i want to connect to redis as cluster, so i change the code as

/**
 * 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

Now it throw this error:

Here is my Redis cluster in my google compute console:

解决方案

Ok, I think there is a confusion here.

A Redis Cluster deployment is not the same than a number of standard Redis instances protected by Sentinel. Two very different things.

The click-to-deploy option of GCE deploys a number of standard Redis instances protected by Sentinel, not Redis Cluster.

ioredis can handle both kind of deployments, but you have to use the corresponding API. Here, you were trying to use the Redis Cluster API, resulting in this error (cluster related commands are not activated for standard Redis instances).

According to ioredis documentation, you are supposed to connect with:

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

Of course, check the sentinel ports and name of the master. ioredis will manage automatically the switch to a slave instance when the master fails, and sentinel will ensure the slave is promoted as master just before.

Note that since you use pub/sub, you will need several redis connections.

这篇关于与Redis集群的连接失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆