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

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

这里对上面几个参数做一下必要的说明:

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的连接
}
}

ZooKeeper的安装、配置、启动和使用(一)——单机模式的更多相关文章

  1. zookeeper 分布式安装/配置/启动

    版本3.4.10,已提前配置好jdk 三台主机:hadoop002,hadoop003.hadoop004 1.配置 将zookeeper-3.4.10.tar.gz解压后进入zookeeper-3. ...

  2. ZooKeeper 组件安装配置

    ZooKeeper 组件安装配置 下载和安装 ZooKeeper ZooKeeper最新的版本可以通过官网 http://hadoop.apache.org/zookeeper/ 来获取,安装 Zoo ...

  3. zookeeper的安装和启动教程

    zookeeper的安装和启动 zookeeper安装包所在目录: 上传文件到虚拟机.现在本地新建一个目录setup,将zookeeper压缩包复制进去. ALT+P打开一个标签,操作如下put命令. ...

  4. zookeeper的安装和启动

    最近的手上一个项目要用到dubbo,顺便研究下zookeeper 1.下载 下载地址:http://www.apache.org/dyn/closer.cgi/zookeeper/,下载解压到D:\t ...

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

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

  6. ZooKeeper下载安装配置-单机版配置

    1,下载地址:http://apache.fayea.com/zookeeper/ 2,检查环境变量(需要确保配置了环境变量): java -version 3,安装配置: (1)解压 tar -zx ...

  7. Zookeeper WINDOWS 安装配置

    下载:zookeeper:http://mirrors.hust.edu.cn/apache/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz 解压zo ...

  8. Centos下zookeeper的安装配置

    下载安装包,下载地址 http://zookeeper.apache.org/releases.html,我下载的版本是zookeeper-3.4.9.tar.gz. # tar xvzf zooke ...

  9. windows下 zookeeper dubbo 安装+配置+demo 详细图文教程

    Java集群优化——dubbo+zookeeper构建 互联网的发展,网站应用的规模不断扩大,常规的垂直应用架构已无法应对,分布式服务架构以及流动计算架构势在必行,Dubbo是一个分布式服务框架,在这 ...

  10. yii2框架的安装&配置启动

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

随机推荐

  1. FileStream -- 复制文件

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...

  2. WCF技术剖析之一:通过一个ASP.NET程序模拟WCF基础架构

    原文:WCF技术剖析之一:通过一个ASP.NET程序模拟WCF基础架构 细算起来,已经有好几个月没有真正的写过文章了.近半年以来,一直忙于我的第一本WCF专著<WCF技术剖析>的写作,一直 ...

  3. 使用最新的log4cplus(1.1.1)隔离不同的 log 文件输出

    部分参考了博客. http://www.cppblog.com/tx7do/articles/11719.html 基于脚本配置来过滤log信息 除了通过程序实现对log环境的配置之外,log4cpl ...

  4. Ubuntu下SVN命令行递归加入文件夹文件(免去一个一个的加入 --force)

    因为在Linux下一直没有找到好的svn工具(类似于TortiseSVN的).当然eSVN这些也不错,但就是使用上认为还不是很习惯.终于还是选择了svn原始的命令行工具来进行版本号控制操作. 命令行的 ...

  5. FindChildControl与FindComponent

    前两天编码遇到了要使用FindChildControl方法获取指定名称的TSpeedButton按钮,结果折腾了半天就是没得结果(基础不扎实,呵呵),于是赶紧搜索了下,补习关于这两个方法的用法. TW ...

  6. Android中获取电池电量

    /** * * @author chrp * *显示当前电池电量 */ public class MainActivity extends Activity { private TextView tv ...

  7. Ubuntu_文件夹名字转化成英文

    打开终端命令行输入: export LANG=en_US xdg-user-dirs-gtk-update 之后重启,就看到中文的文件夹变成英文的了 想要换回中文的输入: export LANG=zh ...

  8. 【ASP.NET Web API教程】2.3.3 创建Admin控制器

    原文:[ASP.NET Web API教程]2.3.3 创建Admin控制器 注:本文是[ASP.NET Web API系列教程]的一部分,如果您是第一次看本博客文章,请先看前面的内容. Part 3 ...

  9. 基于visual Studio2013解决C语言竞赛题之1094纵横图

        题目 解决代码及点评 /************************************************************************/ /* ...

  10. 【deep learning学习笔记】注释yusugomori的LR代码 --- LogisticRegression.cpp

    模型实现代码,关键是train函数和predict函数,都很容易. #include <iostream> #include <string> #include <mat ...