小编典典

如何告诉客户端新Redis主服务器在哪里使用Sentinel

redis

好的,我觉得我缺少一些关键的信息。

在本地,我有1个主Redis服务器和1个从Redis服务器运行在不同的端口上
http://redis.io/topics/sentinel

我也有3个哨兵,他们似乎彼此了解,并按预期工作。

现在,我有大量的Java代码指向我的主Redis服务器所在的127.0.0.1:6379。

如果我撤下主服务器,哨兵将按预期的方式将奴隶提升为主服务器,因此现在新主服务器已启动

127.0.0.1:6380

我的问题是我的代码如何知道这一点并自动切换?


阅读 468

收藏
2020-06-20

共1个答案

小编典典

您必须在其pubsub频道之一上订阅哨兵消息。您可以在发布的链接中看到哨兵将发布消息,例如

+odown <instance details> -- The specified instance is now in Objectively Down state.
-odown <instance details> -- The specified instance is no longer in Objectively Down state.
+failover-takedown <instance details> -- 25% of the configured failover timeout has elapsed, but this sentinel can't see any progress, and is the new leader. It starts to act as the new leader reconfiguring the remaining slaves to replicate with the new master.
+failover-triggered <instance details> -- We are starting a new failover as a the leader sentinel.

因此,当您在其中一个频道上看到一个哨兵发布时,您需要解析消息并让您的客户端做出相应的响应。Redis并不聪明-您必须使用客户端库来处理这些事情。

具体来说,最有用的渠道是

+odown
+failover-detected
+switch-master
2020-06-20