python - 将 Redis 与 Sentinel 和 redis-py 一起使用时如何故障转移到新的主节点?

标签 python redis high-availability failover redis-py

要订阅 Sentinel 故障转移,Channel 的名称是什么,我如何检测到我需要在订阅的函数中刷新 master?

我有一个多节点 Redis 设置,使用 Redis Sentinel 实现高可用性和故障转移。

我需要为 Redis 设置一个 Pub/Sub 来检测 Redis Master 何时发生故障以及系统何时选举了一个新的 Master。

_sentinel = redis.sentinel.Sentinel([(app.config["REDIS_HOSTNAME"],app.config["REDIS_SENTINEL_PORT"])])
_master = _sentinel.master_for(app.config["REDIS_SERVICE_NAME"])

def _sentinel_message_handler(message):
    #TODO how do I detect that there is a new Redis Master?

_pubsub = _master.pubsub()
_pubsub.subscribe(**{app.config["TODO"]:_sentinel_message_handler})

最佳答案

为此,您需要订阅哨兵节点,而不是主节点。您要查找的 channel 是 "+switch-master"。 Sentinel 文档将 channel 指示为 “switch-master”“没有 +”,但从 3.2.4 开始,+ 已包含在内。

Redis Sentinel Pub/Sub Message Docs

您应该能够引用 redis-py 文档以了解其余部分。 Redis-py Docs


更新

您还可以考虑使用 client-reconfig-script 设置。

When the master changed because of a failover a script can be called in order to perform application-specific tasks to notify the clients that the configuration has changed and the master is at a different address.

http://download.redis.io/redis-stable/sentinel.conf

希望对你有帮助

关于python - 将 Redis 与 Sentinel 和 redis-py 一起使用时如何故障转移到新的主节点?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40941997/

相关文章:

python - 使用 Python/Django 保存/导出文件时打开标准文件浏览器窗口

java - Tomcat 中的高可用性单例处理器

java - Google App Engine 中常驻后端实例的可用性

python - Django - 如何将异步任务队列与 celery 和 redis 一起使用

Heroku:当我的 Redis 连接都被使用时会发生什么?

redis - 按模式/前缀获取所有哈希

virtualbox - KeepAlived + HAProxy 一段时间后连接被拒绝

python - import cntk 在 VS2017 "python environment"中有效,但在 "python project"中无效

Python - 替换部分 C 程序,但跳过注释

python - Matplotlib xticks 标签 : how to modify attributes value