<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."\n";
}
else{
$mysql_ip = $zk->get('/mysql/0002');
print $mysql_ip."\n";
}; 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\n" );
};
print "\@arr2 is @arr2\n";
$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 节点故障 重连机制的更多相关文章

  1. zookeeper 故障重连机制

    一.连接多个服务器,用逗号隔开 如果在连接时候zk服务器宕机 To create a client session the application code must provide a connec ...

  2. zookeeper节点失效重连机制

    http://www.blogjava.net/xylz/archive/2011/12/05/365578.html http://blog.csdn.net/tswisdom/article/de ...

  3. 正确理解IM长连接的心跳及重连机制,并动手实现(有完整IM源码)

    1.引言 说道“心跳”这个词大家都不陌生,当然不是指男女之间的心跳,而是和长连接相关的.顾名思义就是证明是否还活着的依据. 什么场景下需要心跳呢?目前我们接触到的大多是一些基于长连接的应用需要心跳来“ ...

  4. 详细解析kafka之 kafka消费者组与重平衡机制

    消费组组(Consumer group)可以说是kafka很有亮点的一个设计.传统的消息引擎处理模型主要有两种,队列模型,和发布-订阅模型. 队列模型:早期消息处理引擎就是按照队列模型设计的,所谓队列 ...

  5. ActiveMQ的断线重连机制

    断线重连机制是ActiveMQ的高可用性具体体现之一.ActiveMQ提供failover机制去实现断线重连的高可用性,可以使得连接断开之后,不断的重试连接到一个或多个brokerURL. 默认情况下 ...

  6. Zookeeper 重连机制

    Zookeeper 重连机制 public class ZKConnectSessionWatcher implements Watcher { public final static String ...

  7. zk 节点宕机如何处理?

    Zookeeper 本身也是集群,推荐配置不少于 3 个服务器.Zookeeper 自身也要保 证当一个节点宕机时,其他节点会继续提供服务. 如果是一个 Follower 宕机,还有 2 台服务器提供 ...

  8. Dubbo超时和重连机制

    dubbo启动时默认有重试机制和超时机制.超时机制的规则是如果在一定的时间内,provider没有返回,则认为本次调用失败,重试机制在出现调用失败时,会再次调用.如果在配置的调用次数内都失败,则认为此 ...

  9. testng增加失败重跑机制

    注: 以下内容引自 http://www.yeetrack.com/?p=1015 testng增加失败重跑机制 Posted on 2014 年 10 月 31 日 使用Testng框架搭建自动测试 ...

随机推荐

  1. 正式学习React( 三)

    最基本的jsx语法什么的,我就不介绍了,唯一觉得有用点的,就是声明周期了. 下面的内容是转来的,自己也可以网上去搜,我觉得别人归纳的挺不错的,不过写法可能不是es6的,不影响学习. 在组件的整个生命周 ...

  2. MEMS加速度计工作原理

    一般加速度计有两块芯片组成,一块是MEMS传感器,另一块是客户化的信号处理芯片. 加速度计也称惯性传感器,因为它的工作原理就是靠MEMS中可移动部分的惯性.由于中间电容板的质量很大,而且它是一种悬臂构 ...

  3. C# 线程池异步调用

    许多应用程序使用多个线程,但这些线程经常在休眠状态中耗费大量的时间来等待事件发生.其他线程可能进入休眠状态,并且仅定期被唤醒以轮询更改或更新状态信息,然后再次进入休眠状态.为了简化对这些线程的管理,. ...

  4. Docker学习笔记之一,搭建一个JAVA Tomcat运行环境

    Docker学习笔记之一,搭建一个JAVA Tomcat运行环境 前言 Docker旨在提供一种应用程序的自动化部署解决方案,在 Linux 系统上迅速创建一个容器(轻量级虚拟机)并部署和运行应用程序 ...

  5. linux之SQL语句简明教程---IN

    在 SQL 中,在两个情况下会用到 IN 这个指令:这一页将介绍其中之一 -- 与 WHERE 有关的那一个情况.在这个用法下,我们事先已知道至少一个我们需要的值,而我们将这些知道的值都放入IN 这个 ...

  6. php IP string与整型互换

    PHP中有内置函数ip2long可以将ip地址转换整型. 使用long2ip把整型转换回ip地址 例子: $ip = '58.6.207.207'; $ip_int = ip2long($ip); e ...

  7. GCC 编译选项

    http://www.cnblogs.com/xmphoenix/archive/2011/03/21/1989944.html GCC 编译选项(转) gcc提供了大量的警告选项,对代码中可能存在的 ...

  8. eclipse sysout快捷输入启用

    Window-Preference-java-editor-content assist-advanced 一定要勾选Template Propasals. 关于Template Propasals: ...

  9. C#中Abstract和Virtual 【转】

    http://www.cnblogs.com/blsong/archive/2010/08/12/1798064.html 在C#的学习中,容易混淆virtual方法和abstract方法的使用,现在 ...

  10. 【Quick-COCOS2D-X 3.3 怎样绑定自己定义类至Lua之四】使用绑定C++至Lua的自己定义类

    续[Quick-COCOS2D-X 3.3 怎样绑定自己定义类至Lua之三]动手绑定自己定义类至Lua 之后.我们已经完毕了自己定义类至Lua的绑定.在接下来的环节,我们将使用它. 首先,我们须要确定 ...