Zookeeper 搭建教程

Zookeeper

一、Zookeeper介绍
  • ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
  • ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
  • ZooKeeper包含一个简单的原语集,[1] 提供Java和C的接口。
  • ZooKeeper代码版本中,提供了分布式独享锁、选举、队列的接口,代码在zookeeper-3.4.3\src\recipes。其中分布锁和队列有Java和C两个版本,选举只有Java版本。
二、Zookeeper安装
  • 1、Zookeeper下载、解压

    • A、Zookeeper官网下载

      wget https://mirrors.tuna.tsinghua.edu.cn/apache/zookeeper/stable/zookeeper-3.4.10.tar.gz
    • Zookeeper解压

      //tar解压
      tar -zxvf zookeeper-3.4.10.tar.gz
      //把zookeeper移动到etc目录
      cp -r zookeeper-3.4.10 /etc/zookeeper
  • 2、Zookeeper目录介绍

    • bin
      • zk的可执行脚本目录,包括zk服务进程,zk客户端等脚本
    • conf
      • 配置文件目录。zoo_sample.cfg为样例配置文件,需要修改为自己的名称,一般为zoo.cfg。log4j.properties为日志配置文件
    • lib
      • zk依赖的包
    • contrib
      • 一些用于操作zk的工具包
    • recipes
      • zk某些用法的代码示例
  • 3、Zookeeper配置修改

    • 把zoo_sample.cfg复制zoo.cfg

      cd /etc/zookeeper
      cp conf/zoo_sample.cfg  conf/zoo.cfg
    • zoo.cfg配置介绍

      • tickTime
        • tickTime的默认值为2000毫秒,更低的tickTime值可以更快地发现超时问题,但也会导致更高的网络流量(心跳消息)和更高的CPU使用率(会话的跟踪处理)
      • clientPort
        • zk服务进程监听的TCP端口,默认情况下,服务端会监听2181端口。
      • dataDir
        • 用于配置存储快照文件的目录。如果没有配置dataLogDir,那么事务日志也会存储在此目录。
      • initLimit
        • ZooKeeper集群模式下包含多个zk进程,其中一个进程为leader,余下的进程为follower。
        • 当follower最初与leader建立连接时,它们之间会传输相当多的数据,尤其是follower的数据落后leader很多。initLimit配置follower与leader之间建立连接后进行同步的最长时间。
      • syncLimit
        • 配置follower和leader之间发送消息,请求和应答的最大时间长度。
      • server.id=host:port1:port2
        • 其中id为一个数字,表示zk进程的id,这个id也是dataDir目录下myid文件的内容。
        • host是该zk进程所在的IP地址,port1表示follower和leader交换消息所使用的端口,port2表示选举leader所使用的端口。
三、Zookeeper模式
  • 1、单机模式

    • 配置zoo.cfg

      
      # The number of milliseconds of each tick
      
      tickTime=2000
      
      # 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 connect
      
      clientPort=2181
    • 启动Zookeeper

      //启动
      ./bin/zkServer.sh start
      //关闭
      ./bin/zkServer.sh stop
    • 查看是否启动成功

      • 查看端口启动监听,配置端口为clientPort=2181
        netstat -ntlp
    • 查看Zookeeper信息

       [root@node378v /etc/zookeeper]# telnet 10.121.215.37 2181
      Trying 10.121.215.37...
      Connected to 10.121.215.37.
      Escape character is '^]'.
      stated
      Zookeeper version: 3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
      Clients:
       /10.121.215.37:58984[0](queued=0,recved=1,sent=0)
      
      Latency min/avg/max: 0/4/18
      Received: 8
      Sent: 7
      Connections: 1
      Outstanding: 0
      Zxid: 0x3
      Mode: standalone
      Node count: 4
      Connection closed by foreign host.
    • 连接Zookeeper命令

      bin/zkCli.sh -server 10.121.215.37:2181
  • 2、集群模式

    • 1、在服务器10.121.215.37、10.121.215.38、10.121.215.39搭建Zookeeper服务
    • 2、在三台服务器配置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=/tmp/zookeeper
      
      # the port at which the clients will connect
      
      clientPort=2181
      server.1=10.121.215.37:2287:3387
      server.2=10.121.215.38:2288:3388
      server.3=10.121.215.39:2289:3389
    • 在三台服务器,在/tmp/zookeeper中创建myid文件,myid文件中数字亦即上面介绍server.id中的id,表示zk进程的id。比如:10.121.215.37为1、10.121.215.38为2,10.121.215.39为3

    • 3、启动Zookeeper服务

      ./bin/zkServer.sh start
    • 4、连接Zookeeper服务器bin/zkCli.sh -server 10.121.215.37:2181,10.121.215.38:2181,10.121.215.39:2181

      [root@node378v /etc/zookeeper]# bin/zkCli.sh -server 10.121.215.37:2181,10.121.215.38:2181,10.121.215.39:2181
      2017-10-09 16:33:51,565 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=10.121.215.37:2181,10.121.215.38:2181,10.121.215.39:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@69d0a921
      Welcome to ZooKeeper!
      2017-10-09 16:33:51,601 [myid:] - INFO  [main-SendThread(10.121.215.37:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 10.121.215.37/10.121.215.37:2181. Will not attempt to authenticate using SASL (unknown error)
      JLine support is enabled
      2017-10-09 16:33:51,692 [myid:] - INFO  [main-SendThread(10.121.215.37:2181):ClientCnxn$SendThread@876] - Socket connection established to 10.121.215.37/10.121.215.37:2181, initiating session
      2017-10-09 16:33:51,708 [myid:] - INFO  [main-SendThread(10.121.215.37:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 10.121.215.37/10.121.215.37:2181, sessionid = 0x15f0042f67d0002, negotiated timeout = 30000
      WATCHER:
      WatchedEvent state:SyncConnected type:None path:null
      [zk: 10.121.215.37:2181,10.121.215.38:2181,10.121.215.39:2181(CONNECTED) 0] ^C[root@node378v /etc/zookeeper]#

      从日志输出可以看到,客户端连接的是10.121.215.37:2181进程(连接上哪台机器的zk进程是随机的),客户端已成功连接上zk集群。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值