zjtest7-redis:/root/zk# cat test_zk.pl use ZooKeeper; use AnyEvent; use AE; use Data::Dumper; use IO::Socket; sub check_port { my $server='120.55.118.6'; my $port=shift; $sock = IO::Socket::INET->new(PeerAddr => $server, PeerPort => $port, Proto => 'tcp'); if ($sock) {return 1} else {return 0 }; }; my @port=(2181,2182,2183); @zk_port=(); foreach $port (@port){ my $result =&check_port($port); if ($result == 1){ push (@zk_port,$port)}; }; print "@zk_port is @zk_port "; my @ip_list=(); if (scalar(@zk_port)==3){ print "333333333333333333333333 "; $zk = ZooKeeper->new(hosts => '120.55.118.6:2181,120.55.118.6:2182,120.55.118.6:2183') ; }; if (scalar(@zk_port)==2){ print "22222222222222222 "; $zk = ZooKeeper->new(hosts => "120.55.118.6:$zk_port[0],120.55.118.6:$zk_port[1]"); }; #my $zk = ZooKeeper->new(hosts => '120.55.118.6:2182,120.55.118.6:2183') ; print "-------------------- "; 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; }; zjtest7-redis:/root/zk# perl test_zk.pl @zk_port is 2182 2183 22222222222222222 -------------------- 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 当zk 节点 比如这里2181 挂后,需要手动程序了T掉已挂节点