zookeeper学习(上)

在前面的文章里我多次提到zookeeper对于分布式系统开发的重要性,因此对zookeeper的学习是非常必要的。本篇博文主要是讲解zookeeper的安装和zookeeper的一些基本的应用,同时我还会教大家如何安装伪分布式,伪分布式不能在windows下实现,只能在linux下实现,我的伪分布式是通过电脑的虚拟机完成了,好了,不废话了,具体内容如下:

  首先我们要下载一个zookeeper,下载地址是:

  http://www.apache.org/dyn/closer.cgi/zookeeper/

  一般我们会选择一个stable版(稳定版)进行下载,我下载的版本是zookeeper-3.4.5。

  我笔记本的操作系统是windows7,windows操作系统可以作为zookeeper的开发平台,但是不能作为zookeeper的生产平台,首先我们在windows下安装一个单机版的zookeeper。

  我们先解压zookeeper的安装包,解压后的zookeeper安装包我放置的路径是:

  E:\zookeeper\zookeeper-3.4.5

  下图是zookeeper的目录结构:

  我们进入conf包,将zoo_sample.cfg文件复制一份,并将复制好的文件改名为zoo.cfg。打开新建的zoo.cfg文件,将里面的内容进行修改,修改后的文件内容如下:

#initLimit=10
#syncLimit=5
tickTime=2000
dataDir=E:/zookeeper/zookeeper-3.4.5/data
clientPort=2181

  下面我来解释下配置文件里的各个参数:

  initLimit和syncLimit是针对集群的参数,在我后面讲解伪分布式安装时候我会再讲解。

  tickTime:该参数用来定义心跳的间隔时间,zookeeper的客户端和服务端之间也有和web开发里类似的session的概念,而zookeeper里最小的session过期时间就是tickTime的两倍。

  dataDir:英文注释可以翻译为存储在内存中的数据库快照功能,我们可以看看运行后dataDir所指向的文件存储了什么样的数据,如下图所示:

  看来dataDir里还存储了日志信息,dataDir不能存放在命名为tmp的文件里。

  clientPort:是监听客户端连接的端口号。

  接下来我们要将zookeeper的安装信息配置到windows的环境变量里,我们在“我的电脑”上点击右键,选择属性,再点击高级系统设置,点击环境变量按钮,在系统变量这一栏,点击新建,添加:

变量名:ZOOKEEPER_HOME
变量值:E:\zookeeper\zookeeper-3.4.5

  还是在系统变量这一栏,找到path,点击编辑path,在变量值里添加:% ZOOKEEPER_HOME %\bin; % ZOOKEEPER_HOME %\conf;

  Zookeeper使用java编写的,因此安装zookeeper之前一定要先安装好jdk,并且jdk的版本要大于或等于1.6。

  这样单机版的zookeeper就安装好了,下面我们将运行zookeeper。

  首先我们打开windows的命令行工具,将文件夹转到zookeeper安装目录的下的bin目录,然后输入zkServer命令,回车执行,那么zookeeper服务就启动成功了。

  下面我们用客户端连接zookeeper的服务端,我们再打开一个命令行工具,输入命令:

zkCli -server localhost:2181

  下面是相关测试,如下图所示:

  伪分布式的安装,zookeeper和hadoop一样也可以进行伪分布式的安装,下面我就讲解如何进行伪分布式安装。

  我开始尝试在windows下安装伪分布式,但是没有成功,最后是在linux操作系统下才安装好伪分布式,我们首先下载好zookeeper的安装程序,然后新建三个配置文件分别是:

zoo1.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=E:/zookeeper/zookeeper-3.4.5/d_1
# the port at which the clients will connect
clientPort=2181
#
# 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
dataLogDir=E:/zookeeper/zookeeper-3.4.5/log1_2
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

zoo2.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=E:/zookeeper/zookeeper-3.4.5/d_2
# the port at which the clients will connect
clientPort=2182
#
# 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
dataLogDir=E:/zookeeper/zookeeper-3.4.5/logs_2
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

zoo3.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=E:/zookeeper/zookeeper-3.4.5/d_3
# the port at which the clients will connect
clientPort=2183
#
# 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
dataLogDir=E:/zookeeper/zookeeper-3.4.5/logs_3
server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

  这里我们把每个配置文件里的clientPort做了一定修改,让每个文件之间的clientPort不一样,dataDir属性也做了同样的调整,同时还添加了新配置内容,如下所示:

server.1=localhost:2887:3887
server.2=localhost:2888:3888
server.3=localhost:2889:3889

  这里localhost指的是组成zookeeper服务的机器IP的地址,2887是用于进行leader选举的端口,3887是zookeeper集群里各个机器之间的通信接口。

  initLimit:是指follower连接并同步到leader的初始化连接,它是通过tickTime的倍数表示,例如我们上面的配置就是10倍的tickTime,当初始化连接时间超过设置的倍数时候则连接失败。

  syncLimit:是指follower和leader之间发送消息时请求和应答的时间长度,如果follower在设置的时间范围内不能喝leader通信,那么该follower将会被丢弃,它也是按tickTime的倍数进行设置的。

  dataLogDir:这个配置是指zookeeper运行的相关日志写入的目录,设定了配置,那么dataLog里日志的目录将无效,专门的日志存放路径,对zookeeper的性能和稳定性有好处。

  这里每一个配置文件都代表一个zookeeper服务器,下面我们启动伪分布式的zookeeper集群。

zkServer.sh start zoo1.cfg
 
zkServer.sh start zoo2.cfg
 
zkServer.sh start zoo3.cfg

  下面我写一个java程序,该程序作为客户端调用zookeeper的服务,代码如下:

package cn.com.test;
 
import java.io.IOException;
 
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
 
public class zkClient {
 
    public static void main(String[] args) throws Exception{
        Watcher wh = new Watcher(){
            @Override
            public void process(WatchedEvent event) {
                System.out.println(event.toString());
            }
        };
        ZooKeeper zk = new ZooKeeper("localhost:2181",30000,wh);
        System.out.println("=========创建节点===========");
        zk.create("/sharpxiajun", "znode1".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
        System.err.println("=============查看节点是否安装成功===============");
        System.out.println(new String(zk.getData("/sharpxiajun", false, null)));
        System.out.println("=========修改节点的数据==========");
        zk.setData("/sharpxiajun", "sharpxiajun130901".getBytes(), -1);
        System.out.println("========查看修改的节点是否成功=========");
        System.out.println(new String(zk.getData("/sharpxiajun", false, null)));
        System.out.println("=======删除节点==========");
        zk.delete("/sharpxiajun", -1);
        System.out.println("==========查看节点是否被删除============");
        System.out.println("节点状态:" + zk.exists("/sharpxiajun", false));
        zk.close();
    }
 
}

  执行结果如下:

log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).
log4j:WARN Please initialize the log4j system properly.
=========创建节点===========
WatchedEvent state:SyncConnected type:None path:null
=============查看节点是否安装成功===============
znode1
=========修改节点的数据==========
========查看修改的节点是否成功=========
sharpxiajun130901
=======删除节点==========
==========查看节点是否被删除============
节点状态:null

  程序我今天不讲解了,只是给大伙展示下使用zookeeper的方式,本文可能没啥新颖的东西,但是本文是一个基础,有了这个基础我们才能真正操作zookeeper。

 
 
分类: hadoop云计算

zookeeper学习(上)的更多相关文章

  1. Zookeeper学习笔记(上)

    Zookeeper学习笔记 本篇主要是一些基本的介绍和API的使用介绍, 有些只是记录了知识点,而没有完全在笔记中详细解释, 需要自行查找资料补充相关概念 主要参考了课程中的内容: Zookeeper ...

  2. ZooKeeper学习第二期--ZooKeeper安装配置

    一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式. ■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境:■ 伪集群模式:就是在一台物 ...

  3. ZooKeeper 学习笔记

    ZooKeeper学习笔记 1.   zookeeper基本概念 zookeeper是一个分布式的,开放源码的分布式应用程序协调服务,是hadoop和Habase的重要组件,是为分布式应用提供一致性服 ...

  4. 【转载】ZooKeeper学习第二期--ZooKeeper安装配置

    原文地址(https://www.cnblogs.com/sunddenly/p/4018459.html) 一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及 ...

  5. [转]ZooKeeper学习第一期---Zookeeper简单介绍

    ZooKeeper学习第一期---Zookeeper简单介绍 http://www.cnblogs.com/sunddenly/p/4033574.html 一.分布式协调技术 在给大家介绍ZooKe ...

  6. ZooKeeper学习2---ZooKeeper安装配置

    一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪集群模式. ■ 单机模式:Zookeeper只运行在一台服务器上,适合测试环境:■ 伪集群模式:就是在一台物 ...

  7. ZooKeeper学习第二期--ZooKeeper安装配置(转)

    转载来源:https://www.cnblogs.com/sunddenly/p/4018459.html 一.Zookeeper的搭建方式 Zookeeper安装方式有三种,单机模式和集群模式以及伪 ...

  8. zookeeper学习(零)_安装与启动

    zookeeper学习(零)_安装与启动 最近换了新的电脑,终于买了梦寐以求的macbook.最近也换了新的公司,公司技术栈用到了zookeeper.当然自己也要安装学习下.省的渣渣的我,被鄙视就麻烦 ...

  9. ZooKeeper学习笔记(二)——内部原理

    zookeeper学习笔记(二)--内部原理 1. zookeeper的节点的类型 总的来说可以分为持久型和短暂型,主要区别如下: 持久:客户端与服务器端断开连接的以后,创建的节点不会被删除: 持久化 ...

随机推荐

  1. 前端学习笔记(zepto或jquery)——对li标签的相关操作(四)

    对li标签的相关操作——五种方式给奇数项li标签设置样式 demo演示: 1 2 3 4 5 6 7 // 详解: 通常我们为多个li添加样式时常用的是使用filter,但我们在第三节中可以看到fil ...

  2. Cocos2d-x之MenuItem

    ***************************************转载请注明出处:http://blog.csdn.net/lttree************************** ...

  3. 动态类(Dynamic)应用

    动态类(Dynamic)应用 背景: 在Coding中有时候会遇到一些需要解析的数据,可是数据的字段数量和名称未统一,我们没法定义实体类来对应.那么我们就会想到通过C#的dynamic动态类来实现,如 ...

  4. NPOI mvc easyui 根据Excel模板 生成Excel

    1.首先下载 NPOI  https://npoi.codeplex.com/releases  只要dll 就好 示例代码库太难懂了. NPOI 是一个开源  免费的 东西.而且不依赖 office ...

  5. [Openstack] Expecting an auth URL via either --os-auth-url or env[OS_AUTH_URL]

    直接使用devstack在ubuntu14.04单个节点的建筑openstack 使用keystone查询租户和用户始终报告时,这个错误! 主要看下这些配置是否正确.我们将能够解决这个问题 opens ...

  6. 远程调用之RMI技术

    ---恢复内容开始--- RMI已经不是什么新的技术了,但是相对于webservice来说,rmi比较简单,比较适合一些小的应用,下面的helloword列子可以介绍rmi的相关技术 服务器端代码: ...

  7. ym——android源代码大放送(实战开发必备)

    转载请注明本文出自Cym的博客(http://blog.csdn.net/cym492224103),谢谢支持! 目录 PATH 列表 卷序列号为 000A-8F50 E:. │  javaapk.c ...

  8. JAVA学习课第五十三届 — IO流程(七)File打靶 & Properties设置

    一个.锻炼 深度遍历目录 深度遍历非常自然而然想到递归,而递归就非常自然的想到事实上现的底层算法是栈 对指定文件夹下列出全部内容(包括子文件夹的内容) PS:建议不要遍历C盘 import java. ...

  9. 亮点面试题&&实现Singleton(辛格尔顿)模式-JAVA版本

    称号:设计一个类.我们只能产生这个类的一个实例.(来自<剑指Offer>) 解析:仅仅能生产一个实例的类是实现Singleton(单例)模式的类型.因为设计模式在面向对象程序设计中起着举足 ...

  10. DDD分层架构之值对象(层超类型篇)

    DDD分层架构之值对象(层超类型篇) 上一篇介绍了值对象的基本概念,得到了一些朋友的支持,另外也有一些朋友提出了不同意见.这其实是很自然的事情,设计本来就充满了各种可能性,没有绝对正确的做法,只有更好 ...