Linux—zookeeper

1、概念简介

1.1、选举机制

2、环境部署

下载链接:https://zookeeper.apache.org/releases.html
下载版本:apache-zookeeper-3.7.1-bin.tar.gz

此处下载带 bin 的包,即经过编译的安装包;如果下载源码包,要重新编译打包后才可以使用;否则启动服务会报错 Error: Could not find or load main class org.apache.zookeeper.server.quorum.QuorumPeerMain

2.1、单机部署

1)下载解压
解压和安装路径:/software/Zookeeper

2)配置调整

zk 默认没有 zoo.cfg 文件,需要复制 conf 下的样例配置文件;

[gdsuser@hz-yyserv bin]$ pwd
/software/Zookeeper/bin
[gdsuser@hz-yyserv bin]$ cp ../conf/zoo_sample.cfg ../conf/zoo.cfg
[gdsuser@hz-yyserv bin]$ 
[gdsuser@hz-yyserv bin]$ ll ../conf
total 16
-rw-r--r-- 1 gdsuser gdsgrp  535 May  7 14:44 configuration.xsl
-rw-r--r-- 1 gdsuser gdsgrp 3435 May  7 14:44 log4j.properties
-rw-r--r-- 1 gdsuser gdsgrp 1148 Aug 18 14:28 zoo.cfg
-rw-r--r-- 1 gdsuser gdsgrp 1148 May  7 14:44 zoo_sample.cfg
[gdsuser@hz-yyserv bin]$

设置数据文件路径:为 ../dataDir

[gdsuser@hz-yyserv conf]$ pwd
/software/Zookeeper/conf
[gdsuser@hz-yyserv conf]$ vim 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=../dataDir
# 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.
#

3)启动服务端

[gdsuser@hz-yyserv bin]$ 
[gdsuser@hz-yyserv bin]$ sh zkServer.sh start
[gdsuser@hz-yyserv bin]$ sh zkServer.sh start
ZooKeeper JMX enabled by default
Using config: /software/Zookeeper/bin/../conf/zoo.cfg
Starting zookeeper ... STARTED
[gdsuser@hz-yyserv bin]$ 
  • 状态查看
[gdsuser@hz-yyserv bin]$ sh zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /software/Zookeeper/bin/../conf/zoo.cfg
Client port found: 2181. Client address: localhost. Client SSL: false.
Mode: standalone
[gdsuser@hz-yyserv bin]$
  • 停止服务
    sh zkServer.sh stop

4)客户端启动
sh zkCli.sh
输出信息:

Connecting to localhost:2181
................
................
................
Welcome to ZooKeeper!
................
................
WATCHER::
WatchedEvent state:SyncConnected type: None path:null
[zk: localhost:2181(CONNECTED) 0]

2.2、集群部署

zookeeper 的三个端口作用

A)2181 : 对 client 端提供服务
B)2888 : 集群内机器通信使用
C)3888 : 选举 leader 使用

按照3个节点组成一套zookeeper集群,相比于单节点的部署方式,集群新增如下步骤:
1)在每个节点 zoo.cfg 文件配置末尾添加如下配置

server.1=node1_ip:2888:3888
server.2=node2_ip:2888:3888
server.3=node3_ip:2888:3888

2)配置 myid
在 zookeeper根目录新建 myid文件,按照节点编号分别配置,如 1 2 3;

3)服务端启动
说明:该步骤和单节点相同;

3)状态查看
区别于单节点的状态Mode: standalone,集群模式下模式的值分为 leader或 follswer;

2.3、客户端指令

  • 节点访问
[gdsuser@hz-yyserv bin]$ sh zkCli.sh
[zk: localhost:2181(CONNECTED) 0] ls /
[zookeeper]
[zk: localhost:2181(CONNECTED) 1] ls /zookeeper
[config, quota]
[zk: localhost:2181(CONNECTED) 2] ls /zookeeper/config
[]
  • 节点属性
[zk: localhost:2181(CONNECTED) 3] get -s -w /zookeeper/config

cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = 0
dataVersion = 0
aclVersion = -1
ephemeralOwner = 0x0
dataLength = 0
numChildren = 0
[zk: localhost:2181(CONNECTED) 4] 

说明:'get path [watch]' has been deprecated. Please use 'get [-s] [-w] path' instead.

  • 节点状态
[zk: localhost:2181(CONNECTED) 8] stat -w /zookeeper/config
cZxid = 0x0
ctime = Thu Jan 01 08:00:00 CST 1970
mZxid = 0x0
mtime = Thu Jan 01 08:00:00 CST 1970
pZxid = 0x0
cversion = 0
dataVersion = 0
aclVersion = -1
ephemeralOwner = 0x0
dataLength = 0
numChildren = 0
[zk: localhost:2181(CONNECTED) 9]
  • 退出连接
[zk: localhost:2181(CONNECTED) 9] quit

3、配置应用

============================================== over =============================================

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值