zookeeper 故障重连机制

本文介绍 ZooKeeper 客户端如何通过连接字符串建立会话,并详细解释了客户端如何在服务器故障时自动切换连接,以及 SessionMovedException 的产生原因。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、连接多个服务器,用逗号隔开

  

  1. 如果在连接时候zk服务器宕机  
  2. 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.   
  3.   
  4. "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   
  5.   
  6. 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.  
  7.   
  8.   
  9.   
  10.   
  11. 创建一个client session 应用代码必须提供一个连接字符串包含一个逗号分隔的主机:端口 列表,  
  12.   
  13. 每个对应到一个ZooKeeper server (e.g.   
  14.   
  15. "127.0.0.1:4545" or "127.0.0.1:3000,127.0.0.1:3001,127.0.0.1:3002").   
  16.   
  17. ZooKeeper 客户端库 会选择一个任意的server 和尝试连接到它。  
  18.   
  19. 如果这个连接失败,或者 如果客户端变的不能连接,客户端会自动 尝试下一个server 在列表里,知道一个连接重新创建。  
  20.   
  21.   
  22.   
  23. 如果已经连接之后服务器宕机  
  24. 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   
  25.   
  26. 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   
  27.   
  28. 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   
  29.   
  30. 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   
  31.   
  32. 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   
  33.   
  34. its connection/session indefinitely).  
  35.   
  36.   
  37. SessionMovedException. 有一个内部的异常,那是通常不被客户端请求SessionMovedException 看到。  
  38.   
  39.   
  40.   
  41. 这个exception 因为 一个请求被接收在一个连接对于一个会话 精被重新建立到一个不同的zk server.  
  42.   
  43. 这个错误的通常情况是一个客户端发送一个请求到一个服务器,  
  44.   
  45. 但是网络包延时了,因此客户端超时 ,连接到一个新的服务器.当延迟的packet 到达第一个server,  
  46.   
  47.   
  48. old server 检测倒 session 已经移动了,关闭客户端连接。  
  49.   
  50. 客户端通常不会看到这个错误 因为它们不会从老的连接读取(老的连接通常是被关闭的)  
  51.   
  52.   
  53. 一种情况是这个状况可以被看到当 2个客户端尝试重建建立相同的连接使用一个保存的会话id和密码。  

参考链接:http://blog.youkuaiyun.com/zhaoyangjian724/article/details/52218172  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值