單機模式:html
1) 首先下載zookeeper壓縮包, 這裏採用zookeeper3.4.8....java
wget http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz
2) 解壓縮python
首先建立文件夾,這裏放到/user/zookeeper/文件夾下apache
mkdir zookeeper1
mkdir zookeeper2
mkdir zookeeper3
建立三個文件夾用於存放三個實例,下面解壓到目標文件夾,找到壓縮包存放路徑,執行下面命令服務器
tar -zxvf zookeeper-3.4.8.tar.gz -C /usr/zookeeper/zookeeper1
tar -zxvf zookeeper-3.4.8.tar.gz -C /usr/zookeeper/zookeeper2
tar -zxvf zookeeper-3.4.8.tar.gz -C /usr/zookeeper/zookeeper3
3)運行單價環境maven
先來整下zookeeper1這個實例,首先分佈式
cd zookeeper1/
利用ll命令, 顯示全部文件詳情,ide
drwxr-xr-x. 2 1000 1000 4096 Mar 17 17:27 bin -rw-rw-r--. 1 1000 1000 83235 Feb 6 11:46 build.xml -rw-rw-r--. 1 1000 1000 88625 Feb 6 11:46 CHANGES.txt drwxr-xr-x. 2 1000 1000 4096 Mar 17 17:21 conf drwxr-xr-x. 10 1000 1000 4096 Feb 6 11:46 contrib drwxr-xr-x. 2 1000 1000 4096 Feb 6 11:50 dist-maven drwxr-xr-x. 6 1000 1000 4096 Feb 6 11:49 docs -rw-rw-r--. 1 1000 1000 1953 Feb 6 11:46 ivysettings.xml -rw-rw-r--. 1 1000 1000 3498 Feb 6 11:46 ivy.xml drwxr-xr-x. 4 1000 1000 4096 Feb 6 11:49 lib -rw-rw-r--. 1 1000 1000 11938 Feb 6 11:46 LICENSE.txt -rw-rw-r--. 1 1000 1000 171 Feb 6 11:46 NOTICE.txt -rw-rw-r--. 1 1000 1000 1770 Feb 6 11:46 README_packaging.txt -rw-rw-r--. 1 1000 1000 1585 Feb 6 11:46 README.txt drwxr-xr-x. 5 1000 1000 4096 Feb 6 11:46 recipes drwxr-xr-x. 8 1000 1000 4096 Feb 6 11:49 src -rw-rw-r--. 1 1000 1000 1360961 Feb 6 11:46 zookeeper-3.4.8.jar -rw-rw-r--. 1 1000 1000 819 Feb 6 11:50 zookeeper-3.4.8.jar.asc -rw-rw-r--. 1 1000 1000 33 Feb 6 11:46 zookeeper-3.4.8.jar.md5 -rw-rw-r--. 1 1000 1000 41 Feb 6 11:46 zookeeper-3.4.8.jar.sha1
cp下配置文件,ui
mv conf/zoo_sample.cfg conf/zoo.cfg
啓動zk,this
./bin/zdServer.sh start
4) 使用java 客戶端鏈接ZooKeeper
./bin/zkCli.sh -server 127.0.0.1:2181
而後就可使用各類命令了,跟文件操做命令很相似,輸入help
能夠看到全部命令。
5) 關閉
./bin/zdServer.sh stop
ZooKeeper集羣通常被稱爲ZooKeeper ensemble,或者 quorum.
由於咱們這裏只有一臺機器,經過不一樣端口號,作成僞集羣的方式,若是你機器比較多,能夠在每一個機器上安裝一個zk,配置方式是相似。
所謂 「僞分佈式集羣」 就是在,在一臺PC中,啓動多個ZooKeeper的實例。「徹底分佈式集羣」 是每臺PC,啓動一個ZooKeeper實例。
ZooKeeper不存在明顯的master/slave關係,各個節點都是服務器,leader掛了,會立馬從follower中選舉一個出來做爲leader.
因爲沒有主從關係,也不用配置SSH無密碼登陸了,各個zk服務器是本身啓動的,互相之間經過TCP端口來交換數據
將zookeeper2,zookeeper3的配置文件改下,由於zookeeper1在咱們作單機時候,已經修改過,在zookeeper2,zookeeper3 conf文件夾下,執行
mv zoo_sample.cfg zoo.cfg
建立環境目錄
切換到var/zookeeper路徑下,新建三個文件夾zookeeper1,zookeeper2,zookeeper3....用於存放pid
mkdir /var/zookeeper/zookeeper1 mkdir /var/zookeeper/zookeeper2 mkdir /var/zookeeper/zookeeper3
分別在 zookeeper1,zookeeper2,zookeeper3 ,新建三個文件 myid
~ echo "1" > /var/zookeeper/zookeeper1/myid ~ echo "2" > /var/zookeeper/zookeeper2/myid ~ echo "3" > /var/zookeeper/zookeeper3/myid
注意myid只能爲數字,由於我試驗了下,包含英文的話zk啓動不起來,拋出異常
分別修改配置文件
修改:dataDir,clientPort
增長:集羣的實例,server.X,」X」表示每一個目錄中的myid的值
vi /usr/zookeeper/zookeeper1/conf/zoo.cfg
# 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=/var/zookeeper/zookeeper1 # the port at which the clients will connect clientPort=2181 server.1=127.0.0.1:2888:3888 server.2=127.0.0.1:2889:3889 server.3=127.0.0.1:2890:3890 # 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
如下只貼出不一樣部分,其餘和zk1相同
vi /usr/zookeeper/zookeeper2/conf/zoo.cfg
dataDir=/var/zookeeper/zookeeper2
clientPort=2182
vi /user/zookeeper/zookeeper3/conf/zoo.cfg
dataDir=/var/zookeeper/zookeeper3
clientPort=2182
啓動每臺機器
/usr/zookeeper/zookeeper1/bin/zkServer.sh start /usr/zookeeper/zookeeper2/bin/zkServer.sh start /usr/zookeeper/zookeeper3/bin/zkServer.sh start
查看啓動狀態