Zookeeper 简明教程

Zookeeper - Installation

在安装 ZooKeeper 之前,请确保您的系统在下列任何操作系统上运行:

Before installing ZooKeeper, make sure your system is running on any of the following operating systems −

  1. Any of Linux OS − Supports development and deployment. It is preferred for demo applications.

  2. Windows OS − Supports only development.

  3. Mac OS − Supports only development.

ZooKeeper 服务器在 Java 中创建,并在 JVM 上运行。您需要使用 JDK 6 或更高版本。

ZooKeeper server is created in Java and it runs on JVM. You need to use JDK 6 or greater.

现在,按照以下步骤在您的机器上安装 ZooKeeper 框架。

Now, follow the steps given below to install ZooKeeper framework on your machine.

Step 1: Verifying Java Installation

我们相信您已经在系统上安装了 Java 环境。只需使用以下命令进行验证。

We believe you already have a Java environment installed on your system. Just verify it using the following command.

$ java -version

如果您的机器上已安装 Java,那么您可以看到已安装 Java 的版本。否则,请按照以下简单步骤安装最新版本的 Java。

If you have Java installed on your machine, then you could see the version of installed Java. Otherwise, follow the simple steps given below to install the latest version of Java.

Step 1.1: Download JDK

访问以下链接并下载 Java 的最新版本。 Java

Download the latest version of JDK by visiting the following link and download the latest version. Java

最新版本(在编写本教程时)是 JDK 8u 60,文件为“jdk-8u60-linuxx64.tar.gz”。请下载该文件到您的机器上。

The latest version (while writing this tutorial) is JDK 8u 60 and the file is “jdk-8u60-linuxx64.tar.gz”. Please download the file on your machine.

Step 1.2: Extract the files

通常,文件会下载到 downloads 文件夹。验证此文件夹并使用以下命令提取tar设置。

Generally, files are downloaded to the downloads folder. Verify it and extract the tar setup using the following commands.

$ cd /go/to/download/path
$ tar -zxf jdk-8u60-linux-x64.gz

Step 1.3: Move to opt directory

为了让所有用户都可以使用 Java,将提取的 Java 内容移动到“/usr/local/java”文件夹中。

To make Java available to all users, move the extracted java content to “/usr/local/java” folder.

$ su
password: (type password of root user)
$ mkdir /opt/jdk
$ mv jdk-1.8.0_60 /opt/jdk/

Step 1.4: Set path

若要设置 path 及 JAVA_HOME 变量,请将以下命令添加到 ~/.bashrc 文件中。

To set path and JAVA_HOME variables, add the following commands to ~/.bashrc file.

export JAVA_HOME = /usr/jdk/jdk-1.8.0_60
export PATH=$PATH:$JAVA_HOME/bin

现在,将所有更改应用到当前正在运行的系统中。

Now, apply all the changes into the current running system.

$ source ~/.bashrc

Step 1.5: Java alternatives

使用以下命令更改 Java 备用项。

Use the following command to change Java alternatives.

update-alternatives --install /usr/bin/java java /opt/jdk/jdk1.8.0_60/bin/java 100

Step 1.6

使用验证命令 (java -version) (第1步中已说明)验证Java安装。

Verify the Java installation using the verification command (java -version) explained in Step 1.

Step 2: ZooKeeper Framework Installation

Step 2.1: Download ZooKeeper

若要在你的机器上安装ZooKeeper框架,请访问以下链接并下载ZooKeeper的最新版本。 http://zookeeper.apache.org/releases.html

To install ZooKeeper framework on your machine, visit the following link and download the latest version of ZooKeeper. http://zookeeper.apache.org/releases.html

到目前为止,ZooKeeper 的最新版本是 3.4.6 (ZooKeeper-3.4.6.tar.gz)。

As of now, the latest version of ZooKeeper is 3.4.6 (ZooKeeper-3.4.6.tar.gz).

Step 2.2: Extract the tar file

使用以下命令提取 tar 文件 −

Extract the tar file using the following commands −

$ cd opt/
$ tar -zxf zookeeper-3.4.6.tar.gz
$ cd zookeeper-3.4.6
$ mkdir data

Step 2.3: Create configuration file

使用命令 vi conf/zoo.cfg 打开名为 conf/zoo.cfg 的配置文件,并将所有以下参数设置为起始点。

Open the configuration file named conf/zoo.cfg using the command vi conf/zoo.cfg and all the following parameters to set as starting point.

$ vi conf/zoo.cfg

tickTime = 2000
dataDir = /path/to/zookeeper/data
clientPort = 2181
initLimit = 5
syncLimit = 2

成功保存配置文件后,再次返回终端。你现在可以启动zookeeper服务器。

Once the configuration file has been saved successfully, return to the terminal again. You can now start the zookeeper server.

Step 2.4: Start ZooKeeper server

执行以下命令−

Execute the following command −

$ bin/zkServer.sh start

执行该命令后,你会收到如下的响应 −

After executing this command, you will get a response as follows −

$ JMX enabled by default
$ Using config: /Users/../zookeeper-3.4.6/bin/../conf/zoo.cfg
$ Starting zookeeper ... STARTED

Step 2.5: Start CLI

键入以下命令−

Type the following command −

$ bin/zkCli.sh

键入以上命令后,你将连接到ZooKeeper服务器,并且你应该得到以下响应。

After typing the above command, you will be connected to the ZooKeeper server and you should get the following response.

Connecting to localhost:2181
................
................
................
Welcome to ZooKeeper!
................
................
WATCHER::
WatchedEvent state:SyncConnected type: None path:null
[zk: localhost:2181(CONNECTED) 0]

Stop ZooKeeper Server

连接服务器并执行所有操作后,你可以使用以下命令停止zookeeper服务器。

After connecting the server and performing all the operations, you can stop the zookeeper server by using the following command.

$ bin/zkServer.sh stop