我已经设置了一个 Redis 副本,其中有一个主节点和两个从节点,并使用 Redis Sentinel 监控每个节点。我正在使用 redis-py 库与主节点和从节点通信。以下是我用于连接主节点和从节点的代码:
from redis.sentinel import Sentinel
sentinel = Sentinel([("localhost", 5000)]) # connect to one of the sentinels
master = sentinel.master_for("mymaster") # get master object
slave = sentinel.slave_for("mymaster") # get slave object
现在,我将使用主服务器进行写入操作,从服务器进行读取操作。但问题在于,当发生故障转移时,需要刷新
master
对象以指向新的主服务器。为此,正如这个答案和文档中所述,我应该订阅+switch-master
频道以接收新主服务器的地址。但问题是,我应该使用哪个对象来订阅该频道?我尝试使用以下代码中的
master
对象:ps = master.pubsub()
ps.subscribe("+switch-master")
当主服务器宕机时,我尝试以下方式获取信息:
ps.get_message()
但是,由于主服务器宕机,该方法没有返回预期的消息。
sentinel对象不支持pubsub功能。
>>> sentinel.pubsub()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: 'Sentinel' object has no attribute 'pubsub'
作为一种替代方法,在链接的答案中建议使用
client-reconfig-script
。 但是我应该如何使用外部脚本修改我的应用程序中的对象?我在这里缺少什么? 我对Redis不熟悉,所以任何参考资料都将不胜感激。