请稍等 ...
×

采纳答案成功!

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

可以获取数据,但是无法监听节点状态

ZooKeeper伪集群防火墙设置
服务:2181,2182,2183
选举leader使用:3181,3182,3183
集群内机器通讯使用:4181,4182,4183
只需要开放2181,2182,2183就可以,还是其他也需要开放呀




代码基本与老师的一样


/**
 * 
 */
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;
/**
 * @Description: 获取节点数据
 * @author 姜锋
 * @date 2018年8月20日 下午5:04:28
 * @version V1.0
 */
public class ZKGetNodeData implements Watcher {
private ZooKeeper zooKeeper = null;
//final static Logger log = LoggerFactory.getLogger(ZKGetNodeData.class);
// 服务端地址
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() {}
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);
System.out.println("等待countdown累减 ");
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());
System.out.println("完成countdown累减   1");
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;
}
}





日志记录

11:42:19.041 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.name=Ginger
11:42:19.041 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.home=C:\Users\Ginger
11:42:19.041 [main] INFO org.apache.zookeeper.ZooKeeper - Client environment:user.dir=D:\eclipse-workspace\ginger-ssm
11:42:19.043 [main] INFO org.apache.zookeeper.ZooKeeper - Initiating client connection, connectString=www.justginger.top:2181,www.justginger.top:2182,www.justginger.top:2183 sessionTimeout=5000 watcher=com.ginger.zookeeper.ZKNodeOpteator@2d363fb3
11:42:19.048 [main] DEBUG org.apache.zookeeper.ClientCnxn - zookeeper.disableAutoWatchReset is false
11:42:19.535 [main-SendThread(www.justginger.top:2183)] INFO org.apache.zookeeper.ClientCnxn - Opening socket connection to server www.justginger.top/139.199.36.32:2183. Will not attempt to authenticate using SASL (unknown error)
11:42:19.543 [main-SendThread(www.justginger.top:2183)] INFO org.apache.zookeeper.ClientCnxn - Socket connection established to www.justginger.top/139.199.36.32:2183, initiating session
11:42:19.545 [main-SendThread(www.justginger.top:2183)] DEBUG org.apache.zookeeper.ClientCnxn - Session establishment request sent on www.justginger.top/139.199.36.32:2183
11:42:19.562 [main-SendThread(www.justginger.top:2183)] INFO org.apache.zookeeper.ClientCnxn - Session establishment complete on server www.justginger.top/139.199.36.32:2183, sessionid = 0x3002e159d830001, negotiated timeout = 5000
11:42:19.575 [main-SendThread(www.justginger.top:2183)] DEBUG org.apache.zookeeper.ClientCnxn - Reading reply sessionid:0x3002e159d830001, packet:: clientPath:null serverPath:null finished:false header:: 1,4  replyHeader:: 1,17179869190,0  request:: '/ginger,T  response:: #68616861,s{4294967304,17179869187,1534647571665,1535859263432,13,5,0,0,4,3,4294967378} 
此次查询结果haha
等待countdown累减 
11:42:35.088 [main-SendThread(www.justginger.top:2183)] DEBUG org.apache.zookeeper.ClientCnxn - Got WatchedEvent state:SyncConnected type:NodeDataChanged path:/ginger for sessionid 0x3002e159d830001
11:42:36.302 [main-SendThread(www.justginger.top:2183)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x3002e159d830001 after 69ms
11:42:37.906 [main-SendThread(www.justginger.top:2183)] DEBUG org.apache.zookeeper.ClientCnxn - Got ping response for sessionid: 0x3002e159d830001 after 8ms


正在回答

1回答

防火墙直接关了吧,伪集群的话一般2181这几个端口开了就行

0 回复 有任何疑惑可以回复我~
  • 提问者 Qolome #1
    我用的云主机,那是不是我的IP要写成外网的IP,但是网卡信息上只有内网的IP
    回复 有任何疑惑可以回复我~ 2018-09-02 11:18:54
  • 提问者 Qolome #2
    配置只能写IP,不能写域名吗?
    回复 有任何疑惑可以回复我~ 2018-09-02 11:19:44
  • 域名也可以,前提是备案过并且解析成功,内网访问用内网ip,外网访问用外网ip
    回复 有任何疑惑可以回复我~ 2018-09-02 11:29:29
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信