zk 节点故障 重连机制

摘要:
客户端通常不会看到这个错误因为它们不会从老的连接读取一种情况是这个状况可以被看到当2个客户端尝试重建建立相同的连接使用一个保存的会话id和密码。
<pre name="code" class="html">如果在连接时候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.




创建一个client session 应用代码必须提供一个连接字符串包含一个逗号分隔的主机:端口 列表,

每个对应到一个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"). 

ZooKeeper 客户端库 会选择一个任意的server 和尝试连接到它。

如果这个连接失败,或者 如果客户端变的不能连接,客户端会自动 尝试下一个server 在列表里,知道一个连接重新创建。



如果已经连接之后服务器宕机
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).


SessionMovedException. 有一个内部的异常,那是通常不被客户端请求SessionMovedException 看到。



这个exception 因为 一个请求被接收在一个连接对于一个会话 精被重新建立到一个不同的zk server.

这个错误的通常情况是一个客户端发送一个请求到一个服务器,

但是网络包延时了,因此客户端超时 ,连接到一个新的服务器.当延迟的packet 到达第一个server,


old server 检测倒 session 已经移动了,关闭客户端连接。

客户端通常不会看到这个错误 因为它们不会从老的连接读取(老的连接通常是被关闭的)


一种情况是这个状况可以被看到当 2个客户端尝试重建建立相同的连接使用一个保存的会话id和密码。




demo:

zjtest7-redis:/root/zk# cat test_zk.pl 
use ZooKeeper;
use AnyEvent;
use AE;
use Data::Dumper;
use IO::Socket;
     sub check_port {
                        ( $server, $port ) = ('127.0.0.1','3306');
                              $sock = IO::Socket::INET->new(PeerAddr => $server,
                              PeerPort => $port,
                              Proto => 'tcp');
                             if ($sock)
                                {return 1}
                             else
                                {return 0 };
                   }; 


    my @ip_list=();
   my $zk = ZooKeeper->new(hosts => '1.1.1.1:2181,1.1.1.1:2182,1.1.1.1:2183') ;
   #my $zk = ZooKeeper->new(hosts => '1.1.1.1:2182,1.1.1.1:2183') ;
   print Dumper($zk);
   eval {
   
   my $stat = $zk->exists('/mysql/0001');
   if  ($stat){
         $mysql_ip =   $zk->get('/mysql/0001');
         print $mysql_ip."
";
             }
         else{
            $mysql_ip =   $zk->get('/mysql/0002');
            print $mysql_ip."
";
             };
  
     use DBI;
     my $database='zjzc';  
     my $user="zjzc_app";  
     my $passwd="1234567"; 
     my @arr2=();  
     my $dbh  = DBI->connect("dbi:mysql:database=$database;host=$mysql_ip;port=3306",$user,$passwd,{  
                          RaiseError => 1,  
                          AutoCommit => 0
                           } ) or die "can't connect to database ". DBI-errstr;
    my $hostSql = qq{select  id,name from scan; }; 
    my ($a1, $a2, $a3,$a4,$a5,$a6,$a7,$a8,$a9);  
    my $selStmt = $dbh->prepare($hostSql);  
    $selStmt->execute();  
    $selStmt->bind_columns(undef, $a1, $a2);  
    $selStmt->execute();  
    while( $selStmt->fetch() )
         { push (@arr2, "$a1  $a2  $a3
" );
         };
        print "@arr2 is @arr2
";
        $dbh->disconnect;
        };


默认连接3个zk server '1.1.1.1:2181,1.1.1.1:2182,1.1.1.1:2183


测试1 关闭 2181 服务器:

zjtest7-redis:/root/zk# perl test_zk.pl 
$VAR1 = bless( {
                 'buffer_length' => 2048,
                 'default_acl' => [
                                    {
                                      'scheme' => 'world',
                                      'id' => 'anyone',
                                      'perms' => 31
                                    }
                                  ],
                 'dispatcher' => bless( {
                                          'ignore_session_events' => 1,
                                          'dispatch_cb' => sub { "DUMMY" },
                                          'channel' => bless( {}, 'ZooKeeper::Channel' ),
                                          'ae_watcher' => bless( do{(my $o = '¸`ÿp‹ᅡþ欁ᄋ')}, 'EV::IO' ),
                                          'watchers' => {}
                                        }, 'ZooKeeper::Dispatcher::AnyEvent' ),
                 'timeout' => 10000,
                 'hosts' => '1.1.1.1:2181,1.1.1.1:2182,1.1.1.1:2183'
               }, 'ZooKeeper' );
192.168.32.6
@arr2 is 1  aaabbb  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  
 2  cccddeqe  

zjtest7-redis:/root/zk# perl test_zk.pl 
$VAR1 = bless( {
                 'hosts' => '1.1.1.1:2181,1.1.1.1:2182,1.1.1.1:2183',
                 'default_acl' => [
                                    {
                                      'perms' => 31,
                                      'id' => 'anyone',
                                      'scheme' => 'world'
                                    }
                                  ],
                 'timeout' => 10000,
                 'buffer_length' => 2048,
                 'dispatcher' => bless( {
                                          'watchers' => {},
                                          'dispatch_cb' => sub { "DUMMY" },
                                          'ae_watcher' => bless( do{(my $o = 'ʂˎྋ샀ž')}, 'EV::IO' ),
                                          'channel' => bless( {}, 'ZooKeeper::Channel' ),
                                          'ignore_session_events' => 1
                                        }, 'ZooKeeper::Dispatcher::AnyEvent' )
               }, 'ZooKeeper' );



此时有可能连接不上,原因为连接到了1.1.1.1:2181

 



测试2:

   #my $zk = ZooKeeper->new(hosts => '1.1.1.1:2181,1.1.1.1:2182,1.1.1.1:2183') ;
   my $zk = ZooKeeper->new(hosts => '1.1.1.1:2182,1.1.1.1:2183') ;

此时一切正常


















免责声明:文章转载自《zk 节点故障 重连机制》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇PCB设计检查表SqlServer.查询-行号下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

ZK框架笔记5、事件

        事件是org.zkoss.zk.ui.event.Event类,它通知应用程序发生了什么事情。每一种类型的事件都由一个特定的类来表示。         要响应一个事件,应用程序必须为事件注册一个或更多事件监听器。有3种方式可以为一个组件事件监听器。   (1)一般制定onXXX事件监听器为组件的属性,作为属性定义的事件监听器。 &...

zookeeper学习(零)_安装与启动

zookeeper学习(零)_安装与启动 最近换了新的电脑,终于买了梦寐以求的macbook。最近也换了新的公司,公司技术栈用到了zookeeper.当然自己也要安装学习下。省的渣渣的我,被鄙视就麻烦了。本篇文章只介绍如何安装,先安装上之后,再进行具体学习。 安装 本文使用brew 进行安装,默默说一句,mac的系统是比win好用。没有安装brew和jdk...

kafka学习指南(总结版)

版本介绍   目前最新版本为2.3(20190808更新)。   从使用上来看,以0.9为分界线,0.9开始不再区分高级(相当于mysql binlog的GTID,只需要跟topic打交道,服务器自动管理偏移量和负载均衡)/低级消费者API(相当于mysql binlog的文件+position,直接和分区以及偏移量打交道)。   从兼容性上来看,以0.8...

zookeeper集群(linux)和单机(windows)搭建

Zookeeper集群(linxu)搭建 环境要求:必须要有jdk环境,本次讲课使用jdk1.8 3.1结构 一共三个节点集群的服务器数量一般为2n+1个,(zk服务器集群规模不小于3个节点),要求服务器之间系统时间保持一致。 3.2上传zk并且解压进行解压: tar -zxvf zookeeper-3.4.6.tar.gz重命名: mv zookeep...

Zookeeper学习(转自知乎)

https://zhuanlan.zhihu.com/p/24996631 众所周知,zookeeper与分布式有着千丝万缕的联系。它虽然源自于hadoop,但目前zookeeper脱离hadoop的范畴开发分布式应用越来越普遍,想到这里,不禁为hadoop扼腕叹息。 那么Zookeeper究竟是什么呢? 从他的名字就可以看出来 ------ 动物园管理员...

06_zookeeper原生Java API使用

【Zookeeper构造方法概述】 /** * 客户端和zk服务端的连接是一个异步的过程 * 当连接成功后,客户端会收到一个watch通知 * * ZooKeeper(String connectString, int sessionTimeout, Watcher watcher, *...