zookeeper集群配置

一、准备三台linux服务器

192.168.64.123
192.168.64.124
192.168.64.125

二、zookeeper下载 安装

1、下载 http://apache.fayea.com/zookeeper/zookeeper-3.5.5/( apache-zookeeper-3.5.5-bin.tar.gz)

2、解压 tar -zxvf apache-zookeeper-3.5.5-bin.tar.gz

3、创建配置文件

解压后的路径下找到conf文件夹,进入conf文件夹复制zoo_sample.cfg,命名为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/zookeeper
# 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
server.1=192.168.64.123:2888:3888
server.2=192.168.64.124:2888:3888
server.3=192.168.64.125:2888:3888

dataDir 默认对应的路径是/tmp/zookeeper ,现在修改成我们自己服务器下的地址路径 /data/zookeeper ,并在此路径下创建myid文件,值对应server.1的1,

如果/tmp/zookeeper没有myid 启动zookeeper服务时会报错

[ zookeeper]# touch myid
[ zookeeper]# echo ‘1‘>>myid
zookeeper有三个端口(可以修改)
1、2181:对cline端提供服务
2、3888:选举leader使用
3、2888:集群内机器通讯使用(Leader监听此端口)

按照上面的方法配置其它两台linux服务器

zoo.cfg 内容解释 参考 https://blog.csdn.net/liyiming2017/article/details/83501836

三、测试服务

1、启动服务:./zkServer.sh start (如果启动不了 可能是dataDir下没有myid)
[ bin]# ./zkServer.sh start
/usr/bin/java
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[ bin]#
2、查看服务状态:./zkServer.sh status 如果没有Mode (检查下防火墙关了没)
[ bin]# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /opt/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost.
Mode: leader
[ bin]#
3、检测是否成功启动,用zookeeper客户端连接下服务端:./zkCli.sh -server 192.168.64.124:2181
[ bin]# ./zkCli.sh -server 192.168.64.124:2181
Connecting to 192.168.64.124:2181
2019-12-19 17:58:42,518 [myid:] - INFO  [main:109] - Client environment:zookeeper.version=3.5.5-390fe37ea45dee01bf87dc1c042b5e3dcce88653, built on 05/03/2019 12:07 GMT
2019-12-19 17:58:42,521 [myid:] - INFO  [main:109] - Client environment:host.name=localhost
2019-12-19 17:58:42,521 [myid:] - INFO  [main:109] - Client environment:java.version=1.8.0_161
2019-12-19 17:58:42,524 [myid:] - INFO  [main:109] - Client environment:java.vendor=Oracle Corporation
2019-12-19 17:58:42,524 [myid:] - INFO  [main:109] - Client environment:java.home=/opt/jdk1.8.0_161/jre
2019-12-19 17:58:42,524 [myid:] - INFO  [main:109] - Client environment:java.class.path=/opt/zookeeper/bin/../zookeeper-server/target/classes:/opt/zookeeper/bin/../build/classes:/opt/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/opt/zookeeper/bin/../build/lib/*.jar:/opt/zookeeper/bin/../lib/zookeeper-jute-3.5.5.jar:/opt/zookeeper/bin/../lib/zookeeper-3.5.5.jar:/opt/zookeeper/bin/../lib/slf4j-log4j12-1.7.25.jar:/opt/zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/opt/zookeeper/bin/../lib/netty-all-4.1.29.Final.jar:/opt/zookeeper/bin/../lib/log4j-1.2.17.jar:/opt/zookeeper/bin/../lib/json-simple-1.1.1.jar:/opt/zookeeper/bin/../lib/jline-2.11.jar:/opt/zookeeper/bin/../lib/jetty-util-9.4.17.v20190418.jar:/opt/zookeeper/bin/../lib/jetty-servlet-9.4.17.v20190418.jar:/opt/zookeeper/bin/../lib/jetty-server-9.4.17.v20190418.jar:/opt/zookeeper/bin/../lib/jetty-security-9.4.17.v20190418.jar:/opt/zookeeper/bin/../lib/jetty-io-9.4.17.v20190418.jar:/opt/zookeeper/bin/../lib/jetty-http-9.4.17.v20190418.jar:/opt/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/opt/zookeeper/bin/../lib/jackson-databind-2.9.8.jar:/opt/zookeeper/bin/../lib/jackson-core-2.9.8.jar:/opt/zookeeper/bin/../lib/jackson-annotations-2.9.0.jar:/opt/zookeeper/bin/../lib/commons-cli-1.2.jar:/opt/zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/opt/zookeeper/bin/../zookeeper-*.jar:/opt/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/opt/zookeeper/bin/../conf:.:/opt/jdk1.8.0_161/lib:/opt/jdk1.8.0_161/jre/lib:
2019-12-19 17:58:42,525 [myid:] - INFO  [main:] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2019-12-19 17:58:42,525 [myid:] - INFO  [main:] - Client environment:java.io.tmpdir=/tmp
2019-12-19 17:58:42,525 [myid:] - INFO  [main:] - Client environment:java.compiler=<NA>
2019-12-19 17:58:42,525 [myid:] - INFO  [main:] - Client environment:os.name=Linux
2019-12-19 17:58:42,525 [myid:] - INFO  [main:] - Client environment:os.arch=amd64
2019-12-19 17:58:42,525 [myid:] - INFO  [main:] - Client environment:os.version=3.10.0-327.el7.x86_64
2019-12-19 17:58:42,525 [myid:] - INFO  [main:] - Client environment:user.name=root
2019-12-19 17:58:42,526 [myid:] - INFO  [main:] - Client environment:user.home=/root
2019-12-19 17:58:42,526 [myid:] - INFO  [main:] - Client environment:user.dir=/opt/zookeeper/bin
2019-12-19 17:58:42,526 [myid:] - INFO  [main:] - Client environment:os.memory.free=24MB
2019-12-19 17:58:42,529 [myid:] - INFO  [main:] - Client environment:os.memory.max=228MB
2019-12-19 17:58:42,529 [myid:] - INFO  [main:] - Client environment:os.memory.total=29MB
2019-12-19 17:58:42,532 [myid:] - INFO  [main:] - Initiating client connection, connectString=192.168.64.124:2181 sessionTimeout=30000 
2019-12-19 17:58:42,539 [myid:] - INFO  [main:] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2019-12-19 17:58:42,549 [myid:] - INFO  [main:] - jute.maxbuffer value is 4194304 Bytes
2019-12-19 17:58:42,558 [myid:] - INFO  [main:] - zookeeper.request.timeout value is 0. feature enabled=
Welcome to ZooKeeper!
2019-12-19 17:58:42,577 [myid:192.168.64.124:2181] - INFO  [main-SendThread(192.168.64.124:2181):] - Opening socket connection to server 192.168.64.124/192.168.64.124:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2019-12-19 17:58:42,653 [myid:192.168.64.124:2181] - INFO  [main-SendThread(192.168.64.124:2181):] - Socket connection established, initiating session, client: /192.168.64.124:51564, server: 192.168.64.124/192.168.64.124:2181
2019-12-19 17:58:42,666 [myid:192.168.64.124:2181] - INFO  [main-SendThread(192.168.64.124:2181):] - Session establishment complete on server 192.168.64.124/192.168.64.124:2181, sessionid = 0x20011e34d0b0005, negotiated timeout = 30000

WATCHER::

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

4、现在就可以通过客户端执行基本命令

[zk: 192.168.64.124:2181(CONNECTED) 0] create /test 111
Node already exists: /test
[zk: 192.168.64.124:2181(CONNECTED) 1] get /test
2221
[zk: 192.168.64.124:2181(CONNECTED) 2]

 验证数据是否同步

[zk: 192.168.64.123:2181(CONNECTED) 0] get /test
2221
[zk: 192.168.64.123:2181(CONNECTED) 1]

通过验证可以看出 在服务192.168.64.124上创建的节点,在192.168.64.123上也可以获取到,说明集群部署是OK的.