zookeeper-server-client 简单例子

客户端将初始值加1,然后写入server。
2个客户端输出的值的顺序是一致的。 说明客户端可已感知其他客户端的处理结果。


github 地址
[url]https://github.com/noobthinker/zookeeper-simple-demo[/url]

贴代码区域

server

public class Server {

NIOServerCnxnFactory serverCnxnFactory;

public void zkStart(Conf conf) throws IOException, InterruptedException {
ZooKeeperServer zkServer = new ZooKeeperServer();
File dataDirectory = new File(conf.DIR);
FileTxnSnapLog ftxn = new FileTxnSnapLog(dataDirectory, dataDirectory);
zkServer.setTxnLogFactory(ftxn);
zkServer.setTickTime(conf.TICK_TIME);
serverCnxnFactory = new NIOServerCnxnFactory();
serverCnxnFactory.configure(new InetSocketAddress(conf.PORT), conf.MAX_CLIENT_CONNECTIONS);
serverCnxnFactory.startup(zkServer);
}
}


client

public class Client implements Watcher, AsyncCallback.StatCallback {

ZooKeeper zk;
boolean dead;
String znode;
Stat stat;
int index;

public void initZkClient(String ipport,int timetout,String znode) {
try {
this.znode = znode;
zk = new ZooKeeper(ipport, timetout, this);
stat = zk.exists(znode, false);
if(null == stat){
zk.create(znode,new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE,
CreateMode.PERSISTENT);
}
zk.getData(znode,this,stat);
} catch (IOException e) {
e.printStackTrace();
}catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}

public void processResult(int rc, String s, Object o, Stat stat) {
boolean exists;
switch (rc) {
case KeeperException.Code.Ok:
exists = true;
break;
case KeeperException.Code.NoNode:
exists = false;
break;
case KeeperException.Code.SessionExpired:
case KeeperException.Code.NoAuth:
dead = true;
return;
default:
zk.exists(znode, true, this, null);
return;
}

byte b[] = null;
if (exists) {
try {
b = zk.getData(znode, false, null);
System.out.println(new String(b));
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
return;
}
}
}

public void process(WatchedEvent event) {
String path = event.getPath();
if (event.getType() == Event.EventType.None) {
switch (event.getState()) {
case SyncConnected:
break;
case Expired:
dead = true;
break;
}
} else {
if (path != null && path.equals(znode)) {
zk.exists(znode, true, this, null);
}
}
}

public void dataMaker(int i){
index=i;
int max=index+10;
boolean run=true;
while(run){
try {
zk.setData(znode, ((index++) + "").getBytes(), -1);
Thread.sleep(50);
if(index>max){
run=false;
zk.close();
}
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}catch (Exception e){
e.printStackTrace();
}
}
}
}


conf

public class Conf {
public String DIR;
public int TICK_TIME;
public int PORT;
public int MAX_CLIENT_CONNECTIONS;
}



test 类

server

public class ZkServer {
public static void main(String[]args){
Conf conf = new Conf();
conf.DIR="/zookeeper";
conf.PORT=22801;
conf.TICK_TIME=2000;
conf.MAX_CLIENT_CONNECTIONS=60;
Server srv = new Server();
try {
srv.zkStart(conf);
} catch (IOException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

client1

public class ZkClient1 {
public static void main(String[]args){
Client client = new Client();
client.initZkClient("localhost:22801",3000,"/zka");
client.dataMaker(0);
}
}

client1 输出

0 20 1 21 2 22 3 23 4 24 5 25 6 26 7 27 8 28 9 29 10 30


client2

public class ZkClient2 {
public static void main(String[]args){
Client client = new Client();
client.initZkClient("localhost:22801",3000,"/zka");
client.dataMaker(20);
}
}


client2输出

20 1 21 2 22 3 23 4 24 5 25 6 26 7 27 8 28 9 29 10 30


2个端输出一致。
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值