ZooKeeper的安装非常简单,它的工作模式分为单机模式、集群模式和伪集群模式,本博客旨在总结ZooKeeper单机模式下如何安装、配置、启动和使用:

一、安装配置ZooKeeper(在Windows操作系统下)

a、下载ZooKeeper压缩安装文件,这里下载稳定版——zookeeper-3.4.5.tar.gz

b、解压压缩文件,这里将其解压到C盘根目录下,打开解压后的文件夹,得到下图:

c、点击上图名为“conf”的文件夹,可以看到下图:

d、用记事本打开上图名为“zoo_sample.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
#
# 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服务器之间或客户端与服务器之间维持心跳的时间间隔,即每隔tickTime毫秒就会发送一个心跳。上面设置的是2000毫秒。
initLimit:用来设置ZooKeeper服务器集群中连接到Leader的Follower服务器最长能能接受("设定值"*tickTime)毫秒时间的心跳。超过该时间后ZooKeeper服务器集群中的Follower服务器还没有返回信息,那么表明该Follower服务器连接失败。上面设置的是(10*2000)毫秒。该属性是针对ZooKeeper为集群模式或伪集群模式时使用的参数
syncLimit:用于设置ZooKeeper服务器集群中Leader服务器与Follower服务器之间发送消息时请求和应答的最大时长,其时间长度为("设定值"*tickTime)毫秒。上面设置的是(5*2000)毫秒。该属性是针对ZooKeeper为集群模式或伪集群模式时使用的参数
dataDir:ZooKeeper保存数据的目录,默认情况下,ZooKeeper将写数据的日志文件也保存在这个目录里。注意:该目录不能是/tmp
clientPort:客户端连接ZooKeeper服务器的端口,Zookeeper监听该端口并通过该端口接受客户端的访问请求

e、在conf文件夹中新建名为“zoo.cfg”的文件(ZooKeeper在启动时会找名为“zoo.cfg”的文件并将其作为默认配置文件),并用记事本打开,将原来名为“zoo_sample.cfg”的文件中的内容拷贝到新建的“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=C:/zookeeper-3.4.5/data
# 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

至此ZooKeeper在Windows操作系统中安装配置完毕,但需要指出的是ZooKeeper是使用Java编写的,因此运行ZooKeeper之前必须安装Java环境——配置JDK,且JDK的版本要大于或等于1.6。

二、启动ZooKeeper

打开上面第一张图片中显示的名为“bin”的文件夹,得到下图:

a、启动ZooKeeper服务器端:在Windows操作系统中双击上图名为“zkServer.cmd”的文件,在Linux操作系统中使用命令运行名为“zkServer.sh”的文件;

b、启动ZooKeeper客户端:在Windows操作系统中双击上图名为“zkCli.cmd”的文件,在Linux操作系统中使用命令运行名为“zkCli.sh”的文件;

注意:上面两步不能颠倒,否则ZooKeeper客户端不能成功启动;

三、使用ZooKeeper

在Windows操作系统中双击“zkServer.cmd”文件和“zkCli.cmd”文件后会出现两个cmd窗口,千万不要关闭哟,否则下面程序就不能运行了:

package com.ghj.packageoftest;import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;public class ZooKeeperClient {public static void main(String[] args) throws Exception{Watcher watcher = new Watcher(){// 监控所有被触发的事件public void process(WatchedEvent event) { System.out.println("触发了" + event.getType() + "事件!"); }};ZooKeeper zooKeeper = new ZooKeeper("127.0.0.1:2181", 5000, watcher);//第一个参数:ZooKeeper服务器客户端的连接地址,如果ZooKeeper是集群模式或伪集群模式(即ZooKeeper服务器有多个),那么每个连接地址之间使用英文逗号间隔,单个连接地址的语法格式为“主机IP:ZooKeeper服务器端口号”;//第二个参数:session超时时长(单位:毫秒)//第三个参数:用于监控目录节点数据变化和子目录状态变化的Watcher对象zooKeeper.create("/RootNode", "RootNodeData".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);//创建一个节点名为“/RootNode”的目录节点System.out.println("“/RootNode”节点状态:" + zooKeeper.exists("/RootNode",true));//判断指定目录节点是否存在System.out.println("“RootNode”节点上数据:"+new String(zooKeeper.getData("/RootNode", false, null)));//获取“RootNode”节点上的数据zooKeeper.create("/RootNode/ChildNode1", "ChildNode1Data".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);//在“RootNode”节点下创建一个名为“ChildNode1”的子目录节点zooKeeper.create("/RootNode/ChildNode2", "ChildNode2Data".getBytes(), Ids.OPEN_ACL_UNSAFE,CreateMode.PERSISTENT);//在“RootNode”节点下创建一个和“ChildNode1”同级的名为“ChildNode2”的子目录节点System.out.println("目录节点“RootNode”下的所有子目录节点有:"+zooKeeper.getChildren("/RootNode",true)); //取出目录节点“RootNode”下的所有子目录节点zooKeeper.setData("/RootNode/ChildNode2","NewChildNode2Data".getBytes(),-1);//修改名为“ChildNode2”的目录节点数据zooKeeper.delete("/RootNode/ChildNode1", -1);//删除“/RootNode/ChildNode1”目录节点System.out.println("“/RootNode/ChildNode1”节点状态:" + zooKeeper.exists("/RootNode/ChildNode1", false));//判断“/RootNode/ChildNode1”目录节点是否存在zooKeeper.delete("/RootNode/ChildNode2", -1);//删除“/RootNode/ChildNode2”目录节点zooKeeper.delete("/RootNode", -1);//删除“/RootNode”目录节点zooKeeper.close(); //关闭与ZooKeeper的连接}
}

0分下载示例代码

ZooKeeper的安装、配置、启动和使用(一)——单机模式相关推荐

  1. Web基础配置篇(十一): Zookeeper的安装配置及使用

    Web基础配置篇(十一): Zookeeper的安装配置及使用 一.概述 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop ...

  2. windows环境下Zookeeper的安装配置调试

    了解Zookeeper:---->   https://blog.csdn.net/gs80140/article/details/51496925 安装jdk 安装Zookeeper. 在官网 ...

  3. Zookeeper的安装配置及基本开发

    一.简介 Zookeeper 是分布式服务框架,主要是用来解决分布式应用中经常遇到的一些数据管理问题,如:统一命名服务.状态同步服务.集群管理.分布式应用配置项的管理等等. ZooKeeper的目标就 ...

  4. saltstack(八):saltstack配置管理-安装配置启动tomcat样例

    Saltstack配置启动tomcat 判断minion有没有安装jdk和tomcat,没有就分别源码安装,并以普通用户启动. #有一个认识上的坑,saltstack实际上是基于状态管理,例如说tom ...

  5. yii2框架的安装配置启动

    top:环境MacBook 1.通过composer 安装yii2 [yii2需要php的PDO和pdo_mysql扩展,需要确认已安装] a. 首先需要配置composer: 我使用的是阿里云的镜像 ...

  6. Nexus【环境搭建 02】最新版本 nexus-3.35.0-02-unix.tar.gz 安装配置启动及测试(JDK版本+虚拟机参数配置说明)

    一下安装以 nexus-3.35.0-02-unix.tar.gz 进行说明,老版本 nexus-3.4.0-02-unix.tar.gz 的安装说明可以查看<CentOS 7.5 环境下搭建私 ...

  7. 自己写分布式配置中心(上篇)- 单机模式

    作者:SnoWalker 来源:http://wuwenliang.net/2018/12/05/%E8%87%AA%E5%B7%B1%E5%86%99%E5%88%86%E5%B8%83%E5%BC ...

  8. zookeeper 的安装配置及简单使用

    ===> Zookeeper 是什么? => ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重 ...

  9. 01_Influxdb1.7.7的安装配置启动

    目录 1.Influxdb简介 2.influxdb的points数据说明 3.influxdb的下载和安装 首先我们要用root用户权限进入系统,这样方便操作 1.Influxdb简介 influx ...

  10. Zookeeper本地安装配置(windows)

    1. 下载zookeeper,下载地址: https://www.apache.org/dyn/closer.cgi/zookeeper/ 2. 解压后,进入目录中的conf目录,有一个zoo_sam ...

最新文章

  1. 浅谈 MVP in Android
  2. python电影数据分析的代码_python-small-examples
  3. python中的继承有什么特点_python类的继承是什么?类的继承有什么样的规则?
  4. 全球及中国天然香豆素行业竞争态势与投资份额调研报告2022版
  5. [Spark][Python]sortByKey 例子
  6. Linux系统TCP内核参数优化总结
  7. 选购光纤交换机时需要注意光模块的哪些配置?
  8. 小甲鱼 OllyDbg 教程系列 (八) :fjproducer 逆向 之 困境
  9. 南开大学c语言试题,南开大学二级C语言试题库,共71页
  10. redis集群连接 java_Redis分布式集群和直连的Java客户端调用方式详解
  11. flask中蓝图的使用
  12. Flask 中的数据库迁移
  13. 苹果自带输入法怎么换行_小屏幕手机的福音:分享自用输入法皮肤丨免费
  14. html怎么设置表单的样式,html表单样式 如何用js给html表单设置style
  15. Magic-api介绍及使用
  16. SQL基础1--select
  17. ​浓情七夕,有礼相送!
  18. 只有你能听见(Calling you)2
  19. 暴雪修改手机500服务器错误,网站http服务器内部500错误的解决方法 [图文]
  20. Jsp程序设计-数据库练习题(二)

热门文章

  1. NTP授时系统(GPS时钟产品-GPS授时产品)
  2. Greenplum技术浅析
  3. Dictionary Union and Sort by value
  4. Javascript实现子窗口向父窗口传值(转)
  5. 一文看尽中亦科技EVO-ITSM 3.0新品发布会
  6. 微型 ORM-FluentData 实例详解
  7. docker的核心原理-cgroup
  8. 澳大利亚通信软件服务公司 Whispir 完成1175万美元 A 轮融资
  9. Ubuntu14.04安装VMwareTools
  10. Linux locale