use ZooKeeper;
use AnyEvent;
use AE;
use Data::Dumper;
use IO::Socket;
my $zk = ZooKeeper->new(hosts => '120.55.118.6:2181');
my $stat = $zk->exists('/mysql');
unless ($stat){
$zk->create('/mysql');
}
my $created_path = $zk->create('/mysql/0001',
ephemeral => 1
);
print "----------------
";
print $created_path;
print "----------------
";
###获取数据
print Dumper($zk->get("/mysql/0001"));
###修改数据
$zk->set('/mysql/0001' =>'192.168.32.6');
print Dumper($zk->get("/mysql/0001"));
my $cv = AE::cv;
##所有子节点数组
my @children = $zk->get_children('/mysql', watcher => sub {
###事件状态
my $event = shift;
print "触发了事件.";
print $event->{'type'}."
";
print "事件状态.";
print $event->{'state'}."
";
$cv->send($event) });
print "------------------
";
print @children;
print "
";
###检测3306端口
sub check_port {
( $server, $port ) = ('127.0.0.1','3306');
$sock = IO::Socket::INET->new(PeerAddr => $server,
PeerPort => $port,
Proto => 'tcp');
print "$sock
";
if ($sock)
{return 1}
else
{$zk->close; print "close zk
"; exit 0 };
};
##定义watch
my $t = AnyEvent->timer(
after => 0,
interval => 5,
cb => &check_port
);
##不要再每秒打印时间
##undef $t;
my $child_event = $cv->recv;