zookeeper安装配置

来源:互联网 发布:京贷网网络贷款 编辑:程序博客网 时间:2024/06/07 06:32

Linux版本:CentOS 6.5 64位

1.下载安装包

cd /usr/localwget http://mirrors.hust.edu.cn/apache/zookeeper/stable/zookeeper-3.4.10.tar.gz

2.解压

tar -zxvf zookeeper-3.4.10.tar.gz

3.创建数据目录

cd /usr/local/zookeeper-3.4.10mkdir data

4.配置

  • 环境变量配置
  cd /etc  #添加环境变量至文件末尾  vi profile  #zookeeper  export ZOOKEEPER_HOME=/usr/local/zookeeper-3.4.10  export PATH=$PATH:$ZOOKEEPER_HOME/bin
  • 配置文件修改
  cd /usr/local/zookeeper-3.4.10/conf  cp zoo_sample.cfg zoo.cfg  vi zoo.cfg

3.4.10 版本提供的配置文件样例如下

# The number of milliseconds of each ticktickTime=2000# The number of ticks that the initial# synchronization phase can takeinitLimit=10# The number of ticks that can pass between# sending a request and getting an acknowledgementsyncLimit=5# the directory where the snapshot is stored.# do not use /tmp for storage, /tmp here is just# example sakes.dataDir=/tmp/zookeeper# the port at which the clients will connectclientPort=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=2000dataDir=/var/lib/zookeeperclientPort=2181

其中 tickTime 为 zookeeper 的基本时间单位,单位为毫秒,最小会话超时时间为此时间的2倍;
dataDir 为存放内存数据库快照的路径,并且若未特别指明,这也是数据库事务日志 dataLogDir 的存储路径,但是推荐将这两者分开存放以提高性能。dateDir 与 dateLogDir 路径必须存在,如无则需自行新建;
clientPort为监听客户端连接的端口。

5.运行

  • 开启服务
 [root@local bin]# sh ./zkServer.sh start ZooKeeper JMX enabled by default Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg Starting zookeeper ... STARTED

类似的,停止服务与查看服务状态为将start分别替换为stop与status

  • 启动客户端检测是否成功启动
[root@local bin]# ./zkServer.shZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgUsage: ./zkServer.sh {start|start-foreground|stop|restart|status|upgrade|print-cmd}[root@local bin]# ./zkServer.sh startZooKeeper JMX enabled by defaultUsing config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfgStarting zookeeper ... already running as process 7591.[root@local bin]# ./zkCli.shConnecting to localhost:21812017-12-15 11:16:00,331 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT2017-12-15 11:16:00,345 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=local2017-12-15 11:16:00,346 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_1312017-12-15 11:16:00,356 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation2017-12-15 11:16:00,357 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=/usr/java/jdk1.8.0_131/jre2017-12-15 11:16:00,357 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper-3.4.10/bin/../build/classes:/usr/local/zookeeper-3.4.10/bin/../build/lib/*.jar:/usr/local/zookeeper-3.4.10/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper-3.4.10/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper-3.4.10/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper-3.4.10/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper-3.4.10/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper-3.4.10/bin/../zookeeper-3.4.10.jar:/usr/local/zookeeper-3.4.10/bin/../src/java/lib/*.jar:/usr/local/zookeeper-3.4.10/bin/../conf:2017-12-15 11:16:00,357 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib2017-12-15 11:16:00,357 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=/tmp2017-12-15 11:16:00,358 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>2017-12-15 11:16:00,358 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Linux2017-12-15 11:16:00,359 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd642017-12-15 11:16:00,360 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=2.6.32-696.16.1.el6.x86_642017-12-15 11:16:00,361 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=root2017-12-15 11:16:00,363 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=/root2017-12-15 11:16:00,363 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper-3.4.10/bin2017-12-15 11:16:00,370 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@277050dcWelcome to ZooKeeper!2017-12-15 11:16:00,478 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)JLine support is enabled2017-12-15 11:16:00,894 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@876] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session[zk: localhost:2181(CONNECTING) 0] 2017-12-15 11:16:01,598 [myid:] - INFO  [main-SendThread(localhost:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x1605828a18a0000, negotiated timeout = 30000WATCHER::WatchedEvent state:SyncConnected type:None path:null

至此,单机模式下的 zookeeper 就安装完成了。

若是集群模式,则在第4步修改配置文件时,需根据需求修改配置中的相关内容:
initLimit:允许服务器连接 leader 所花费的最大时长,若连接 leader 的时间超过这一时长,则判断为连接失败。该时长以 tickTime 的倍数表示,如样例文件中,initLimit 为 tickTime 的 10 倍;
syncLimit:允许服务器与 leader 同步数据的最大时长,若超过此时长,则断开此次连接。该时长同样以 tickTime 的倍数表示,如样例文件中,syncLimit 为 tickTime 的 5 倍;
maxClientCnxns:允许的最大连接数,默认为 60。
还需在文件中添加不同的 zookeeper 服务器信息

server.1=zoo1:2888:3888server.2=zoo2:2888:3888server.3=zoo3:2888:3888

其中 server. 后的数字 1,2,3 服务器 id,要求唯一且取值为 [1,255];zoo1 为各个服务器的ip地址;2888 为 follower 访问 leader 的端口,3888 为进行 leader 选举的端口。
由于 zookeeper 使用 ZAB 协议,需要通过多数派判断原子广播(Atomic Broadcast)是否响应成功,建议在集群中使用奇数个服务器而非偶数个。如,若集群中有3台服务器,一台服务器 down 了,则剩下的仍能运行,而若为2台服务器出现此种情况,剩下的始终不能响应成功了。
配置完成后,需将配置文件远程分发给各个服务器,保证配置文件的一致性。同时,需在各个服务器的 dataDir 路径下创建 myid 文件,内容仅为服务器自身 id。