1、匯入依賴
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
2.撰寫序列化類(解決獲取資料時候無法序列化而報錯)
import org.I0Itec.zkclient.exception.ZkMarshallingError;
public class ZkSerializer implements org.I0Itec.zkclient.serialize.ZkSerializer {
//序列化,資料--》byte[]
public byte[] serialize(Object o) throws ZkMarshallingError {
return String.valueOf(o).getBytes();
}
//反序列化,byte[]--->資料
public Object deserialize(byte[] bytes) throws ZkMarshallingError {
return new String(bytes);
}
}
3、常規操作(增刪查改)
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;
import org.apache.zookeeper.CreateMode;
import java.util.List;
public class ZkTest {
//zookeeper集群地址
public static final String URL = "192.168.112.101:2181,192.168.112.102:2181,192.168.112.103:2181";
//連接超時時間
public static final int TIME_OUT = 5000;
public static void main(String[] args) throws InterruptedException {
ZkClient client = new ZkClient(new ZkConnection(URL),TIME_OUT);
//設定序列化
client.setZkSerializer(new ZkSerializer());
//增加臨時節點
client.createEphemeral("/a1",123);
//增加永久節點
client.create("/a2",123, CreateMode.PERSISTENT);
//Thread.sleep(10000);
//洗掉節點
client.delete("/a2");
//查詢節點有哪些子節點
List<String> children = client.getChildren("/");
System.out.println(children);
//獲取節點資料
Object data = client.readData("/a3");
System.out.println(data);
//修改
client.writeData("/a3",456);
client.close();
}
}
4.監聽操作
import org.I0Itec.zkclient.IZkChildListener;
import org.I0Itec.zkclient.IZkDataListener;
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import org.apache.zookeeper.CreateMode;
import java.util.List;
public class ZkWatch {
//zookeeper集群地址
public static final String URL = "192.168.112.101:2181,192.168.112.102:2181,192.168.112.103:2181";
//連接超時時間
public static final int TIME_OUT = 5000;
public static void main(String[] args) throws InterruptedException {
ZkClient client = new ZkClient(new ZkConnection(URL),TIME_OUT);
//設定序列化
client.setZkSerializer(new SerializableSerializer());
//開始監聽節點變化
client.subscribeChildChanges("/", new IZkChildListener() {
@Override
public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception {
System.out.println(parentPath);
System.out.println(currentChilds);
}
});
//開始監聽節點資料變化
client.subscribeDataChanges("/a4", new IZkDataListener() {
@Override
public void handleDataChange(String dataPath, Object data) throws Exception {
System.out.println("dataPath:" + dataPath);
System.out.println("data:" + data);
}
@Override
public void handleDataDeleted(String dataPath) throws Exception {
System.out.println("dataPath:" + dataPath);
}
});
Thread.sleep(2000);
new Thread(new Runnable() {
@Override
public void run() {
client.writeData("/a4",56);
}
}).start();
Thread.sleep(60000);
client.close();
}
}
5.自定義zkCli.sh
5.1撰寫MyCli
import org.I0Itec.zkclient.ZkClient;
import org.I0Itec.zkclient.ZkConnection;
import org.I0Itec.zkclient.serialize.SerializableSerializer;
import java.util.List;
import java.util.Scanner;
public class MyCli {
public static void main(String[] args) throws InterruptedException {
ZkClient client = new ZkClient(new ZkConnection(args[0]),Integer.parseInt(args[01]));
//設定序列化
client.setZkSerializer(new SerializableSerializer());
Scanner sc = new Scanner(System.in);
loop:while(true){
String cmd = sc.nextLine();
String[] cmds = cmd.split(" ");
switch (cmds[0]){
case "list":
List<String> children = client.getChildren(cmds[1]);
for (String str:children) {
System.out.print(str + " ");
}
System.out.println();
case "quit":
client.close();
break loop;
}
}
sc.close();
}
}
5.2修改pom.xml檔案(添加打包插件)
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bigdata</groupId>
<artifactId>bigdata_zookeeper</artifactId>
<version>1.0</version>
<dependencies>
<!-- zookeeper -->
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>3.4.6</version>
</dependency>
<!-- zkClient 客戶端連接-->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- maven編譯插件-->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<testExcludes>
<testExclude>/src/test/**</testExclude>
</testExcludes>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<!-- maven專案打包插件-->
<plugin>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
<archive>
<manifest>
<mainClass>MyCli</mainClass> <!-- jar包程式的入口主類-->
</manifest>
</archive>
</configuration>
<executions>
<execution>
<id>make-assembly</id> <!-- this is used for inheritance merges -->
<phase>package</phase> <!-- 指定在打包節點執行jar包合并操作 -->
<goals>
<goal>single</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>
5.3.1打包(IDEA)

5.3.1打包(eclipse) 右鍵點擊專案名

5.4執行
上傳jar包至服務器
java -jar *.jar 引數1 引數2 ...
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/236070.html
標籤:java
