日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

zk 08之:Curator之一:zk客户端Curator

發(fā)布時(shí)間:2023/12/9 编程问答 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 zk 08之:Curator之一:zk客户端Curator 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

Curator是Netflix公司開(kāi)源的一個(gè)Zookeeper客戶端,與Zookeeper提供的原生客戶端相比,Curator的抽象層次更高,簡(jiǎn)化了Zookeeper客戶端編程。

它包含以下幾個(gè)組件:

Componentdescription
RecipesImplementations of some of the common ZooKeeper “recipes”. The implementations are built on top of the Curator Framework.
FrameworkThe 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.
UtilitiesVarious utilities that are useful when using ZooKeeper.
ClientA replacement for the bundled ZooKeeper class that takes care of some low-level housekeeping and provides some useful utilities.
ErrorsHow Curator deals with errors, connection issues, recoverable exceptions, etc.
ExtensionsThe 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代碼:

示例一:常見(jiàn)的添加、修改、刪除示例:

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. 創(chuàng)建永久性節(jié)點(diǎn) client.create().creatingParentContainersIfNeeded().withMode(CreateMode.PERSISTENT).withACL(ZooDefs.Ids.OPEN_ACL_UNSAFE).forPath(path, "hello, zk".getBytes());System.out.println("b");//b. 創(chuàng)建臨時(shí)節(jié)點(diǎn)client.create().withMode(CreateMode.EPHEMERAL).forPath("/temptest5", "hello".getBytes()); System.out.println("c");//c.獲取節(jié)點(diǎn)值byte[] buf = client.getData().forPath(path);System.out.println("get data path:"+path+", data:"+new String(buf));System.out.println("d");//d.設(shè)置節(jié)點(diǎn)值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.刪除節(jié)點(diǎn) 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();}}

示例二:臨時(shí)節(jié)點(diǎn)不能有子節(jié)點(diǎn)、不能級(jí)聯(lián)創(chuàng)建示例:

/*** 級(jí)聯(lián)創(chuàng)建臨時(shí)節(jié)點(diǎn)測(cè)試,結(jié)果會(huì)創(chuàng)建不成功,報(bào)錯(cuò)* @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. 創(chuàng)建臨時(shí)節(jié)點(diǎn)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創(chuàng)建臨時(shí)節(jié)點(diǎn)的時(shí)候,不能創(chuàng)建級(jí)聯(lián)的節(jié)點(diǎn),下面的create會(huì)報(bào)錯(cuò)String path2 = "/linshi/test";//b. 創(chuàng)建臨時(shí)節(jié)點(diǎn)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、臨時(shí)節(jié)點(diǎn)何時(shí)失效演示(在會(huì)話結(jié)束時(shí)被移除)

/*** 臨時(shí)節(jié)點(diǎn)什么時(shí)候失效?是在本次會(huì)話結(jié)束時(shí)* @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. 創(chuàng)建臨時(shí)節(jié)點(diǎn)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();}}

?

?

總結(jié)

以上是生活随笔為你收集整理的zk 08之:Curator之一:zk客户端Curator的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。