老师我的不知道为什么不走process
来源:6-8 获取zk节点数据

Qolome
2018-08-20
/** * */ package com.ginger.zookeeper; import java.util.concurrent.CountDownLatch; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.Watcher.Event.EventType; import org.apache.zookeeper.ZooKeeper; import org.apache.zookeeper.data.Stat; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * @Description: 获取节点数据 * @author 姜锋 * @date 2018年8月20日 下午5:04:28 * @version V1.0 */ public class ZKGetNodeData implements Watcher { final static Logger log = LoggerFactory.getLogger(ZKGetNodeData.class); private ZooKeeper zooKeeper = null; // 服务端地址 public static final String connectionString = "www.justginger.top:2181,www.justginger.top:2182,www.justginger.top:2183"; // session过期时间 public static final Integer sessionTimeout = 5000; private static Stat stat = new Stat(); public ZKGetNodeData() { } /** * @Description: TODO(这里用一句话描述这个方法的作用) @author 姜锋 @date 2018年8月20日 下午5:46:30 @param @param * i @param @return @return CountDownLatch @throws */ public ZKGetNodeData(String connectionString) { try { zooKeeper = new ZooKeeper(connectionString, sessionTimeout, new ZKNodeOpteator()); } catch (Exception e) { e.printStackTrace(); if (zooKeeper != null) { try { zooKeeper.close(); } catch (InterruptedException e1) { e1.printStackTrace(); } } } } private static CountDownLatch countDown = new CountDownLatch(1); public static void main(String[] args) throws Exception { ZKGetNodeData zkServer = new ZKGetNodeData(connectionString); /** * 参数: path:节点路径 watch:true或者false, 注册一个watch事件 stat : 状态 */ byte[] resByte = zkServer.getZooKeeper().getData("/ginger", true, stat); String result = new String(resByte); System.out.println("此次查询结果" + result); countDown.await(); } @Override public void process(WatchedEvent event) { try { if (event.getType() == EventType.NodeDataChanged) { ZKGetNodeData zkServer = new ZKGetNodeData(connectionString); byte[] resByte = zkServer.getZooKeeper().getData("/ginger", false, stat); String result = new String(resByte); System.out.println("更改后的值" + result); System.out.println("更改后的版本号New Version: " + stat.getVersion()); countDown.countDown(); } else if (event.getType() == EventType.NodeCreated) { } else if (event.getType() == EventType.NodeChildrenChanged) { } else if (event.getType() == EventType.NodeDeleted) { } } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } /** * @return zooKeeper */ public ZooKeeper getZooKeeper() { return zooKeeper; } /** * @param zooKeeper 要设置的 zooKeeper */ public void setZooKeeper(ZooKeeper zooKeeper) { this.zooKeeper = zooKeeper; } }
控制台日志
18:31:43.208 [main] DEBUG org.apache.zookeeper.ClientCnxn - zookeeper.disableAutoWatchReset is false
18:31:43.607 [main-SendThread(www.justginger.top:2181)] INFO org.apache.zookeeper.ClientCnxn - Opening socket connection to server www.justginger.top/139.199.36.32:2181. Will not attempt to authenticate using SASL (unknown error)
18:31:43.618 [main-SendThread(www.justginger.top:2181)] INFO org.apache.zookeeper.ClientCnxn - Socket connection established to www.justginger.top/139.199.36.32:2181, initiating session
18:31:43.619 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Session establishment request sent on www.justginger.top/139.199.36.32:2181
18:31:43.640 [main-SendThread(www.justginger.top:2181)] INFO org.apache.zookeeper.ClientCnxn - Session establishment complete on server www.justginger.top/139.199.36.32:2181, sessionid = 0x100000ced1f000e, negotiated timeout = 5000
18:31:43.656 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x100000ced1f000e, packet:: clientPath:null serverPath:null finished:false header:: 1,4 replyHeader:: 1,4294967371,0 request:: '/ginger,T response:: #617364617364617364,s{4294967304,4294967369,1534647571665,1534761083862,6,2,0,0,9,0,4294967312}
此次查询结果asdasdasd
18:31:45.322 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x100000ced1f000e after 10ms
18:31:46.986 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x100000ced1f000e after 7ms
18:31:48.594 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got notification sessionid:0x100000ced1f000e
18:31:48.595 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got WatchedEvent state:SyncConnected type:NodeDataChanged path:/ginger for sessionid 0x100000ced1f000e
18:31:48.602 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x100000ced1f000e after 8ms
18:31:50.268 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x100000ced1f000e after 8ms
18:31:51.935 [main-SendThread(www.justginger.top:2181)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x100000ced1f000e after 8ms
3回答
-
你把域名改为ip试试,另外要触发watch的话,在另一端去修改监听的数据即可
052018-09-02 -
Qolome
提问者
2018-08-20
18:50:26.062 [main] DEBUG org.apache.zookeeper.ClientCnxn - zookeeper.disableAutoWatchReset is false 18:50:38.561 [main-SendThread(139.199.36.32:2183)] INFO org.apache.zookeeper.ClientCnxn - Opening socket connection to server 139.199.36.32/139.199.36.32:2183. Will not attempt to authenticate using SASL (unknown error) 18:50:38.563 [main-SendThread(139.199.36.32:2183)] WARN org.apache.zookeeper.ClientCnxn - Client session timed out, have not heard from server in 12076ms for sessionid 0x0 18:50:38.563 [main-SendThread(139.199.36.32:2183)] INFO org.apache.zookeeper.ClientCnxn - Client session timed out, have not heard from server in 12076ms for sessionid 0x0, closing socket connection and attempting reconnect 18:50:38.568 [main-SendThread(139.199.36.32:2183)] DEBUG org.apache.zookeeper.ClientCnxnSocketNIO - Ignoring exception during shutdown input java.net.SocketException: Socket is not connected at sun.nio.ch.Net.translateToSocketException(Net.java:123) at sun.nio.ch.Net.translateException(Net.java:157) at sun.nio.ch.Net.translateException(Net.java:163) at sun.nio.ch.SocketAdaptor.shutdownInput(SocketAdaptor.java:401) at org.apache.zookeeper.ClientCnxnSocketNIO.cleanup(ClientCnxnSocketNIO.java:200) at org.apache.zookeeper.ClientCnxn$SendThread.cleanup(ClientCnxn.java:1250) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1174) Caused by: java.nio.channels.NotYetConnectedException: null at sun.nio.ch.SocketChannelImpl.shutdownInput(SocketChannelImpl.java:782) at sun.nio.ch.SocketAdaptor.shutdownInput(SocketAdaptor.java:399) ... 3 common frames omitted 18:50:38.569 [main-SendThread(139.199.36.32:2183)] DEBUG org.apache.zookeeper.ClientCnxnSocketNIO - Ignoring exception during shutdown output java.net.SocketException: Socket is not connected at sun.nio.ch.Net.translateToSocketException(Net.java:123) at sun.nio.ch.Net.translateException(Net.java:157) at sun.nio.ch.Net.translateException(Net.java:163) at sun.nio.ch.SocketAdaptor.shutdownOutput(SocketAdaptor.java:409) at org.apache.zookeeper.ClientCnxnSocketNIO.cleanup(ClientCnxnSocketNIO.java:207) at org.apache.zookeeper.ClientCnxn$SendThread.cleanup(ClientCnxn.java:1250) at org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1174) Caused by: java.nio.channels.NotYetConnectedException: null at sun.nio.ch.SocketChannelImpl.shutdownOutput(SocketChannelImpl.java:799) at sun.nio.ch.SocketAdaptor.shutdownOutput(SocketAdaptor.java:407) ... 3 common frames omitted Exception in thread "main" org.apache.zookeeper.KeeperException$ConnectionLossException: KeeperErrorCode = ConnectionLoss for /ginger at org.apache.zookeeper.KeeperException.create(KeeperException.java:102) at org.apache.zookeeper.KeeperException.create(KeeperException.java:54) at org.apache.zookeeper.ZooKeeper.getData(ZooKeeper.java:1221) at org.apache.zookeeper.ZooKeeper.getData(ZooKeeper.java:1250) at com.ginger.zookeeper.ZKGetNodeData.main(ZKGetNodeData.java:60)
改成IP之后完全连不上了
012018-08-20 -
风间影月
2018-08-20
Opening socket connection to server www.justginger.top/139.199.36.32:2181. Will not attempt to authenticate using SASL (unknown error)
连接的时候有问题吧,这边报了一个未知错误
00
相似问题