簡介#
Zookeeper是一個開源的分散式邊界服務,很多分散式開源專案都有使用到zookeeper。 它有很多方面的應用,如分散式鎖,分散式選舉等。
下載安裝#如果想嘗試最新的zookeeper可以在這裡下載最新的zookeeper檔案, 下載完成之後
tar -xvf apache-zookeeper-3.5.6.tar.gzcd ~/apache-zookeeper-3.5.6/confcp zoo_sample.cfg zoo.cfgzkServer.sh start # 預設呼叫zoo.cfg
部署#單節點部署
配置檔案
# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial # synchronization phase can takeinitLimit=10# sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.dataDir=~/zookeeper-3.5.6/data/zk# the port at which the clients will connectclientPort=2181# increase this if you need to handle more clientsmaxClientCnxns=60
單節點部署可以直接呼叫zkServer.sh start 啟動
多接點部署為了實驗目的可以在同一臺物理機器上部署多個zookeeper server。可以通過如下步驟來啟動多個zookeeper server
- copy配置檔案```cp conf/zoo.cfg conf/zoo1.cfgcp conf/zoo.cfg conf/zoo1.cfgcp conf/zoo.cfg conf/zoo1.cfg```- 修改配置檔案zoo1.cfg, zoo2.cfg, zoo3.cfg 三者的唯一不通點在於dataDir,clientPort,需要為三者設定不同的儲存目錄和埠號```##### zoo1.cfg tickTime=2000 initLimit=10 syncLimit=5 dataDir=/Users/jet/app/zookeeper-3.5.6/data/zk1 clientPort=2181 maxClientCnxns=60 server.1=localhost:2887:3887 server.2=localhost:2888:3888 server.3=localhost:2889:3889##### zoo2.cfg ... dataDir=/Users/jet/app/zookeeper-3.5.6/data/zk2 clientPort=2182 ...##### zoo3.cfg ... dataDir=/Users/jet/app/zookeeper-3.5.6/data/zk3 clientPort=2183 ...```- 建立zookeeper id, 在dataDir目錄下為三者分別建立`myid`檔案,myid 中指定zkserver的全域性唯一ID,如 1, 如下圖 ![Zookeeper Server](./assets/zkserver.png)
應用#通過zkAll.sh啟動zk1,2,3#Zookeeper config ~/zookeeper-3.5.6/bin/../confZooKeeper JMX enabled by defaultUsing config: ~/zookeeper-3.5.6/conf/zoo1.cfgStarting zookeeper ... STARTEDZookeeper config ~/zookeeper-3.5.6/bin/../confZooKeeper JMX enabled by defaultUsing config: ~/zookeeper-3.5.6/conf/zoo2.cfgStarting zookeeper ... STARTEDZookeeper config ~/zookeeper-3.5.6/bin/../confZooKeeper JMX enabled by defaultUsing config: ~/zookeeper-3.5.6/conf/zoo3.cfgStarting zookeeper ... STARTED
連線到zkServer
zkCli.sh -server localhost:2181Zookeeper config ~/zookeeper-3.5.6/bin/../confConnecting to localhost:2181......Welcome to ZooKeeper!2019-11-21 22:49:34,610 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1112] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)JLine support is enabled2019-11-21 22:49:34,628 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@959] - Socket connection established, initiating session, client: /127.0.0.1:57401, server: localhost/127.0.0.1:21812019-11-21 22:49:34,671 [myid:localhost:2181] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1394] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x10003be32c80000, negotiated timeout = 30000WATCHER::## 建立node[zk: localhost:2181(CONNECTED) 0] create tmpPath must start with / character[zk: localhost:2181(CONNECTED) 2] create /tmpCreated /tmp## 給節點賦值[zk: localhost:2181(CONNECTED) 4] set /tmp 'hello zookeeper'## 獲取節值[zk: localhost:2181(CONNECTED) 6] get /tmphello zookeeper## 刪除node[zk: localhost:2181(CONNECTED) 7] delete /tmp[zk: localhost:2181(CONNECTED) 8] get /tmporg.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /tmp
最新評論