三種連線ZooKeeper的方式
1. 使用客戶端命令連線Zookeeper 連線Server 使用命令./zkCli.sh -server 127.0.0.1:2181
連線成功後返回如下資訊:
Connecting to 127.0.0.1:2181
2018-06-20 00:13:40,884 [myid:] - INFO [main:[email protected]] - Client environment:zookeeper.version=3.4.12-e5259e437540f349646870ea94dc2658c4e44b3b, built on 03/27/2018 03:55 GMT
2018-06-20 00:13:40,887 [myid:] - INFO [main:
WATCHER::
WatchedEvent state:SyncConnected type:None path:null 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 輸入命令help 顯示所有命令
ZooKeeper -server host:port cmd args stat path [watch] set path data [version] ls path [watch] delquota [-n|-b] path ls2 path [watch] setAcl path acl setquota -n|-b val path history redo cmdno printwatches on|off delete path [version] sync path listquota path rmr path get path [watch] create [-s] [-e] path data acl addauth scheme auth quit getAcl path close connect host:port 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 注意 ls,get,stat 命令有 [watch]引數,可以用來監控節點的變化
命令列工具的一些常用操作命令如下: connect 切換客戶端
connect 127.0.0.1:2182 1 create 使用create命令,可以建立一個Zookeeper節點, create [-s] [-e] path data acl 其中,-s或-e分別指定節點特性,順序或臨時節點,若不指定,則表示持久節點;acl用來進行許可權控制。
使用 命令建立zk-test順序節點
[zk: 127.0.0.1:2181(CONNECTED) 96] create -s /zk-test 0 Created /zk-test0000000003 1 2 使用 create -e /zk-test 123 命令建立zk-test臨時節點
[zk: 127.0.0.1:2181(CONNECTED) 96] create -s /zk-test 0 Created /zk-test0000000003 1 2 使用 create /zk-permanent 123 命令建立zk-permanent永久節點
[zk: 127.0.0.1:2182(CONNECTED) 11] create /zk-permanent 123 Created /zk-permanent
//檢視所有節點 [zk: 127.0.0.1:2182(CONNECTED) 12] ls / [test0000000000, zk-permanent, zookeeper, test, zk-test0000000003] 1 2 3 4 5 6 7 讀取節點 ls path [watch] ls命令可以列出Zookeeper指定節點下的所有第一級子節點 get path [watch] 可以獲取Zookeeper指定節點的資料內容和屬性資訊 ls2 path [watch] 比ls顯示更多資訊
命令輸出結果如下:
[zk: 127.0.0.1:2182(CONNECTED) 12] ls / [test0000000000, zk-permanent, zookeeper, test, zk-test0000000003]
[zk: 127.0.0.1:2182(CONNECTED) 13] ls /test [node4, node5, node2, node3, node8, node6, node7, node1]
[zk: 127.0.0.1:2182(CONNECTED) 14] get /test 123 cZxid = 0x200000010 ctime = Tue Jun 19 20:00:12 PDT 2018 mZxid = 0x200000010 mtime = Tue Jun 19 20:00:12 PDT 2018 pZxid = 0x300000007 cversion = 12 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 8
[zk: 127.0.0.1:2182(CONNECTED) 15] ls2 /test [node4, node5, node2, node3, node8, node6, node7, node1] cZxid = 0x200000010 ctime = Tue Jun 19 20:00:12 PDT 2018 mZxid = 0x200000010 mtime = Tue Jun 19 20:00:12 PDT 2018 pZxid = 0x300000007 cversion = 12 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 3 numChildren = 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 更新節點 使用set命令,可以更新指定節點的資料內容,用法如下
set path data [version]
使用命令set /test/node1 1更新節點, 每更新一次節點dataVersion 都會變化
[zk: 127.0.0.1:2182(CONNECTED) 17] set /test/node1 1 cZxid = 0x200000011 ctime = Tue Jun 19 20:00:57 PDT 2018 mZxid = 0x30000000f mtime = Wed Jun 20 01:23:38 PDT 2018 pZxid = 0x200000011 cversion = 0 dataVersion = 6 aclVersion = 0 ephemeralOwner = 0x0 dataLength = 1 numChildren = 0 1 2 3 4 5 6 7 8 9 10 11 12 13 刪除節點 使用delete命令可以刪除Zookeeper上的指定節點,用法如下 delete path [version] 其中version也是表示資料版本,使用delete /zk-permanent 命令即可刪除/zk-permanent節點
2. 使用JAVA連線使用ZK POM新增引用
<dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.6</version> </dependency> 1 2 3 4 5 使用程式碼模擬增刪改查
import org.apache.zookeeper.*; import org.apache.zookeeper.data.Stat;
import java.io.IOException; import java.util.concurrent.CountDownLatch;
public class zkClient { public static void main(String[] args) throws IOException, KeeperException, InterruptedException { final CountDownLatch countDownLatch = new CountDownLatch(1);
ZooKeeper zk = new ZooKeeper("10.168.x.23:2181,10.168.x.23:2182,10.168.x.23:2183", 3000, new Watcher() { @Override public void process(WatchedEvent event) { if (event.getState() == Event.KeeperState.SyncConnected) { countDownLatch.countDown(); } System.out.println("Watch =>" + event.getType()); } }); countDownLatch.await();
System.out.println(zk.getState()); String node = "/app1"; Stat state = zk.exists(node, false); if (state == null) { System.out.println("建立節點"); String createResult = zk.create(node, "0".getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT); System.out.println(createResult); }
byte[] b = zk.getData(node, false, state); System.out.println("獲取data值 =》" + new String(b));
state = zk.setData(node, "1".getBytes(), state.getVersion()); System.out.println("after update, version changed to =>" + state.getVersion()); zk.delete(node,state.getVersion()); System.out.println("delete complete");
zk.close(); } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 3. 使用curator連線使用ZK 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.apache.zookeeper.data.Stat;
public class CuratorDemo {
final static String BasePath = "/curatorTest";
public static void main(String[] args) throws Exception { CuratorFramework curatorFramework = CuratorFrameworkFactory.builder().connectString("10.168.12.23:2181,10.168.12.23:2182,10.168.12.23:2183").sessionTimeoutMs(4000) .retryPolicy(new ExponentialBackoffRetry(1000, 3)) .namespace("curator").build();
curatorFramework.start();
Stat stat = curatorFramework.checkExists().forPath(BasePath + "/node1"); Object o; if (stat == null) {
o= curatorFramework.create().creatingParentContainersIfNeeded(). withMode(CreateMode.PERSISTENT).forPath(BasePath + "/node1", "0".getBytes()); }
//儲存Stat curatorFramework.getData().storingStatIn(stat).forPath(BasePath + "/node1");
//更新時使用State stat = curatorFramework.setData().withVersion(stat.getVersion()).forPath(BasePath + "/node1", "1".getBytes()); System.out.println("update => " + stat.getVersion());
curatorFramework.close(); } } --------------------- 作者:肥龍上天 來源:CSDN 原文:https://blog.csdn.net/yangsnow_rain_wind/article/details/80749402 版權宣告:本文為博主原創文章,轉載請附上博文連結!