zookeeper自身节点故障
客户端连接zookeeper服务时,必须指定某一个zookeeper节点的ip连接上去,但是如果该zookeeper节点自身宕机了,客户端如何知道连接zookeeper服务时,要使用哪个新的ip?(注意:指的是zookeeper服务自身的节点宕机了)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
指定一个域名,域名可以做负载的。
而且我今天也在想这个问题,觉得client还可以做一个发现server的ip功能,这样server宕机,client可以及时切换到另一个可用的server,而且还可以自己做负载,当然这个不是针对zookeeper的。
不过lz可以谷歌下,zookeeper应有成熟方案解决单点问题的方案。
这是我随手google的 http://www.cnblogs.com/yjmyzz/p/Solve-the-problem-of-single-point-of-failure-using-ZooKeeper.html
答案来自zk文档
如果在连接时候zk服务器宕机
To create a client session the application code must provide a connection string containing a comma separated list of host:port pairs, each corresponding to a ZooKeeper server (e.g. "127.0.0.1:4545" or "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002"). The ZooKeeper client library will pick an arbitrary server and try to connect to it. If this connection fails, or if the client becomes disconnected from the server for any reason, the client will automatically try the next server in the list, until a connection is (re-)established.
如果已经连接之后服务器宕机
SessionMovedException. There is an internal exception that is generally not seen by clients called the SessionMovedException. This exception occurs because a request was received on a connection for a session which has been reestablished on a different server. The normal cause of this error is a client that sends a request to a server, but the network packet gets delayed, so the client times out and connects to a new server. When the delayed packet arrives at the first server, the old server detects that the session has moved, and closes the client connection. Clients normally do not see this error since they do not read from those old connections. (Old connections are usually closed.) One situation in which this condition can be seen is when two clients try to reestablish the same connection using a saved session id and password. One of the clients will reestablish the connection and the second client will be disconnected (causing the pair to attempt to re-establish its connection/session indefinitely).
连接的时候 zookeeper 集群ip 都写上就行了。
eg:
ZkClient zkClient = new ZkClient("192.168.19.130:2181,192.168.19.130:2182,192.168.19.130:2183");
这样 如果一台 zookeeper 宕机 ,client 会自动连接 其他的。