我正在尝试连接到 aws 中的 redis 实例。我可以使用类似的东西连接到它
redis-cli -h localhost -p 6379 -a <auth_token> --tls PING
但是,当我尝试使用节点(redis 库 v4.2.0)执行类似操作时,它会挂起
const redis = require("redis");
(async () => {
const client = redis.createClient( {
auth_pass:
"<auth_token>",
tls: { servername: "localhost", port: 6379 },
});
client.on("error", (err) => {
console.log("Redis Client Error", err);
});
client.connect();
console.log(await client.ping());
})();
在aws中为redis设置了端口转发,这就是使用localhost的原因。 身份验证令牌与我在配置 Redis 时输入到 Sparkleformation 的令牌相同。静态加密和传输加密也已配置。 我一直试图在谷歌上寻找答案,但是似乎有很多旧文档,并且没有一个新文档清楚说明如何使用 tls 和身份验证令牌进行连接。知道如何让它发挥作用吗?
如果有人遇到同样的问题,我可以使用 ioredis 使其正常工作。
const Redis = require("ioredis");
(async () => {
const redisRef = new Redis("rediss://:<auth_token>@localhost:6379");
console.log(await redisRef.ping());
})();
并在本地运行时设置此环境变量:
export NODE_TLS_REJECT_UNAUTHORIZED=0
我最近在
node-redis
v4(npm 中的 [email protected]
)中遇到了同样的问题,接受的解决方案也可以在其中进行一些修改:
const { createClient} = require('redis');
(async () => {
const client = createClient({
url: "redis://:<auth_token>@localhost:6379");
});
await client.connect();
console.log(await client.ping());
})();
这里要记住的主要事情之一是
protocol
(我的意思是redis://
和rediss://
)。
您必须记住,
rediss://
协议启用了tls
,如果您的redis主机不支持tls(例如本例中的localhost),您将收到超时错误,或者它会挂起。
花了几个小时后,这些设置对我来说对 AWS ElastiCache 有效。希望我能拯救某人的生命。
const standalone = createClient({
password: '<PASSWORD>', // use your password here
socket: {
host: 'host.cache-cluster.com',
port: 6379,
tls: true
}
});
const cluster = createCluster({
rootNodes: [{url: 'redis://host.cache-cluster.com:6379'}],
defaults: {
password: '<PASSWORD>',
socket: {
tls: true
}
}
});
const cluster = new Redis.Cluster(
[{ host: 'host.cache-cluster.com' }], {
dnsLookup: (address, callback) => callback(null, address),
redisOptions: {
tls: true,
password: '<PASSWORD>'
}
});