首先創(chuàng)建一個Maven項目勤讽,引入pom,
<dependencies>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-framework</artifactId>
<version>4.3.0</version>
</dependency>
<dependency>
<groupId>org.apache.curator</groupId>
<artifactId>curator-recipes</artifactId>
<version>4.3.0</version>
</dependency>
</dependencies>
- 創(chuàng)建會話
public static void main(String[] args) throws Exception{
RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 3);
CuratorFramework client = CuratorFrameworkFactory.newClient("127.0.0.1:2181", retryPolicy);
client.start();
Thread.sleep(Integer.MAX_VALUE);
}
輸出結(jié)果:
2021-05-04 11:19:38.354 [main] INFO o.a.curator.framework.imps.CuratorFrameworkImpl - Starting
2021-05-04 11:19:38.358 [main] INFO org.apache.zookeeper.ZooKeeper - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=60000 watcher=org.apache.curator.ConnectionState@76a3e297
2021-05-04 11:19:38.362 [main] INFO org.apache.zookeeper.common.X509Util - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2021-05-04 11:19:38.374 [main] INFO org.apache.zookeeper.ClientCnxnSocket - jute.maxbuffer value is 4194304 Bytes
2021-05-04 11:19:38.380 [main] INFO org.apache.zookeeper.ClientCnxn - zookeeper.request.timeout value is 0. feature enabled=
2021-05-04 11:19:38.390 [main-SendThread(127.0.0.1:2181)] INFO org.apache.zookeeper.ClientCnxn - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2021-05-04 11:19:38.393 [main] INFO o.a.curator.framework.imps.CuratorFrameworkImpl - Default schema
2021-05-04 11:19:38.407 [main-SendThread(127.0.0.1:2181)] INFO org.apache.zookeeper.ClientCnxn - Socket connection established, initiating session, client: /127.0.0.1:61954, server: localhost/127.0.0.1:2181
2021-05-04 11:19:38.446 [main-SendThread(127.0.0.1:2181)] INFO org.apache.zookeeper.ClientCnxn - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x1003dddef680000, negotiated timeout = 40000
2021-05-04 11:19:38.451 [main-EventThread] INFO o.a.curator.framework.state.ConnectionStateManager - State change: CONNECTED
- 創(chuàng)建節(jié)點
private static String path = "/zk-test/create-node";
private static CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("127.0.0.1:2181")
.sessionTimeoutMs(5000)
.retryPolicy(new ExponentialBackoffRetry(1000,3))
.build();
public static void main(String[] args) throws Exception{
client.start();
client.create()
.creatingParentsIfNeeded() // 如果父節(jié)點不存在待错,會遞歸的創(chuàng)建
// .withMode(CreateMode.EPHEMERAL) // 節(jié)點類型
.forPath(path, "init".getBytes()); // 內(nèi)容
Thread.sleep(30000);
}
- 刪除節(jié)點
private static String path = "/zk-test/delete-node";
private static CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("127.0.0.1:2181")
.sessionTimeoutMs(5000)
.retryPolicy(new ExponentialBackoffRetry(1000,3))
.build();
public static void main(String[] args) throws Exception{
client.start();
client.create()
.creatingParentsIfNeeded() // 如果父節(jié)點不存在,會遞歸的創(chuàng)建
.withMode(CreateMode.EPHEMERAL) // 節(jié)點類型
.forPath(path, "delete".getBytes()); // 內(nèi)容
Stat stat = new Stat();
byte[] bytes = client.getData().storingStatIn(stat).forPath(path);
System.out.println("============" + stat);
System.out.println("content = " + new String(bytes));
client.delete().deletingChildrenIfNeeded() // 遞歸刪除子節(jié)點
.withVersion(stat.getVersion())
.forPath(path);
byte[] bytes2 = client.getData().storingStatIn(stat).forPath(path);
System.out.println("after delete content = " + new String(bytes2));
Thread.sleep(30000);
}
- 讀取數(shù)據(jù)
private static String path = "/zk-test/get-node";
private static CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("127.0.0.1:2181")
.sessionTimeoutMs(5000)
.retryPolicy(new ExponentialBackoffRetry(1000,3))
.build();
public static void main(String[] args) throws Exception{
client.start();
client.create()
.creatingParentsIfNeeded() // 如果父節(jié)點不存在会钝,會遞歸的創(chuàng)建
.withMode(CreateMode.EPHEMERAL) // 節(jié)點類型
.forPath(path, "get".getBytes()); // 內(nèi)容
Stat stat = new Stat();
byte[] bytes = client.getData()
.storingStatIn(stat) // 把信息屬性賦值給stat對象
.forPath(path);
System.out.println("============" + stat);
System.out.println("content = " + new String(bytes));
Thread.sleep(30000);
}
- 更新數(shù)據(jù)
private static String path = "/zk-test/set-node";
private static CuratorFramework client = CuratorFrameworkFactory.builder()
.connectString("127.0.0.1:2181")
.sessionTimeoutMs(5000)
.retryPolicy(new ExponentialBackoffRetry(1000,3))
.build();
public static void main(String[] args) throws Exception{
client.start();
client.create()
.creatingParentsIfNeeded() // 如果父節(jié)點不存在纫雁,會遞歸的創(chuàng)建
.withMode(CreateMode.EPHEMERAL) // 節(jié)點類型
.forPath(path, "set".getBytes()); // 內(nèi)容
Stat stat = new Stat();
byte[] bytes = client.getData().storingStatIn(stat).forPath(path);
System.out.println("============" + stat);
System.out.println("content = " + new String(bytes));
client.setData().forPath(path, "newContent".getBytes());
byte[] bytes2 = client.getData().storingStatIn(stat).forPath(path);
System.out.println("============" + stat);
System.out.println("new content = " + new String(bytes2));
Thread.sleep(30000);
}