Zookeeper入门

一,概述

Zookeeper是一个开源的分布式,为分布式应用提供协调服务的Apache项目

1.1 Zookeeper的工作机制

Zookeeper从设计模式角度来理解:是一个基于观察者模式设计的分布式服务管理框架,它负责存储和管理大家都关心的数据,然后接受观察者的注册,一旦这些数据的状态发生变化,Zookeeper就将负责通知已经在Zookeeper上注册的那些观察者做出相应的反应。

Zookeeper = 文件系统 + 通知机制

1.2 Zookeeper特点

  • Zookeeper:一个领导者(Leader),多个跟随者(Follower)组成的集群。
  • 集群中只要有半数以上节点 存活,Zookeeper集群就能正常服务。
  • 全局数据一致:每个Server保存一份相同的数据副本,Client无论连接到哪个Server,数据都是一致的。
  • 更新请求顺序进行,来自同一个Client的更新请求按其发送顺序依次执行。
  • 数据更新原子性:一次数据更新要么成功,要么失败。
  • 实时性:在一定时间范围内,Client能读到最新数据。

1.3 Zookeeper应用场景

​ Zookeeper提供的服务包括:统一命名服务,统一配置管理,统一集群管理,服务器节点动态上下线,软负载均衡等

  • 统一命名服务:在分布式环境下,经常需要对应用/服务进行统一命名,便于识别。

  • 统一配置管理:一般要求一个集群中,所有节点的配置信息是一致的。

    Zookeeper实现配置管理:

    1. 可将配置信息写入Zookeeper上的一个Znode。
    2. 各个客户端服务器监听这个Znode。
    3. 一旦Znode中的数据被修改,Zookeeper将通知各个客户端服务器。
  • 统一集群管理:分布式环境下,实时掌握每个节点的状态是必要的。

    Zookeeper实现实时监控节点状态变化:

    1. 可将节点信息写入Zookeeper上的一个ZNode
    2. 监听这个ZNode可获取它的实时状态变化。
  • 服务器动态上下线:客户端能实时洞察到服务器上下线的变化

  • 软负载均衡:在Zookeeper中记录每台服务器的访问数,让访问数最少的服务器去处理最新的客户端请求

在这里插入图片描述

二,Zookeeper安装

2.1 本地模式安装

第一步:下载Zookeeper,安装地址: http://zookeeper.apache.org/ 。

第二步:解压至Linux目录下,进入conf,修改文件zoo-simple.cfg为zoo.cfg

第三步:打开zoo.cfg,修改dataDir为…/XXX/XXX/Zookeeper3.5.6/zkData,然后在Zookeeper3.5.6目录下创建zkData目录

  • 启动Zookeeper服务端:bin/zkServer.sh start
  • 启动Zookeeper客户端:bin/zkCli.sh
  • 关闭Zookeeper服务端:bin/zkServer.sh stop
  • 关闭Zookeeper客户端:quit

2.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=/home/lmc/java/Zookeeper3.5.6/zkData
# 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服务器与客户端心跳时间,单位毫秒

    ​ Zookeeper使用的基本时间,服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个tickTime时间就会发送一个心跳。

    ​ 它用于心跳机制,并且设置最小的session超时时间为两倍心跳时间。(session的最小超时时间是2 * tickTime)。

  • initLimit:LF 初始通信时限

    ​ 集群中的Follow跟随者服务器与Leader领导者服务器之间初始连接时能容忍的最多心跳数(tickTime的数量),用它来限定集群中的Zookeeper服务器连接到Leader的时限。

  • syncLimit:LF 同步通信时限

    ​ 集群中Leader与Follower之间的最大响应时间单位,假如响应超过syncLimit * tickTime,Leader认为Follow死掉,从服务器列表中删除Follower。

  • dataDir:数据文件目录 + 数据持久化路径

    ​ 主要用于保存Zookeeper中的数据。

  • clientPort:客户端连接端口

    ​ 监听客户端连接的端口

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值