问题描述
我正在尝试使用 redis sentinel
从 nodeJS
连接 ioredis
实例。尽管尝试了多个可用选项,但仍无法连接 redis 哨兵实例。我们还没有配置哨兵密码。但是,能够使用 .net core
从 StackExchange.Redis
连接同一个 redis 哨兵实例。请在下面找到nodeJS
代码,
import { AppModule } from './app.module';
import IORedis from 'ioredis';
async function bootstrap() {
const app = await nestFactory.create(AppModule);
const ioredis = new IORedis({
sentinels: [
{ host: 'sentinel-host-1' },{ host: 'sentinel-host-2' },{ host: 'sentinel-host-3' },],name: 'mastername',password: 'password',showFriendlyErrorStack: true,});
try {
ioredis.set('foo','bar');
} catch (exception) {
console.log(exception);
}
await app.listen(3000);
}
bootstrap();
我们得到的错误是,
[ioredis] Unhandled error event: Error: connect ETIMEDOUT
node_modules\ioredis\built\redis\index.js:317:37)
at Object.onceWrapper (node:events:475:28)
at Socket.emit (node:events:369:20)
at Socket._onTimeout (node:net:481:8)
at listOnTimeout (node:internal/timers:557:17)
at processtimers (node:internal/timers:500:7)
从 .net core
使用的连接字符串如下,
Redis_Configuration = "host-1,host-2,host-3,serviceName=mastername,password=password,abortConnect=False,connectTimeout=1000,responseTimeout=1000";
解决方法
尝试将 password
替换为 sentinelPassword
。
回答这个是为了他人的利益。一切都很好,但是这个 nodeJS
包正在将 redis
实例解析为我无法从本地访问的私有 IP。因此,必须将它放在子网组上并使其工作。但是,仅供参考 - .net core
包无法解析为私有 IP,因此我能够从本地访问实例。