一、准备三台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
1 # The number of milliseconds of each tick 2 tickTime=2000 3 # The number of ticks that the initial 4 # synchronization phase can take 5 initLimit=10 6 # The number of ticks that can pass between 7 # sending a request and getting an acknowledgement 8 syncLimit=5 9 # the directory where the snapshot is stored. 10 # do not use /tmp for storage, /tmp here is just 11 # example sakes. 12 dataDir=/data/zookeeper 13 # the port at which the clients will connect 14 clientPort=2181 15 # the maximum number of client connections. 16 # increase this if you need to handle more clients 17 #maxClientCnxns=60 18 # 19 # Be sure to read the maintenance section of the 20 # administrator guide before turning on autopurge. 21 # 22 # http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance 23 # 24 # The number of snapshots to retain in dataDir 25 server.1=192.168.64.123:2888:3888 26 server.2=192.168.64.124:2888:3888 27 server.3=192.168.64.125:2888:3888
dataDir 默认对应的路径是/tmp/zookeeper ,现在修改成我们自己服务器下的地址路径 /data/zookeeper ,并在此路径下创建myid文件,值对应server.1的1,
如果/tmp/zookeeper没有myid 启动zookeeper服务时会报错
[root@localhost zookeeper]# touch myid [root@localhost 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)
[root@localhost bin]# ./zkServer.sh start /usr/bin/java ZooKeeper JMX enabled by default Using config: /opt/zookeeper/bin/../conf/zoo.cfg Starting zookeeper ... STARTED [root@localhost bin]#
2、查看服务状态:./zkServer.sh status 如果没有Mode (检查下防火墙关了没)
[root@localhost 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 [root@localhost bin]#
3、检测是否成功启动,用zookeeper客户端连接下服务端:./zkCli.sh -server 192.168.64.124:2181
[root@localhost 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:Environment@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:Environment@109] - Client environment:host.name=localhost 2019-12-19 17:58:42,521 [myid:] - INFO [main:Environment@109] - Client environment:java.version=1.8.0_161 2019-12-19 17:58:42,524 [myid:] - INFO [main:Environment@109] - Client environment:java.vendor=Oracle Corporation 2019-12-19 17:58:42,524 [myid:] - INFO [main:Environment@109] - Client environment:java.home=/opt/jdk1.8.0_161/jre 2019-12-19 17:58:42,524 [myid:] - INFO [main:Environment@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:Environment@109] - 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:Environment@109] - Client environment:java.io.tmpdir=/tmp 2019-12-19 17:58:42,525 [myid:] - INFO [main:Environment@109] - Client environment:java.compiler=<NA> 2019-12-19 17:58:42,525 [myid:] - INFO [main:Environment@109] - Client environment:os.name=Linux 2019-12-19 17:58:42,525 [myid:] - INFO [main:Environment@109] - Client environment:os.arch=amd64 2019-12-19 17:58:42,525 [myid:] - INFO [main:Environment@109] - Client environment:os.version=3.10.0-327.el7.x86_64 2019-12-19 17:58:42,525 [myid:] - INFO [main:Environment@109] - Client environment:user.name=root 2019-12-19 17:58:42,526 [myid:] - INFO [main:Environment@109] - Client environment:user.home=/root 2019-12-19 17:58:42,526 [myid:] - INFO [main:Environment@109] - Client environment:user.dir=/opt/zookeeper/bin 2019-12-19 17:58:42,526 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.free=24MB 2019-12-19 17:58:42,529 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.max=228MB 2019-12-19 17:58:42,529 [myid:] - INFO [main:Environment@109] - Client environment:os.memory.total=29MB 2019-12-19 17:58:42,532 [myid:] - INFO [main:ZooKeeper@868] - Initiating client connection, connectString=192.168.64.124:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@5eb5c224 2019-12-19 17:58:42,539 [myid:] - INFO [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation 2019-12-19 17:58:42,549 [myid:] - INFO [main:ClientCnxnSocket@237] - jute.maxbuffer value is 4194304 Bytes 2019-12-19 17:58:42,558 [myid:] - INFO [main:ClientCnxn@1653] - 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):ClientCnxn$SendThread@1112] - 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):ClientCnxn$SendThread@959] - 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):ClientCnxn$SendThread@1394] - 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、现在就可以通过客户端执行基本命令 ls /
[zk: 192.168.64.124:2181(CONNECTED) 0] create /test 111 Node already exists: /test
[zk: 192.168.64.124:2181(CONNECTED) 16] ls /
[dubbo, test, zookeeper] [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的.