1.创建一个Maven工程
2.添加pom文件
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.8.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.10</version>
</dependency>
</dependencies>
3.拷贝log4j.properties文件到项目根目录
需要在项目的src/main/resources目录下,新建一个文件,命名为“log4j.properties”,在文件中填入。
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d %p [%c] - %m%n
log4j.appender.logfile=org.apache.log4j.FileAppender
log4j.appender.logfile.File=target/spring.log
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d %p [%c] - %m%n
2.常用操作
package zookeeper;
import org.apache.zookeeper.*;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.List;
import static org.apache.zookeeper.ZooDefs.*;
public class TestZookeeper {
private String connetcString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private int sessionTimeout = 2000;
ZooKeeper zooKeeper;
@Before
public void init() throws IOException {
zooKeeper = new ZooKeeper(connetcString, sessionTimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
// 获取子节点并监控数据的变化
// System.out.println("--------start-------");
// List<String> children = null;
// try {
// children = zooKeeper.getChildren("/", true);
// for (String child : children) {
// System.out.println(child);
// }
// } catch (KeeperException e) {
// e.printStackTrace();
// } catch (InterruptedException e) {
// e.printStackTrace();
// }
// System.out.println("--------end-------");
}
});
}
//1.创建节点
@Test
public void createNode() throws KeeperException, InterruptedException {
String path = zooKeeper.create("/atguigu", "dahaigezuishuai".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.print(path);
}
//2获取子节点并监控数据的变化
@Test
public void getDataAndWatch() throws KeeperException, InterruptedException {
Thread.sleep(Long.MAX_VALUE);
}
//3判断节点是否存在
@Test
public void exist() throws KeeperException, InterruptedException {
Stat stat = zooKeeper.exists("/banzhang", false);
System.out.print(stat==null ? "not exist" : "exist");
}
}
3.监听服务器节点动态上下线案例
(1)服务器端向Zookeeper注册代码
package zookeeper;
import org.apache.zookeeper.*;
import java.io.IOException;
public class DistributeServer {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
args = new String[]{"hadoop102"};
DistributeServer server = new DistributeServer();
//1.连接zookkeeper
server.getConnect();
//2.注册节点
server.regist(args[0]);
//3.业务逻辑处理
server.business();
}
private void business() throws InterruptedException {
Thread.sleep(Long.MAX_VALUE);
}
private void regist(String hostname) throws KeeperException, InterruptedException {
String path = zkClient.create("/servers/server", hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println(hostname+"is onlne");
}
private int sessiontimeout = 2000;
private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private ZooKeeper zkClient;
private void getConnect() throws IOException {
zkClient = new ZooKeeper(connectString, sessiontimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
}
});
}
}
(2)客户端代码
package zookeeper;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class DistributeClient {
public static void main(String[] args) throws IOException, KeeperException, InterruptedException {
DistributeClient client = new DistributeClient();
//1.获取zookeeper集群连接
client.getconnect();
//2.注册监听
client.getChildren();
//3.业务逻辑处理
client.bussiness();
}
private void bussiness() throws InterruptedException {
Thread.sleep(Long.MAX_VALUE);
}
private void getChildren() throws KeeperException, InterruptedException {
List<String> children = zkclient.getChildren("/servers", true);
ArrayList<String> hosts = new ArrayList<String>();
for(String child : children){
byte[] data = zkclient.getData("/servers/" + child, false, null);
hosts.add(new String(data));
}
//将所有在线主机名称打印到控制台
System.out.println(hosts);
}
private String connectstring = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private int sessiontimeout = 2000;
private ZooKeeper zkclient;
private void getconnect() throws IOException {
zkclient = new ZooKeeper(connectstring, sessiontimeout, new Watcher() {
public void process(WatchedEvent watchedEvent) {
try {
getChildren();
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
}
}