<?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.test</groupId>
<artifactId>train-rpc</artifactId>
<version>0.0.1</version>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<zookeeper.version>3.4.9</zookeeper.version>
<zkclient.version>0.2-SNAPSHOT</zkclient.version>
</properties>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
</dependency>
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.0.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-All</artifactId>
</dependency>
<!-- Msgpack序列化 -->
<dependency>
<groupId>org.msgpack</groupId>
<artifactId>msgpack</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
</dependencies>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.apache.zookeeper</groupId>
<artifactId>zookeeper</artifactId>
<version>${zookeeper.version}</version>
</dependency>
<!-- https://mvnrepository.com/artifact/com.101tec/zkclient -->
<dependency>
<groupId>com.101tec</groupId>
<artifactId>zkclient</artifactId>
<version>0.10</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-test</artifactId>
<version>2.4.2</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-All</artifactId>
<version>4.1.16.Final</version>
<!--<version>4.1.6.Final</version>-->
</dependency>
<!-- Msgpack序列化 -->
<dependency>
<groupId>org.msgpack</groupId>
<artifactId>msgpack</artifactId>
<version>0.6.12</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.12</version>
</dependency>
</dependencies>
</dependencyManagement>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<!-- see http://maven.apache.org/ref/current/maven-core/default-bindings.html#Plugin_bindings_for_jar_packaging -->
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
<version>2.5.2</version>
</plugin>
<plugin>
<artifactId>maven-deploy-plugin</artifactId>
<version>2.8.2</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
package com.test.zookeeper.nativedemo;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
/**
* Created by test
* 2020/9/28.
*/
public class ZooKeeperUtil {
/**
* 判断节点是否存在
* @param zooKeeper
* @param path
* @return
* @throws KeeperException
* @throws InterruptedException
*/
public static Stat exists(ZooKeeper zooKeeper, String path) throws KeeperException, InterruptedException {
Stat stat = zooKeeper.exists(path, false);
System.out.println("node exists = " + stat);
return stat;
}
/**
* 删除节点
* @param zooKeeper
* @param path
* @param version
* @throws KeeperException
* @throws InterruptedException
*/
public static void deleteNode(ZooKeeper zooKeeper, String path, int version) throws KeeperException, InterruptedException {
zooKeeper.delete(path, version);
System.out.printf("delete node sucess, nodepath = %s\r\n", path);
}
/**
* 根据节点查询节点内容
* @param zooKeeper
* @param path
* @return
* @throws KeeperException
* @throws InterruptedException
*/
public static String getData(ZooKeeper zooKeeper, String path) throws KeeperException, InterruptedException {
String data = null;
Stat stat = exists(zooKeeper, path);
byte[] bytes = zooKeeper.getData(path, null, stat);
data = new String(bytes);
System.out.printf("节点{%s} 对应的数据内容为{%s}\r\n", path, data);
return data;
}
public static String getData(ZooKeeper zooKeeper, String path, Stat stat) throws KeeperException, InterruptedException {
String data = null;
byte[] bytes = zooKeeper.getData(path, null, stat);
data = new String(bytes);
System.out.printf("节点{%s} 对应的数据内容为{%s}\r\n", path, data);
return data;
}
/**
* 更新节点内容
* @param zooKeeper
* @param path
* @param value
* @return
* @throws KeeperException
* @throws InterruptedException
*/
public static Stat updateNode(ZooKeeper zooKeeper, String path, byte[] value) throws KeeperException, InterruptedException {
Stat stat = exists(zooKeeper, path);
stat = zooKeeper.setData(path, value, stat.getVersion());
System.out.printf("节点{%s} 修改后的内容为{%s}\r\n", path, new String(value));
return stat;
}
/**
* 创建持久节点
* @param zooKeeper
* @param path
* @return
* @throws KeeperException
* @throws InterruptedException
*/
public static String createPersistentNode(ZooKeeper zooKeeper, String path) throws KeeperException, InterruptedException {
//判断节点是否存在,如果不存在则直接创建,否则先删除再创建
Stat stat = exists(zooKeeper, path);
if (null != stat) {
System.out.println(path + " exists,delete it first!");
deleteNode(zooKeeper, path, stat.getVersion());
} else {
System.out.println("节点不存在直接创建新节点");
}
String resultPath = zooKeeper.create(path, "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("node path =" + resultPath);
return resultPath;
}
/**
* 创建持久有序节点
* @param zooKeeper
* @param path
* @return
* @throws KeeperException
* @throws InterruptedException
*/
public static String createPersistentSequentialNode(ZooKeeper zooKeeper, String path) throws KeeperException, InterruptedException {
//有序节点不必判断节点是否存在,直接创建(可以用来创建全局id)
String resultPath = zooKeeper.create(path, "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT_SEQUENTIAL);
System.out.println("node path =" + resultPath);
return resultPath;
}
/**
* 创建临时节点
* @param zooKeeper
* @param path
* @return
* @throws KeeperException
* @throws InterruptedException
*/
public static String createEphemeralNode(ZooKeeper zooKeeper, String path) throws KeeperException, InterruptedException {
String resultPath = zooKeeper.create(path, "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
System.out.println("node path =" + resultPath);
return resultPath;
}
/**
* 创建临时有序节点
* @param zooKeeper
* @param path
* @return
* @throws KeeperException
* @throws InterruptedException
*/
public static String createEphemeralSequentialNode(ZooKeeper zooKeeper, String path) throws KeeperException, InterruptedException {
String resultPath = zooKeeper.create(path, "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
System.out.println("node path =" + resultPath);
return resultPath;
}
}
package com.test.zookeeper.nativedemo;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooKeeper;
import org.apache.zookeeper.data.Stat;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import java.util.concurrent.CountDownLatch;
/**
* Created by test
* 2020/9/28.
*/
public class ZooKeeperUtilTest {
public static void main(String[] args) {
try {
ZooKeeper zooKeeper = new ZooKeeper("127.0.0.1:2181", 4000, new Watcher() {
@Override
public void process(WatchedEvent event) {
System.out.println("event===" + event);
if (Event.KeeperState.SyncConnected.equals(event.getState())) {
System.out.println("syncConnect success");
}
}
});
System.out.println(zooKeeper.getState());
Thread.sleep(1000);
System.out.println("after sleep 1 second");
System.out.println(zooKeeper.getState());
// zooKeeper.create("/rootdemo","0".getBytes(),)
} catch (Exception e) {
e.printStackTrace();
} finally {
}
}
private ZooKeeper zooKeeper;
@Before
public void zooKeeperConnect() {
final CountDownLatch zkSemaphore = new CountDownLatch(1);
try {
zooKeeper = new ZooKeeper("127.0.0.1:2181", 3000, new Watcher() {
@Override
public void process(WatchedEvent event) {
if (Event.KeeperState.SyncConnected == event.getState()) {
zkSemaphore.countDown();
System.out.println("connected success!");
}
}
});
} catch (IOException e) {
e.printStackTrace();
}
}
@Test
public void testCreateNode_Ephemeral() {
String ephemaralNodePath = "/root_ephemaral";
try {
ZooKeeperUtil.createEphemeralNode(zooKeeper, ephemaralNodePath);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void testCreateNode_EphemeralSequential() {
String ephemaralNodePath = "/root_ephemaral_sequentail_";
try {
ZooKeeperUtil.createEphemeralSequentialNode(zooKeeper, ephemaralNodePath);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void testCreateNode_persistent() {
String persistentPath = "/root_persistent";
try {
ZooKeeperUtil.createPersistentNode(zooKeeper, persistentPath);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void testCreateNode_PersistentSequential() {
String nodePath = "/root_persistent_squential_";
try {
String persistentSequentialNode = ZooKeeperUtil.createPersistentSequentialNode(zooKeeper, nodePath);
Stat exists = ZooKeeperUtil.exists(zooKeeper, persistentSequentialNode);
ZooKeeperUtil.deleteNode(zooKeeper, persistentSequentialNode, exists.getVersion());
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Test
public void updateNode() {
String nodePath = "/root_ephemaraSequetiallNode";
try {
//1.创建节点
String sequetiallNode = ZooKeeperUtil.createEphemeralSequentialNode(zooKeeper, nodePath);
//2.获取创建的节点原有数据
String data = ZooKeeperUtil.getData(zooKeeper, sequetiallNode);
System.out.println("原有数据:" + data);
//3.修改数据
Stat stat2 = ZooKeeperUtil.updateNode(zooKeeper, sequetiallNode, "updated value".getBytes());
String data1 = ZooKeeperUtil.getData(zooKeeper, sequetiallNode, stat2);
System.out.println("修改后的数据:" + data1);
} catch (KeeperException e) {
e.printStackTrace();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}