Zookeeper简介

Zookeeper基本框架:

Zookeeper集群主要角色有Leader,Learner(Follower,Observer(当服务器增加到一定程度,由于投票的压力增大从而使得吞吐量降低,所以增加了Observer。)以及client:

Leader:领导者,负责投票的发起和决议,以及更新系统状态

Follower:接受客户端的请求并返回结果给客户端,并参与投票

Observer:接受客户端的请求,将写的请求转发给leader,不参与投票。Observer目的是扩展系统,提高读的速度。

Client:客户端,想Zookeeper发起请求。

ZK特性:
    1) 高可用: HDFS/HBase/Spark  HA
    2) API:ZK/Curator
        开发:通过Java/Scala操作ZK
        Kafka:offset(偏移量)可以存储在ZK  ==> 实时流处理:Kafka+Streaming zero data lost

enables highly reliable distributed coordination
分布式协调服务

分布式系统:
    统一的NN进行访问  active(x)  standby(√)(主备,主若挂了备用启动)
    
ZK 集群: n(>1)/2+1  一般是3/5/7/9(台数须为单数)

    N REDIS/... configuration file same
    ZK:主要用来管理配置文件,所以一般来说放的东西都很小
    1: /ruozedata/conf/ruoze.conf
        port=9999 
    2: watch(监听到了变动,于是自动下载)
        ..... download ...
    3: .....(同上)
    所以一般来说只管理一台,其他的自动处理

ZK部署:
1) 下载
2) 解压到~/app
3) 添加到环境变量
export ZK_HOME=/home/hadoop/app/zookeeper-3.4.5-cdh5.7.0
export PATH=$ZK_HOME/bin:$PATH    
4) 配置文件
cp zoo_sample.cfg zoo.cfg    
dataDir=/home/hadoop/app/tmp/zookeeper
clientPort=2181    
5) 启动
./zkServer.sh start
6) 检测
jps: QuorumPeerMain
    
ZK基本数据模型
    树形结构(似目录)

znode(似UDF函数)
    临时节点:当前窗口有效,该节点下不可挂节点
    永久节点:该节点下可挂节点

特点:
    znode: id(可能重复)  ==> id -> pid(递归关系)
    znode data change ==> version + 1(版本变更)
    data: not big
    
ZK常用命令
    ls     查看目录
    get   查看文件详细信息
    ls2    类似get,但不能查看内容
    create 创建节点
        -e: 临时节点
        -s: 顺序节点
        pZxid(最后更新的子节点id)
    
    set 和 delete 要注意version(version不对,直接报错)

ZK四字命令:

ZooKeeper responds to a small set of commands. Each command is composed of four letters. You issue the commands to ZooKeeper via telnet or nc, at the client port.

Three of the more interesting commands: "stat" gives some general information about the server and connected clients, while "srvr" and "cons" give extended details on server and connections respectively.

conf

New in 3.3.0: Print details about serving configuration.

cons

New in 3.3.0: List full connection/session details for all clients connected to this server. Includes information on numbers of packets received/sent, session id, operation latencies, last operation performed, etc...

crst

New in 3.3.0: Reset connection/session statistics for all connections.

dump

Lists the outstanding sessions and ephemeral nodes. This only works on the leader.

envi

Print details about serving environment

ruok

Tests if server is running in a non-error state. The server will respond with imok if it is running. Otherwise it will not respond at all.

A response of "imok" does not necessarily indicate that the server has joined the quorum, just that the server process is active and bound to the specified client port. Use "stat" for details on state wrt quorum and client connection information.

srst

Reset server statistics.

srvr

New in 3.3.0: Lists full details for the server.

stat

Lists brief details for the server and connected clients.

wchs

New in 3.3.0: Lists brief information on watches for the server.

wchc

New in 3.3.0: Lists detailed information on watches for the server, by session. This outputs a list of sessions(connections) with associated watches (paths). Note, depending on the number of watches this operation may be expensive (ie impact server performance), use it carefully.

wchp

New in 3.3.0: Lists detailed information on watches for the server, by path. This outputs a list of paths (znodes) with associated sessions. Note, depending on the number of watches this operation may be expensive (ie impact server performance), use it carefully.

mntr

New in 3.4.0: Outputs a list of variables that could be used for monitoring the health of the cluster.

ZK集群:
    主从关系
    有选举机制: 3 zk,主挂了,通过选举从变主

伪分布式:1台机器上搭建多个ZK节点    
zk001
dataDir=/home/hadoop/app/tmp/zookeeper001
server.1=localhost:2888:3888
server.2=localhost:2889:3889
server.3=localhost:2890:3890

真正分布式    
dataDir=/home/hadoop/app/tmp/zookeeper
server.1=ruoze-hadoop000:2888:3888
server.2=ruoze-hadoop001:2888:3888
server.3=ruoze-hadoop002:2888:3888

在以下目录创建该文件,文件中写id号
${dataDir}/myid     

watch监听
    针对每个节点的操作,都有一个监督站:watcher
    zk里面的watcher是一次性的  
        NodeDeleted
        NodeDataChanged
        NodeCreated
 
ZK API
pom.xml:添加以下依赖
<zk.version>3.4.5-cdh5.7.0</zk.version>

<dependency>
  <groupId>org.apache.zookeeper</groupId>
  <artifactId>zookeeper</artifactId>
  <version>${zk.version}</version>
</dependency>

常用的Java API操作ZK的客户端
    zookeeper
    zkclient: spark streaming + kafka
    curator(常用)

参考链接:https://blog.csdn.net/qunqin/article/details/44179035

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值