本文介绍zookeeper伪分布式安装。
所谓 “伪分布式集群” 就是在1台PC中启动多个zookeeper的实例。“完全分布式集群” 是每1台PC启动1个ZooKeeper实例。
由于我的测试环境PC数量有限,所以在1台PC中启动3个zookeeper的实例。下面以3个节点为例,介绍安装的过程
新建3个文件夹
[work@xxx zookeeper-3.4.6]$ mkdir zoo_1 zoo_2 zoo_3
复制配置文件
[work@xxx conf]$ cp zoo.cfg zoo1.cfg
[work@xxx conf]$ cp zoo.cfg zoo2.cfg
[work@xxx conf]$ cp zoo.cfg zoo3.cfg
修改配置文件
3个节点的配置文件分别如下,注意修改路径和端口
介绍一下配置文件里面的字段含义:
1、tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳
2、dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里
3、clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求
4、initLimit:这个配置项是用来配置 Zookeeper 接受客户端(这里所说的客户端不是用户连接 Zookeeper 服务器的客户端,而是 Zookeeper 服务器集群中连接到 Leader 的 Follower 服务器)初始化连接时最长能忍受多少个心跳时间间隔数。当已经超过 5个心跳的时间(也就是 tickTime)长度后 Zookeeper 服务器还没有收到客户端的返回信息,那么表明这个客户端连接失败。总的时间长度就是 5 * 2000 = 10 秒
5、syncLimit:这个配置项标识 Leader 与 Follower 之间发送消息,请求和应答时间长度,最长不能超过多少个 tickTime 的时间长度,总的时间长度就是 2 * 2000 = 4 秒
6、server.A=B:C:D:其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的 Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号
写入myid
[work@xxx zookeeper-3.4.6]$ echo "1" > zoo_1/myid
[work@xxx zookeeper-3.4.6]$ echo "2" > zoo_2/myid
[work@xxx zookeeper-3.4.6]$ echo "3" > zoo_3/myid
启动
[work@xxx zookeeper-3.4.6]$ bin/zkServer.sh start zoo1.cfg
JMX enabled by default
Using config: /home/work/soft/zookeeper-3.4.6/bin/../conf/zoo1.cfg
Starting zookeeper ... STARTED
[work@xxx zookeeper-3.4.6]$ bin/zkServer.sh start zoo2.cfg
JMX enabled by default
Using config: /home/work/soft/zookeeper-3.4.6/bin/../conf/zoo2.cfg
Starting zookeeper ... STARTED
[work@xxx zookeeper-3.4.6]$ bin/zkServer.sh start zoo3.cfg
JMX enabled by default
Using config: /home/work/soft/zookeeper-3.4.6/bin/../conf/zoo3.cfg
Starting zookeeper ... STARTED
查看状态
[work@xxx zookeeper-3.4.6]$ bin/zkServer.sh status zoo1.cfg
JMX enabled by default
Using config: /home/work/soft/zookeeper-3.4.6/bin/../conf/zoo1.cfg
Mode: follower
[work@xxx zookeeper-3.4.6]$ bin/zkServer.sh status zoo2.cfg
JMX enabled by default
Using config: /home/work/soft/zookeeper-3.4.6/bin/../conf/zoo2.cfg
Mode: leader
[work@xxx zookeeper-3.4.6]$ bin/zkServer.sh status zoo3.cfg
JMX enabled by default
Using config: /home/work/soft/zookeeper-3.4.6/bin/../conf/zoo3.cfg
Mode: follower
可以发现zoo_2被选为了leader
查看目录
[work@xxx zookeeper-3.4.6]$ tree zoo_1
zoo_1
|-- myid
|-- version-2
| |-- acceptedEpoch
| |-- currentEpoch
| `-- snapshot.0
`-- zookeeper_server.pid
1 directory, 5 files
[work@xxx zookeeper-3.4.6]$ tree zoo_2
zoo_2
|-- myid
|-- version-2
| |-- acceptedEpoch
| `-- currentEpoch
`-- zookeeper_server.pid
1 directory, 4 files
[work@xxx zookeeper-3.4.6]$ tree zoo_3
zoo_3
|-- myid
|-- version-2
| |-- acceptedEpoch
| |-- currentEpoch
| `-- snapshot.100000000
`-- zookeeper_server.pid
1 directory, 5 files
[work@xxx zookeeper-3.4.6]$
安装成功后,用cli测试一下,一直报下面这个SB错误!
[work@xxx zookeeper-3.4.6]$ bin/zkCli.sh -server 127.0.0.1:2182
Connecting to 127.0.0.1:2182
2018-01-05 21:32:45,482 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT
2018-01-05 21:32:45,485 [myid:] - INFO [main:Environment@100] - Client environment:host.name=xxx
2018-01-05 21:32:45,486 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.7.0_111
2018-01-05 21:32:45,488 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-01-05 21:32:45,488 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/lib/jvm/java-1.7.0-openjdk-1.7.0.111.x86_64/jre
2018-01-05 21:32:45,488 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/home/work/soft/zookeeper-3.4.6/bin/../build/classes:/home/work/soft/zookeeper-3.4.6/bin/../build/lib/*.jar:/home/work/soft/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/home/work/soft/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/home/work/soft/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/home/work/soft/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/home/work/soft/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/home/work/soft/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/home/work/soft/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/home/work/soft/zookeeper-3.4.6/bin/../conf:
2018-01-05 21:32:45,488 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-01-05 21:32:45,488 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-01-05 21:32:45,488 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2018-01-05 21:32:45,488 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2018-01-05 21:32:45,489 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2018-01-05 21:32:45,489 [myid:] - INFO [main:Environment@100] - Client environment:os.version=3.10.0_1-0-0-8
2018-01-05 21:32:45,489 [myid:] - INFO [main:Environment@100] - Client environment:user.name=work
2018-01-05 21:32:45,489 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/home/work
2018-01-05 21:32:45,489 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/home/work/soft/zookeeper-3.4.6
2018-01-05 21:32:45,490 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:2182 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@7a04c4aa
Welcome to ZooKeeper!
2018-01-05 21:32:45,519 [myid:] - INFO [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@975] - Opening socket connection to server 127.0.0.1/127.0.0.1:2182. Will not attempt to authenticate using SASL (unknown error)
2018-01-05 21:32:45,525 [myid:] - WARN [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@1102] - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:744)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)
JLine support is enabled
[zk: 127.0.0.1:2182(CONNECTING) 0] 2018-01-05 21:32:46,629 [myid:] - INFO [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@975] - Opening socket connection to server 127.0.0.1/127.0.0.1:2182. Will not attempt to authenticate using SASL (unknown error)
2018-01-05 21:32:46,630 [myid:] - WARN [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@1102] - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:744)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)
2018-01-05 21:32:47,731 [myid:] - INFO [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@975] - Opening socket connection to server 127.0.0.1/127.0.0.1:2182. Will not attempt to authenticate using SASL (unknown error)
2018-01-05 21:32:47,731 [myid:] - WARN [main-SendThread(127.0.0.1:2182):ClientCnxn$SendThread@1102] - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
java.net.ConnectException: Connection refused
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:744)
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)
在网上查了很久也没有找到解决方法,但是用cli连接单节点的zookeeper是没有问题的,奇怪!
参考了http://blog.itpub.net/12219480/viewspace-1778740/ 里面的说明,不过没有验证,先贴到下面:
1、zookeeper配置完毕之候报错,信息如下:
-
grid@master1:~$ zkCli.sh -server 10.10.10.70:2181
-
..............................
-
2015-08-21 06:29:48,412 [myid:] - INFO [main-SendThread(10.10.10.70:2181):ClientCnxn$SendThread@975] - Opening socket connection to server 10.10.10.70/10.10.10.70:2181. Will not attempt to authenticate using SASL (unknown error)
-
Welcome to
-
2015-08-21 06:29:48,420 [myid:] - WARN [main-SendThread(10.10.10.70:2181):ClientCnxn$SendThread@1102] - Session 0x0 for server null, unexpected error, closing socket connection and attempting reconnect
-
java.net.ConnectException: Connection refused
-
at sun.nio.ch.SocketChannelImpl.checkConnect(Native Method)
-
at sun.nio.ch.SocketChannelImpl.finishConnect(SocketChannelImpl.java:739)
-
at org.apache.zookeeper.ClientCnxnSocketNIO.doTransport(ClientCnxnSocketNIO.java:361)
- at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1081)
这是连接zookeeper的server一直不断的报错
2、查看zookeeper配置信息
-
grid@master1:/usr/local/zookeeper/conf$ cat 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=/data/zk
-
dataLogDir=/data/zk/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
-
-
server.70=10.10.10.70:2888:3888
-
server.71=10.10.10.71:2888:3888
-
server.72=10.10.10.72:2888:3888
- server.73=10.10.10.73:2888:3888
2、在每个服务器上更改myid文件,如10.10.10.70文件
70
之后启动zookeeper,服务正常
本文参考自:
https://www.cnblogs.com/haippy/archive/2012/07/19/2599989.html
http://blog.csdn.net/chengwenyao18/article/details/23611995
https://www.cnblogs.com/driftingshine/p/6649104.html
http://blog.itpub.net/12219480/viewspace-1778740/