上次jdk很艰难的安装完成,想了一下还是试着安装zookeeper,这个项目中也用到过,也算是给其他安装铺一下道路。

zk相对来说还是比较好安装的。首先也是准备好文件:

链接:https://pan.baidu.com/s/13CDjwIqaeHdsomUcnBNsOA      提取码:lpgh 
当然也可以去相应的网站下载,这里就自行搜索吧。

接下来通过软件工具把此文件放到linux系统中,我用的是红帽系统,文件放在usr下

cd /usr
$ cd local
$ ls
bin  games    lib    libexec  share  zookeeper-3.4.13
etc  include  lib64  sbin     src    zookeeper-3.4.13.tar.gz

然后通过命令解压到当前文件夹:

tar -zxvf /usr/local/zookeeper-3.4.13.tar.gz

解压后,对应的文件夹中会多出zookeeper-3.4.13文件夹,然后进入此文件夹

bin         docs             NOTICE.txt            zookeeper-3.4.13.jar
build.xml   ivysettings.xml  README.md             zookeeper-3.4.13.jar.asc
conf        ivy.xml          README_packaging.txt  zookeeper-3.4.13.jar.md5
contrib     lib              recipes               zookeeper-3.4.13.jar.sha1
dist-maven  LICENSE.txt      src

找到conf文件夹下的zoo_sample.cfg,然后拷贝一份并重命名为zoo.cfg

cp zoo_sample.cfg zoo.cfg

拷贝完成之后,编辑zoo.cfg文件

//用此命令进入编辑文件
vi 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=/tmp/zookeeper-3.4.12/data
dataLogDir=/tmp/zookeeper-3.4.12/log
# the port at which the clients will connect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle more clients
#maxClientCnxns=60
#
# 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
"zoo.cfg" [readonly] 29L, 971C                                1,1           Top

点击键盘上字母i进入编辑,修改新增

dataDir=/tmp/zookeeper-3.4.12/data
dataLogDir=/tmp/zookeeper-3.4.12/log  //如果没有,可以新增dataLogDir

注意:如果dataDir和dataLogDir对应的目录不存在,可以先创建出来。

然后Esc退出,输入:wq 保存。

配置一下环境变量(和配置jdk类似):

[root@localhost ~]#vi /etc/profile -- 编辑此配置文件
在文件末尾添加此段内容:
-- 开始
export ZOOKEEPER_INSTALL=/usr/local/zookeeper-3.4.13
export PATH=$PATH:$ZOOKEEPER_INSTALL/bin
-- 结束

 至此安装完成,可以开启关闭服务

开启服务,关闭服务, 查看
[root@localhost bin]# cd /usr/local/zookeeper.3.4.13/bin/
[root@localhost bin]# ./zkServer.sh stop -- 停止服务
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.13/bin/../conf/zoo.cfg
Stopping zookeeper ... STOPPED
 
[root@localhost bin]# ./zkServer.sh start -- 开启服务
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.13/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
 
[root@localhost bin]# ./zkServer.sh status -- 查看状态
JMX enabled by default
Using config: /usr/local/zookeeper-3.4.13/bin/../conf/zoo.cfg
Mode: standalone
[root@localhost bin]#

还可以查看客户端:

[root@localhost bin]# ./zkCli.sh -- 客户端启动, 可以查看启动状态和日志
Connecting to localhost:2181
2017-03-08 06:15:26,918 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 06/25/2019 09:09 GMT
2017-03-08 06:15:26,923 [myid:] - INFO [main:Environment@100] - Client environment:host.name=localhost
2017-03-08 06:15:26,923 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.8.0_121
2017-03-08 06:15:26,925 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2017-03-08 06:15:26,926 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/local/java/jdk1.8.0_211/jre
2017-03-08 06:15:26,926 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper/zookeeper-3.4.6/bin/../build/classes:/usr/local/zookeeper/zookeeper-3.4.6/bin/../build/lib/*.jar:/usr/local/zookeeper-3.4.13/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper-3.4.13/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper-3.4.13/bin/../lib/netty-3.7.0.Final.jar:/usr/local/zookeeper-3.4.13/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper-3.4.13/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper-3.4.13/bin/../zookeeper-3.4.6.jar:/usr/local/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/usr/local/zookeeper-3.4.6/bin/../conf:.:/usr/local/java/jdk1.8.0_211/lib/dt.jar:/usr/local/java/jdk1.8.0_211/lib/tools.jar:/usr/local/java/jdk1.8.0_211/jre/lib:.:/usr/local/java/jdk1.8.0_211/lib/dt.jar:/usr/local/java/jdk1.8.0_211/lib/tools.jar:/usr/local/java/jdk1.8.0_211/jre/lib:.:/usr/local/java/jdk1.8.0_211/lib/dt.jar:/usr/local/java/jdk1.8.0_211/lib/tools.jar:/usr/local/java/jdk1.8.0_211/jre/lib:
2019-06-27 06:15:26,926 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2019-06-27 06:15:26,926 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2019-06-27 06:15:26,926 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2019-06-27 06:15:26,930 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2019-06-27 06:15:26,930 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2019-06-27 06:15:26,930 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.32-431.el6.x86_64
2019-06-27 06:15:26,931 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2019-06-27 06:15:26,931 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2019-06-27 06:15:26,931 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper/zookeeper-3.4.6/bin
2019-06-27 06:15:26,940 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@277050dc
Welcome to ZooKeeper!
2019-06-27 06:15:27,039 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@975] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2019-06-26 06:15:27,290 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@852] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2181, initiating session
[zk: localhost:2181(CONNECTING) 0] 2019-06-26 06:15:27,445 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2181, sessionid = 0x15aae45f1330000, negotiated timeout = 30000
 
WATCHER::
 
WatchedEvent state:SyncConnected type:None path:null
[root@localhost bin]# ps -ef | grep zookeeper -- 查看zookeeper的服务进程

这是单机状态下的zookeeper安装。

如果是要做成集群的话,需要把zookeeper复制出来几份(最好是单数),可以重命名

zookeeper-3.4.13-1
zookeeper-3.4.13-2
zookeeper-3.4.13-3

配置每一个Zookeeper 的dataDir和dataLogDir(zoo.cfg) ,clientPort端口号 分别为2181  2182  2183;在每一个文件中创建data目录,并创建一个 myid 文件,内容分别是1、2、3 。这个文件就是记录每个服务器的ID。

在每一个zookeeper 的 zoo.cfg配置客户端访问端口(clientPort)和集群服务器IP列表

server.1=IP:2881:3881
server.2=IP:2882:3882
server.3=IP:2883:3883

注:如果是多台服务器,则IP不同,端口号可以相同,myid中存储的数字分别为1,2,3等

进入各自的zk的bin文件夹下,分别运行就可以启动完成。

注意:以下是在网上找的一份关于server.1=ip:2881:3881的文字解释:

同一IP上搭建多个节点的集群时,必须要注意端口问题,端口必须不一致才行;

创建多个节点集群时,在dataDir目录下必须创建myid文件,myid文件用于zookeeper验证server序号等,myid文件只有一行,并且为当前server的序号,例如server.1的myid就是1,server2的myid就是2等。

server.A=B:C:D;其中 A 是一个数字,表示这个是第几号服务器;B 是这个服务器的 ip 地址;C 表示的是这个服务器与集群中的 Leader 服务器交换信息的端口;D 表示的是万一集群中的 Leader 服务器挂了,需要一个端口来重新进行选举,选出一个新的 Leader,而这个端口就是用来执行选举时服务器相互通信的端口。如果是伪集群的配置方式,由于 B 都是一样,所以不同的  Zookeeper 实例通信端口号不能一样,所以要给它们分配不同的端口号。

 

启动过程:启动顺序为server1、server2、server3。在启动server1,server2时zk会报错,当所有节点全部启动时错误会消失。

多IP多节点:

将zookeeper拷贝到每个节点一份。

多IP多节点与单IP多节点搭建过程基本一致,上述过程不再重复描述,仅重点说一个地方:server的IP地址、端口为真实即可。

注意:zk的部署个数最好为基数,ZK集群的机制是只要超过半数的节点OK,集群就能正常提供服务。只有ZK节点挂得太多,只剩一半或不到一半节点能工作,集群才失效。

 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐