Zookeeper安装

下载稳定版

官方地址:https://zookeeper.apache.org/releases.html

选择合适的版本,我的选择

wget https://www.apache.org/dyn/closer.lua/zookeeper/zookeeper-3.6.2/apache-zookeeper-3.6.2-bin.tar.gz

配置zookeeper

官方地址:https://zookeeper.apache.org/doc/r3.6.2/zookeeperStarted.html

配置文件,conf/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=/root/apache-zookeeper-3.6.2-bin/data
# 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
## Metrics Providers
# https://prometheus.io Metrics Exporter
#metricsProvider.className=org.apache.zookeeper.metrics.prometheus.PrometheusMetricsProvider
#metricsProvider.httpPort=7000
#metricsProvider.exportJvmInfo=true

tickTime

  • CS通信心跳时间
  • 心跳间隔,单位是毫秒,系统默认是2000毫秒,也就是间隔两秒心跳一次

tickTime的意义

  • 客户端与服务器或者服务器与服务器之间维持心跳
  • 通过心跳不仅能够用来监听机器的工作状态
  • 还可以通过心跳来控制Flower跟Leader的通信时间
  • 默认情况下FL的会话时常是心跳间隔的两倍。

initLimit

  • 集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量)

syncLimit

  • 集群中flower服务器(F)跟leader(L)服务器之间的请求和答应最多能容忍的心跳数  

dataDir

  • 该属性对应的目录是用来存放myid信息跟一些版本,日志,跟服务器唯一的ID信息等
  • the directory where the snapshot is stored. do not use /tmp for storage, /tmp here is just example sakes.

不要使用/tmp来做存储快照的目录,/tmp这只是一个案例

  • 在集群zookeeper服务在启动的时候回去读取zoo.cfg这个文件
  • 从这个文件中找到这个属性然后获取它的值也就是 dataDir 的路径
  • 它会从这个路径下面读取 myid 这个文件
  • 从这个文件中获取要启动的当前服务器的地址,当它找不到这个地址的时候就会抛出异常,我们可以去查看状态

配置结束后,别忘了在dataDir的文件夹下创建myid,内容可以是1,然后在zoo.cfg中添加

server.1=zk_master:2888:3888;2181

上面的是3.5.*以上的配置,与前面版本的配置有较大区别,参考文章:https://www.cnblogs.com/YC-L/p/15064957.html

然后可以启动了

cd bin
./zkServer.sh start

通过status查看一下状态

./zkServer.sh status

显示standalone,单机模式

集群信息的配置

格式:service.N = YYY:A:B

  • N:代表服务器编号(也就是myid里面的值)
  • YYY:服务器地址
  • A:表示 Flower 跟 Leader的通信端口,Leader接受write请求(默认2888)
  • B:表示 选举端口,Leader挂掉以后,使用该端口执行选举操作(默认是3888)
server.1=node01:2888:3888
server.2=node02:2888:3888
server.3=node03:2888:3888

server的myid配置

在每个结点server指定的dataDir目录下创建myid文件并写入对应的服务器编号

第一个结点,填入1,第二个结点,填入2,依此类推

clientPort

  • 客户端连接的接口,客户端连接zookeeper服务器的端口
  • zookeeper会监听这个端口,接收客户端的请求访问
  • 这个端口默认是2181

导入环境变量

打开文件/etc/profile,填入自己的zooke安装目录

export ZOOKEEPER_HOME=/root/apache-zookeeper-3.6.2-bin
export PATH=$PATH:$ZOOKEEPER_HOME/bin

使能

source /etc/profile

使用客户端管理节点

创建节点并添加数据

获取节点数据

create -e 创建临时节点

create -s 持久序列

通过help查看指令帮助

[zk: localhost:2181(CONNECTED) 3] help
ZooKeeper -server host:port -client-configuration properties-file cmd args
    addWatch [-m mode] path # optional mode is one of [PERSISTENT, PERSISTENT_RECURSIVE] - default is PERSISTENT_RECURSIVE
    addauth scheme auth
    close 
    config [-c] [-w] [-s]
    connect host:port
    create [-s] [-e] [-c] [-t ttl] path [data] [acl]
    delete [-v version] path
    deleteall path [-b batch size]
    delquota [-n|-b] path
    get [-s] [-w] path
    getAcl [-s] path
    getAllChildrenNumber path
    getEphemerals path
    history 
    listquota path
    ls [-s] [-w] [-R] path
    printwatches on|off
    quit 
    reconfig [-s] [-v version] [[-file path] | [-members serverID=host:port1:port2;port3[,...]*]] | [-add serverId=host:port1:port2;port3[,...]]* [-remove serverId[,...]*]
    redo cmdno
    removewatches path [-c|-d|-a] [-l]
    set [-s] [-v version] path data
    setAcl [-s] [-v version] [-R] path acl
    setquota -n|-b val path
    stat [-w] path
    sync path
    version 

分布式ID,防止重命名

通过添加版本号,维护同名节点,防止重命名

论读书
睁开眼,书在面前
闭上眼,书在心里
原文地址:https://www.cnblogs.com/YC-L/p/15009235.html