zk 08之:Curator之一:zk客户端Curator
生活随笔
收集整理的這篇文章主要介紹了
zk 08之:Curator之一:zk客户端Curator
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
Curator是Netflix公司開源的一個Zookeeper客戶端,與Zookeeper提供的原生客戶端相比,Curator的抽象層次更高,簡化了Zookeeper客戶端編程。
它包含以下幾個組件:
| Recipes | Implementations of some of the common ZooKeeper “recipes”. The implementations are built on top of the Curator Framework. |
| Framework | The Curator Framework is a high-level API that greatly simplifies using ZooKeeper. It adds many features that build on ZooKeeper and handles the complexity of managing connections to the ZooKeeper cluster and retrying operations. |
| Utilities | Various utilities that are useful when using ZooKeeper. |
| Client | A replacement for the bundled ZooKeeper class that takes care of some low-level housekeeping and provides some useful utilities. |
| Errors | How Curator deals with errors, connection issues, recoverable exceptions, etc. |
| Extensions | The curator-recipes package implements the common recipes that are described in the ZooKeeper documentation. To avoid bloating that package, recipes/applications that have a vertical appeal will be put in separate “extension” packages using the naming convention curator-x-name. |
?
示例:
<?xml version="1.0"?> <project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"><modelVersion>4.0.0</modelVersion><groupId>com.sf.zkclient</groupId><artifactId>zkclient</artifactId><version>0.0.1-SNAPSHOT</version><name>zkclient</name><url>http://maven.apache.org</url><properties><curator.version>2.11.1</curator.version><project.build.sourceEncoding>UTF-8</project.build.sourceEncoding></properties><dependencies><dependency><groupId>org.apache.zookeeper</groupId><artifactId>zookeeper</artifactId><version>3.4.8</version><exclusions><exclusion><artifactId>slf4j-log4j12</artifactId><groupId>org.slf4j</groupId></exclusion><exclusion><artifactId>log4j</artifactId><groupId>log4j</groupId></exclusion></exclusions></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-framework</artifactId><version>${curator.version}</version></dependency><dependency><groupId>org.apache.curator</groupId><artifactId>curator-client</artifactId><version>${curator.version}</version><exclusions><exclusion><groupId>com.google.guava</groupId><artifactId>guava</artifactId></exclusion></exclusions></dependency><dependency><groupId>com.google.guava</groupId><artifactId>guava</artifactId><version>18.0</version></dependency> </dependencies> </project>java代碼:
示例一:常見的添加、修改、刪除示例:
public static void test() throws Exception {String address = "localhost:2181";CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3));try{client.start();String path = "/yongjiu2/yongjiu3/test5";//a. 創建永久性節點 client.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).forPath(path, "hello, zk".getBytes());System.out.println("b");//b. 創建臨時節點client.create().withMode(CreateMode.EPHEMERAL).forPath("/temptest5", "hello".getBytes()); System.out.println("c");//c.獲取節點值byte[] buf = client.getData().forPath(path);System.out.println("get data path:"+path+", data:"+new String(buf));System.out.println("d");//d.設置節點值client.setData().inBackground().forPath(path, "ricky".getBytes());System.out.println("e");//e.checkExistsStat stat = client.checkExists().forPath(path);if(stat==null){System.out.println("exec create path:"+path);}else {System.out.println("exec getData");}System.out.println("f");//f.刪除節點 client.delete().deletingChildrenIfNeeded().forPath(path);byte[] buf2 = client.getData().forPath(path);System.out.println("get data path:"+path+", data:"+new String(buf2));}finally {if(client!=null)client.close();}}示例二:臨時節點不能有子節點、不能級聯創建示例:
/*** 級聯創建臨時節點測試,結果會創建不成功,報錯* @throws Exception*/public static void test2() throws Exception {String address = "localhost:2181";CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3));try{client.start();String path = "/linshi";//b. 創建臨時節點client.create().withMode(CreateMode.EPHEMERAL).forPath(path, "hello".getBytes()); byte[] buf = client.getData().forPath(path);System.out.println("get data path:"+path+", data:"+new String(buf));//Zookeeper創建臨時節點的時候,不能創建級聯的節點,下面的create會報錯String path2 = "/linshi/test";//b. 創建臨時節點client.create().withMode(CreateMode.EPHEMERAL).forPath(path2, "hello".getBytes()); //byte[] buf2 = client.getData().forPath(path2);//System.out.println("get data path:"+path2+", data:"+new String(buf2)); TimeUnit.SECONDS.sleep(100 * 1000);}finally {if(client!=null)client.close();}}示例三:3、臨時節點何時失效演示(在會話結束時被移除)
/*** 臨時節點什么時候失效?是在本次會話結束時* @throws Exception*/public static void test3() throws Exception {String address = "localhost:2181";CuratorFramework client = CuratorFrameworkFactory.newClient(address, new ExponentialBackoffRetry(1000, 3));try{client.start();String path = "/linshi";//b. 創建臨時節點client.create().withMode(CreateMode.EPHEMERAL).forPath(path, "hello".getBytes()); byte[] buf = client.getData().forPath(path);System.out.println("get data path:"+path+", data:"+new String(buf));TimeUnit.SECONDS.sleep(100 * 1000);}finally {if(client!=null)client.close();}}?
?
總結
以上是生活随笔為你收集整理的zk 08之:Curator之一:zk客户端Curator的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机IMSI号码编码规则表
- 下一篇: ip_vs实现分析(2)