mac搭建zookeeper

  1. 参考资料:http://blog.csdn.net/poechant/article/details/6633923   

                   http://www.cnblogs.com/yuyijq/p/4117634.html

  2. 下载zookeeper3.4.6 

  3. 解压,在系统中建立三个文件夹,server1,server2,server3 ,将解压之后得zookeeper拷贝到server1,2,3里面去,同时在server1-3文件夹下面再建立data,dataLog,logs三个文件夹

  4. 再data下面创建myid文件,内容分别为1,2,3

  5. 进入zookeeper得conf文件夹分别创建zoo.cfg,内容如下。不同文件夹下不一样(见注释):


     

tickTime=2000


#zookeeper1

initLimit=5

syncLimit=2

dataDir=/Users/mayanli/Downloads/server1/data

dataLogDir=/Users/mayanli/Downloads/server1/dataLog

clientPort=2181

server.1=127.0.0.1:2888:3888

server.2=127.0.0.1:2889:3889

server.3=127.0.0.1:2890:3890

#zookeeper2

tickTime=2000

initLimit=5

syncLimit=2

dataDir=/Users/mayanli/Downloads/server2/data

dataLogDir=/Users/mayanli/Downloads/server2/dataLog

clientPort=2182

server.1=127.0.0.1:2888:3888

server.2=127.0.0.1:2889:3889

server.3=127.0.0.1:2890:3890

#zookeeper3

tickTime=2000

initLimit=5

syncLimit=2

dataDir=/Users/mayanli/Downloads/server3/data

dataLogDir=/Users/mayanli/Downloads/server3/dataLog

clientPort=2183

server.1=127.0.0.1:2888:3888

server.2=127.0.0.1:2889:3889

server.3=127.0.0.1:2890:3890


修改完成之后,进入zookeeper得bin目录启动zookeeper,再进入客户端;




example1:

package com.zookeeper.main;


import java.io.IOException;

import java.util.concurrent.CountDownLatch;


import org.apache.zookeeper.CreateMode;

import org.apache.zookeeper.KeeperException;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.Watcher.Event.EventType;

import org.apache.zookeeper.ZooDefs.Ids;

import org.apache.zookeeper.ZooKeeper;

import org.apache.zookeeper.data.Stat;


public class SyscConfig implements Watcher {


private static final String host = "127.0.0.1";

private static final String port = "2181";

private static final int sessionTimeout = 1000;

private static final String znode = "/catepl";

private ZooKeeper zk;

private CountDownLatch cdl = new CountDownLatch(1);


@Override

public void process(WatchedEvent event) {

if (event.getState().SyncConnected == Event.KeeperState.SyncConnected) {

cdl.countDown();

}


}


public SyscConfig() {

try {

zk = new ZooKeeper(host + ":" + port, sessionTimeout, this);

} catch (IOException e) {

e.printStackTrace();

}

}


public void createNode(String data) {

try {

if (zk.exists(znode, this) != null) {

try {

zk.setData(znode, data.getBytes(), -1);

} catch (KeeperException | InterruptedException e) {

e.printStackTrace();

}

} else {

zk.create(znode, data.getBytes(), Ids.OPEN_ACL_UNSAFE,

CreateMode.PERSISTENT);

}

} catch (KeeperException | InterruptedException e1) {

e1.printStackTrace();

}

}


public void close() {

try {

zk.close();

} catch (InterruptedException e) {

e.printStackTrace();

}

}


public static void main(String[] args) {

SyscConfig syscConfig = new SyscConfig();

syscConfig.createNode("zhenglong");

syscConfig.close();

}

}

持续监听:

package com.zookeeper.main;


import java.io.IOException;

import java.util.concurrent.CopyOnWriteArrayList;


import org.apache.zookeeper.AsyncCallback.StatCallback;

import org.apache.zookeeper.CreateMode;

import org.apache.zookeeper.KeeperException;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.ZooDefs.Ids;

import org.apache.zookeeper.ZooKeeper;

import org.apache.zookeeper.data.Stat;


public class ZookeeperExample implements Watcher, StatCallback, Runnable {


private volatile boolean dead = true;

private static String znode = "/createpl";

private CopyOnWriteArrayList<String> set = new CopyOnWriteArrayList<String>();


public ZooKeeper zk;

public Stat stat;


public ZookeeperExample() {

try {

zk = new ZooKeeper("127.0.0.1:2181", 1000, this);

if (zk.exists(znode, this) == null) {

zk.create(znode, "招聘|常规招聘=56".getBytes(), Ids.OPEN_ACL_UNSAFE,

CreateMode.PERSISTENT);

}

stat = new Stat();

System.err.println("第一次获取到catepl的数据如下");

System.err.println(new String(zk.getData(znode, true, stat)));

System.err.println(stat);

} catch (IOException e) {

e.printStackTrace();

} catch (KeeperException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}


}


@Override

public void processResult(int rc, String path, Object ctx, Stat stat) {

System.err.println(11);


}


/**

* 监视事件被触发时执行此方法。

*/

public void process(WatchedEvent event) {

String path = event.getPath();

if (event.getType() == Event.EventType.None) {

// 节点没有发生改变,无节点创建、无接点删除、节点数据未改变、子节点未改变

// 那么说明可能是会话状态发生了改变

switch (event.getState()) {

case SyncConnected:

//

System.err.println(" 此客户端处于连接状态,不需要做任何事");

break;

case Expired:

// 会话失效,结束

this.close();

break;

}

} else {

// 状态改变了,检查是否znode节点值改变。如果改变则取出

System.err.println(112233);

if (path != null && path.equals(znode)) {

zk.exists(znode, true, this, null);

}

}

}


@Override

public void run() {

synchronized (this) {

try {

wait();

} catch (InterruptedException e) {

e.printStackTrace();

}

}

}


public void close() {

synchronized (this) {

this.notifyAll();

}

}


public static void main(String[] args) {

ZookeeperExample temp = new ZookeeperExample();

new Thread(new ZookeeperExample()).start();

try {

temp.zk.setData(znode, "第二次放入的数据".getBytes(),

temp.stat.getVersion());

} catch (KeeperException e) {

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}

/**

* synchronized (temp) { try { System.err.println("wait.....");

* temp.wait(); } catch (InterruptedException e) { e.printStackTrace();

* } }

*/

}

}


package com.chongshi.test;


import java.io.IOException;

import java.util.concurrent.CountDownLatch;


import org.apache.zookeeper.CreateMode;

import org.apache.zookeeper.KeeperException;

import org.apache.zookeeper.WatchedEvent;

import org.apache.zookeeper.Watcher;

import org.apache.zookeeper.Watcher.Event.KeeperState;

import org.apache.zookeeper.ZooDefs.Ids;

import org.apache.zookeeper.ZooKeeper;

import org.w3c.dom.ProcessingInstruction;


public class ZKTest {


static ZooKeeper zk = null;

static CountDownLatch cdl = new CountDownLatch(1);


static {

try {

zk = new ZooKeeper("127.0.0.1:2181", 1000, new MyWatcher(cdl));

} catch (IOException e) {

e.printStackTrace();

}

}


public static void main(String[] args) {

try {

cdl.await();

} catch (InterruptedException e1) {

e1.printStackTrace();

}

try {

zk.create("/s", "test".getBytes(), Ids.OPEN_ACL_UNSAFE,

CreateMode.PERSISTENT);

} catch (KeeperException e) {

// TODO Auto-generated catch block

e.printStackTrace();

} catch (InterruptedException e) {

e.printStackTrace();

}


}


}


class MyWatcher implements Watcher {


private CountDownLatch countDownLatch;


public MyWatcher(CountDownLatch countDownLatch) {

this.countDownLatch = countDownLatch;

}


public void process(WatchedEvent event) {

if (event.getState() == KeeperState.SyncConnected) {

System.err.println("连接上");

countDownLatch.countDown();

}

}

}


你可能感兴趣的:(mac搭建zookeeper)