zookeeper入门(一)——ZooKeeper伪集群安装

zookeeper入门(一)——ZooKeeper伪集群安装

在进行本篇文章之前,先请大家了解一下zookeeper(后面的文章为了省事有可能直接使用zk缩写来替代),关于zookeeper的介绍这里就不做相应的说明了,需要了解的同学可以去参考百度百科或zk官网相关描述,本篇内容主要讲zk的安装。

zk主要有三种安装模式

  • 集群安装
  • 单机安装
  • 伪集群安装

由于是个人开发机器使用,限于环境约束,本篇只做了伪集群安装,具体的生产环境下集群安装可以参考官网相关描述。好了下来进入主题。

本篇中我们将要在本地开发机上安装三个zk实例,其安装位置分别如下

/data/application/zookeeper/zk1
/data/application/zookeeper/zk2
/data/application/zookeeper/zk3

具体的安装步骤如下:

  1. 下载zookeeper
  2. 解压及配置
  3. 启动zookeeper实例
  4. 使用客户端创建一个节点

1. 下载zookeeper

此处我们下载的是当前稳定版本,3.4.8这个版本

wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/zookeeper-3.4.8/zookeeper-3.4.8.tar.gz

2. 解压及配置

进入第一个zk实例下, 然后解压下载的文件

cd /data/application/zookeeper/zk1
tar -xzvf zookeeper-3.4.8.tar.gz

解压完之后在zk1目录下面创建两个目录data和logs,分别用于存储当前zk实例数据和日志

mkdir data logs
➜  zk1 ls
data                   logs                   zookeeper-3.4.8

进入zookeeper-3.4.8/conf下,修改 zoo_sample.cfg,重命名为zoo.cgf,打开zoo.cfg进行配置,具体内容如下

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/application/zookeeper/zk1/data
dataLogDir=/data/application/zookeeper/zk1/logs
clientPort=2181
server.1=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773

其中dataDir是数据存放目录、dataLogDir是日志存放目录。
server.id=host:port:port
id为server的id,用来标识该机器在集群中的机器序列号。同样需要在每个zk实例的dataLogDir下创建一个myid文件,其中内容即为该server的id

接下来通过zk1拷贝出来zk2和zk3

cp -R zk1 zk2
cp -R zk1 zk3

修改zk2和zk3下zoo.cfg文件
zk2下zoo.cfg内容

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/application/zookeeper/zk2/data
dataLogDir=/data/application/zookeeper/zk2/logs
clientPort=2182
server.1=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773

zk3下zoo.cfg内容

tickTime=2000
initLimit=10
syncLimit=5
dataDir=/data/application/zookeeper/zk3/data
dataLogDir=/data/application/zookeeper/zk3/logs
clientPort=2183
server.1=127.0.0.1:8881:7771
server.2=127.0.0.1:8882:7772
server.3=127.0.0.1:8883:7773

当前伪集群模式下,各个zk实例位置如下

➜  zookeeper pwd
/data/application/zookeeper
➜  zookeeper tree -d -L 2
.
├── zk1
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.8
├── zk2
│   ├── data
│   ├── logs
│   └── zookeeper-3.4.8
└── zk3
    ├── data
    ├── logs
    └── zookeeper-3.4.8

12 directories

接下来执行如下3条命令

➜  zookeeper echo "1" >> /data/application/zookeeper/zk1/data/myid
➜  zookeeper echo "2" >> /data/application/zookeeper/zk2/data/myid
➜  zookeeper echo "3" >> /data/application/zookeeper/zk3/data/myid

3. 启动zookeeper实例

接下来分别启动这三个zk实例

➜  zookeeper /data/application/zookeeper/zk1/zookeeper-3.4.8/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /data/application/zookeeper/zk1/zookeeper-3.4.8/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
➜  zookeeper /data/application/zookeeper/zk2/zookeeper-3.4.8/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /data/application/zookeeper/zk2/zookeeper-3.4.8/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
➜  zookeeper /data/application/zookeeper/zk3/zookeeper-3.4.8/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /data/application/zookeeper/zk3/zookeeper-3.4.8/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED

为了方便起见,这里写了一个简单的shell脚本(zookeeper.sh)用以启动和停止zookeeper,对shell不太熟悉,写的比较烂哈

# Zookeeper启动脚本
#!/bin/sh

# 传入操作类型start or stop
OPERATOR=$1

# 如果参数为空,默认为启动操作
if [ "${OPERATOR}" = "" ]
then
  OPERATOR="start"
fi
echo "Begin ${OPERATOR} zookeeper......"

BASE_DIR="/data/application/zookeeper"
ZK_VERSION="zookeeper-3.4.8"

for zk in zk1 zk2 zk3
do
    ${BASE_DIR}/${zk}/${ZK_VERSION}/bin/zkServer.sh ${OPERATOR}
done

echo "Success ${OPERATOR} zookeeper ......"

使用方式
+ 启动集群服务

./zookeeper.sh
或者
./zookeeper.sh start


  • 停掉集群服务

./zookeeper.sh stop

启动完之后我们选择其中一个实例,这里我们选择zk2这个实例,使用zkCli.sh连接一下服务

➜  zookeeper /data/application/zookeeper/zk2/zookeeper-3.4.8/bin/zkCli.sh -server 127.0.0.1:2182
Connecting to 127.0.0.1:2182
2016-06-29 15:34:49,338 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
2016-06-29 15:34:49,343 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=localhost
2016-06-29 15:34:49,343 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_71
2016-06-29 15:34:49,346 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2016-06-29 15:34:49,346 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/Library/Java/JavaVirtualMachines/jdk1.8.0_71.jdk/Contents/Home/jre
2016-06-29 15:34:49,346 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/data/application/zookeeper/zk2/zookeeper-3.4.8/bin/../build/classes:/data/application/zookeeper/zk2/zookeeper-3.4.8/bin/../build/lib/*.jar:/data/application/zookeeper/zk2/zookeeper-3.4.8/bin/../lib/slf4j-log4j12-1.6.1.jar:/data/application/zookeeper/zk2/zookeeper-3.4.8/bin/../lib/slf4j-api-1.6.1.jar:/data/application/zookeeper/zk2/zookeeper-3.4.8/bin/../lib/netty-3.7.0.Final.jar:/data/application/zookeeper/zk2/zookeeper-3.4.8/bin/../lib/log4j-1.2.16.jar:/data/application/zookeeper/zk2/zookeeper-3.4.8/bin/../lib/jline-0.9.94.jar:/data/application/zookeeper/zk2/zookeeper-3.4.8/bin/../zookeeper-3.4.8.jar:/data/application/zookeeper/zk2/zookeeper-3.4.8/bin/../src/java/lib/*.jar:/data/application/zookeeper/zk2/zookeeper-3.4.8/bin/../conf:
2016-06-29 15:34:49,346 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/Users/didi/Library/Java/Extensions:/Library/Java/Extensions:/Network/Library/Java/Extensions:/System/Library/Java/Extensions:/usr/lib/java:.
2016-06-29 15:34:49,347 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/var/folders/r9/35q9g3d56_d9g0v59w9x2l9w0000gn/T/
2016-06-29 15:34:49,347 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=
2016-06-29 15:34:49,347 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Mac OS X
2016-06-29 15:34:49,347 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=x86_64
2016-06-29 15:34:49,348 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=10.11.1
2016-06-29 15:34:49,348 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=mahaiyuan
2016-06-29 15:34:49,348 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/Users/didi
2016-06-29 15:34:49,348 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/data/application/zookeeper
2016-06-29 15:34:49,350 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:2182 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@531d72ca
Welcome to ZooKeeper!
2016-06-29 15:34:49,388 [myid:] - INFO  [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@1032] - Opening socket connection to server 127.0.0.1/127.0.0.1:2182. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2016-06-29 15:34:49,486 [myid:] - INFO  [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@876] - Socket connection established to 127.0.0.1/127.0.0.1:2182, initiating session
[zk: 127.0.0.1:2182(CONNECTING) 0] 2016-06-29 15:34:49,601 [myid:] - INFO  [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@1299] - Session establishment complete on server 127.0.0.1/127.0.0.1:2182, sessionid = 0x2559b1280e60000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

同样写了个脚本zkCli.sh,用于连接zk服务

#!/bin/sh

# 使用哪一个zk的client 其值可以为zk1 zk2 zk3 如果不传默认为zk1
zk=$1
if [ -z "${zk}" ]
then
  zk="zk1"
fi

# 连接哪个Server,如果不传默认使用当前client对应的server
server=$2
if [ -n "${server}" ]
then
  server=" -server ${server}"
fi

BASE_DIR="/data/application/zookeeper"
ZK_VERSION="zookeeper-3.4.8"

${BASE_DIR}/${zk}/${ZK_VERSION}/bin/zkCli.sh ${server}

使用方式

./zkCli.sh
或者
./zkCli.sh zk1 127.0.0.1:2182

4. 使用客户端创建一个节点

接下来我们在当前所连接的物理节点上(这里连接的是zk2这个实例)创建一个数据节点

[zk: 127.0.0.1:2182(CONNECTED) 0] create /zk-demo 123
Created /zk-demo
[zk: 127.0.0.1:2182(CONNECTED) 1] get /zk-demo
123
cZxid = 0x100000002
ctime = Wed Jun 29 15:39:44 CST 2016
mZxid = 0x100000002
mtime = Wed Jun 29 15:39:44 CST 2016
pZxid = 0x100000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0

然后我们再连接另一个zk实例(连接zk1或zk3),看是否能获取到该数据节点

/data/application/zookeeper/zk1/zookeeper-3.4.8/bin/zkCli.sh -server 127.0.0.1:2183
[zk: 127.0.0.1:2183(CONNECTED) 0] get /zk-demo
123
cZxid = 0x100000002
ctime = Wed Jun 29 15:39:44 CST 2016
mZxid = 0x100000002
mtime = Wed Jun 29 15:39:44 CST 2016
pZxid = 0x100000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 3
numChildren = 0

同样能获取到刚才在节点zk2中设置的/zk-demo数据节点的内容,到此关于ZooKeeper伪集群安装完毕。

参考内容:

  • zk官网:http://zookeeper.apache.org/doc/r3.4.8/zookeeperStarted.html
  • 《从PAXOS到ZOOKEEPER分布式一致性原理与实践》

你可能感兴趣的:(zookeeper)