要订阅 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/