请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

老师我的不知道为什么不走process

/**
 * 
 */
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的话,在另一端去修改监听的数据即可

0 回复 有任何疑惑可以回复我~
  • 提问者 Qolome #1
    非常感谢!老师回复真快
    回复 有任何疑惑可以回复我~ 2018-08-20 18:41:50
  • 提问者 Qolome #2
    改了IP,连不上了,Initiating client connection, connectString=139.199.36.32:2181,139.199.36.32:2182,139.199.36.32:2183 sessionTimeout=5000 watcher=com.ginger.zookeeper.ZKNodeOpteator@2d363fb3
    18:46:43.520 [main] DEBUG org.apache.zookeeper.ClientCnxn - zookeeper.disableAutoWatchReset is false
    回复 有任何疑惑可以回复我~ 2018-08-20 18:48:24
  • 风间影月 回复 提问者 Qolome #3
    防火墙要关闭啊
    回复 有任何疑惑可以回复我~ 2018-08-20 18:49:35
提问者 Qolome 2018-08-20 18:51:15
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之后完全连不上了

0 回复 有任何疑惑可以回复我~
  • 1. 包装zk安装ok,测试没问题
    2. 防火墙关闭或者安全组开放端口
    这样就行了
    回复 有任何疑惑可以回复我~ 2018-08-20 18:53:59
风间影月 2018-08-20 18:38:01

Opening socket connection to server www.justginger.top/139.199.36.32:2181. Will not attempt to authenticate using SASL (unknown error)

连接的时候有问题吧,这边报了一个未知错误

0 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信