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框架搭建自动测试 ...
随机推荐
- 计算两点间的距离-hdu2001
Problem Description 输入两点坐标(X1,Y1),(X2,Y2),计算并输出两点间的距离. Input 输入数据有多组,每组占一行,由4个实数组成,分别表示x1,y1,x2,y2 ...
- SQL Server 分区表的创建方法与管理
背景知识: 分区表.可以把表中的数据按范围保存到不同的文件组中. 举个例子吧: 2014年以前的数据保存到文件组A 2014~2015的数据保存到文件组B 2015年以后的数据保存到文件组C 好处: ...
- C#中委托和事件
目 录 将方法作为方法的参数 将方法绑定到委托 更好的封装性 限制类型能力 范例说明 Observer 设计模式简介 实现范例的Observer 设计模式 .NET 框架中的委托与事件 为什么委托定义 ...
- 一个失误导致微信下载图片接口Token失效
公司的应用调了一个微信上传下载图片的接口,本来在线上跑的好好的,什么问题没有,但是这两天总是不定时的出现下载下来的图片损坏,拿着Token和serverid去接口测试网页验证,返回的是Token失效了 ...
- U盘开发之安全U盘
普通型安全U盘,虚拟KEY和U盘两个设备,由主机软件分别对KEY和U盘进行操作,U盘与上位机采用usb mass storage接口,KEY采用HID接口,两者均无需驱动.也有虚拟成光盘和U盘两个设备 ...
- U盘详解
摘要:U盘,称呼最早来源于朗科公司生产的一种新型存储设备,名曰“优盘”,使用USB接口进行连接.USB接口就连到电脑的主机后,U盘的资料可与电脑交换.而之后生产的类似技术的设备由于朗科已进行专利注册, ...
- Erlang语言介绍
Erlang (/ˈɜrlæŋ/ er-lang) is a general-purpose concurrent, garbage-collected programming language an ...
- Hibernate征途(三)之CRUD
上篇博客<Hibernate征途(二)之基础与核心>介绍了Hibernate的基础内容和核心内容,这篇博客简单实践一下.第一篇博客也说过Hibernate是一种JDBC的简化方案,既然是和 ...
- webapi拦截请求
[AttributeUsage(AttributeTargets.Method)] public class WebApiSensitive : ActionFilterAttribute { pub ...
- 开启MSSQLServer跨服务器查询功能
首先在MSSQL客户端中进行如下图文操作配置 其次使用脚本进行操作配置 ---开启SQLServer 跨服务器查询功能 exec sp_configure 'show advanced options ...