cp zoo_sample.cfg zoo.cfg
運行 vi zoo.cfg 咱們先來看看配置html
# The number of milliseconds of each tick tickTime=2000 # The number of ticks that the initial # synchronization phase can take initLimit=10 # The number of ticks that can pass between # sending a request and getting an acknowledgement syncLimit=5 # the directory where the snapshot is stored. # do not use /tmp for storage, /tmp here is just # example sakes. dataDir=../zookeeper-data # the port at which the clients will connect clientPort=2181 # the maximum number of client connections. # increase this if you need to handle more clients #maxClientCnxns=60 # # Be sure to read the maintenance section of the # administrator guide before turning on autopurge. # # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance # # The number of snapshots to retain in dataDir #autopurge.snapRetainCount=3 # Purge task interval in hours # Set to "0" to disable auto purge feature #autopurge.purgeInterval=1
在這裏咱們保持默認配置先不變。更改一下dataDir這個值,注意:先在zookeeper的根目錄下把該文件夾建好java
./zkServer.sh start
./zkCli.sh -server 127.0.0.1:2181
運行事後會獲得以下提示界面:node
Connecting to localhost:2181 log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper). log4j:WARN Please initialize the log4j system properly. Welcome to ZooKeeper! JLine support is enabled [zkshell: 0]
緊接着咱們運行help命令能夠看看當前能夠運行哪些指令:shell
[zkshell: 0] help ZooKeeper host:port cmd args get path [watch] ls path [watch] set path data [version] delquota [-n|-b] path quit printwatches on|off create path data acl stat path [watch] listquota path history setAcl path acl getAcl path sync path redo cmdno addauth scheme auth delete path [version] setquota -n|-b val path
其中 get:爲獲取節點數據 , ls查看當前節點的子節點 create:建立節點 delete:刪除節點 set設置節點的內容數據apache
下面咱們依次運行以下命令看看:服務器
[zkshell: 8] ls / [zookeeper]
[zkshell: 9] create /zk_test my_data Created /zk_test
[zkshell: 11] ls / [zookeeper, zk_test]
[zkshell: 12] get /zk_test my_data cZxid = 5 ctime = Fri Jun 05 13:57:06 PDT 2009 mZxid = 5 mtime = Fri Jun 05 13:57:06 PDT 2009 pZxid = 5 cversion = 0 dataVersion = 0 aclVersion = 0 ephemeralOwner = 0 dataLength = 7 numChildren = 0
[zkshell: 14] set /zk_test junk cZxid = 5 ctime = Fri Jun 05 13:57:06 PDT 2009 mZxid = 6 mtime = Fri Jun 05 14:01:52 PDT 2009 pZxid = 5 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0 dataLength = 4 numChildren = 0 [zkshell: 15] get /zk_test junk cZxid = 5 ctime = Fri Jun 05 13:57:06 PDT 2009 mZxid = 6 mtime = Fri Jun 05 14:01:52 PDT 2009 pZxid = 5 cversion = 0 dataVersion = 1 aclVersion = 0 ephemeralOwner = 0 dataLength = 4 numChildren = 0
[zkshell: 16] delete /zk_test [zkshell: 17] ls / [zookeeper] [zkshell: 18]
一般狀況下咱們可使用zookeeper提供的原生jar包操做zookeeper服務,可是zookeeper原生方式操做很麻煩,不過咱們能夠用第三方的組件來操做zookeeper,好比說:zkClient 或者curator。curator提供了更豐富的功能,可是使用起來比zkClient稍微複雜一點。關於curator咱們後續篇幅會介紹,這裏先貼出zkClient的例子:app
添加zkClient的依賴:分佈式
// https://mvnrepository.com/artifact/com.101tec/zkclient compile group: 'com.101tec', name: 'zkclient', version: '0.10'
示例代碼:ide
package com.bdqn.lyrk.register; import org.I0Itec.zkclient.IZkDataListener; import org.I0Itec.zkclient.ZkClient; import org.apache.zookeeper.CreateMode; import java.io.IOException; public class ResgisterApplication { public static void main(String[] args) throws IOException { ZkClient zkClient = new ZkClient("localhost:2181", 1000); //獲取指定路徑的子節點個數 System.out.println(zkClient.countChildren("/")); //若是節點存在則刪除該節點 if (zkClient.exists("/dubbo")) { zkClient.delete("/dubbo"); } //建立永久的節點 String nodeName = zkClient.create("/dubbo", "{\"name\":\"admin\"}", CreateMode.PERSISTENT); System.out.println(nodeName); //建立臨時節點 zkClient.createEphemeralSequential("/dubbo/test", "a"); zkClient.createEphemeralSequential("/dubbo/test", "b"); //讀取節點數據 System.out.println(zkClient.readData("/dubbo").toString()); //訂閱dubbo數據的變化 zkClient.subscribeDataChanges("/dubbo", new IZkDataListener() { @Override public void handleDataChange(String dataPath, Object data) throws Exception { System.out.println(dataPath+"節點數據發生變化。。。"); } @Override public void handleDataDeleted(String dataPath) throws Exception { System.out.println(dataPath+"節點數據被刪除...."); } }); //訂閱dubbo子節點的變化 zkClient.subscribeChildChanges("/dubbo",(parentPath, currentChilds) -> System.out.println("dubbo節點發生變化")); //更新dubbo節點的數據 zkClient.writeData("/dubbo", "dubbo"); System.in.read(); } }
注意如下幾點:oop
1.不能刪除已經存在子節點的節點
2.不能再臨時節點上建立節點
一個分佈式系統不可能同時知足C(一致性)、A(可用性)和P(分區容錯性)
zookeeper優先保證CP,當服務發生故障會進行leader的選舉,整個期間服務處在不可用狀態,若是選舉時間過長勢必會大幅度下降性能,另外就用途來講zookeeper偏向於服務的協調,固然含有註冊中心的做用
eureka優先保證AP, 即服務的節點各個都是平等的,沒有leader不leader一說, 當服務發生故障時,其他的節點仍然能夠提供服務,所以在出現故障時,性能表現優於zookeeper,可是可能會形成數據不一致的狀況。