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

时间:2017-05-09 14:41:10

标签: node.js amazon-web-services amazon-ec2 redis amazon-elasticache

  

我们知道ElastiCache is not recommended to be accessed outside Amazon instances,所以我们只在Amazon EC2实例中尝试以下内容。

我们有一个包含9个节点的ElastiCache Redis Cluster。当我们尝试使用normal redis implementation连接到它时,它会抛出some Moved errors

已根据retry strategy method尝试了@Miller。还尝试了RedisClusterunstable实施{。}}。

这些实现都不起作用。有什么建议吗?

1 个答案:

答案 0 :(得分:17)

  

Sharing the code面向未来的读者:

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);
});