Zookeeper单机安装

avatar 2020年2月27日18:19:21 评论 1,483 次浏览

ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,是Google的Chubby一个开源的实现,是Hadoop和Hbase的重要组件。它是一个为分布式应用提供一致性服务的软件,提供的功能包括:配置维护、域名服务、分布式同步、组服务等。
ZooKeeper的目标就是封装好复杂易出错的关键服务,将简单易用的接口和性能高效、功能稳定的系统提供给用户。
ZooKeeper包含一个简单的原语集,提供Java和C的接口。

安装环境:

1、CentOS 7.6
2、Java 1.8

zookeeper分为单机,集群和伪集群部署方式,下面是单机的服务方式。

下载zookeeper包

下载并解压zookeeper,并迁移到指定的路径下。

[root@wulaoer.org ~]# wget https://downloads.apache.org/zookeeper/stable/apache-zookeeper-3.5.7-bin.tar.gz
[root@wulaoer.org ~]# tar -zxf apache-zookeeper-3.5.7-bin.tar.gz 
[root@wulaoer.org ~]# mv apache-zookeeper-3.5.7 /usr/local/zookeeper
[root@wulaoer.org ~]# cd /usr/local/zookeeper/

下载的是apache-zookeeper-3.5.7.tar.gz,但是安装之后启动报错

[root@wulaoer.org logs]# tail -f zookeeper-root-server-localhost.localdomain.out 
Error: Could not find or load main class org.apache.zookeeper.server.quorum.QuorumPeerMain

这个是源码包安装,如果使用apache-zookeeper-3.5.7-bin.tar.gz就没有报错,下面继续安装。

创建日志和数据存放路径

[root@wulaoer.org zookeeper]# mkdir {log,tmp}
[root@wulaoer.org zookeeper]# cp conf/zoo_sample.cfg conf/zoo.cfg 

修改zookeeper日志和数据存放路径。

[root@wulaoer.org zookeeper]# vim conf/zoo.cfg 
# The number of milliseconds of each tick
# tickTime:CS通信心跳数
# Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳。tickTime以毫秒为单位。
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
#initLimit:LF初始通信时限
# 集群中的follower服务器(F)与leader服务器(L)之间初始连接时能容忍的最多心跳数(tickTime的数量)。
initLimit=10
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
# syncLimit:LF同步通信时限
# 集群中的follower服务器与leader服务器之间请求和应答之间能容忍的最多心跳数(tickTime的数量)。
syncLimit=5
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
# dataDir:数据文件目录
# Zookeeper保存数据的目录,默认情况下,Zookeeper将写数据的日志文件也保存在这个目录里。
dataDir=/usr/local/zookeeper/tmp    #数据存放路径
# dataLogDir:日志文件目录
# Zookeeper保存日志文件的目录。
dataLogDir=/usr/local/zookeeper/log	#日志存放路径
# the port at which the clients will connect
# clientPort:客户端连接端口
# 客户端连接 Zookeeper 服务器的端口,Zookeeper 会监听这个端口,接受客户端的访问请求。
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
#
# The number of snapshots to retain in dataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable auto purge feature
#autopurge.purgeInterval=1

############################以下内容配置文件中没有,配置集群时需要自己手动添加####################################
# 服务器名称与地址:集群信息(服务器编号,服务器地址,LF通信端口,选举端口)
# 这个配置项的书写格式比较特殊,规则如下: 
# server.N=YYY:A:B  
#其中N表示服务器编号,YYY表示服务器的IP地址,A为LF通信端口,表示该服务器与集群中的leader交换的信息的端口。B为选举端口,表示选举新leader时服务器间相互通信的端口(当leader挂掉时,其余服务器会相互通信,选择出新的leader)。一般来说,集群中每个服务器的A端口都是一样,每个服务器的B端口也是一样。但是当所采用的为伪集群时,IP地址都一样,只能时A端口和B端口不一样。

启动服务,查看一下服务的状态

[root@wulaoer.org zookeeper]# ./bin/zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[root@wulaoer.org zookeeper]# ./bin/zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: wulaoer.org.
Mode: standalone

zookeeper启动成功,下面进入zookeeper,然后操作一下

[root@wulaoer.org zookeeper]# ./bin/zkCli.sh -server 127.0.0.1:2181
Connecting to 127.0.0.1:2181
2020-02-24 14:41:20,968 [myid:] - INFO  [main:Environment@109] - Client environment:zookeeper.version=3.5.7-f0fdd52973d373ffd9c86b81d99842dc2c7f660e, built on 02/10/2020 11:30 GMT
2020-02-24 14:41:20,970 [myid:] - INFO  [main:Environment@109] - Client environment:host.name=wulaoer.org
2020-02-24 14:41:20,970 [myid:] - INFO  [main:Environment@109] - Client environment:java.version=1.8.0_231
2020-02-24 14:41:20,971 [myid:] - INFO  [main:Environment@109] - Client environment:java.vendor=Oracle Corporation
2020-02-24 14:41:20,971 [myid:] - INFO  [main:Environment@109] - Client environment:java.home=/usr/local/jdk/jre
2020-02-24 14:41:20,971 [myid:] - INFO  [main:Environment@109] - Client environment:java.class.path=/usr/local/zookeeper/bin/../zookeeper-server/target/classes:/usr/local/zookeeper/bin/../build/classes:/usr/local/zookeeper/bin/../zookeeper-server/target/lib/*.jar:/usr/local/zookeeper/bin/../build/lib/*.jar:/usr/local/zookeeper/bin/../lib/zookeeper-jute-3.5.7.jar:/usr/local/zookeeper/bin/../lib/zookeeper-3.5.7.jar:/usr/local/zookeeper/bin/../lib/slf4j-log4j12-1.7.25.jar:/usr/local/zookeeper/bin/../lib/slf4j-api-1.7.25.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-unix-common-4.1.45.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-native-epoll-4.1.45.Final.jar:/usr/local/zookeeper/bin/../lib/netty-transport-4.1.45.Final.jar:/usr/local/zookeeper/bin/../lib/netty-resolver-4.1.45.Final.jar:/usr/local/zookeeper/bin/../lib/netty-handler-4.1.45.Final.jar:/usr/local/zookeeper/bin/../lib/netty-common-4.1.45.Final.jar:/usr/local/zookeeper/bin/../lib/netty-codec-4.1.45.Final.jar:/usr/local/zookeeper/bin/../lib/netty-buffer-4.1.45.Final.jar:/usr/local/zookeeper/bin/../lib/log4j-1.2.17.jar:/usr/local/zookeeper/bin/../lib/json-simple-1.1.1.jar:/usr/local/zookeeper/bin/../lib/jline-2.11.jar:/usr/local/zookeeper/bin/../lib/jetty-util-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-servlet-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-server-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-security-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-io-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/jetty-http-9.4.24.v20191120.jar:/usr/local/zookeeper/bin/../lib/javax.servlet-api-3.1.0.jar:/usr/local/zookeeper/bin/../lib/jackson-databind-2.9.10.2.jar:/usr/local/zookeeper/bin/../lib/jackson-core-2.9.10.jar:/usr/local/zookeeper/bin/../lib/jackson-annotations-2.9.10.jar:/usr/local/zookeeper/bin/../lib/commons-cli-1.2.jar:/usr/local/zookeeper/bin/../lib/audience-annotations-0.5.0.jar:/usr/local/zookeeper/bin/../zookeeper-*.jar:/usr/local/zookeeper/bin/../zookeeper-server/src/main/resources/lib/*.jar:/usr/local/zookeeper/bin/../conf:.:/usr/local/jdk/lib/dt.jar:/usr/local/jdk/lib/tools.jar
2020-02-24 14:41:20,971 [myid:] - INFO  [main:Environment@109] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2020-02-24 14:41:20,972 [myid:] - INFO  [main:Environment@109] - Client environment:java.io.tmpdir=/tmp
2020-02-24 14:41:20,972 [myid:] - INFO  [main:Environment@109] - Client environment:java.compiler=<NA>
2020-02-24 14:41:20,972 [myid:] - INFO  [main:Environment@109] - Client environment:os.name=Linux
2020-02-24 14:41:20,972 [myid:] - INFO  [main:Environment@109] - Client environment:os.arch=amd64
2020-02-24 14:41:20,972 [myid:] - INFO  [main:Environment@109] - Client environment:os.version=3.10.0-1062.el7.x86_64
2020-02-24 14:41:20,972 [myid:] - INFO  [main:Environment@109] - Client environment:user.name=root
2020-02-24 14:41:20,972 [myid:] - INFO  [main:Environment@109] - Client environment:user.home=/root
2020-02-24 14:41:20,973 [myid:] - INFO  [main:Environment@109] - Client environment:user.dir=/usr/local/zookeeper
2020-02-24 14:41:20,974 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.free=13MB
2020-02-24 14:41:20,974 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.max=247MB
2020-02-24 14:41:20,974 [myid:] - INFO  [main:Environment@109] - Client environment:os.memory.total=15MB
2020-02-24 14:41:20,979 [myid:] - INFO  [main:ZooKeeper@868] - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@45fe3ee3
2020-02-24 14:41:20,982 [myid:] - INFO  [main:X509Util@79] - Setting -D jdk.tls.rejectClientInitiatedRenegotiation=true to disable client-initiated TLS renegotiation
2020-02-24 14:41:20,988 [myid:] - INFO  [main:ClientCnxnSocket@237] - jute.maxbuffer value is 4194304 Bytes
2020-02-24 14:41:20,996 [myid:] - INFO  [main:ClientCnxn@1653] - zookeeper.request.timeout value is 0. feature enabled=
Welcome to ZooKeeper!
JLine support is enabled
2020-02-24 14:41:21,021 [myid:127.0.0.1:2181] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1112] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
2020-02-24 14:41:21,100 [myid:127.0.0.1:2181] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@959] - Socket connection established, initiating session, client: /127.0.0.1:45982, server: localhost/127.0.0.1:2181
[zk: 127.0.0.1:2181(CONNECTING) 0] 2020-02-24 14:41:21,254 [myid:127.0.0.1:2181] - INFO  [main-SendThread(127.0.0.1:2181):ClientCnxn$SendThread@1394] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x100025c90260000, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
ls /
[zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 1] create /zk myData   #创建一个znode节点以及关联的字符串
Created /zk
[zk: 127.0.0.1:2181(CONNECTED) 3] ls /			#查看zookeeper中包含的内容
[zk, zookeeper]
[zk: 127.0.0.1:2181(CONNECTED) 4] get /zk		#获取znode节点的'zk'
myData
[zk: 127.0.0.1:2181(CONNECTED) 5] delete /zk	#删除znode节点的'zk'
[zk: 127.0.0.1:2181(CONNECTED) 6] quit			#退出客户端

这是zookeeper的单机安装以及基本的操作,下一章zookeeper的伪集群部署方式。

avatar

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: