配置
maven项目中的porn文件
<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>
资源文件夹下创建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
基础API操作
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;
public class TestZookeeper {
ZooKeeper zkCli;
/**
* 测试客户端对象
* @throws IOException
*/
@Before
public void testZkClient() throws IOException {
String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
int sessionTimeout = 10000;
zkCli = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent event) {
}
});
System.out.println("zooKeeper = " + zkCli);
}
/**
* 测试创建节点
* @throws KeeperException
* @throws InterruptedException
*/
@Test
public void testCreate() throws KeeperException, InterruptedException {
String PATH = zkCli.create("/zzuli/fei", "cai".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("PATH = " + PATH);
}
/**
* 监听节点的变化
* @throws Exception
*/
@Test
public void testGetChildren() throws Exception{
List<String> children = zkCli.getChildren("/zzuli", new Watcher() {
public void process(WatchedEvent event) {
System.out.println("节点已经发生了变化!");
}
});
System.out.println("children = " + children);
Thread.sleep(Long.MAX_VALUE);
}
/**
* 其他API
*/
@Test
public void testOtherApi() throws Exception{
// 判断节点是否存在
Stat exists = zkCli.exists("/zzuli/fei", false);
System.out.println("exists = " + exists);
// 获取节点内容 (stat 为null,就是不传结构体)
byte[] data = zkCli.getData("/zzuli/tang", false, null);
System.out.println("data = " + data.toString());
// 设置节点内容
Stat stat = zkCli.setData("/zzuli/tang", "tang ge is ugly".getBytes(), -1);
System.out.println("stat = " + stat);
// 删除节点
zkCli.delete("/zzuli/tang",-1);
}
}
实现服务器节点动态上下线
server端:
package com.zzuli.exer;
import org.apache.zookeeper.*;
public class Server {
private String connectString = "hadoop102:2181,hadoop103:2181,hadoop104:2181";
private int sessionTimeout = 10000;
private ZooKeeper zk = null;
private String parentNode = "/servers";
public static void main(String[] args) throws Exception {
Server server = new Server();
// 1 连接zookeeper集群
server.getConnect();
// 2 向集群注册服务器
server.registServer(args[0]);
// 3 启动业务功能
server.run();
Thread.sleep(Long.MAX_VALUE);
}
private void run() {
System.out.println("业务启动!");
}
private void registServer(String arg) throws Exception{
String path = zk.create(parentNode + "/server", arg.getBytes(),
ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("创建成功:path = " + path);
}
private void getConnect() throws Exception{
zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
public void process(WatchedEvent event) {
}
});
}
}
Client端:
package com.zzuli.exer;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import java.util.List;
public class Client {
ZooKeeper zk = null;
private String connectString="hadoop102:2181,hadoop103:2181,hadoop104:2181";
private int sessionTimeOut = 10000;
public static void main(String[] args) throws Exception{
// 1 创建zookeeper客户端对象
Client client = new Client();
client.init();
// 2 获取当前在线的服务器
client.connect();
// 3 保证与zk的连接
Thread.sleep(Long.MAX_VALUE);
}
private void connect() throws Exception{
List<String> children = zk.getChildren("/servers", new Watcher() {
public void process(WatchedEvent event) {
try {
connect();
} catch (Exception e) {
e.printStackTrace();
}
}
});
System.out.println("当前的连接数:"+children);
}
private void init() throws Exception {
zk = new ZooKeeper(connectString, sessionTimeOut, new Watcher() {
public void process(WatchedEvent event) {
}
});
System.out.println("客户端创建成功!");
}
}