如何使用redis.py运行哨兵命令

时间:2020-01-13 10:40:50

标签: python redis redis-sentinel

是否可以使用python运行诸如SENTINEL masters之类的前哨命令?链接是here

在使用sentinel.master_for("master_name")之前,我必须知道主名称,但是,我使用的redis服务是由公司的私有云平台提供的,并且sentinel conf中的主名称包含当前主节点的ip,并且会如果主节点发生更改,将被重写和更改,因此不可能使用硬代码。

当前,对我来说,一个主意是对频道__sentinel__:hello进行子处理并从中解析主名称,但是,它看起来如此不舒服。

还有另一种获取主人姓名的方法吗?在终端中,我们可以使用redis-cli连接到哨兵,并使用SENTINEL masters获取主信息,这可以在python中完成吗?

1 个答案:

答案 0 :(得分:0)

redis.py本身可以用于连接到哨兵。

import redis
sentinel_obj = redis.Redis(host="127.0.0.1", port=26379)  # pass the host and port of sentinel
sentinel_obj.sentinel_masters()  # this will get all available master-group-names in sentinel