1.zookeeper版本:
2.切换到此目录:cd /usr/local/
3.解压:tar -zxvf xxx.tar.zip
4.切换到zookeeper目录:cd xxx
5.在zookeeper目录创建数据保存文件夹:vi zData
6.修改配置名(必须):mv zoo_sample.cfg zoo.cfg
7.打开zoo.cfg,配置之前创建zData的路径,还有集群的ip,id:
dataDir=/usr/local/zookeeper-3.4.11/data
server.0=192.168.192.128:2888:3888
server.1=192.168.192.129:2888:3888
server.2=192.168.192.130:2888:3888
8.在zData的文件下创建对应的myid:(server.0为对应的id)指令:vi myid,里面的内容为0或1或2,不用写server.
9.启动服务:在bin的文件下,bin/zkServer.sh start
10,查看服务启动状态:bin/zkServer.sh status
11.启客户端:bin/zkClient.sh(没有start)
12,三台服务器重复以上操作即可,第8步记得myid的内容是对应的Id
13.测试连接代码
package com.liang.zookeeper;
import java.io.IOException;
import java.util.List;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.ZooDefs.Ids;
import org.junit.Test;
public class ZookeeperTest {
String connectString="192.168.55.88:2181";
int sessionTimeout=3000;
ZooKeeper zooKeeper;
@Test
public void good() throws IOException, KeeperException, InterruptedException{
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent event) {
System.out.println(event.getPath());
System.out.println(event.getState());
List<String> list = null;
try {
list = zooKeeper.getChildren("/", true);
} catch (KeeperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(String stringList:list){
System.out.println(stringList);
}
}
});
String number=zooKeeper.create("/student","lovename".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
System.out.println(number);
//byte[] name=zooKeeper.getData("/student0000000003",true, null);
//String string =new String(name);
//System.out.println(string);
/* List<String> list=zooKeeper.getChildren("/", true);
for(String stringList:list){
System.out.println(stringList);
}
*/
//Thread.sleep(Long.MAX_VALUE);
}
@Test
public void good2() throws IOException, KeeperException, InterruptedException{
}
}
(2)第二例子:
package com.liang.zookeeper;
import java.io.IOException;
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;
public class ZooServer {
ZooKeeper zooKeeper;
private String connectString="192.168.55.88:2181";
private int sessionTimeout=2000;
private String parent="/server";
public void createConnection() throws IOException{
zooKeeper=new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent event) {
}
});
}
public void regist(String data) throws KeeperException, InterruptedException{
String name=zooKeeper.create(parent+"/servser", data.getBytes(), Ids.CREATOR_ALL_ACL, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("name:"+name);
}
public void bin() throws InterruptedException{
Thread.sleep(Long.MAX_VALUE);
}
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
ZooServer zooServer =new ZooServer();
zooServer.createConnection();
zooServer.regist(args[0]);
zooServer.bin();
}
}