[hadoop@Slave1 usr]$ tar -zxf /opt/sofeware/zookeeper-3.4.8.tar.gz -C /opt/modules/
[hadoop@Slave1 usr zookeeper-3.4.10]$ cp conf/zoo_sample.cfg conf/zoo.cfg
#dataDir属性设置zookeeper的数据文件存放的目录:
dataDir=/usr/zookeeper-3.4.10/zkData
#指定zookeeper集群中各个机器的信息:
server.1=Slave1.Hadoop:2888:3888
server.2=Slave2.Hadoop:2888:3888
server.3=Slave3.Hadoop:2888:3888
[MHadoop@Slave1 zookeeper-3.4.10]$ touch zkData/myid
[MHadoop@Slave1 zookeeper-3.4.10]$ echo 1 >> zkData/myid
[MHadoop@Slave1 zookeeper-3.4.10]$ cat zkData/myid
1
[MHadoop@Slave1 zookeeper-3.4.10]$ scp -r /usr/zookeeper-3.4.10 Slave2.MHadoop:/usr/zookeeper-3.4.10
[MHadoop@Slave1 zookeeper-3.4.10]$ scp -r /usr/zookeeper-3.4.10 Slave3.MHadoop:/usr/zookeeper-3.4.10
在其它机器上修改myid:
启动zookeeper集群:需要在各个机器上分别启动zookeeper。
[MHadoop@Slave1 zookeeper-3.4.10]$ bin/zkServer.sh start
[MHadoop@Slave2 zookeeper-3.4.10]$ bin/zkServer.sh start
[MHadoop@Slave3 zookeeper-3.4.10]$ bin/zkServer.sh start
package com.zcs.ZookeeperDemo;
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.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;
/**
* Hello world!
*
*/
public class TestZookeeper
{
private String connectString = "172.21.3.211:2181,172.21.3.212:2181,172.21.3.213:2181";
private int sessionTimeout = 90000;
private ZooKeeper zooKeeper;
@Before
public void init() throws IOException {
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
// TODO Auto-generated method stub
// List children = null;
// try {
// children = 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 child : children) {
// System.out.println(child);
// }
// System.out.println("------------分割线--------------");
}
});
}
//1.创建节点
@Test
public void createNode() throws KeeperException, InterruptedException {
String path = zooKeeper.create("/zcc", "test".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println(path);
}
//2.获取子节点并监控节点的变化
@Test
public void getDataAndWatch() throws KeeperException, InterruptedException {
List<String> children = zooKeeper.getChildren("/", true);
for(String child : children) {
System.out.println(child);
}
//使之程序处于监控状态
Thread.sleep(Long.MAX_VALUE);
}
//3.判断节点是否存在
@Test
public void exists() throws KeeperException, InterruptedException {
Stat stat = zooKeeper.exists("/zcs", false);
System.out.println(stat == null ? "not exist" : "exist");
}
}
服务器动态上下线案例分析:
DistributeServer .java
package com.zcs.ZookeeperDemo;
import java.io.IOException;
import java.util.Set;
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;
public class DistributeServer {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
// TODO Auto-generated method stub
DistributeServer server = new DistributeServer();
// 1. 连接zookeeper集群
server.getConnect();
// 2. 注册节点
server.regist(args[0]);
// 3. 业务逻辑处理
server.business();
}
private void business() throws InterruptedException {
// TODO Auto-generated method stub
Thread.sleep(Long.MAX_VALUE);
}
private void regist(String hostname) throws KeeperException, InterruptedException {
// TODO Auto-generated method stub
String create = zooKeeper.create("/servers/server", hostname.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(hostname +" is online..");
}
private String connectString = "172.21.3.211:2181,172.21.3.212:2181,172.21.3.213:2181";
private int sessionTimeout = 90000;
private ZooKeeper zooKeeper;
private void getConnect() throws IOException {
// TODO Auto-generated method stub
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
// TODO Auto-generated method stub
}
});
}
}
package com.zcs.ZookeeperDemo;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
public class DistributeClient {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
DistributeClient client = new DistributeClient();
// 1.获取集群连接
client.getConnect();
// 2.注册监听
client.getChildren();
// 3. 业务逻辑处理
client.business();
}
private void business() throws InterruptedException {
// TODO Auto-generated method stub
Thread.sleep(Long.MAX_VALUE);
}
private void getChildren() throws KeeperException, InterruptedException {
// TODO Auto-generated method stub
List<String> children = zooKeeper.getChildren("/servers", true);
ArrayList<String> hosts = new ArrayList<String>();
for (String child : children) {
byte[] data = zooKeeper.getData("/servers/"+child, false, null);
hosts.add(new String(data));
}
System.out.println(hosts);
}
private String connectString = "172.21.3.211:2181,172.21.3.212:2181,172.21.3.213:2181";
private int sessionTimeout = 90000;
private ZooKeeper zooKeeper;
private void getConnect() throws IOException {
// TODO Auto-generated method stub
zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
@Override
public void process(WatchedEvent event) {
// TODO Auto-generated method stub
try {
getChildren();
} catch (KeeperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
}
}