zookeeper的使用

首先需要启动zookeeper

API使用方法:

package zookeeperutil;


import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;


public class ConnectionZk {
private static final String connectString="192.168.65.132:2181";
private static final int sessionTimeout=2000;
private static final String parentNode="/servers";
ZooKeeper zkClient=null;
public void connection() throws Exception{
zkClient=new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent event) {
System.out.println(event.getType()+","+event.getPath());
try {
zkClient.getChildren("/", true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void registerServer(String hostname) throws Exception{
zkClient.create(parentNode+"/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(parentNode+" is created");
}
public void handleBussiness(String hostname) throws Exception{
System.out.println(hostname+" is working");
Thread.sleep(Long.MAX_VALUE);
}
public static void main(String[] args) {
try {
ConnectionZk zk=new ConnectionZk();
zk.connection();
zk.registerServer(args[0]);
zk.handleBussiness(args[0]);
} catch (Exception e) {
e.printStackTrace();
}
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值