小编典典

如何使用node.js连接到ElastiCache集群

redis

我们知道不建议在Amazon实例外部访问ElastiCache,因此我们仅在Amazon
EC2实例内部进行尝试。

我们有一个具有9个节点的ElastiCache
Redis集群
。当我们尝试使用常规redis实现连接到它时,它会引发一些Moved错误

根据@Miller尝试了重试策略方法。还尝试使用不稳定稳定(可怜的人)实现的RedisCluster
这些实现均无作用。有什么建议吗?


阅读 393

收藏
2020-06-20

共1个答案

小编典典

为以后的读者共享代码

var RedisClustr = require('redis-clustr');
var RedisClient = require('redis');
var config = require("./config.json");

var redis = new RedisClustr({
    servers: [
        {
            host: config.redisClusterHost,
            port: config.redisClusterPort
        }
    ],
    createClient: function (port, host) {
        // this is the default behaviour
        return RedisClient.createClient(port, host);
    }
});

//connect to redis
redis.on("connect", function () {
  console.log("connected");
});

//check the functioning
redis.set("framework", "AngularJS", function (err, reply) {
  console.log("redis.set " , reply);
});

redis.get("framework", function (err, reply) {
  console.log("redis.get ", reply);
});
2020-06-20