Zookeeper入门demo1
java代码
package cn.test;
import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.framework.recipes.cache.ChildData;
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.Test;
public class ZookeeperAPITest {
@Test
//创建永久节点
public void createZnode() throws Exception {
//1.定制一个重试策略
/*
参数1:重试的间隔时间
参数2:重试的最大次数
*/
RetryPolicy retry = new ExponentialBackoffRetry(1000, 1);
//2.获取一个客户端对象
/*
参数1:要连接的zookeeper服务器列表
参数2:会话的超时时间
参数3:链接超时时间
参数4:重试策略
*/
String connectString="node01:2181,node02:2181,node03:2181";
CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, 8000, 8000, retry);
//3.开启客户端
client.start();
//4.创建节点 CreateMode:PERSISTENT永久节点 EPHEMERAL_SEQUENTIAL永久序列化节点EPHEMERAL临时节点EPHEMERAL_SEQUENTIAL临时序列化节点
client.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/hello2", "world".getBytes());
//5.关闭客户端
client.close();
}
@Test
//创建临时节点
public void createTmpZnode() throws Exception {
//1.定制一个重试策略
/*
参数1:重试的间隔时间
参数2:重试的最大次数
*/
RetryPolicy retry = new ExponentialBackoffRetry(1000, 1);
//2.获取一个客户端对象
/*
参数1:要连接的zookeeper服务器列表
参数2:会话的超时时间
参数3:链接超时时间
参数4:重试策略
*/
String connectString="node01:2181,node02:2181,node03:2181";
CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, 8000, 8000, retry);
//3.开启客户端
client.start();
//4.创建节点 CreateMode:PERSISTENT永久节点 EPHEMERAL_SEQUENTIAL永久序列化节点EPHEMERAL临时节点EPHEMERAL_SEQUENTIAL临时序列化节点
client.create().creatingParentsIfNeeded().withMode(CreateMode.EPHEMERAL).forPath("/hello3", "world".getBytes());
//5.关闭客户端
Thread.sleep(5000);//睡眠5秒,看临时节点
client.close();
}
@Test
//修改节点数据
public void setZnode() throws Exception {
//1.定制一个重试策略
/*
参数1:重试的间隔时间
参数2:重试的最大次数
*/
RetryPolicy retry = new ExponentialBackoffRetry(1000, 1);
//2.获取一个客户端对象
/*
参数1:要连接的zookeeper服务器列表
参数2:会话的超时时间
参数3:链接超时时间
参数4:重试策略
*/
String connectString="node01:2181,node02:2181,node03:2181";
CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, 8000, 8000, retry);
//3.开启客户端
client.start();
//4.修改节点数据
client.setData().forPath("/hello2", "world King".getBytes());
//5.关闭客户端
client.close();
}
@Test
//获取节点数据
public void getZnode() throws Exception {
//1.定制一个重试策略
/*
参数1:重试的间隔时间
参数2:重试的最大次数
*/
RetryPolicy retry = new ExponentialBackoffRetry(1000, 1);
//2.获取一个客户端对象
/*
参数1:要连接的zookeeper服务器列表
参数2:会话的超时时间
参数3:链接超时时间
参数4:重试策略
*/
String connectString="node01:2181,node02:2181,node03:2181";
CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, 8000, 8000, retry);
//3.开启客户端
client.start();
//4.修改节点数据
byte[] bytes = client.getData().forPath("/hello2");
System.out.println(new String(bytes));
//5.关闭客户端
client.close();
}
@Test
//watch机制
public void watchZnode() throws Exception {
//1.定制一个重试策略
/*
参数1:重试的间隔时间
参数2:重试的最大次数
*/
RetryPolicy retry = new ExponentialBackoffRetry(1000, 1);
//2.获取一个客户端对象
/*
参数1:要连接的zookeeper服务器列表
参数2:会话的超时时间
参数3:链接超时时间
参数4:重试策略
*/
String connectString="node01:2181,node02:2181,node03:2181";
CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, 8000, 8000, retry);
//3.开启客户端
client.start();
//4.创建节点的cache
TreeCache treeCache = new TreeCache(client, "/hello2");
//5.自定义一个监听器
treeCache.getListenable().addListener(new TreeCacheListener() {
@Override
public void childEvent(CuratorFramework client, TreeCacheEvent event) throws Exception {
// TODO Auto-generated method stub
ChildData data = event.getData();
if(data!=null) {
switch (event.getType()) {
case NODE_ADDED:
System.out.println("监测到有节点新增");
break;
case NODE_REMOVED:
System.out.println("监听到有节点被移除");
break;
case NODE_UPDATED:
System.out.println("监控到节点被更新");
break;
default:
break;
}
}
}
});
//开始监听
treeCache.start();
Thread.sleep(100000);
}
}
pom.xml配置
<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>cn.test</groupId>
<artifactId>zookeeper_api_demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<!-- <repositories> <repository><id>cloudera</id> <url>https://repository.cloudera.com/artifactory/cloudera-repos/</url>
</repository> </repositories> -->
<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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>RELEASE</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
</dependencies>
<build>
<plugins>
<!-- java编译插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>UTF-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
还没有评论,来说两句吧...