ZooKeeper Linux 服务器安装

需要安装 Java 环境

环境检查

1
2
3
4
5
6
7
8
9
10
$ cat /etc/redhat-release
Red Hat Enterprise Linux Server release 7.5 (Maipo)

$ java -version
java version "1.8.0_181"
Java(TM) SE Runtime Environment (build 1.8.0_181-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.181-b13, mixed mode)

$ echo $JAVA_HOME
/home/redhad/soft/jdk1.8.0_181

安装

1
2
$ wget http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.10/zookeeper-3.4.10.tar.gz
$ tar -zxvf zookeeper-3.4.10.tar.gz

进入解压目录

1
$ cd zookeeper-3.4.10/

conf/目录下有个zoo_sample.cfg,复制一份命名为zoo.cfg

1
$ cp zoo_sample.cfg zoo.cfg

zoo.cfg 文件内容

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
$ vi 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=/tmp/zookeeper/data
dataLogDir=/tmp/zookeeper/logs
# 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

启动服务

1
2
3
4
$ bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /home/boat/zookeeper-3.4.10/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

查看状态

1
2
3
4
$ bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /home/boat/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: standalone

查看端口号是否被占用

1
$ netstat -lpn | grep 2181

启动 Cli

1
2
3
4
5
6
7
8
9
10
11
#zkCli.sh命令默认以主机号 127.0.0.1,端口号 2181 来连接zk
$ bin/zkCli.sh

Connecting to localhost:2181
2019-02-17 23:42:15,403 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
Welcome to ZooKeeper!

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0]

创建节点及查看

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
[zk: localhost:2181(CONNECTED) 0] create /zktest 186
Created /zktest
[zk: localhost:2181(CONNECTED) 1] ls /
[dubbo, zktest, zookeeper]
[zk: localhost:2181(CONNECTED) 2] get /zktest
186
cZxid = 0xf4
ctime = Fri Jun 21 02:06:52 PDT 2019
mZxid = 0xf4
mtime = Fri Jun 21 02:06:52 PDT 2019
pZxid = 0xf4
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0
[zk: localhost:2181(CONNECTED) 3] ls /dubbo
[com.example.demoWeb.DemoService, com.example.dubbo.DemoService]

停用 zookeeper

1
2
3
4
$ bin/zkServer.sh stop
ZooKeeper JMX enabled by default
Using config: /home/boat/zookeeper-3.4.10/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED