zookeeper安装及使用

一、zookeeper下载安装

1、下载zookeeper-3.4.6安装包

 cd/usr/developSoft/

wget http://www.apache.org/dist/zookeeper/zookeeper-3.4.6/zookeeper-3.4.6.tar.gz

2、解压到 /usr/local/ 目录下

tar -zxvf  zookeeper-3.4.6.tar.gz  -C /usr/local/

进入/usr/local/zookeeper-3.4.6/bin目录

./zkServer.sh start 启动服务

./zkServer.sh stop  停止服务


二、zookeeper独立模式(开发及测试)配置

1、进入/usr/local/zookeeper-3.4.6/conf中修改zoo.cfg 配置

cd /usr/local/zookeeper-3.4.6/conf

vi   zoo.cfg 

2、独立模式  可修改zoo.cfg配置如下:

 
# The number of milliseconds of each tick
#ZK中的一个时间单元。ZK中所有时间都是以这个时间单元为基础,进行整数倍配置的
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 anacknowledgement
syncLimit=5  #leader与learner(follower、obverser)直接心跳检测时间间隔
# the directory where the snapshot isstored.
# do not use /tmp for storage, /tmp here isjust
# example sakes.
#存储快照文件snapshot的目录。默认情况下,事务日志也会存储在这里。建议同时配置参数dataLogDir, 事务日志的写性能直接影响zk性能。
dataDir=/usr/local/zookeeper-3.4.6/zk_data
# dataLogDir事务日志输出目录。尽量给事务日志的输出配置单独的磁盘或是挂载点,这将极大的提升ZK性能
dataLogDir=/usr/local/zookeeper-3.4.6/zk_dataLog
# the port at which the clients willconnect
clientPort=2181
# the maximum number of client connections.
# increase this if you need to handle moreclients
#maxClientCnxns=60
#
# Be sure to read the maintenance sectionof the
# administrator guide before turning onautopurge.
#
#http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain indataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable autopurge feature
#autopurge.purgeInterval=1

cd bin

./zkServer.sh start-foreground    #以前台程序启动服务

或者

./zkServer.sh start  #以后台程序启动zkServer 服务器

./zkServer.shstop  #停止zkServer服务器


三、zookeeper集群模式(可用于生产)配置

准备三台机器,搭建3个节点的zookeeper集群。

(为简便起见,我在一台虚拟机中搭建了3zookeeper实例并使用不同的端口,模拟三台物理机。实际生产环境下强烈建议使用不同物理机)

1、  下载安装zookeeper-3.4.6,参上不再赘述。

2、  在/usr/local解压了 zookeeper-3.4.6_1, zookeeper-3.4.6_2, zookeeper-3.4.6_3 作为Cluster中3个zookeeper节点实例。


修改每个zookeeper服务节点的zoo.cfg配置和myid文件,以第一个节点为例:

zookeeper-3.4.6_1/conf/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 anacknowledgement
syncLimit=5
# the directory where the snapshot isstored.
# do not use /tmp for storage, /tmp here isjust
# example sakes.
dataDir=/usr/local/zookeeper-3.4.6_1/zk_data
dataLogDir=/usr/local/zookeeper-3.4.6_1/zk_log
 
# the port at which the clients willconnect
clientPort=2182
# the maximum number of client connections.
# increase this if you need to handle moreclients
#maxClientCnxns=60
#
# Be sure to read the maintenance sectionof the
# administrator guide before turning onautopurge.
#
#http://zookeeper.apache.org/doc/current/zookeeperAdmin.html#sc_maintenance
#
# The number of snapshots to retain indataDir
#autopurge.snapRetainCount=3
# Purge task interval in hours
# Set to "0" to disable autopurge feature
#autopurge.purgeInterval=1
server.1=localhost:2881:3881
server.2=localhost:2882:3882
server.3=localhost:2883:3883


3 配置myId

在dataDir里会放置一个myid文件,里面就一个数字,用来唯一标识这个服务。这个id是很重要的,一定要保证整个集群中唯一。zookeeper会根据这个id来取出server.x上的配置。比如当前id为1,则对应着zoo.cfg里的server.1的配置。

echo 1 > /usr/local/zookeeper-3.4.6_1/zk_data/myid


第二个和第三个节点的配置(zoo.cfg 、myid文件)与第一个节点类似,不同的仅是dataDir dataLogDir  myid文件内容。

第二个节点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=/usr/local/zookeeper-3.4.6_2/zk_data
dataLogDir=/usr/local/zookeeper-3.4.6_2/zk_log
# the port at which the clients will connect
clientPort=2183
# 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
server.1=localhost:2881:3881
server.2=localhost:2882:3882
server.3=localhost:2883:3883

myid 文件

echo 2 > /usr/local/zookeeper-3.4.6_2/zk_data/myid


第三个节点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=/usr/local/zookeeper-3.4.6_3/zk_data
dataLogDir=/usr/local/zookeeper-3.4.6_3/zk_log
# the port at which the clients will connect
clientPort=2184
# 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
server.1=localhost:2881:3881
server.2=localhost:2882:3882
server.3=localhost:2883:3883


myid 文件

echo 3 > /usr/local/zookeeper-3.4.6_3/zk_data/myid

ok,依次启动3个几点

cd /usr/local/zookeeper-3.4.6_1/bin/

 ./zkServer.sh start

cd /usr/local/zookeeper-3.4.6_2/bin/

 ./zkServer.sh start

cd /usr/local/zookeeper-3.4.6_3/bin/

 ./zkServer.sh start 




四、zookeeper 客户端命令

./zkCli.sh -server127.0.0.1:2181

显示根目录下文件

ls /              //查看当前节点数据

ls2 /             //查看当前节点数据并能看到更新次数等数据

创建文件, 并设置初始内容:

create /config"test" //创建一个新的节点并设置关联值

create /config“”     //创建一个新的空节点

获取文件内容

get /brokers      //获取节点内容

修改文件内容

set /zk"zkbak"   //对 zk 所关联的字符串进行设置

删除文件

delete /brokers  //删除节点

rmr    /brokers //删除节点及子节点




猜你喜欢

转载自blog.csdn.net/jasnet_u/article/details/71514094
今日推荐