“使用Node.js在AWS ElastiCach(Redis)上无法获得插槽分配”错误

问题描述 投票:2回答:2

我能够使用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的新手,任何帮助将不胜感激。谢谢。

node.js redis amazon-elasticache redis-cluster
2个回答
0
投票

我经历了同样的事情。我的原因是我的ElastiCache被配置为主从服务器,因此不需要redis-clustr。使用例如ioredis:

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

0
投票

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

© www.soinside.com 2019 - 2024. All rights reserved.