ZooKeeper:win7上安装单机及伪分布式安装

Posted yy

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ZooKeeper:win7上安装单机及伪分布式安装相关的知识,希望对你有一定的参考价值。

  zookeeper是一个为分布式应用所设计的分布式的、开源的调度服务,它主要用来解决分布式应用中经常遇到的一些数据管理问题,简化分布式应用,协调及其管理的难度,提高性能的分布式服务。

  本章的目的:如何安装zookeeper(在win7环境下),zookeeper下载https://mirrors.cnnic.cn/apache/zookeeper/。这里下载的版本是3.4.8.  

  zoomkeeper本身是可以单节点模式安装运行,不过一般为了确保zookeeper的稳定性和可用性,都会采用分布式部署,也进而保证了分布式应用的可靠性。它在分布式zookepper集群中,会基于选举算法来实现一个leader,多个follower。

  参考文章:http://blog.csdn.net/lovesummerforever/article/details/48975703

单节点安装:

1)下载zookeeper-3.4.8.tar.gz,并解压到目录:D:\\Work\\zookeeper\\zookeeper-3.4.8下

2)进入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=D:\\\\Work\\\\zookeeper\\\\data
# log config
dataLogDir=D:\\\\Work\\\\zookeeper\\\\log
# 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

3)启动zookeeper服务:

进入目录:D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin,执行zkServer.cmd,这样就启动了zookeeper服务了。

--启动打印信息
D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin>zkServer.cmd

D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin>call "D:\\Program Files\\Java\\jdk1.8.0_111"\\bin\\java "-Dzookeeper.log.dir=D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\.." "-Dzookeeper.root.logger=INFO,CONSOLE" -cp "D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\build\\classes;D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\build\\lib\\*;D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\*;D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\lib\\*;D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\conf" org.apache.zookeeper.server.quorum.QuorumPeerMain "D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\conf\\zoo.cfg"
2017-09-09 21:02:58,180 [myid:] - INFO  [main:QuorumPeerConfig@103] - Reading configuration from: D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\conf\\zoo.cfg
2017-09-09 21:02:58,204 [myid:] - INFO  [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2017-09-09 21:02:58,205 [myid:] - INFO  [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2017-09-09 21:02:58,205 [myid:] - INFO  [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2017-09-09 21:02:58,211 [myid:] - WARN  [main:QuorumPeerMain@113] - Either no config or no quorum defined in config, running  in standalone mode
2017-09-09 21:02:58,445 [myid:] - INFO  [main:QuorumPeerConfig@103] - Reading configuration from: D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\conf\\zoo.cfg
2017-09-09 21:02:58,446 [myid:] - INFO  [main:ZooKeeperServerMain@95] - Starting server
2017-09-09 21:02:58,769 [myid:] - INFO  [main:Environment@100] - Server environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
2017-09-09 21:02:58,772 [myid:] - INFO  [main:Environment@100] - Server environment:host.name=192.168.233.1
2017-09-09 21:02:58,777 [myid:] - INFO  [main:Environment@100] - Server environment:java.version=1.8.0_111
2017-09-09 21:02:58,778 [myid:] - INFO  [main:Environment@100] - Server environment:java.vendor=Oracle Corporation
2017-09-09 21:02:58,780 [myid:] - INFO  [main:Environment@100] - Server environment:java.home=D:\\Program Files\\Java\\jdk1.8.0_111\\jre
2017-09-09 21:02:58,782 [myid:] - INFO  [main:Environment@100] - Server environment:java.class.path=D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\build\\classes;D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\build\\lib\\*;D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\zookeeper-3.4.8.jar;D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\lib\\jline-0.9.94.jar;D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\lib\\log4j-1.2.16.jar;。。。2017-09-09 21:02:58,784 [myid:] - INFO  [main:Environment@100] - Server environment:java.library.path=D:\\Program Files\\Java\\jdk1.8.0_111\\bin;D:\\Program Files\\Java\\jdk1.8.0_111\\bin;D:\\Program Files\\Java\\jdk1.8.0_111\\jre\\bin;
.
2017-09-09 21:02:58,789 [myid:] - INFO  [main:Environment@100] - Server environment:java.io.tmpdir=C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\
2017-09-09 21:02:58,791 [myid:] - INFO  [main:Environment@100] - Server environment:java.compiler=<NA>
2017-09-09 21:02:58,800 [myid:] - INFO  [main:Environment@100] - Server environment:os.name=Windows 7
2017-09-09 21:02:58,801 [myid:] - INFO  [main:Environment@100] - Server environment:os.arch=amd64
2017-09-09 21:02:58,804 [myid:] - INFO  [main:Environment@100] - Server environment:os.version=6.1
2017-09-09 21:02:58,811 [myid:] - INFO  [main:Environment@100] - Server environment:user.name=Administrator
2017-09-09 21:02:58,813 [myid:] - INFO  [main:Environment@100] - Server environment:user.home=C:\\Users\\Administrator
2017-09-09 21:02:58,816 [myid:] - INFO  [main:Environment@100] - Server environment:user.dir=D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin
2017-09-09 21:02:58,849 [myid:] - INFO  [main:ZooKeeperServer@787] - tickTime set to 2000
2017-09-09 21:02:58,851 [myid:] - INFO  [main:ZooKeeperServer@796] - minSessionTimeout set to -1
2017-09-09 21:02:58,852 [myid:] - INFO  [main:ZooKeeperServer@805] - maxSessionTimeout set to -1
2017-09-09 21:03:00,548 [myid:] - INFO  [main:NioserverCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181


--当客户端启动后,就立即打印出来以下信息:
2017-09-09 21:05:23,463 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@192] - Accepted socket connection from /127.0.0.1:33988
2017-09-09 21:05:23,483 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:ZooKeeperServer@900] - Client attempting to establish new session at /127.0.0.1:33988
2017-09-09 21:05:23,496 [myid:] - INFO  [SyncThread:0:FileTxnLog@199] - Creating new log file: log.1
2017-09-09 21:05:23,542 [myid:] - INFO  [SyncThread:0:ZooKeeperServer@645] - Established session 0x15e66bc75ec0000 with negotiated timeout 30000 for client /127.0.0.1:33988

4)启动客户端

进入目录:D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin,执行zkCli.cmd -server 127.0.0.1:2181,这样就启动了zookeeper服务了。

D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin>zkCli.cmd -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
2017-09-09 21:05:21,934 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
2017-09-09 21:05:21,942 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=192.168.233.1
2017-09-09 21:05:21,943 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_111
2017-09-09 21:05:21,947 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-09-09 21:05:21,948 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=D:\\Program Files\\Java\\jdk1.8.0_111\\jre
2017-09-09 21:05:21,949 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..\\build\\classes;D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin\\..。。。。;
.
2017-09-09 21:05:21,960 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=C:\\Users\\ADMINI~1\\AppData\\Local\\Temp\\
2017-09-09 21:05:21,963 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2017-09-09 21:05:21,965 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Windows 7
2017-09-09 21:05:21,967 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2017-09-09 21:05:21,969 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=6.1
2017-09-09 21:05:21,973 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=Administrator
2017-09-09 21:05:21,975 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=C:\\Users\\Administrator
2017-09-09 21:05:21,977 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin
2017-09-09 21:05:21,983 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@7aec35a
Welcome to ZooKeeper!
2017-09-09 21:05:23,459 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2017-09-09 21:05:23,464 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@876] - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
JLine support is enabled
[zk: 127.0.0.1:2181(CONNECTING) 0] 2017-09-09 21:05:23,548 [myid:] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1299] - Session establishmentcomplete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x15e66bc75ec0000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null

这样就安装完成了单节点zookeeper服务。

 伪分布式安装:

伪分布式安装就是在同一台pc上安装,安装时使用同一个zookeeper包,多个配置文件分别配置为不同的端口。

1)将D:\\Work\\zookeeper\\zookeeper-3.4.8\\conf\\下的zoo.cfg分别复制出文件zoo1.cfg,zoo2.cfg,zoo3.cfg三个文件,分别修改配置为:

zoo1.cfg

# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=D:\\\\Work\\\\zookeeper\\\\data\\\\1
# log config
dataLogDir=D:\\\\Work\\\\zookeeper\\\\log\\\\1
# the port at which the clients will connect
clientPort=2181

server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

zoo2.cfg

# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=D:\\\\Work\\\\zookeeper\\\\data\\\\2
# log config
dataLogDir=D:\\\\Work\\\\zookeeper\\\\log\\\\2
# the port at which the clients will connect
clientPort=2182

server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

zoo3.cfg

# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just 
# example sakes.
dataDir=D:\\\\Work\\\\zookeeper\\\\data\\\\3
# log config
dataLogDir=D:\\\\Work\\\\zookeeper\\\\log\\\\3
# the port at which the clients will connect
clientPort=2183

server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

 备注:

  假设把配置文件表示为:zoo{num}.cfg, server.{num}=ip/domain:Port1:Port2

  其中num:表示数字表示第几号服务器;ip/domain :是服务器域名或者ip地址。

   Port1:表示这个服务器和集群中的Leader服务器交换信息的端口;

  Port2:表示万一集群中的Leader服务器挂了,需要一个端口重新进行选举,选出一个新的Leader,这个端口就是用来执行选举时服务器相互通信的端口。

        由于我们是伪集群,所以ip或者域名是一样的,所以要分配不同的端口号

2)创建目录D:\\Work\\zookeeper\\data\\1,D:\\Work\\zookeeper\\data\\2,D:\\Work\\zookeeper\\data\\3

分别在三个文件下创建myid文件,文件内容依次为:1,2,3

3)进入D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin下复制文件zkServer.cmd为zkServer-1.cmd,zkServer-2.cmd,zkServer-3.cmd

zkServer-1.cmd内容修改为如下:

setlocal
call "%~dp0zkEnv.cmd"

set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
set ZOOCFG=D:\\\\Work\\\\zookeeper\\\\zookeeper-3.4.8\\\\conf\\\\zoo1.cfg

echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*

endlocal

zkServer-2.cmd内容修改为如下:

setlocal
call "%~dp0zkEnv.cmd"

set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
set ZOOCFG=D:\\\\Work\\\\zookeeper\\\\zookeeper-3.4.8\\\\conf\\\\zoo2.cfg

echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*

endlocal

zkServer-3.cmd内容修改为如下:

setlocal
call "%~dp0zkEnv.cmd"

set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
set ZOOCFG=D:\\\\Work\\\\zookeeper\\\\zookeeper-3.4.8\\\\conf\\\\zoo3.cfg

echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*

endlocal

4)分别启动zkServer-1.cmd,zkServer-2.cmd,zkServer-3.cmd

进入目录:D:\\Work\\zookeeper\\zookeeper-3.4.8\\bin,分布执行zkServer-1.cmd,zkServer-2.cmd,zkServer-3.cmd,启动伪分布式zookeeper集群,启动过程中如果前两个启动的服务户出现异常情况为正常,直到3个zkServer-x.cmd都启动完后就不会出现异常情况。

以上是关于ZooKeeper:win7上安装单机及伪分布式安装的主要内容,如果未能解决你的问题,请参考以下文章

Zookeeper集群安装部署

Linux系统zookeeper环境搭建(单机伪分布式分布式)

linux下安装zookeeper(单机版)

Zookeeper实战之单机集群模式

linux下安装zookeeper(单机版)

zookeeper单机版安装