0 课程地址
https://coding.imooc.com/lesson/201.html#mid=12718
1 重点关注
1.1 本节内容
验证了使用sessionid和sessionPassword重连的session和之前的session一致,详见3.1
2 课程内容
2.1 扩展之:echo dump|nc localhost 2181
- echo 打印输出
- dump 列出所有会话和临时节点
- nc - Linux nc命令用于设置路由器。语法为nc IP地址 端口号。
- IP地址 - 即ZooKeeper所在的服务器IP地址
- 端口 - 即ZooKeeper的端口号。
- | 管道符 后边作为参数,在前边输出
更多zk命令参考:
https://blog.csdn.net/succing/article/details/121793494
2.2 扩展之:nc
- 原因:源于使用nc命令时报错:
Ncat: Connection refused
- 解决方案:
直接在命令行里输入: nc 如果显示: Ncat: You must specify a host to connect to. QUITTING. 恭喜你,与博主的问题一致,下面是解决方案与问题解析 解决方法 wget https://sourceforge.net/projects/netcat/files/netcat/0.7.1/netcat-0.7.1.tar.gz --no-check-certificate tar -zxf netcat-0.7.1.tar.gz cd netcat-0.7.1/ ./configure sudo make sudo make install 然后写入环境: sudo vim /etc/profile 在最后写入: export NETCAT_HOME=/usr/local/netcat export PATH=$PATH:$NETCAT_HOME/bin 最后使配置生效: source /etc/profile 然后输入nc就可以得到: Cmd line: 这样nc命令就成功了
- 注意:环境变量部分
如果你的配置文件里已经有环境变量了,只需要把Path追加进去就好,比如我的profile
export PATH=$PATH:$ZOOKEEPER_HOME/bin:$JAVA_HOME/bin:$NETCAT_HOME/bin export NETCAT_HOME=/usr/local/netcat
- 报错分析原理:(这个我没用,使用解决方案已经解决了我的问题)
nc与netcat通常是一个东西,而ncat(也称nmap-ncat)是另一个东西,默认的nc命令链接到了ncat,所以自然得不到我们想要的结果
首先尝试输入:
netcat
没有这个命令,然后通过命令:
whereis nc
可以看到连接的是:nc: /usr/bin/nc /usr/share/man/man1/nc.1.gz,并不是netcat,所以遇到这种情况大概率是没有安装过netcat,安装一下就行,同时编译安装后会自动踢掉这个软连接,安装完成后输入nc就可以得到:
Cmd line:
- ncat简介:
netcat称为网络工具中的瑞士军刀,体积小巧,但功能强大,为了方便常用nc作为其简称。 它可以在两台设备上面相互交互,主要就是侦听模式和传输模式。大致功能如下:
Telnet功能(本案例使用)
获取banner信息
传输文本信息
传输文件/目录
加密传输文件,默认不加密
远程控制
加密所有流量
流媒体服务器
远程克隆硬盘
参考:
https://blog.csdn.net/qq_63844103/article/details/127681824
https://blog.csdn.net/weixin_35757704/article/details/121456089
2.3 扩展之 wget安装:
- 原因:使用2.2命令wget连接时报错
wget: 未找到命令
- 解决方案:
-bash: wget: 未找到命令,原因为linux系统缺少命令环境。 centos 系统下,执行以下命令进行安装 yum install wget -y ubuntu系统,需要执行以下命令 apt-get install -y wget
参考:
https://blog.csdn.net/luobotoutou123/article/details/130609494
3 Coding
3.1 验证使用sessionid和sessionPassword重连的session和之前的session一致
- 启动zk服务端
进入到 cd /usr/local/zookeeper/bin 重启zookeeper服务端 ./zkServer.sh restart
- 验证类:
package com.imooc.zk.demo; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import org.slf4j.Logger; import org.slf4j.LoggerFactory; /** * * @Title: ZKConnectDemo.java * @Description: zookeeper 恢复之前的会话连接demo演示 */ public class ZKConnectSessionWatcher implements Watcher { final static Logger log = LoggerFactory.getLogger(ZKConnectSessionWatcher.class); public static final String zkServerPath = "172.26.139.4:2181"; public static final Integer timeout = 5000; public static void main(String[] args) throws Exception { //1 第1次获取连接 ZooKeeper zk = new ZooKeeper(zkServerPath, timeout, new ZKConnectSessionWatcher()); long sessionId = zk.getSessionId(); //1.1 因为sessionid是long类型的,要和服务端的sessionid进行对比,故转化成16进制的方便对比 String ssid = "0x" + Long.toHexString(sessionId); System.out.println(ssid); byte[] sessionPassword = zk.getSessionPasswd(); log.warn("客户端开始连接zookeeper服务器..."); log.warn("连接状态:{}", zk.getState()); new Thread().sleep(1000); log.warn("连接状态:{}", zk.getState()); new Thread().sleep(200); // 开始会话重连 log.warn("开始会话重连..."); //2 第2次获取连接 ZooKeeper zkSession = new ZooKeeper(zkServerPath, timeout, new ZKConnectSessionWatcher(), sessionId, sessionPassword); log.warn("重新连接状态zkSession:{}", zkSession.getState()); new Thread().sleep(1000); log.warn("重新连接状态zkSession:{}", zkSession.getState()); } @Override public void process(WatchedEvent event) { log.warn("接受到watch通知:{}", event); } }
- debug分别在第1次连接和第2次连接时断点,查看zk服务端的sessionId
可以和下边的日志(标红部分)对比,sessionid一致
- STS日志
2024-03-30 07:46:50,950 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:SyncConnected type:None path:null 0x10016d7df880002 2024-03-30 07:47:18,645 [main] [com.imooc.zk.demo.ZKConnectSessionWatcher.main(ZKConnectSessionWatcher.java:30)] - [WARN] 客户端开始连接zookeeper服务器... 2024-03-30 07:47:19,240 [main] [com.imooc.zk.demo.ZKConnectSessionWatcher.main(ZKConnectSessionWatcher.java:31)] - [WARN] 连接状态:CONNECTED 2024-03-30 07:47:21,256 [main] [com.imooc.zk.demo.ZKConnectSessionWatcher.main(ZKConnectSessionWatcher.java:33)] - [WARN] 连接状态:CONNECTED 2024-03-30 07:47:22,901 [main] [com.imooc.zk.demo.ZKConnectSessionWatcher.main(ZKConnectSessionWatcher.java:38)] - [WARN] 开始会话重连... 2024-03-30 07:47:33,453 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:SyncConnected type:None path:null 2024-03-30 07:47:33,564 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:Disconnected type:None path:null 2024-03-30 07:47:35,007 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:SyncConnected type:None path:null 2024-03-30 07:47:35,115 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:Disconnected type:None path:null 2024-03-30 07:47:36,567 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:SyncConnected type:None path:null 2024-03-30 07:47:36,677 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:Disconnected type:None path:null 2024-03-30 07:47:37,788 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:SyncConnected type:None path:null 2024-03-30 07:47:37,898 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:Disconnected type:None path:null 2024-03-30 07:47:38,928 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:SyncConnected type:None path:null 2024-03-30 07:47:39,036 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:Disconnected type:None path:null 2024-03-30 07:47:40,164 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:SyncConnected type:None path:null 2024-03-30 07:47:40,265 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:Disconnected type:None path:null 2024-03-30 07:47:41,781 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:SyncConnected type:None path:null 2024-03-30 07:47:41,896 [main-EventThread] [com.imooc.zk.demo.ZKConnectSessionWatcher.process(ZKConnectSessionWatcher.java:52)] - [WARN] 接受到watch通知:WatchedEvent state:Disconnected type:None path:null 2024-03-30 07:47:42,406 [main] [com.imooc.zk.demo.ZKConnectSessionWatcher.main(ZKConnectSessionWatcher.java:45)] - [WARN] 重新连接状态zkSession:CONNECTED 2024-03-30 07:47:43,415 [main] [com.imooc.zk.demo.ZKConnectSessionWatcher.main(ZKConnectSessionWatcher.java:47)] - [WARN] 重新连接状态zkSession:CONNECTED
- log4j.properties
log4j.rootLogger=WARN,console
log4j.appender.console=org.apache.log4j.ConsoleAppender
log4j.appender.console.encoding=UTF-8
log4j.appender.console.layout=org.apache.log4j.PatternLayout
log4j.appender.console.layout.ConversionPattern=%-d{yyyy-MM-dd HH:mm:ss,SSS} [%t] [%l] - [%p] %m%n