ookeeper支持windows、linux、mac等操作系统,其搭建方式也有集群、伪集群、单机环境。下面研究三种方式的搭建。
单机环境:windows操作系统
伪集群:windows
集群:linux
单机环境
下面在windows下面搭建zookeeper的单机环境。windows下面也适合做开发。但是不适合生产环境的部署安装。
java安装
zookeeper依赖于Java环境,所以先自行安装JDK。
Zookeeper下载及其安装
(1)下载zookeeperxxx.tar.gz
载地址:http://zookeeper.apache.org/releases.html 下载xxx.tar.gz解压即可,里面有sh脚本,也有cmd脚本。
(2)解压下载文件
(3)在conf目录下面创建zoo.cfg (在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=E:北大青鸟Y2ookeeperzookeeper-3.4.6data # 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
解释:
tickTime:这个时间是作为 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。
dataDir:顾名思义就是 Zookeeper 保存数据的目录,默认情况下,Zookeeper 将写数据的日志文件也保存在这个目录里。
dataLogDir:log目录, 同样可以是任意目录. 如果没有设置该参数, 将使用和dataDir相同的设置.
clientPort:这个端口就是客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
(4)在对应目录建立上面的data目录即可
(5)启动zookeeper
E:zookeeperzookeeper-3.4.13in> .zkServer.cmd E:zookeeperzookeeper-3.4.13in>call "C:Program FilesJavajdk1.8.0_121"injava "-Dzookeeper.log.dir=E:zookeeperzookeeper-3.4.13in.." "-Dzookeeper.root.logger=INFO,CONSOLE" -cp "E:zookeeperzookeeper-3.4.13in..uildclasses;E:zookeeperzookeeper-3.4.13in..uildlib*;E:zookeeperzookeeper-3.4.13in..*;E:zookeeperzookeeper-3.4.13in..lib*;E:zookeeperzookeeper-3.4.13in..conf" org.apache.zookeeper.server.quorum.QuorumPeerMain "E:zookeeperzookeeper-3.4.13in..confzoo.cfg" 2019-03-07 18:14:37,163 [myid:] - INFO [main:QuorumPeerConfig@136] - Reading configuration from: E:zookeeperzookeeper-3.4.13in..confzoo.cfg 2019-03-07 18:14:37,170 [myid:] - INFO [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3 2019-03-07 18:14:37,171 [myid:] - INFO [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0 2019-03-07 18:14:37,171 [myid:] - INFO [main:DatadirCleanupManager@101] - Purge task is not scheduled. 2019-03-07 18:14:37,173 [myid:] - WARN [main:QuorumPeerMain@116] - Either no config or no quorum defined in config, running in standalone mode 2019-03-07 18:14:37,229 [myid:] - INFO [main:QuorumPeerConfig@136] - Reading configuration from: E:zookeeperzookeeper-3.4.13in..confzoo.cfg 2019-03-07 18:14:37,230 [myid:] - INFO [main:ZooKeeperServerMain@98] - Starting server 2019-03-07 18:14:37,271 [myid:] - INFO [main:Environment@100] - Server environment:zookeeper.version=3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 04:05 GMT 2019-03-07 18:14:37,271 [myid:] - INFO [main:Environment@100] - Server environment:host.name=MicroWin10-1535 2019-03-07 18:14:37,272 [myid:] - INFO [main:Environment@100] - Server environment:java.version=1.8.0_121 2019-03-07 18:14:37,272 [myid:] - INFO [main:Environment@100] - Server environment:java.vendor=Oracle Corporation 2019-03-07 18:14:37,272 [myid:] - INFO [main:Environment@100] - Server environment:java.home=C:Program FilesJavajdk1.8.0_121jre 2019-03-07 18:14:37,273 [myid:] - INFO [main:Environment@100] - Server environment:java.class.path=E:zookeeperzookeeper-3.4.13in..uildclasses;E:zookeeperzookeeper-3.4.13in..uildlib*;E:zookeeperzookeeper-3.4.13in..zookeeper-3.4.13.jar;E:zookeeperzookeeper-3.4.13in..libaudience-annotations-0.5.0.jar;E:zookeeperzookeeper-3.4.13in..libjline-0.9.94.jar;E:zookeeperzookeeper-3.4.13in..liblog4j-1.2.17.jar;E:zookeeperzookeeper-3.4.13in..lib etty-3.10.6.Final.jar;E:zookeeperzookeeper-3.4.13in..libslf4j-api-1.7.25.jar;E:zookeeperzookeeper-3.4.13in..libslf4j-log4j12-1.7.25.jar;E:zookeeperzookeeper-3.4.13in..conf 2019-03-07 18:14:37,274 [myid:] - INFO [main:Environment@100] - Server environment:java.library.path=C:Program FilesJavajdk1.8.0_121in;C:WINDOWSSunJavain;C:WINDOWSsystem32;C:WINDOWS;E:Anaconda3exe;E:Anaconda3exeLibrarymingw-w64in;E:Anaconda3exeLibraryusrin;E:Anaconda3exeLibraryin;E:Anaconda3exeScripts;E:ImageMagick-7.0.8-Q16;C:oraclexeapporacleproduct11.2.0serverin;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;E:softapache-maven-3.5.3in;C:WINDOWSSystem32OpenSSH;E:gitGitcmd;E:SVNin;C:Program Files (x86)Microsoft SQL Server110ToolsBinn;C:Program FilesMicrosoft SQL Server110ToolsBinn;C:Program FilesMicrosoft SQL Server110DTSBinn;C:Program Files (x86)Microsoft SQL Server110ToolsBinnManagementStudio;C:Program Files (x86)Microsoft Visual Studio 10.0Common7IDEPrivateAssemblies;C:Program Files (x86)Microsoft SQL Server110DTSBinn;C:UsersAdministratorAppDataLocalMicrosoftWindowsApps;E:softmavenapache-maven-3.3.9in;C:Program FilesMySQLMySQL Server 5.7in;C:Program FilesJavajdk1.8.0_121in;E:gitGitin;E:gitGitusrin;E:gitGit;C:Program FilesJavajdk1.8.0_121jrein;D:zdcontomcatzdc8loprogram;E: esseract4Tesseract-OCR;E: edis edis-win;;. 2019-03-07 18:14:37,276 [myid:] - INFO [main:Environment@100] - Server environment:java.io.tmpdir=C:UsersADMINI~1AppDataLocalTemp 2019-03-07 18:14:37,276 [myid:] - INFO [main:Environment@100] - Server environment:java.compiler=<NA> 2019-03-07 18:14:37,277 [myid:] - INFO [main:Environment@100] - Server environment:os.name=Windows 10 2019-03-07 18:14:37,278 [myid:] - INFO [main:Environment@100] - Server environment:os.arch=amd64 2019-03-07 18:14:37,279 [myid:] - INFO [main:Environment@100] - Server environment:os.version=10.0 2019-03-07 18:14:37,285 [myid:] - INFO [main:Environment@100] - Server environment:user.name=Administrator 2019-03-07 18:14:37,287 [myid:] - INFO [main:Environment@100] - Server environment:user.home=C:UsersAdministrator 2019-03-07 18:14:37,289 [myid:] - INFO [main:Environment@100] - Server environment:user.dir=E:zookeeperzookeeper-3.4.13in 2019-03-07 18:14:37,296 [myid:] - INFO [main:ZooKeeperServer@836] - tickTime set to 2000 2019-03-07 18:14:37,297 [myid:] - INFO [main:ZooKeeperServer@845] - minSessionTimeout set to -1 2019-03-07 18:14:37,302 [myid:] - INFO [main:ZooKeeperServer@854] - maxSessionTimeout set to -1 2019-03-07 18:14:37,585 [myid:] - INFO [main:ServerCnxnFactory@117] - Using org.apache.zookeeper.server.NIOServerCnxnFactory as server connection factory 2019-03-07 18:14:37,588 [myid:] - INFO [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181
3.测试
(1)用netstat或者jps查看都可以
C:UsersAdministrator>netstat -ano|findstr 2181 TCP 0.0.0.0:2181 0.0.0.0:0 LISTENING 15532 TCP [::]:2181 [::]:0 LISTENING 15532 C:UsersAdministrator>jps -l 19320 sun.tools.jps.Jps 15532 org.apache.zookeeper.server.quorum.QuorumPeerMain 4780
(2)zkCli.cmd连接进行测试:
PS E:zookeeperzookeeper-3.4.13in> .zkCli.cmd Connecting to localhost:2181 ......... [zk: localhost:2181(CONNECTING) 0] 2019-03-07 18:23:07,894 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1303] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x10035685eb40002, negotiated timeout = 30000 WATCHER:: WatchedEvent state:SyncConnected type:None path:null [zk: localhost:2181(CONNECTED) 0]
[zk: localhost:2181(CONNECTED) 0] help ZooKeeper -server host:port cmd args stat path [watch] set path data [version] ls path [watch] delquota [-n|-b] path ls2 path [watch] setAcl path acl setquota -n|-b val path history redo cmdno printwatches on|off delete path [version] sync path listquota path rmr path get path [watch] create [-s] [-e] path data acl addauth scheme auth quit getAcl path close connect host:port [zk: localhost:2181(CONNECTED) 1] quit Quitting... 2019-03-07 18:24:38,781 [myid:] - INFO [main:ZooKeeper@693] - Session: 0x10035685eb40002 closed 2019-03-07 18:24:38,783 [myid:] - INFO [main-EventThread:ClientCnxn$EventThread@522] - EventThread shut down for session: 0x10035685eb40002 PS E:zookeeperzookeeper-3.4.13in>
集群
1.准备三台centos操作系统,ip分别为:
192.168.1.130 我们称为A机器
192.168.1.131 我们称为B机器
192.168.1.133 我们称为C机器
2.建立相同的目录并上传zookeeperxxx.tar.gz
3.解压上面的目录 (三个做相同操作)
4.复制confzoo_sample.cfg到conzoo.cfg目录下:
[root@localhost conf]# cp ./zoo_sample.cfg ./zoo.cfg
[root@localhost conf]# ls
configuration.xsl log4j.properties zoo.cfg zoo_sample.cfg
5.修改 zoo.cfg
tickTime = 2000 dataDir = /opt/zookeeper/zookeeper-3.4.13/data clientPort = 2181 initLimit = 5 syncLimit = 2 server.1=192.168.1.130:2888:3888 server.2=192.168.1.131:2888:3888 server.3=192.168.1.133:2888:3888
6.同时创建三个服务器对应的数据目录
mkdir /opt/zookeeper/zookeeper-3.4.13/data
7.在三个机器的对应的data目录下面分别创建对应的id文件(非常重要)
A机器:
[root@localhost data]# echo "1">myid [root@localhost data]# cat myid 1
B机器:
[root@localhost data]# echo "2">myid [root@localhost data]# cat myid 2
C机器
[root@localhost data]# echo "3">myid [root@localhost data]# cat myid 3
8.接下来启动三个服务器即可(同时发送命令)
/opt/zookeeper/zookeeper-3.4.13/bin/zkServer.sh start
9.JPS查看JVM
三个服务器都有主类为QuorumPeerMain的PID即启动成功。
10.查看集群状态
/opt/zookeeper/zookeeper-3.4.13/bin/zkServer.sh status
A机器:
B机器:
C机器: