How to connect to ElastiCache cluster using node.js

ぃ、小莉子 提交于 2019-12-21 07:22:27

问题


We know that ElastiCache is not recommended to be accessed outside Amazon instances, so we're trying below stuff inside Amazon EC2 instances only.

We've got a ElastiCache Redis Cluster with 9 nodes. When we try to connect to it using normal redis implementation, it throws some Moved errors

Have tried the retry strategy method as per @Miller. Have also tried RedisCluster with unstable and stable (poor man) implementations.

None of these implementations are working. Any suggestions please?


回答1:


Sharing the code for future readers:

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


来源:https://stackoverflow.com/questions/43872852/how-to-connect-to-elasticache-cluster-using-node-js

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!