linux搭建zookeeper集群实战教程
解决方法:
安装zookeeper前提是要配置好jdk环境
1.下载zookeeper:http://www.yayihouse.com/yayishuwu/chapter/2912
2.将下载的apache-zookeeper-3.6.2-bin.tar.gz压缩包放在/usr/local/java下,解压:tar -zxvf /usr/local/java/apache-zookeeper-3.6.2-bin.tar.gz
3.将解压后生成的目录改成apache-zookeeper-3.6.2-bin-1,然后复制里面的zoo_sample.cfg并命名zoo.cfg,zoo.cfg内容:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper/zk1/data
dataLogDir=/tmp/zookeeper/zk1/logs
clientPort=2181
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389
补充:在/tmp/zookeeper下创建/tmp/zookeeper/zk1/data
和/tmp/zookeeper/zk1/logs目录,并在data目录下创建名字为myid的文件,内容为数字:1
4.配置第二台服务,复制刚刚修改好的apache-zookeeper-3.6.2-bin-1目录并命名apache-zookeeper-3.6.2-bin-2,修改里面zoo.cfg文件,zoo.cfg内容:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper/zk2/data
dataLogDir=/tmp/zookeeper/zk2/logs
clientPort=2182
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389
补充:在/tmp/zookeeper下创建/tmp/zookeeper/zk2/data
和/tmp/zookeeper/zk2/logs目录,并在data目录下创建名字为myid的文件,内容为数字:2
5.配置第三台服务,复制刚刚修改好的apache-zookeeper-3.6.2-bin-1目录并命名apache-zookeeper-3.6.2-bin-3,修改里面zoo.cfg文件,zoo.cfg内容:
tickTime=2000
initLimit=10
syncLimit=5
dataDir=/tmp/zookeeper/zk2/data
dataLogDir=/tmp/zookeeper/zk2/logs
clientPort=2183
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389
补充:在/tmp/zookeeper下创建/tmp/zookeeper/zk3/data
和/tmp/zookeeper/zk3/logs目录,并在data目录下创建名字为myid的文件,内容为数字:3
6.启动三台zookeeper服务,要关闭服务的话就将start改为stop,执行以下命令:
/usr/local/java/apache-zookeeper-3.6.2-bin-1/bin/zkServer.sh start
/usr/local/java/apache-zookeeper-3.6.2-bin-2/bin/zkServer.sh start
/usr/local/java/apache-zookeeper-3.6.2-bin-3/bin/zkServer.sh start
结果如下说明启动成功:
[root@xxx ~]# /usr/local/java/apache-zookeeper-3.6.2-bin-2/bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/java/apache-zookeeper-3.6.2-bin-2/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
7.查看各个zookeeper服务的角色,执行/usr/local/java/apache-zookeeper-3.6.2-bin-3/bin/zkServer.sh status,Mode: leader表示是领导者:
[root@s ~]# /usr/local/java/apache-zookeeper-3.6.2-bin-3/bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/java/apache-zookeeper-3.6.2-bin-3/bin/../conf/zoo.cfg
Client port found: 2183. Client address: localhost. Client SSL: false.
Mode: leader
8.客户端连接任意一台服务,假如我想连接第一台就执行:/usr/local/java/apache-zookeeper-3.6.2-bin-1/bin/zkCli.sh -server 127.0.0.1:2181
9.连接成功后,创建一个测试节点试一试集群效果,节点名是/myZnode,值是my znode:
create /myZnode "my znode"
10.再另外打开一个窗口重新连接另一台服务:/usr/local/java/apache-zookeeper-3.6.2-bin-2/bin/zkCli.sh -server 127.0.0.1:2182
然后执行get /myZnode 也能获取得到第一个设置的值说明集群成功。
结果:
[zk: 127.0.0.1:2182(CONNECTED) 0] get /myZnode
my znode