Skip to content

Commit

Permalink
feat: 增加zookeeper使用用例
Browse files Browse the repository at this point in the history
  • Loading branch information
TomShiDi committed Jan 23, 2024
1 parent 8e71ddf commit 44650de
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 0 deletions.
1 change: 1 addition & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
<module>model</module>
<module>base</module>
<module>mybatis-demo</module>
<module>zookeeper-demo</module>
</modules>
<parent>
<groupId>org.springframework.boot</groupId>
Expand Down
28 changes: 28 additions & 0 deletions zookeeper-demo/pom.xml
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>
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);
}
}
3 changes: 3 additions & 0 deletions zookeeper-demo/src/main/resources/bootstrap.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
spring:
application:
name: zookeeper-demo
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();
}
}

0 comments on commit 44650de

Please sign in to comment.