附录份我本地windows下伪集群下zk的配置的。(3.4.10版本)
1. 配置文件调整
(图-1)
如图在zk的conf下,配置多个配置文件。
zoo-1.cfg配置如下:
# The number of milliseconds of each tick
tickTime=2000
# The number of ticks that the initial
# synchronization phase can take
###initLimit=10
initLimit=5
# The number of ticks that can pass between
# sending a request and getting an acknowledgement
###syncLimit=5
syncLimit=2
# the directory where the snapshot is stored.
# do not use /tmp for storage, /tmp here is just
# example sakes.
###dataDir=/tmp/zookeeper
dataDir=/zookeeper/data/zk1
# the port at which the clients will connect
clientPort=2182
# 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.A=B:C:D 伪集群时,B都是一样,通信端口就要保持不一致
#集群模式下还有配置一个文件myid,这个文件在dataDir目录下,文件里面有只有一个数据就是A的值,zk启动时会读取这个文件,拿
#到里面的数据与zoo.cfg里面的配置信息比较从而判断到底是哪个server
#A:一个数字,表示第几号服务器
#B:这个服务器的ip地址
#C:表示这个服务器与集群中Leader服务器交换信息的端口
#D:万一集群中的Leader服务器挂了,需求一个端口来重新进行选举,这个端口就是用来执行选举是服务器互通信息的
server.1=localhost:2287:3387
server.2=localhost:2288:3388
server.3=localhost:2289:3389
zoo-2.cfg和zoo-3.cfg的配置跟上门的差不多,存放data的路径和端口调整下不同:
dataDir=/zookeeper/data/zk2
clientPort=2183
dataDir=/zookeeper/data/zk3
clientPort=2184
2. 服务启动调整
(图-2)
如图添加 zkServer-*.cmd 用于启动相应的服务的。内容是根据zkServer.cmd中复制对应调整即可。
zkServer-1.cmd的内容如下:
@echo off
REM Licensed to the Apache Software Foundation (ASF) under one or more
REM contributor license agreements. See the NOTICE file distributed with
REM this work for additional information regarding copyright ownership.
REM The ASF licenses this file to You under the Apache License, Version 2.0
REM (the "License"); you may not use this file except in compliance with
REM the License. You may obtain a copy of the License at
REM
REM http://www.apache.org/licenses/LICENSE-2.0
REM
REM Unless required by applicable law or agreed to in writing, software
REM distributed under the License is distributed on an "AS IS" BASIS,
REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
REM See the License for the specific language governing permissions and
REM limitations under the License.
setlocal
call "%~dp0zkEnv.cmd"
REM 原本在zkEnv.cmd中配置 现调整到zkServer.cmd中配置
set ZOOCFG=%ZOOCFGDIR%\zoo-1.cfg
REM 原本在zkEnv.cmd中配置 现调整到zkServer.cmd中配置
set ZOO_LOG_DIR=\zookeeper\log\zk1
set ZOOMAIN=org.apache.zookeeper.server.quorum.QuorumPeerMain
echo on
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% "%ZOOCFG%" %*
endlocal
zkServer-2.cmd/zkServer-3.cmd的配置大同小异:
set ZOOCFG=%ZOOCFGDIR%\zoo-2.cfg
set ZOO_LOG_DIR=\zookeeper\log\zk2
set ZOOCFG=%ZOOCFGDIR%\zoo-3.cfg
set ZOO_LOG_DIR=\zookeeper\log\zk3
3.客户端配置:
上图-2中zkCli-*.cmd是从zkCli.cmd中复制调整的,用于windows下客户端的。
zkCli-1.cmd配置如下:
@echo off
REM Licensed to the Apache Software Foundation (ASF) under one or more
REM contributor license agreements. See the NOTICE file distributed with
REM this work for additional information regarding copyright ownership.
REM The ASF licenses this file to You under the Apache License, Version 2.0
REM (the "License"); you may not use this file except in compliance with
REM the License. You may obtain a copy of the License at
REM
REM http://www.apache.org/licenses/LICENSE-2.0
REM
REM Unless required by applicable law or agreed to in writing, software
REM distributed under the License is distributed on an "AS IS" BASIS,
REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
REM See the License for the specific language governing permissions and
REM limitations under the License.
setlocal
call "%~dp0zkEnv.cmd"
REM 注意:原本这个设置放到zkEnv.cmd中的,应为客户端运行也需要日志地址,这里添加下
set ZOO_LOG_DIR=\zookeeper\log\cl1
REM 因为使用了伪集群,端口号调整了。所以下面的call启动时需要指定端口号。查看源码 需要传入参数 "-server" "127.0.0.1:2182"
set ZOOMAIN=org.apache.zookeeper.ZooKeeperMain
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% %* "-server" "127.0.0.1:2182,127.0.0.1:2183,127.0.0.1:2184"
endlocal
zkCli-2的配置:
@echo off
REM Licensed to the Apache Software Foundation (ASF) under one or more
REM contributor license agreements. See the NOTICE file distributed with
REM this work for additional information regarding copyright ownership.
REM The ASF licenses this file to You under the Apache License, Version 2.0
REM (the "License"); you may not use this file except in compliance with
REM the License. You may obtain a copy of the License at
REM
REM http://www.apache.org/licenses/LICENSE-2.0
REM
REM Unless required by applicable law or agreed to in writing, software
REM distributed under the License is distributed on an "AS IS" BASIS,
REM WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
REM See the License for the specific language governing permissions and
REM limitations under the License.
setlocal
call "%~dp0zkEnv.cmd"
REM 注意:原本这个设置放到zkEnv.cmd中的,应为客户端运行也需要日志地址,这里添加下
set ZOO_LOG_DIR=\zookeeper\log\cl2
REM 因为使用了伪集群,端口号调整了。所以下面的call启动时需要指定端口号。查看源码 需要传入参数 "-server" "127.0.0.1:2182"
REM 开始时用的是 "localhost:2182" 运行时报错的,所以这里需要的是ip地址的方式
set ZOOMAIN=org.apache.zookeeper.ZooKeeperMain
call %JAVA% "-Dzookeeper.log.dir=%ZOO_LOG_DIR%" "-Dzookeeper.root.logger=%ZOO_LOG4J_PROP%" -cp "%CLASSPATH%" %ZOOMAIN% %* "-server" "127.0.0.1:2182,127.0.0.1:2183,127.0.0.1:2184"
endlocal
4.运行
依次启动zkServer-*.cmd。第一个启动时会报错,无妨,这个因为启动时要选leader的,等后续几台启动成功了就没事。
服务启动完成。依次启动zkCli-*.cmd即可。
5.常用命令
注意:记得zk开始的根目录是 /
ls path [watch]:查看路径下节点
[zk: 127.0.0.1:2182,127.0.0.1:2183,127.0.0.1:2184(CONNECTED) 1] ls /
[zookeeper, lock]
**添加监听:
客户端cli-1:
[zk: 127.0.0.1:2182,127.0.0.1:2183,127.0.0.1:2184(CONNECTED) 7] ls /lock 1
[]
客户端cli-2:
[zk: 127.0.0.1:2182,127.0.0.1:2183,127.0.0.1:2184(CONNECTED) 5] create -e /lock/
lock01 "123"
Created /lock/lock01
这时候可以看到 客户端cli-1中显示:
[zk: 127.0.0.1:2182,127.0.0.1:2183,127.0.0.1:2184(CONNECTED) 8]
WATCHER::
WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/lock
不知道命令时:
[zk: 127.0.0.1:2182,127.0.0.1:2183,127.0.0.1:2184(CONNECTED) 6] -h
ZooKeeper -server host:port cmd args
stat path [watch]
set path data [version]
ls path [watch]
delquota [-n|-b] path
ls2 path [watch]
setAcl path acl
setquota -n|-b val path
history
redo cmdno
printwatches on|off
delete path [version]
sync path
listquota path
rmr path
get path [watch]
create [-s] [-e] path data acl
addauth scheme auth
quit
getAcl path
close
connect host:port
- stat path [watch] 查看path的属性信息
- set path data [version]
更新path的数据内容,version是做类似cas的功能的对应dataversion,命令行可忽略 - ls path [watch] 显示path下的节点,不递归显示,watch注册监听,命令行可忽视
- ls2 path 显示当前节点下的节点和当前节点的属性信息
- setacl path acl
设置节点acl,例子(scheme:id:password=:perm)-(digest:example:sha-1(base64(pwd))=:cdrwa)
create delete read write admin - history 显示操作记录
- redo cmdno redo命令用于再次执行某个命令,使用方式为redo cmdid 如 redo 20。常与history配合使用
- printwatches on|off printWatchers命令用于设置和显示监视状态,值为on或则off
- delete path [version] 删除节点,不能递归删除,只能删除叶子节点
- sync path sync命令用于强制同步,由于请求在半数以上的zk server上生效就表示此请求生效,那么就会有一些zk server上的数据是旧的。sync命令就是强制同步所有的更新操作。
- rmr path 将会删除路径以及其下的所有子节点.
- get path [watch] 获取path的属性信息和数据内容
- create -s -e path data [acl]
创建节点,-s表示顺序,-e表示临时,默认是持久节点,acl缺省表示不做任何权限限制 - addauth scheme auth 添加授权信息。只有授权之后,才能够访问那些具有ACL控制的节点数据.注意”auth”信息为原始的用户名和密码,而不是经过DigestAuthenticationProvider签名之后的. 如果使用了错误的授权信息,可能导致”Authentication is not valid : “.
- quit 退出zkcli
getacl path 获取path节点的acl
配额quota:
- delquota [-n| -b] path 命令用于删除配额,-n为子节点个数,-b为节点数据长度,如:delquota –n 2
- setquota -n| -b val path 命令用于设置节点个数以及数据长度的配额。-n为子节点个数,-b为节点数据长度
- listquota path istquota命令用于显示配额,如listquota /lock
ZooKeeper quota机制支持节点个数(znode)和空间大小(字节数)。这里要说明一下zookeeper的Quota机制是比较温和的,即使超限了,只是在日志中报告一下,并不会限制Client的行为,Client可以继续操作znode。