Redis 不断在expressJS 中与AWS Elasticache 断开连接和连接

问题描述 投票:0回答:1

我正在尝试在expressJs中的应用程序中连接redis,它可以在本地主机上运行,但当我使用来自AWS ElastiCache的redisurl时则不能。

这是我的代码。 Redis.js

const redis = require('redis');
require('dotenv').config();   

const client = redis.createClient( { url: process.env.REDIS_URL } ); 

client.on('connect', () => { console.log('Connected to Redis'); });  

(async () => { await client.connect(); })();  

client.on('error', (err) => { console.error("Redis Error: ${err}"); });  

client.on('reconnecting', () => console.log('client is reconnecting'));  

client.on('ready', () => console.log('client is ready'));

module.exports = client;

这是我的错误

Redis Error: Error: Connection timeout
client is reconnecting 
Redis Error: Error: Connection timeout 
client is reconnecting 
Redis Error: Error: Connection timeout client is reconnecting

这样不断重复。

我尝试使用主机和url作为参数

redis.createClient()

Redis 版本 4.6.x 节点版本 18 Express 版本 4.18.x

node.js express redis amazon-elasticache
1个回答
0
投票

确保您不需要用户名和密码即可连接到 AWS ElastiCache 上的 Redis 实例。如果需要,请在 createClient 选项中提供用户名和密码选项。

const 客户端 = redis.createClient({ 主机:'your-redis-cluster-name.xxxxxx.ng.0001.use1.cache.amazonaws.com', 端口:6379, });

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