CuratorAPI讲解

zkServer.sh start
zkServer.sh status
zkCli.sh
ls /super/c1
get /super/c1
get /super/c2
<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.abcd.curator</groupId>
  <artifactId>curator</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  
  <properties>
		<junit.version>4.12</junit.version>
  </properties>
  
  <dependencies>
	<dependency>
		<groupId>junit</groupId>
		<artifactId>junit</artifactId>
		<version>4.9</version>
	</dependency>	
	<dependency>
	    <groupId>org.apache.curator</groupId>
	    <artifactId>curator-framework</artifactId>
	    <version>2.4.2</version>
	</dependency>
  </dependencies>
	
  
</project>
import java.util.List;

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.data.Stat;

public class CuratorBase {
	
	/** zookeeper地址 */
	static final String CONNECT_ADDR = "localhost:2181";
	/** session超时时间 */
	static final int SESSION_OUTTIME = 5000;//ms 
	
	public static void main(String[] args) throws Exception {
		
		//1 重试策略:初试时间为1s 重试10次
		RetryPolicy retryPolicy = new ExponentialBackoffRetry(1000, 10);
		//2 通过工厂创建连接
		CuratorFramework cf = CuratorFrameworkFactory.builder()
					.connectString(CONNECT_ADDR)
					.sessionTimeoutMs(SESSION_OUTTIME)
					.retryPolicy(retryPolicy)
//					.namespace("super")
					.build();
		//3 开启连接
		cf.start();
		
//		System.out.println(States.CONNECTED);
//		System.out.println(cf.getState());
		
		// 新加、删除
		//4 建立节点 指定节点类型(不加withMode默认为持久类型节点)、路径、数据内容
//		cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c1","c1内容".getBytes());
		
		//5 删除节点
//		cf.delete().guaranteed().deletingChildrenIfNeeded().forPath("/super");
		
		// 读取、修改
		//创建节点
//		cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c1","c1内容".getBytes());
//		cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT).forPath("/super/c2","c2内容".getBytes());
		//读取节点
//		String ret1 = new String(cf.getData().forPath("/super/c2"));
//		System.out.println(ret1);
		//修改节点
//		cf.setData().forPath("/super/c2", "修改c2内容".getBytes());
//		String ret2 = new String(cf.getData().forPath("/super/c2"));
//		System.out.println(ret2);	
		
		// 绑定回调函数
//		ExecutorService pool = Executors.newCachedThreadPool();
//		cf.create().creatingParentsIfNeeded().withMode(CreateMode.PERSISTENT)
//		.inBackground(new BackgroundCallback() {
//			@Override
//			public void processResult(CuratorFramework cf, CuratorEvent ce) throws Exception {
//				System.out.println("code:" + ce.getResultCode());
//				System.out.println("type:" + ce.getType());
//				System.out.println("线程为:" + Thread.currentThread().getName());
//			}
//		}, pool)
//		.forPath("/super/c3","c3内容".getBytes());
//		
//		System.out.println("主线程:" + Thread.currentThread().getName());
//		
//		Thread.sleep(Integer.MAX_VALUE);
		
		
		// 读取子节点getChildren方法 和 判断节点是否存在checkExists方法
//		List<String> list = cf.getChildren().forPath("/super");
//		for(String p : list){
//			System.out.println(p);
//		}
		
//		Stat stat = cf.checkExists().forPath("/super/c3");
//		Stat stat = cf.checkExists().forPath("/super/c4");
//		System.out.println(stat);
//		
//		Thread.sleep(2000);
		cf.delete().guaranteed().deletingChildrenIfNeeded().forPath("/super");
		
		
		//cf.delete().guaranteed().deletingChildrenIfNeeded().forPath("/super");
		
		cf.close();
		
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值