歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux綜合 >> 學習Linux

分布式服務協調技術zookeeper系列(一)—— zookeeper 簡介以及linux上的安裝(單節點),zookeeperlinux

分布式服務協調技術zookeeper系列(一)—— zookeeper 簡介以及linux上的安裝(單節點),zookeeperlinux

分布式服務協調技術zookeeper系列(一)—— zookeeper 簡介以及linux上的安裝(單節點),zookeeperlinux


ZooKeeper簡介

ZooKeeper是一個分布式協調服務框架,通常用於解決分布式項目中遇到的一些管理協調的問題,如統一命名服務、分布式配置管理,分布式鎖、集群節點狀態協調等等。

下載

到http://apache.fayea.com/zookeeper/下載zookeeper-3.4.9,ftp上傳至linux

解壓

[root@localhost ftpuser]# tar -zxvf zookeeper-3.4.9.tar.gz

創建數據日志目錄

在zookeeper的解壓目錄下創建以下兩個文件夾

 

[root@localhost zookeeper-3.4.9]# mkdir data

[root@localhost zookeeper-3.4.9]# mkdir logs

 

拷貝配置文件

到zookeeper的解壓目錄的conf目錄下,將zoo_sample.cfg 文件拷貝一份,命名為為 zoo.cfg

 

[root@localhost conf]# cp zoo_sample.cfg zoo.cfg

修改配置文件

[root@localhost conf]# vi 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

# 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=/home/ftpuser/zookeeper-3.4.9/data

dataLogDir=/home/ftpuser/zookeeper-3.4.9/logs

# the port at which the clients will connect

clientPort=2181

server.1=192.168.2.129:2888:3888

# 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

 

在zoo.cfg 配置dataDir,dataLogDir,server。

server.A=B:C:D:其中 A 是一個數字,表示這個是第幾號服務器;B 是這個服務

器的 IP 地址;C 表示的是這個服務器與集群中的 Leader 服務器交換信息的端口;D 表示的是萬一集群中的 Leader 服務器掛了,需要一個端口來重新進行選舉,選出一個新的 Leader,而這個端口就是用來執行選舉時服務器相互通信的端口。

 

clientPort 這個端口就是客戶端(應用程序)連接 Zookeeper 服務器的端口,Zookeeper 會監聽這個端口接受客戶端的訪問請求

創建 myid 文件

進入/home/ftpuser/zookeeper-3.4.9/data並創建myid文件

 

[root@localhost conf]# cd /home/ftpuser/zookeeper-3.4.9/data

[root@localhost data]# vi myid

1

 

編輯 myid 文件,並在對應的 IP 的機器上輸入對應的編號。如在 zookeeper 上,myid

文件內容就是 1。如果只在單點上進行安裝配置,那麼只有一個 server.1

添加環境變量

[root@localhost data]# vi /etc/profile

 

在文件末尾添加zookeeper 環境變量

 

# zookeeper env

export ZOOKEEPER_HOME=/home/ftpuser/zookeeper-3.4.9/

export PATH=$ZOOKEEPER_HOME/bin:$PATH

 

執行source /etc/profile命令是環境變量生效,執行echo $ZOOKEEPER_HOME查看

打開端口

在防火牆中打開要用到的端口 2181、2888、3888。打開/etc/sysconfig/iptables增加以下 3 行

 

[root@localhost data]# cat /etc/sysconfig/iptables

# Generated by iptables-save v1.4.7 on Thu Jun  2 22:41:13 2016

*filter

:INPUT ACCEPT [5:320]

:FORWARD ACCEPT [0:0]

:OUTPUT ACCEPT [4:464]

-A INPUT -p udp -m udp --dport 23 -j ACCEPT

-A INPUT -p tcp -m tcp --dport 23 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 2181 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 2888 -j ACCEPT

-A INPUT -m state --state NEW -m tcp -p tcp --dport 3888 -j ACCEPT

COMMIT

# Completed on Thu Jun  2 22:41:13 2016

 

[root@localhost data]#

 

查看端口啟動狀態

 

[root@localhost data]# service iptables status

 

啟動zookeeper

[root@localhost zookeeper-3.4.9]# cd bin

[root@localhost bin]# ll

total 36

-rwxr-xr-x. 1 1001 1001  232 Aug 23 15:39 README.txt

-rwxr-xr-x. 1 1001 1001 1937 Aug 23 15:39 zkCleanup.sh

-rwxr-xr-x. 1 1001 1001 1032 Aug 23 15:39 zkCli.cmd

-rwxr-xr-x. 1 1001 1001 1534 Aug 23 15:39 zkCli.sh

-rwxr-xr-x. 1 1001 1001 1579 Aug 23 15:39 zkEnv.cmd

-rwxr-xr-x. 1 1001 1001 2696 Aug 23 15:39 zkEnv.sh

-rwxr-xr-x. 1 1001 1001 1065 Aug 23 15:39 zkServer.cmd

-rwxr-xr-x. 1 1001 1001 6773 Aug 23 15:39 zkServer.sh

[root@localhost bin]# zkServer.sh start

ZooKeeper JMX enabled by default

Using config: /home/ftpuser/zookeeper-3.4.9/bin/../conf/zoo.cfg

Starting zookeeper ... STARTED

查看zookeeper後台日志

[root@localhost ~]# tail -f /home/ftpuser/zookeeper-3.4.9/bin/zookeeper.out

查看zookeeper進程

[root@localhost bin]# jps

2011 QuorumPeerMain

1245 Bootstrap

2030 Jps

[root@localhost bin]#

 

其中,QuorumPeerMain 是 zookeeper 進程,啟動正常。查看狀態

 

[root@localhost bin]# zkServer.sh status

ZooKeeper JMX enabled by default

Using config: /home/ftpuser/zookeeper-3.4.9/bin/../conf/zoo.cfg

Mode: standalone

[root@localhost bin]#

停止zookeeper進程

[root@localhost bin]# zkServer.sh stop

ZooKeeper JMX enabled by default

Using config: /home/ftpuser/zookeeper-3.4.9/bin/../conf/zoo.cfg

Stopping zookeeper ... STOPPED

[root@localhost bin]#

客戶端連接zookeeper

[root@localhost bin]# ./zkCli.sh -server 192.168.2.129:2181

 

輸入help命令來查看有哪些命令

 

[zk: 192.168.2.129:2181(CONNECTED) 0] help

ZooKeeper -server host:port cmd args

connect host:port

get path [watch]

ls path [watch]

set path data [version]

rmr path

delquota [-n|-b] path

quit

printwatches on|off

create [-s] [-e] path data acl

stat path [watch]

close

ls2 path [watch]

history

listquota path

setAcl path acl

getAcl path

sync path

redo cmdno

addauth scheme auth

delete path [version]

setquota -n|-b val path

[zk: 192.168.2.129:2181(CONNECTED) 1]

http://xxxxxx/Linuxjc/1165626.html TechArticle

Copyright © Linux教程網 All Rights Reserved