一、异常描述
通过zookeeper客户端代码连接zookeeper服务端类,运行报"Exception in thread "main" org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /master"异常问题,详情日志如下>>
SLF4J: This version of SLF4J requires log4j version 1.2.12 or later. See also http://www.slf4j.org/codes.html#log4j_version@b@log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).@b@log4j:WARN Please initialize the log4j system properly.@b@watcher received event@b@zookeeper connect ok@b@--------connect zookeeper ok-----------@b@--------create node ok-----------@b@zookeeper@b@--------get children ok-----------@b@Exception in thread "main" org.apache.zookeeper.KeeperException$NoNodeException: KeeperErrorCode = NoNode for /master@b@ at org.apache.zookeeper.KeeperException.create(KeeperException.java:114)@b@ at org.apache.zookeeper.KeeperException.create(KeeperException.java:54)@b@ at org.apache.zookeeper.ZooKeeper.getData(ZooKeeper.java:1221)@b@ at org.apache.zookeeper.ZooKeeper.getData(ZooKeeper.java:1250)@b@ at zookeeper.BaseZookeeper.getData(BaseZookeeper.java:57)@b@ at zookeeper.ZookeepClient.main(ZookeepClient.java:30)
二、解决方法
1. 通过ssh客户端的zk客户端命令连接zk服务端(参考命令:zkCli.sh -server localhost:2181,配置zookeeper服务端)
[root@raid2t shell]# zkCli.sh -server localhost:2181@b@Connecting to localhost:2181@b@2018-12-01 22:31:06,134 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.6-1569965, built on 02/20/2014 09:09 GMT@b@2018-12-01 22:31:06,136 [myid:] - INFO [main:Environment@100] - Client environment:host.name=<NA>@b@2018-12-01 22:31:06,136 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.7.0_79@b@2018-12-01 22:31:06,137 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation@b@2018-12-01 22:31:06,137 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/home/jdk7/jre@b@2018-12-01 22:31:06,137 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/app/zookeeper-3.4.6/bin/../build/classes:/app/zookeeper-3.4.6/bin/../build/lib/*.jar:/app/zookeeper-3.4.6/bin/../lib/slf4j-log4j12-1.6.1.jar:/app/zookeeper-3.4.6/bin/../lib/slf4j-api-1.6.1.jar:/app/zookeeper-3.4.6/bin/../lib/netty-3.7.0.Final.jar:/app/zookeeper-3.4.6/bin/../lib/log4j-1.2.16.jar:/app/zookeeper-3.4.6/bin/../lib/jline-0.9.94.jar:/app/zookeeper-3.4.6/bin/../zookeeper-3.4.6.jar:/app/zookeeper-3.4.6/bin/../src/java/lib/*.jar:/app/zookeeper-3.4.6/bin/../conf:.:/home/jdk7/lib/dt.jar:/home/jdk7/lib/tools.jar:/home/jdk7/jre/lib@b@2018-12-01 22:31:06,137 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib@b@2018-12-01 22:31:06,138 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp@b@2018-12-01 22:31:06,138 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>@b@2018-12-01 22:31:06,138 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux@b@2018-12-01 22:31:06,138 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64@b@2018-12-01 22:31:06,138 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.32-642.el6.x86_64@b@2018-12-01 22:31:06,138 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root@b@2018-12-01 22:31:06,138 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root@b@2018-12-01 22:31:06,138 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/root/shell@b@2018-12-01 22:31:06,139 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@28807653@b@Welcome to ZooKeeper!@b@2018-12-01 22:31:06,156 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@975] - Opening socket connection to server localhost/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)@b@JLine support is enabled@b@2018-12-01 22:31:06,162 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@852] - Socket connection established to localhost/127.0.0.1:2181, initiating session@b@2018-12-01 22:31:06,167 [myid:] - INFO [main-SendThread(localhost:2181):ClientCnxn$SendThread@1235] - Session establishment complete on server localhost/127.0.0.1:2181, sessionid = 0x16769e5a5940003, negotiated timeout = 30000@b@@b@WATCHER::@b@@b@WatchedEvent state:SyncConnected type:None path:null
2. 创建/master数据Node节点(参考命令:create /master myData)
[zk: localhost:2181(CONNECTED) 0] ls /@b@[zookeeper]@b@[zk: localhost:2181(CONNECTED) 1] create /master myData@b@Created /master
3. 重新运行ZookeepClient客户端类,问题解决了,运行后日志如下
SLF4J: This version of SLF4J requires log4j version 1.2.12 or later. See also http://www.slf4j.org/codes.html#log4j_version@b@log4j:WARN No appenders could be found for logger (org.apache.zookeeper.ZooKeeper).@b@log4j:WARN Please initialize the log4j system properly.@b@watcher received event@b@zookeeper connect ok@b@--------connect zookeeper ok-----------@b@--------create node ok-----------@b@master@b@--------get children ok-----------@b@myData@b@--------get node data ok-----------@b@--------set node data ok-----------@b@[116, 101, 115, 116]@b@--------get node new data ok-----------
三、常用操作命令
#1.连接zk命令@b@[root@raid2t shell]# zkCli.sh -server localhost:2181@b@#2.创建zk节点@b@[zk: localhost:2181(CONNECTED) 1] create /master myData@b@#3. 获取master节点数据@b@[zk: localhost:2181(CONNECTED) 1] get /master@b@#4. 给master节点赋值data123456@b@[zk: localhost:2181(CONNECTED) 1] set /master data123456@b@#5. 删除master节点@b@[zk: localhost:2181(CONNECTED) 1] delete /master