首页 > 解决方案 > 如何使用 redis.py 运行哨兵命令

问题描述

是否可以像SENTINEL masters使用 python 一样运行哨兵命令?链接在这里

使用前sentinel.master_for("master_name")需要知道master名,但是我使用的redis服务是公司私有云平台提供的,sentinel conf中的master名包含当前master节点的ip,如果master节点会被重写更改改变了,所以硬编码是不可能的。

目前,我的一个想法是子频道__sentinel__:hello并从中解析主名称,但是,它看起来很不舒服。

有没有其他方法可以得到主人的名字?在终端中,我们可以使用 连接到哨兵redis-cli并获取主信息SENTINEL masters,这可以在 python 中完成吗?

标签: pythonredisredis-sentinel

解决方案


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

推荐阅读