gpt4 book ai didi

node.js - 使用 nodeJS 的 sentinal 识别 Redis Master

转载 作者:IT王子 更新时间:2023-10-29 06:13:19 25 4
gpt4 key购买 nike

我有 2 个 Redis 服务器,一个是主服务器,另一个是从服务器(复制)。一旦 Master 由于某些原因宕机,slave 将成为 Master 并继续充当 Master,直到该服务器出现问题为止。

我有一个 nodeJS 服务器,我想从中将数据推送到当前作为 Master 运行的 Redis。我有一个监视 Redis 服务器的哨兵,但我的问题是如何使用 nodeJS 从哨兵获取主信息?如果有办法,它是否会在不重启任何服务的情况下自动将数据推送到备用 redis 服务器?

最佳答案

ioredis支持哨兵。像这样:

var redis = new Redis({
sentinels: [{ host: 'localhost', port: 26379 }, { host: 'localhost', port: 26380 }],
name: 'mymaster'
});

redis.set('foo', 'bar');

名称标识一个redis集群,你也应该在redis sentinel.conf中指定。你可以引用这个page关于如何配置哨兵。

关于你的第二个问题,见下文:

ioredis guarantees that the node you connected to is always a master even after a failover. When a failover happens, instead of trying to reconnect to the failed node (which will be demoted to slave when it's available again), ioredis will ask sentinels for the new master node and connect to it. All commands sent during the failover are queued and will be executed when the new connection is established so that none of the commands will be lost.

现在node-redis不支持了。

关于node.js - 使用 nodeJS 的 sentinal 识别 Redis Master,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45974274/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com