"Couldn't Get Slot Allocation" 使用 Node.js 在 AWS ElastiCach (Redis) 上出错

"Couldn't Get Slot Allocation" Error On AWS ElastiCach (Redis) using Node.js

我能够使用 Node.js 库连接到 Amazon ElastiCache (Redis),这些库是 redisredis-clustr,没有任何错误。但是,每当我尝试在 Amazon EC2 实例上的 Node.js 应用程序 运行 中设置键值对时,都会出现 couldn't get slot allocation 错误。我的 Amazon ElastiCache 有一个主节点和一个副本(即两个节点)。

这是用于连接的代码示例:

const redis_cluster = require('redis-clustr');
const redis_client = require('redis');

let redis = new redis_cluster({
    servers: [
        {
             host: global.gConfig.redis.redis_host,
             port: global.gConfig.redis.redis_port
        }
    ],
    createClient: (port, host) => {
        return redis_client.createClient(port, host);
    }
});

// connection error
redis.on('error', err => {
    // log the error to log file
    global.gLogger.log('error', err.message, {stack: err.stack});
});

// add to global variables
global.gRedisClient = redis;

在我与 Amazon ElastiCach 建立连接后,下面的代码示例是我如何从 Redis 检索值:

gRedisClient.mget(['run:123', 'walk:123'], (err, replies) => {...});

我使用 Redis multi() 进行 bach 操作,mget() 一次检索值。 我是使用 Amazon ElastiCach 的新手,我们将不胜感激。谢谢。

我也有过同样的经历。 我的原因是我的 ElastiCache 配置为主从,因此不需要 redis-clustr。 使用例如连接到它ioredis:

const Redis = require('ioredis');
const opts = {
    host: "your_host",
    port: 6379,
    autoResubscribe: true,
    maxRetriesPerRequest: 5
};
const redis = new Redis(opts);

只需先尝试 ping() 或其他命令,直到 redis-clustr returns 正确答案。

如果服务器正在使用 tls 但未在客户端中配置,则可能会抛出错误 couldn't get slot allocation。您可以使用 redisOptions:

启用它
let redis = new redis_cluster({
    servers: [
        {
             host: global.gConfig.redis.redis_host,
             port: global.gConfig.redis.redis_port
        }
    ],
    redisOptions: {
        tls: {} // an empty object is enough to enable it with defaults
    },
    createClient: (port, host) => {
        return redis_client.createClient(port, host);
    }
});