ZooKeeper服务器是用Java创建的,它在JVM上运行。你需要使用JDK 6或更高版本。
现在,按照以下步骤在你的机器上安装ZooKeeper框架。
相信你已经在系统上安装了Java环境。现在只需使用以下命令验证它。
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px; position: relative; width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background-color: rgb(32, 30, 47); border-radius: 3px; font-size: 14px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; overflow-x: auto; border: 1px solid rgb(32, 30, 47); color: rgb(248, 248, 242); overflow-wrap: break-word !important; white-space: pre-wrap !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">$ java -version
</pre>
如果你在机器上安装了Java,那么可以看到已安装的Java的版本。否则,请按照以下简单步骤安装最新版本的Java。
通过访问链接下载最新版本的JDK,并下载最新版本的Java。
最新版本(在编写本教程时)是JDK 8u 60,文件是“jdk-8u60-linuxx64.tar.gz"。请在你的机器上下载该文件。
通常,文件会下载到download文件夹中。验证并使用以下命令提取tar设置。
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px; position: relative; width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background-color: rgb(32, 30, 47); border-radius: 3px; font-size: 14px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; overflow-x: auto; border: 1px solid rgb(32, 30, 47); color: rgb(248, 248, 242); overflow-wrap: break-word !important; white-space: pre-wrap !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
tar -zxf jdk-8u60-linux-x64.gz
</pre>
要使Java对所有用户可用,请将提取的Java内容移动到“/usr/local/java"文件夹。
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px; position: relative; width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background-color: rgb(32, 30, 47); border-radius: 3px; font-size: 14px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; overflow-x: auto; border: 1px solid rgb(32, 30, 47); color: rgb(248, 248, 242); overflow-wrap: break-word !important; white-space: pre-wrap !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
mkdir /opt/jdk
$ mv jdk-1.8.0_60 /opt/jdk/
</pre>
要设置路径和JAVA_HOME变量,请将以下命令添加到〜/.bashrc文件中。
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px; position: relative; width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background-color: rgb(32, 30, 47); border-radius: 3px; font-size: 14px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; overflow-x: auto; border: 1px solid rgb(32, 30, 47); color: rgb(248, 248, 242); overflow-wrap: break-word !important; white-space: pre-wrap !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">export JAVA_HOME = /usr/jdk/jdk-1.8.0_60
export PATH=
JAVA_HOME/bin
</pre>
现在,将所有更改应用到当前运行的系统中。
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px; position: relative; width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background-color: rgb(32, 30, 47); border-radius: 3px; font-size: 14px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; overflow-x: auto; border: 1px solid rgb(32, 30, 47); color: rgb(248, 248, 242); overflow-wrap: break-word !important; white-space: pre-wrap !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">$ source ~/.bashrc
</pre>
使用以下命令更改Java替代项。
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px; position: relative; width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background-color: rgb(32, 30, 47); border-radius: 3px; font-size: 14px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; overflow-x: auto; border: 1px solid rgb(32, 30, 47); color: rgb(248, 248, 242); overflow-wrap: break-word !important; white-space: pre-wrap !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">update-alternatives --install /usr/bin/java java /opt/jdk/jdk1.8.0_60/bin/java 100
</pre>
使用步骤1中说明的验证命令(java -version)验证Java安装。
要在你的计算机上安装ZooKeeper框架,请访问以下链接并下载最新版本的ZooKeeper。http://zookeeper.apache.org/releases.html
到目前为止,最新版本的ZooKeeper是3.4.6(ZooKeeper-3.4.6.tar.gz)。
使用以下命令提取tar文件
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px; position: relative; width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background-color: rgb(32, 30, 47); border-radius: 3px; font-size: 14px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; overflow-x: auto; border: 1px solid rgb(32, 30, 47); color: rgb(248, 248, 242); overflow-wrap: break-word !important; white-space: pre-wrap !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">tar -zxf zookeeper-3.4.6.tar.gz
mkdir data
</pre>
使用命令** vi conf/zoo.cfg 和所有以下参数设置为起点,打开名为 conf/zoo.cfg **的配置文件。
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px; position: relative; width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background-color: rgb(32, 30, 47); border-radius: 3px; font-size: 14px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; overflow-x: auto; border: 1px solid rgb(32, 30, 47); color: rgb(248, 248, 242); overflow-wrap: break-word !important; white-space: pre-wrap !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">$ vi conf/zoo.cfg
tickTime = 2000
dataDir = /path/to/zookeeper/data
clientPort = 2181
initLimit = 5
syncLimit = 2
</pre>
一旦成功保存配置文件,再次返回终端。你现在可以启动zookeeper服务器。
执行以下命令
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px; position: relative; width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background-color: rgb(32, 30, 47); border-radius: 3px; font-size: 14px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; overflow-x: auto; border: 1px solid rgb(32, 30, 47); color: rgb(248, 248, 242); overflow-wrap: break-word !important; white-space: pre-wrap !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">$ bin/zkServer.sh start
</pre>
执行此命令后,你将收到以下响应
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px; position: relative; width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background-color: rgb(32, 30, 47); border-radius: 3px; font-size: 14px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; overflow-x: auto; border: 1px solid rgb(32, 30, 47); color: rgb(248, 248, 242); overflow-wrap: break-word !important; white-space: pre-wrap !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">
Using config: /Users/../zookeeper-3.4.6/bin/../conf/zoo.cfg
$ Starting zookeeper ... STARTED
</pre>
键入以下命令
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px; position: relative; width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background-color: rgb(32, 30, 47); border-radius: 3px; font-size: 14px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; overflow-x: auto; border: 1px solid rgb(32, 30, 47); color: rgb(248, 248, 242); overflow-wrap: break-word !important; white-space: pre-wrap !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">$ bin/zkCli.sh
</pre>
键入上述命令后,将连接到ZooKeeper服务器,你应该得到以下响应。
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px; position: relative; width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background-color: rgb(32, 30, 47); border-radius: 3px; font-size: 14px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; overflow-x: auto; border: 1px solid rgb(32, 30, 47); color: rgb(248, 248, 242); overflow-wrap: break-word !important; white-space: pre-wrap !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">Connecting to localhost:2181
................
................
................
Welcome to ZooKeeper!
................
................
WATCHER::
WatchedEvent state:SyncConnected type: None path:null
[zk: localhost:2181(CONNECTED) 0]
</pre>
连接服务器并执行所有操作后,可以使用以下命令停止zookeeper服务器。
<pre class="result notranslate" style="margin: 15px 0px; padding: 10px; position: relative; width: 700px; box-sizing: border-box; display: block; line-height: 1.7; background-color: rgb(32, 30, 47); border-radius: 3px; font-size: 14px; font-family: Consolas, "Liberation Mono", Menlo, Courier, monospace; overflow-x: auto; border: 1px solid rgb(32, 30, 47); color: rgb(248, 248, 242); overflow-wrap: break-word !important; white-space: pre-wrap !important; font-style: normal; font-variant-ligatures: normal; font-variant-caps: normal; font-weight: 400; letter-spacing: normal; orphans: 2; text-align: start; text-indent: 0px; text-transform: none; widows: 2; word-spacing: 0px; -webkit-text-stroke-width: 0px; text-decoration-style: initial; text-decoration-color: initial;">$ bin/zkServer.sh stop</pre>