无法从ElasticBeanstalk上的nodejs服务器连接elasticache

nxagd54h  于 2021-06-08  发布在  Redis
关注(0)|答案(1)|浏览(400)

我们在aws elastic beanstalk上有一个带有express的nodejs服务器,我们正试图将它与nodejs的elasticache(redis clustered)连接起来,但是出现了这个错误 Redis Client Connection Error ClusterAllFailedError: Failed to refresh slots cache. . 这个错误似乎很常见,因为很多人都面临着相同的错误。为了连接到elasticache,我们使用了一个名为 ioredis .
很多人建议对elasticache和elasticbeanstalk使用相同的vpc和安全组。我们已经在使用同一个vpc,在ElasticBeanstalk上我们使用了两个安全组,其中一个与elasticache的安全组匹配。对于默认的专有网络,我们已经启用了 All Traffic 对于入站和出站规则,但我们仍然面临相同的错误。
为了从nodejs服务器连接到elasticache,我使用以下代码:

const Redis = require("ioredis");
exports.connect = () => {
  const client = new Redis.Cluster(
    ["xxxxx.xxxxx.clustercfg.use1.cache.amazonaws.com:6379"],
    {
      slotsRefreshTimeout: 10000,
      dnsLookup: (address, callback) => callback(null, address),
      redisOptions: {
        showFriendlyErrorStack: true,
        tls: {
          checkServerIdentity: (/*host, cert*/) => {
            // skip certificate hostname validation
            return undefined;
          },
        },
      },
    }
  );
  client.on("ready", () => {
    console.log("Redis Client Ready");
  });
  client.on("connect", () => {
    console.log("Redis Client Connected");
  });
  client.on("error", (error) => {
    console.log("Redis Client Connection Error", error);
  });
  client.on("reconnecting", () => {
    console.log("Redis Client Reconnecting");
  });
  client.on("end", () => {
    console.log("Redis Client Connection ended");
  });
  return client;
};

elasticache配置

具有入站和出站规则的默认专有网络安全组


弹性beanstalk安全组(与默认值相同)

来自elastic beanstalk的错误信息

版本:
node.js运行在64位amazon linux平台版本上 4.15.1 nodejs版本: 12.18.3 ioredis版本: 4.17.3 npm版本: 6.14.6 快速版本: 4.17.1 更新:如果我使用ssh和redis cli,我可以从elasticbeanstalk访问elasticache,但是无法使用 ioredis 在elasticbeanstalk上运行的nodejs上。

kadbb459

kadbb4591#

我有一个类似的设置,并最终让它工作,几个关键点:
elasticbeanstalk和elasticache必须在同一vpc中
elasticache的安全组应该有一个入站规则来允许来自elasticbeanstalk的流量
下面是连接的代码:

import { RedisPubSub } from 'graphql-redis-subscriptions';
import Redis from 'ioredis';
import config from '../../config/env';

const options = {
  // AWS host will look like this: somecache-dev-ro.k6sjdj.ng.0001.use1.cache.amazonaws.com
  host: config.redis.host || 'localhost',
  port: config.redis.port || 6379,
  retryStrategy: (times: number): number => {
    // reconnect after
    return Math.min(times * 50, 2000);
  },
};

export const pubsub = new RedisPubSub({
  publisher: new Redis(options),
  subscriber: new Redis(options),
});

相关问题