简书原文地址:https://www.jianshu.com/p/88194fde9a07
或者关注我的公众号“进阶者euj”
前提是本机有jdk
一、单机安装
1、去官网下载zookeeper的安装包
https://zookeeper.apache.org/releases.html
下载bin包
![](https://upload-images.jianshu.io/upload_images/22777058-a3b93ca84826bd6a.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
2、
tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz
解压文件
![](https://upload-images.jianshu.io/upload_images/22777058-516659b51973dfee.png?imageMogr2/auto-orient/strip|imageView2/2/w/772/format/webp)
3、进入conf目录修改cfg文件
![](https://upload-images.jianshu.io/upload_images/22777058-a5e676edec796d6b.png?imageMogr2/auto-orient/strip|imageView2/2/w/664/format/webp)
mv zoo_sample.cfg zoo.cfg
![](https://upload-images.jianshu.io/upload_images/22777058-cae487b6da287766.png?imageMogr2/auto-orient/strip|imageView2/2/w/568/format/webp)
4、添加运行路径
vim /etc/profile
添加以下配置
export ZOOKEEPER=/root/zk/apache-zookeeper-3.6.1-bin
export PATH=$PATH:$ZOOKEEPER/bin
![](https://upload-images.jianshu.io/upload_images/22777058-7692f7142eea8be6.png?imageMogr2/auto-orient/strip|imageView2/2/w/758/format/webp)
刷新配置
source /etc/profile
检验下
echo $PATH
5、启动zookeeper
进入bin目录,执行
zkServer.sh start
![](https://upload-images.jianshu.io/upload_images/22777058-f818e4fe737c8ea2.png?imageMogr2/auto-orient/strip|imageView2/2/w/878/format/webp)
查看状态
zkServer.sh status
![](https://upload-images.jianshu.io/upload_images/22777058-6020c90a79967c36.png?imageMogr2/auto-orient/strip|imageView2/2/w/892/format/webp)
执行失败可以使用下列指令
./zkServer.sh start-foreground
6、使用客户端连接zookeeper
执行
sh zkCli.sh
![](https://upload-images.jianshu.io/upload_images/22777058-d4048209aea333a6.png?imageMogr2/auto-orient/strip|imageView2/2/w/396/format/webp)
执行
ls /
![](https://upload-images.jianshu.io/upload_images/22777058-b6e1f7e784fb6fea.png?imageMogr2/auto-orient/strip|imageView2/2/w/584/format/webp)
二、群集安装
1、在3台服务器分别安装zookeeper,(测试机子的ip是192.168.2.128,192.168.2.129,192.168.2.130)
2、
修改zoo.cfg配置文件,增加三列(三台服务器都要修改)
server.1=192.168.2.128:2888:3888
server.2=192.168.2.129:2888:3888
server.3=192.168.2.130:2888:3888
server.myid=ip:port1:port2
其中myid的范围是1-255
3、在zk的目录下新增myid文件
192.168.2.128服务器执行 echo "1">/tmp/zookeeper/myid
192.168.2.129服务器执行 echo "2">/tmp/zookeeper/myid
192.168.2.130服务器执行 echo "3">/tmp/zookeeper/myid
4、重启zookeeper
然后在其中一个服务器下修改数据,其他服务器会对应修改数据
三、java代码操作zk集群
public class CuratorDemo {
static String host = "192.168.2.128:2181,192.168.2.129:2181,192.168.2.130:2181";
public static void main(String[] args) throws Exception {
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(host,
5000, 5000,
new ExponentialBackoffRetry(1000,3));
curatorFramework.start();
curatorFramework.setData().forPath("/demo", "麦克雷3".getBytes());
}
}
github地址:https://github.com/hd-eujian/zookeeper.git
码云地址:https://gitee.com/guoeryyj/zookeeper.git
------------恢复内容开始------------
前提是本机有jdk
一、单机安装
1、去官网下载zookeeper的安装包
https://zookeeper.apache.org/releases.html
下载bin包
![](https://upload-images.jianshu.io/upload_images/22777058-a3b93ca84826bd6a.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
2、
tar -zxvf apache-zookeeper-3.6.1-bin.tar.gz
解压文件
![](https://upload-images.jianshu.io/upload_images/22777058-516659b51973dfee.png?imageMogr2/auto-orient/strip|imageView2/2/w/772/format/webp)
3、进入conf目录修改cfg文件
![](https://upload-images.jianshu.io/upload_images/22777058-a5e676edec796d6b.png?imageMogr2/auto-orient/strip|imageView2/2/w/664/format/webp)
mv zoo_sample.cfg zoo.cfg
![](https://upload-images.jianshu.io/upload_images/22777058-cae487b6da287766.png?imageMogr2/auto-orient/strip|imageView2/2/w/568/format/webp)
4、添加运行路径
vim /etc/profile
添加以下配置
export ZOOKEEPER=/root/zk/apache-zookeeper-3.6.1-bin
export PATH=$PATH:$ZOOKEEPER/bin
![](https://upload-images.jianshu.io/upload_images/22777058-7692f7142eea8be6.png?imageMogr2/auto-orient/strip|imageView2/2/w/758/format/webp)
刷新配置
source /etc/profile
检验下
echo $PATH
5、启动zookeeper
进入bin目录,执行
zkServer.sh start
![](https://upload-images.jianshu.io/upload_images/22777058-f818e4fe737c8ea2.png?imageMogr2/auto-orient/strip|imageView2/2/w/878/format/webp)
查看状态
zkServer.sh status
![](https://upload-images.jianshu.io/upload_images/22777058-6020c90a79967c36.png?imageMogr2/auto-orient/strip|imageView2/2/w/892/format/webp)
执行失败可以使用下列指令
./zkServer.sh start-foreground
6、使用客户端连接zookeeper
执行
sh zkCli.sh
![](https://upload-images.jianshu.io/upload_images/22777058-d4048209aea333a6.png?imageMogr2/auto-orient/strip|imageView2/2/w/396/format/webp)
执行
ls /
![](https://upload-images.jianshu.io/upload_images/22777058-b6e1f7e784fb6fea.png?imageMogr2/auto-orient/strip|imageView2/2/w/584/format/webp)
二、群集安装
1、在3台服务器分别安装zookeeper,(测试机子的ip是192.168.2.128,192.168.2.129,192.168.2.130)
2、
修改zoo.cfg配置文件,增加三列(三台服务器都要修改)
server.1=192.168.2.128:2888:3888
server.2=192.168.2.129:2888:3888
server.3=192.168.2.130:2888:3888
server.myid=ip:port1:port2
其中myid的范围是1-255
3、在zk的目录下新增myid文件
192.168.2.128服务器执行 echo "1">/tmp/zookeeper/myid
192.168.2.129服务器执行 echo "2">/tmp/zookeeper/myid
192.168.2.130服务器执行 echo "3">/tmp/zookeeper/myid
4、重启zookeeper
然后在其中一个服务器下修改数据,其他服务器会对应修改数据
三、java代码操作zk集群
public class CuratorDemo {
static String host = "192.168.2.128:2181,192.168.2.129:2181,192.168.2.130:2181";
public static void main(String[] args) throws Exception {
CuratorFramework curatorFramework = CuratorFrameworkFactory.newClient(host,
5000, 5000,
new ExponentialBackoffRetry(1000,3));
curatorFramework.start();
curatorFramework.setData().forPath("/demo", "麦克雷3".getBytes());
}
}
github地址:https://github.com/hd-eujian/zookeeper.git
码云地址:https://gitee.com/guoeryyj/zookeeper.git
------------恢复内容结束------------