-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
158 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
<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/maven-v4_0_0.xsd"> | ||
<modelVersion>4.0.0</modelVersion> | ||
<parent> | ||
<groupId>com.tomshidi</groupId> | ||
<artifactId>demo</artifactId> | ||
<version>1.0.0</version> | ||
</parent> | ||
<artifactId>zookeeper-demo</artifactId> | ||
<name>zookeeper示例</name> | ||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.curator</groupId> | ||
<artifactId>curator-framework</artifactId> | ||
<version>2.12.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.curator</groupId> | ||
<artifactId>curator-recipes</artifactId> | ||
<version>2.12.0</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>com.google.collections</groupId> | ||
<artifactId>google-collections</artifactId> | ||
<version>1.0</version> | ||
</dependency> | ||
</dependencies> | ||
</project> |
22 changes: 22 additions & 0 deletions
22
zookeeper-demo/src/main/java/com/tomshidi/zkdemo/ZkDemoApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.tomshidi.zkdemo; | ||
|
||
import org.apache.curator.RetryPolicy; | ||
import org.apache.curator.framework.CuratorFramework; | ||
import org.apache.curator.framework.CuratorFrameworkFactory; | ||
import org.apache.curator.retry.ExponentialBackoffRetry; | ||
import org.apache.zookeeper.CreateMode; | ||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
|
||
/** | ||
* @author TomShiDi | ||
* @since 2024/1/23 13:51 | ||
*/ | ||
//@SpringBootApplication | ||
public class ZkDemoApplication { | ||
public static void main(String[] args) { | ||
// SpringApplication.run(ZkDemoApplication.class, args); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
spring: | ||
application: | ||
name: zookeeper-demo |
104 changes: 104 additions & 0 deletions
104
zookeeper-demo/src/test/java/com/tomshidi/zkdemo/ZkDemoApplicationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,104 @@ | ||
package com.tomshidi.zkdemo; | ||
|
||
import org.apache.curator.RetryPolicy; | ||
import org.apache.curator.framework.CuratorFramework; | ||
import org.apache.curator.framework.CuratorFrameworkFactory; | ||
import org.apache.curator.framework.recipes.cache.TreeCache; | ||
import org.apache.curator.framework.recipes.cache.TreeCacheEvent; | ||
import org.apache.curator.framework.recipes.cache.TreeCacheListener; | ||
import org.apache.curator.retry.ExponentialBackoffRetry; | ||
import org.apache.zookeeper.CreateMode; | ||
import org.junit.jupiter.api.AfterAll; | ||
import org.junit.jupiter.api.BeforeAll; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import java.nio.charset.StandardCharsets; | ||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.jupiter.api.Assertions.*; | ||
/** | ||
* | ||
* @author TomShiDi | ||
* @since 2024/1/23 14:33 | ||
*/ | ||
class ZkDemoApplicationTest { | ||
|
||
private static CuratorFramework client; | ||
|
||
@BeforeAll | ||
public static void init() { | ||
// 1.指定重试策略 | ||
// 参数1为重试间隔时间,参数2为最多重试次数 | ||
RetryPolicy exponentialBackoffRetry = new ExponentialBackoffRetry(100, 5); | ||
|
||
// 2.获取Zookeeper客户端对象 | ||
String clusterUrls = "192.168.232.201:2181,192.168.232.202:2181,192.168.232.203:2181"; | ||
client = CuratorFrameworkFactory.newClient(clusterUrls, exponentialBackoffRetry); | ||
|
||
// 3.启动客户端 | ||
client.start(); | ||
} | ||
|
||
/** | ||
* 创建节点 | ||
*/ | ||
@Test | ||
public void createNode() throws Exception { | ||
// 创建节点 | ||
client.create() | ||
.creatingParentsIfNeeded() | ||
.withMode(CreateMode.PERSISTENT) | ||
.forPath("/app/tomshidi/data2", "Hello world".getBytes(StandardCharsets.UTF_8)); | ||
} | ||
|
||
/** | ||
* 获取节点数据 | ||
* | ||
* @throws Exception 异常 | ||
*/ | ||
@Test | ||
public void getNodeData() throws Exception { | ||
byte[] bytes = client.getData() | ||
.forPath("/app/tomshidi/data2"); | ||
String data = new String(bytes); | ||
System.out.println("节点数据为:" + data); | ||
} | ||
|
||
/** | ||
* 删除节点 | ||
* | ||
* @throws Exception 异常 | ||
*/ | ||
@Test | ||
public void delNode() throws Exception { | ||
client.delete() | ||
.deletingChildrenIfNeeded() | ||
.forPath("/app"); | ||
} | ||
|
||
@Test | ||
public void watchNode() throws Exception { | ||
TreeCache treeCache = new TreeCache(client, "/app1"); | ||
treeCache.getListenable().addListener((curatorFramework, treeCacheEvent) -> { | ||
switch (treeCacheEvent.getType()) { | ||
case NODE_ADDED: | ||
System.out.println("监控到增加节点事件"); | ||
break; | ||
case NODE_REMOVED: | ||
System.out.println("监控到移除节点事件"); | ||
break; | ||
case NODE_UPDATED: | ||
System.out.println("监控到节点修改事件"); | ||
break; | ||
} | ||
}); | ||
treeCache.start(); | ||
|
||
TimeUnit.MINUTES.sleep(6); | ||
} | ||
|
||
@AfterAll | ||
public static void close() { | ||
client.close(); | ||
} | ||
} |