zk 节点故障 重连机制
<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 节点故障 重连机制的更多相关文章
- zookeeper 故障重连机制
一.连接多个服务器,用逗号隔开 如果在连接时候zk服务器宕机 To create a client session the application code must provide a connec ...
- zookeeper节点失效重连机制
http://www.blogjava.net/xylz/archive/2011/12/05/365578.html http://blog.csdn.net/tswisdom/article/de ...
- 正确理解IM长连接的心跳及重连机制,并动手实现(有完整IM源码)
1.引言 说道“心跳”这个词大家都不陌生,当然不是指男女之间的心跳,而是和长连接相关的.顾名思义就是证明是否还活着的依据. 什么场景下需要心跳呢?目前我们接触到的大多是一些基于长连接的应用需要心跳来“ ...
- 详细解析kafka之 kafka消费者组与重平衡机制
消费组组(Consumer group)可以说是kafka很有亮点的一个设计.传统的消息引擎处理模型主要有两种,队列模型,和发布-订阅模型. 队列模型:早期消息处理引擎就是按照队列模型设计的,所谓队列 ...
- ActiveMQ的断线重连机制
断线重连机制是ActiveMQ的高可用性具体体现之一.ActiveMQ提供failover机制去实现断线重连的高可用性,可以使得连接断开之后,不断的重试连接到一个或多个brokerURL. 默认情况下 ...
- Zookeeper 重连机制
Zookeeper 重连机制 public class ZKConnectSessionWatcher implements Watcher { public final static String ...
- zk 节点宕机如何处理?
Zookeeper 本身也是集群,推荐配置不少于 3 个服务器.Zookeeper 自身也要保 证当一个节点宕机时,其他节点会继续提供服务. 如果是一个 Follower 宕机,还有 2 台服务器提供 ...
- Dubbo超时和重连机制
dubbo启动时默认有重试机制和超时机制.超时机制的规则是如果在一定的时间内,provider没有返回,则认为本次调用失败,重试机制在出现调用失败时,会再次调用.如果在配置的调用次数内都失败,则认为此 ...
- testng增加失败重跑机制
注: 以下内容引自 http://www.yeetrack.com/?p=1015 testng增加失败重跑机制 Posted on 2014 年 10 月 31 日 使用Testng框架搭建自动测试 ...
随机推荐
- Thinking in Java——集合(Collection)
一.ArrayList的使用(略) 二.容器的基本概念 (一).Collection是集合类的基本接口 主要方法: public interface Collection<E>{ bool ...
- A Simple Task
A Simple Task Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- EF6.0 自定义Code First约定
自定义Code First约定有三种方式,分别是:Lightweight Conventions(轻量级约定).Configuration Conventions(配置型约定).Model-based ...
- 转:Memcached常用命令及使用说明
一.存储命令 存储命令的格式: 1 2 <command name> <key> <flags> <exptime> <bytes> < ...
- libevent evbuffer bug
今天发现 libevent 2.0.22 一个坑爹的bug,导致消息混乱.查找问题浪费一天,复现代码如下 #include <event2/buffer.h> #include <s ...
- 记录一个js切换随机背景颜色的代码
<!DOCTYPE HTML> <html lang="en-US"> <head> <meta charset="UTF-8& ...
- 如何使用TestFlight进行App构建版本测试(转)
在日常的开发当中,当一个项目在开发过程中或者完成准备上线,都需要我们进行真机测试,否则不可能开发完了就直接扔到了App,等上线了再下载看看,这都是不可能的.那么说到真机测试,大家肯定会想到弄一个99美 ...
- 2014 ACM省赛总结
今年ACM省赛已经过去一个星期左右了,2年的ACM训练是该做个总结了,因为前几日去參加蓝桥杯总决赛,所以没来的及写总结,如今在这小小总结一下吧-- 依晰记得去年省赛时候的样子,如今感觉那时像是个无知的 ...
- 【MFC学习笔记-作业5-小数据库】【单选框,复选框,滚动条,列表框】
界面已经实现完毕. 要完成的操作就是1.性别分组(2选1) 2.属性勾选 3.年龄通过滚动条调整 4.职称通过下方的列表框选择 5.输入姓名 6.存入左方的列表框 7.当选择左方列表框的人时,可以显示 ...
- javascript模式——Facade
Facade模式为许多代码提供一个方便的接口,不现实代码实现的复杂性,这样,使用者只需要关心他的使用接口就可以使用. 下面来看一段Facade模式的运用,绑定事件在浏览器之间是不一样的,利用Facad ...