Zookeeper(五):单机版测试、集群测试、创建根节点
我们已经搭建好了zookeeper的单机版、伪集群版、集群版,现在启动客户端连上zkServer进行测试一、单机版测试1.1、命令测试连接上zookeeper的server:zkCli.sh -server 192.168.133.149:2180# 连接zookeeper单机版的server[root@centos6 bin]# ./zkCli.sh -server 192.168.133.1.
·
我们已经搭建好了zookeeper的单机版、伪集群版、集群版,现在启动客户端连上zkServer进行测试
一、单机版测试
1.1、命令测试
连接上zookeeper的server:zkCli.sh -server 192.168.133.149:2180
# 连接zookeeper单机版的server
[root@centos6 bin]# ./zkCli.sh -server 192.168.133.149:2180
# 连接上的日志
[root@centos6 bin]# ./zkCli.sh -server localhost:2180
Connecting to localhost:2180
2018-02-06 13:54:53,970 [myid:] - INFO [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2018-02-06 13:54:53,974 [myid:] - INFO [main:Environment@100] - Client environment:host.name=localhost
2018-02-06 13:54:53,974 [myid:] - INFO [main:Environment@100] - Client environment:java.version=1.7.0_80
2018-02-06 13:54:53,977 [myid:] - INFO [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-02-06 13:54:53,977 [myid:] - INFO [main:Environment@100] - Client environment:java.home=/usr/java/jdk1.7.0_80/jre
2018-02-06 13:54:53,977 [myid:] - INFO [main:Environment@100] - Client environment:java.class.path=/usr/local/zookeeper-3.4.10/bin/../build/classes:/usr/local/zookeeper-3.4.10/bin/../build/lib/*.jar:/usr/local/zookeeper-3.4.10/bin/../lib/slf4j-log4j12-1.6.1.jar:/usr/local/zookeeper-3.4.10/bin/../lib/slf4j-api-1.6.1.jar:/usr/local/zookeeper-3.4.10/bin/../lib/netty-3.10.5.Final.jar:/usr/local/zookeeper-3.4.10/bin/../lib/log4j-1.2.16.jar:/usr/local/zookeeper-3.4.10/bin/../lib/jline-0.9.94.jar:/usr/local/zookeeper-3.4.10/bin/../zookeeper-3.4.10.jar:/usr/local/zookeeper-3.4.10/bin/../src/java/lib/*.jar:/usr/local/zookeeper-3.4.10/bin/../conf:.:/usr/java/jdk1.7.0_80/jre/lib/rt.jar:/usr/java/jdk1.7.0_80/lib/dt.jar:/usr/java/jdk1.7.0_80/lib/tools.jar
2018-02-06 13:54:53,977 [myid:] - INFO [main:Environment@100] - Client environment:java.library.path=/usr/java/packages/lib/amd64:/usr/lib64:/lib64:/lib:/usr/lib
2018-02-06 13:54:53,977 [myid:] - INFO [main:Environment@100] - Client environment:java.io.tmpdir=/tmp
2018-02-06 13:54:53,977 [myid:] - INFO [main:Environment@100] - Client environment:java.compiler=<NA>
2018-02-06 13:54:53,977 [myid:] - INFO [main:Environment@100] - Client environment:os.name=Linux
2018-02-06 13:54:53,978 [myid:] - INFO [main:Environment@100] - Client environment:os.arch=amd64
2018-02-06 13:54:53,978 [myid:] - INFO [main:Environment@100] - Client environment:os.version=2.6.32-358.el6.x86_64
2018-02-06 13:54:53,978 [myid:] - INFO [main:Environment@100] - Client environment:user.name=root
2018-02-06 13:54:53,978 [myid:] - INFO [main:Environment@100] - Client environment:user.home=/root
2018-02-06 13:54:53,978 [myid:] - INFO [main:Environment@100] - Client environment:user.dir=/usr/local/zookeeper-3.4.10/bin
2018-02-06 13:54:53,979 [myid:] - INFO [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2180 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@594b7042
Welcome to ZooKeeper!
2018-02-06 13:54:54,168 [myid:] - INFO [main-SendThread(localhost:2180):ClientCnxn$SendThread@1032] - Opening socket connection to server localhost/0:0:0:0:0:0:0:1:2180. Will not attempt to authenticate using SASL (unknown error)
JLine support is enabled
2018-02-06 13:54:54,818 [myid:] - INFO [main-SendThread(localhost:2180):ClientCnxn$SendThread@876] - Socket connection established to localhost/0:0:0:0:0:0:0:1:2180, initiating session
2018-02-06 13:54:55,331 [myid:] - INFO [main-SendThread(localhost:2180):ClientCnxn$SendThread@1299] - Session establishment complete on server localhost/0:0:0:0:0:0:0:1:2180, sessionid = 0x16168f11c140000, negotiated timeout = 30000
WATCHER::
WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2180(CONNECTED) 0]
由上面日志可以看出还没有创建节点目录,下面我们使用create /service /CEMS_SERVICE添加一个根节点目录/CEMS_SERVICE
使用 get /service查看是否添加成功
# 添加一个根节点目录/CEMS_SERVICE
[zk: localhost:2180(CONNECTED) 0] create /service CEMS_SERVICE
Created /service
# 查看添加的节点
[zk: localhost:2180(CONNECTED) 1] get /service
CEMS_SERVICE
cZxid = 0x2
ctime = Tue Feb 06 14:07:41 CST 2018
mZxid = 0x2
mtime = Tue Feb 06 14:07:41 CST 2018
pZxid = 0x2
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 12
numChildren = 0
[zk: localhost:2180(CONNECTED) 2]
说明单机配置成功
1.2java代码测试
package com.vrv.linkdood_zookeeper;
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
/**
* <B>说 明<B/>:
*
* @author 作者名:冯龙淼
* E-mail:fenglongmiao@vrvmail.com.cn
*
* @version 版 本 号:1.0.<br/>
* 创建时间:2018年2月8日 下午5:21:21
*/
public class ZKSingleTest {
// 会话超时时间,设置为与系统默认时间一致
private static final int SESSION_TIMEOUT = 30 * 1000;
// 创建 ZooKeeper 实例
private ZooKeeper zk;
// 创建 Watcher 实例
private Watcher wh = new Watcher() {
/**
* Watched事件
*/
public void process(WatchedEvent event) {
System.out.println("WatchedEvent >>> " + event.toString());
}
};
// 初始化 ZooKeeper 实例
private void createZKInstance() throws IOException {
// 连接到ZK服务,多个可以用逗号分割写
zk = new ZooKeeper("192.168.133.149:2180", SESSION_TIMEOUT, wh);
}
private void ZKOperations() throws IOException, InterruptedException, KeeperException {
System.out.println("\n1. 创建 ZooKeeper 节点 (znode : zoo2, 数据: myData2 ,权限: OPEN_ACL_UNSAFE ,节点类型: Persistent");
zk.create("/zoo2", "myData2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("\n2. 查看是否创建成功: ");
System.out.println(new String(zk.getData("/zoo2", this.wh, null)));// 添加Watch
// 前面一行我们添加了对/zoo2节点的监视,所以这里对/zoo2进行修改的时候,会触发Watch事件。
System.out.println("\n3. 修改节点数据 ");
zk.setData("/zoo2", "shanhy20160310".getBytes(), -1);
// 这里再次进行修改,则不会触发Watch事件,这就是我们验证ZK的一个特性“一次性触发”,也就是说设置一次监视,只会对下次操作起一次作用。
System.out.println("\n3-1. 再次修改节点数据 ");
zk.setData("/zoo2", "shanhy20160310-ABCD".getBytes(), -1);
System.out.println("\n4. 查看是否修改成功: ");
System.out.println(new String(zk.getData("/zoo2", false, null)));
System.out.println("\n5. 删除节点 ");
zk.delete("/zoo2", -1);
System.out.println("\n6. 查看节点是否被删除: ");
System.out.println(" 节点状态: [" + zk.exists("/zoo2", false) + "]");
}
private void ZKClose() throws InterruptedException {
zk.close();
}
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
ZKSingleTest dm = new ZKSingleTest();
dm.createZKInstance();
dm.ZKOperations();
dm.ZKClose();
}
}
单机版测试结果:
[18/02/08 17:56:48:854][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
[18/02/08 17:56:48:857][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:host.name=PC-201707281439
[18/02/08 17:56:48:857][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.version=1.7.0_60
[18/02/08 17:56:48:857][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.vendor=Oracle Corporation
[18/02/08 17:56:48:860][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.home=D:\Develop\Java\jdk1.7.0_60\jre
[18/02/08 17:56:48:860][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.class.path=D:\workspace_2018\linkdood-disruptor\target\classes;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-api\2.6.2\log4j-api-2.6.2.jar;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-core\2.6.2\log4j-core-2.6.2.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-context\3.2.17.RELEASE\spring-context-3.2.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-aop\4.3.6.RELEASE\spring-aop-4.3.6.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-beans\4.3.6.RELEASE\spring-beans-4.3.6.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-core\4.3.6.RELEASE\spring-core-4.3.6.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-expression\4.3.6.RELEASE\spring-expression-4.3.6.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-webmvc\3.2.17.RELEASE\spring-webmvc-3.2.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-web\4.3.6.RELEASE\spring-web-4.3.6.RELEASE.jar;C:\Users\Administrator\.m2\repository\com\lmax\disruptor\3.3.6\disruptor-3.3.6.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\core\jersey-client\2.25.1\jersey-client-2.25.1.jar;C:\Users\Administrator\.m2\repository\javax\ws\rs\javax.ws.rs-api\2.0.1\javax.ws.rs-api-2.0.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\core\jersey-common\2.25.1\jersey-common-2.25.1.jar;C:\Users\Administrator\.m2\repository\javax\annotation\javax.annotation-api\1.2\javax.annotation-api-1.2.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\bundles\repackaged\jersey-guava\2.25.1\jersey-guava-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\hk2\osgi-resource-locator\1.0.1\osgi-resource-locator-1.0.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\hk2\hk2-api\2.5.0-b32\hk2-api-2.5.0-b32.jar;C:\Users\Administrator\.m2\repository\org\glassfish\hk2\hk2-utils\2.5.0-b32\hk2-utils-2.5.0-b32.jar;C:\Users\Administrator\.m2\repository\org\glassfish\hk2\external\aopalliance-repackaged\2.5.0-b32\aopalliance-repackaged-2.5.0-b32.jar;C:\Users\Administrator\.m2\repository\org\glassfish\hk2\external\javax.inject\2.5.0-b32\javax.inject-2.5.0-b32.jar;C:\Users\Administrator\.m2\repository\org\glassfish\hk2\hk2-locator\2.5.0-b32\hk2-locator-2.5.0-b32.jar;C:\Users\Administrator\.m2\repository\org\javassist\javassist\3.21.0-GA\javassist-3.21.0-GA.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\containers\jersey-container-servlet\2.25.1\jersey-container-servlet-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\containers\jersey-container-servlet-core\2.25.1\jersey-container-servlet-core-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\core\jersey-server\2.25.1\jersey-server-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\media\jersey-media-jaxb\2.25.1\jersey-media-jaxb-2.25.1.jar;C:\Users\Administrator\.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\media\jersey-media-moxy\2.25.1\jersey-media-moxy-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\ext\jersey-entity-filtering\2.25.1\jersey-entity-filtering-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.moxy\2.6.0\org.eclipse.persistence.moxy-2.6.0.jar;C:\Users\Administrator\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.core\2.6.0\org.eclipse.persistence.core-2.6.0.jar;C:\Users\Administrator\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.asm\2.6.0\org.eclipse.persistence.asm-2.6.0.jar;C:\Users\Administrator\.m2\repository\org\glassfish\javax.json\1.0.4\javax.json-1.0.4.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\media\jersey-media-multipart\2.25.1\jersey-media-multipart-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\jvnet\mimepull\mimepull\1.9.6\mimepull-1.9.6.jar;C:\Users\Administrator\.m2\repository\javax\javaee-api\7.0\javaee-api-7.0.jar;C:\Users\Administrator\.m2\repository\com\sun\mail\javax.mail\1.5.6\javax.mail-1.5.6.jar;C:\Users\Administrator\.m2\repository\javax\activation\activation\1.1\activation-1.1.jar;C:\Users\Administrator\.m2\repository\org\apache\zookeeper\zookeeper\3.4.10\zookeeper-3.4.10.jar;C:\Users\Administrator\.m2\repository\org\slf4j\slf4j-api\1.7.22\slf4j-api-1.7.22.jar;C:\Users\Administrator\.m2\repository\org\slf4j\slf4j-log4j12\1.7.22\slf4j-log4j12-1.7.22.jar;C:\Users\Administrator\.m2\repository\log4j\log4j\1.2.16\log4j-1.2.16.jar;C:\Users\Administrator\.m2\repository\jline\jline\0.9.94\jline-0.9.94.jar;C:\Users\Administrator\.m2\repository\io\netty\netty\3.10.5.Final\netty-3.10.5.Final.jar
[18/02/08 17:56:48:861][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.library.path=D:\Develop\Java\jdk1.7.0_60\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/Develop/Java/jdk1.7.0_60/bin/../jre/bin/server;D:/Develop/Java/jdk1.7.0_60/bin/../jre/bin;D:/Develop/Java/jdk1.7.0_60/bin/../jre/lib/amd64;C:\Program Files (x86)\Common Files\NetSarang;D:\Develop\apache-tomcat-7.0.54\bin;D:\Develop\maven\bin;D:\Develop\Java\jdk1.7.0_60\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Develop\SVN\bin;E:\Python\Scripts\;E:\Python\;D:\Develop\eclipse4.3_x86_64;;.
[18/02/08 17:56:48:861][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.io.tmpdir=C:\Users\ADMINI~1\AppData\Local\Temp\
[18/02/08 17:56:48:861][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.compiler=<NA>
[18/02/08 17:56:48:861][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:os.name=Windows 7
[18/02/08 17:56:48:861][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:os.arch=amd64
[18/02/08 17:56:48:861][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:os.version=6.1
[18/02/08 17:56:48:861][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:user.name=Administrator
[18/02/08 17:56:48:862][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:user.home=C:\Users\Administrator
[18/02/08 17:56:48:862][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:user.dir=D:\workspace_2018\linkdood-disruptor
[18/02/08 17:56:48:863][ INFO][org.apache.zookeeper.ZooKeeper.<init>(ZooKeeper.java:438)] Initiating client connection, connectString=192.168.133.149:2180 sessionTimeout=30000 watcher=com.vrv.linkdood_zookeeper.ZKClusterTest$1@73a0a5f3
[18/02/08 17:56:48:869][DEBUG][org.apache.zookeeper.ClientCnxn.<clinit>(ClientCnxn.java:117)] zookeeper.disableAutoWatchReset is false
1. 创建 ZooKeeper 节点 (znode : zoo2, 数据: myData2 ,权限: OPEN_ACL_UNSAFE ,节点类型: Persistent
[18/02/08 17:56:48:901][ INFO][org.apache.zookeeper.ClientCnxn$SendThread.logStartConnect(ClientCnxn.java:1032)] Opening socket connection to server 192.168.133.149/192.168.133.149:2180. Will not attempt to authenticate using SASL (unknown error)
[18/02/08 17:56:48:904][ INFO][org.apache.zookeeper.ClientCnxn$SendThread.primeConnection(ClientCnxn.java:876)] Socket connection established to 192.168.133.149/192.168.133.149:2180, initiating session
[18/02/08 17:56:48:906][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.primeConnection(ClientCnxn.java:949)] Session establishment request sent on 192.168.133.149/192.168.133.149:2180
[18/02/08 17:56:49:041][ INFO][org.apache.zookeeper.ClientCnxn$SendThread.onConnected(ClientCnxn.java:1299)] Session establishment complete on server 192.168.133.149/192.168.133.149:2180, sessionid = 0x16168f11c140023, negotiated timeout = 30000
WatchedEvent >>> WatchedEvent state:SyncConnected type:None path:null
[18/02/08 17:56:49:048][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x16168f11c140023, packet:: clientPath:null serverPath:null finished:false header:: 1,1 replyHeader:: 1,195,0 request:: '/zoo2,#6d794461746132,v{s{31,s{'world,'anyone}}},0 response:: '/zoo2
2. 查看是否创建成功:
[18/02/08 17:56:49:054][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x16168f11c140023, packet:: clientPath:null serverPath:null finished:false header:: 2,4 replyHeader:: 2,195,0 request:: '/zoo2,T response:: #6d794461746132,s{195,195,1518082914037,1518082914037,0,0,0,0,7,0,195}
myData2
3. 修改节点数据
[18/02/08 17:56:49:057][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:766)] Got notification sessionid:0x16168f11c140023
[18/02/08 17:56:49:058][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:788)] Got WatchedEvent state:SyncConnected type:NodeDataChanged path:/zoo2 for sessionid 0x16168f11c140023
[18/02/08 17:56:49:059][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x16168f11c140023, packet:: clientPath:null serverPath:null finished:false header:: 3,5 replyHeader:: 3,196,0 request:: '/zoo2,#7368616e68793230313630333130,-1 response:: s{195,196,1518082914037,1518082914048,1,0,0,0,14,0,195}
WatchedEvent >>> WatchedEvent state:SyncConnected type:NodeDataChanged path:/zoo2
3-1. 再次修改节点数据
[18/02/08 17:56:49:071][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x16168f11c140023, packet:: clientPath:null serverPath:null finished:false header:: 4,5 replyHeader:: 4,197,0 request:: '/zoo2,#7368616e687932303136303331302d41424344,-1 response:: s{195,197,1518082914037,1518082914055,2,0,0,0,19,0,195}
4. 查看是否修改成功:
[18/02/08 17:56:49:090][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x16168f11c140023, packet:: clientPath:null serverPath:null finished:false header:: 5,4 replyHeader:: 5,197,0 request:: '/zoo2,F response:: #7368616e687932303136303331302d41424344,s{195,197,1518082914037,1518082914055,2,0,0,0,19,0,195}
shanhy20160310-ABCD
5. 删除节点
[18/02/08 17:56:49:092][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x16168f11c140023, packet:: clientPath:null serverPath:null finished:false header:: 6,2 replyHeader:: 6,198,0 request:: '/zoo2,-1 response:: null
6. 查看节点是否被删除:
[18/02/08 17:56:49:096][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x16168f11c140023, packet:: clientPath:null serverPath:null finished:false header:: 7,3 replyHeader:: 7,198,-101 request:: '/zoo2,F response::
节点状态: [null]
[18/02/08 17:56:49:099][DEBUG][org.apache.zookeeper.ZooKeeper.close(ZooKeeper.java:673)] Closing session: 0x16168f11c140023
[18/02/08 17:56:49:099][DEBUG][org.apache.zookeeper.ClientCnxn.close(ClientCnxn.java:1370)] Closing client for session: 0x16168f11c140023
[18/02/08 17:56:49:102][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x16168f11c140023, packet:: clientPath:null serverPath:null finished:false header:: 8,-11 replyHeader:: 8,199,0 request:: null response:: null
[18/02/08 17:56:49:102][DEBUG][org.apache.zookeeper.ClientCnxn.disconnect(ClientCnxn.java:1354)] Disconnecting client for session: 0x16168f11c140023
[18/02/08 17:56:49:102][ INFO][org.apache.zookeeper.ZooKeeper.close(ZooKeeper.java:684)] Session: 0x16168f11c140023 closed
二、集群版测试
192.168.133.149三个伪集群、192.168.133.148、192.168.133.158
2.1 命令测试
149状态信息
# 149 a
[root@centos6 conf]# ../bin/zkServer.sh status ./zoo_flma.cfg
ZooKeeper JMX enabled by default
Using config: ./zoo_flma.cfg
Mode: follower
# 149 b
[root@centos6 conf]# ../bin/zkServer.sh status ./zoo_flmb.cfg
ZooKeeper JMX enabled by default
Using config: ./zoo_flmb.cfg
Mode: follower
#149 c
[root@centos6 conf]# ../bin/zkServer.sh status ./zoo_flmc.cfg
ZooKeeper JMX enabled by default
Using config: ./zoo_flmc.cfg
Mode: follower
148状态信息
[root]:/usr/local/zookeeper-3.4.10/bin# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: follower
158状态信息
[root]:/usr/local/zookeeper-3.4.10/bin# ./zkServer.sh status
ZooKeeper JMX enabled by default
Using config: /usr/local/zookeeper-3.4.10/bin/../conf/zoo.cfg
Mode: leader
同步测试:
148上执行:
# 连接到zookeeper集群上的148server
[root]:/usr/local/zookeeper-3.4.10/bin# ./zkCli.sh -server 192.168.133.148:2181
# 执行create /service /CEMS_SERVICE 创建目录
[zk: 192.168.133.148:2181(CONNECTED) 0] create /service /CEMS_SERVICE
Created /service
# 查看创建的目录
[zk: 192.168.133.148:2181(CONNECTED) 1] get /service
/CEMS_SERVICE
cZxid = 0x700000002
ctime = Tue Feb 06 14:55:37 CST 2018
mZxid = 0x700000002
mtime = Tue Feb 06 14:55:37 CST 2018
pZxid = 0x700000002
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 13
numChildren = 0
[zk: 192.168.133.148:2181(CONNECTED) 2]
158上查看同步结果
查看149a的同步结果
查看149b同步结果
查看149c同步结果
五台集群数据同步正常,zookeeper集群没毛病
2.2 Java代码测试
package com.vrv.linkdood_zookeeper;
import java.io.IOException;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;
/**
* <B>说 明<B/>:
*
* @author 作者名:冯龙淼
* E-mail:fenglongmiao@vrvmail.com.cn
*
* @version 版 本 号:1.0.<br/>
* 创建时间:2018年2月8日 下午5:21:21
*/
public class ZKClusterTest {
// 会话超时时间,设置为与系统默认时间一致
private static final int SESSION_TIMEOUT = 30 * 1000;
// 创建 ZooKeeper 实例
private ZooKeeper zk;
// 创建 Watcher 实例
private Watcher wh = new Watcher() {
/**
* Watched事件
*/
public void process(WatchedEvent event) {
System.out.println("WatchedEvent >>> " + event.toString());
}
};
// 初始化 ZooKeeper 实例
private void createZKInstance() throws IOException {
// 连接到ZK服务,多个可以用逗号分割写
zk = new ZooKeeper("192.168.133.148:2181,192.168.133.148:2182,192.168.133.148:2183,192.168.133.149:2181,192.168.133.158:2181", ZKClusterTest.SESSION_TIMEOUT, this.wh);
}
private void ZKOperations() throws IOException, InterruptedException, KeeperException {
System.out.println("\n1. 创建 ZooKeeper 节点 (znode : zoo2, 数据: myData2 ,权限: OPEN_ACL_UNSAFE ,节点类型: Persistent");
zk.create("/zoo2", "myData2".getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
System.out.println("\n2. 查看是否创建成功: ");
System.out.println(new String(zk.getData("/zoo2", this.wh, null)));// 添加Watch
// 前面一行我们添加了对/zoo2节点的监视,所以这里对/zoo2进行修改的时候,会触发Watch事件。
System.out.println("\n3. 修改节点数据 ");
zk.setData("/zoo2", "shanhy20160310".getBytes(), -1);
// 这里再次进行修改,则不会触发Watch事件,这就是我们验证ZK的一个特性“一次性触发”,也就是说设置一次监视,只会对下次操作起一次作用。
System.out.println("\n3-1. 再次修改节点数据 ");
zk.setData("/zoo2", "shanhy20160310-ABCD".getBytes(), -1);
System.out.println("\n4. 查看是否修改成功: ");
System.out.println(new String(zk.getData("/zoo2", false, null)));
System.out.println("\n5. 删除节点 ");
zk.delete("/zoo2", -1);
System.out.println("\n6. 查看节点是否被删除: ");
System.out.println(" 节点状态: [" + zk.exists("/zoo2", false) + "]");
}
private void ZKClose() throws InterruptedException {
zk.close();
}
public static void main(String[] args) throws IOException, InterruptedException, KeeperException {
ZKClusterTest dm = new ZKClusterTest();
dm.createZKInstance();
dm.ZKOperations();
dm.ZKClose();
}
}
测试结果:
[18/02/08 17:51:41:071][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
[18/02/08 17:51:41:074][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:host.name=PC-201707281439
[18/02/08 17:51:41:074][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.version=1.7.0_60
[18/02/08 17:51:41:074][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.vendor=Oracle Corporation
[18/02/08 17:51:41:077][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.home=D:\Develop\Java\jdk1.7.0_60\jre
[18/02/08 17:51:41:077][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.class.path=D:\workspace_2018\linkdood-disruptor\target\classes;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-api\2.6.2\log4j-api-2.6.2.jar;C:\Users\Administrator\.m2\repository\org\apache\logging\log4j\log4j-core\2.6.2\log4j-core-2.6.2.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-context\3.2.17.RELEASE\spring-context-3.2.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-aop\4.3.6.RELEASE\spring-aop-4.3.6.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-beans\4.3.6.RELEASE\spring-beans-4.3.6.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-core\4.3.6.RELEASE\spring-core-4.3.6.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-expression\4.3.6.RELEASE\spring-expression-4.3.6.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-webmvc\3.2.17.RELEASE\spring-webmvc-3.2.17.RELEASE.jar;C:\Users\Administrator\.m2\repository\org\springframework\spring-web\4.3.6.RELEASE\spring-web-4.3.6.RELEASE.jar;C:\Users\Administrator\.m2\repository\com\lmax\disruptor\3.3.6\disruptor-3.3.6.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\core\jersey-client\2.25.1\jersey-client-2.25.1.jar;C:\Users\Administrator\.m2\repository\javax\ws\rs\javax.ws.rs-api\2.0.1\javax.ws.rs-api-2.0.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\core\jersey-common\2.25.1\jersey-common-2.25.1.jar;C:\Users\Administrator\.m2\repository\javax\annotation\javax.annotation-api\1.2\javax.annotation-api-1.2.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\bundles\repackaged\jersey-guava\2.25.1\jersey-guava-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\hk2\osgi-resource-locator\1.0.1\osgi-resource-locator-1.0.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\hk2\hk2-api\2.5.0-b32\hk2-api-2.5.0-b32.jar;C:\Users\Administrator\.m2\repository\org\glassfish\hk2\hk2-utils\2.5.0-b32\hk2-utils-2.5.0-b32.jar;C:\Users\Administrator\.m2\repository\org\glassfish\hk2\external\aopalliance-repackaged\2.5.0-b32\aopalliance-repackaged-2.5.0-b32.jar;C:\Users\Administrator\.m2\repository\org\glassfish\hk2\external\javax.inject\2.5.0-b32\javax.inject-2.5.0-b32.jar;C:\Users\Administrator\.m2\repository\org\glassfish\hk2\hk2-locator\2.5.0-b32\hk2-locator-2.5.0-b32.jar;C:\Users\Administrator\.m2\repository\org\javassist\javassist\3.21.0-GA\javassist-3.21.0-GA.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\containers\jersey-container-servlet\2.25.1\jersey-container-servlet-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\containers\jersey-container-servlet-core\2.25.1\jersey-container-servlet-core-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\core\jersey-server\2.25.1\jersey-server-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\media\jersey-media-jaxb\2.25.1\jersey-media-jaxb-2.25.1.jar;C:\Users\Administrator\.m2\repository\javax\validation\validation-api\1.1.0.Final\validation-api-1.1.0.Final.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\media\jersey-media-moxy\2.25.1\jersey-media-moxy-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\ext\jersey-entity-filtering\2.25.1\jersey-entity-filtering-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.moxy\2.6.0\org.eclipse.persistence.moxy-2.6.0.jar;C:\Users\Administrator\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.core\2.6.0\org.eclipse.persistence.core-2.6.0.jar;C:\Users\Administrator\.m2\repository\org\eclipse\persistence\org.eclipse.persistence.asm\2.6.0\org.eclipse.persistence.asm-2.6.0.jar;C:\Users\Administrator\.m2\repository\org\glassfish\javax.json\1.0.4\javax.json-1.0.4.jar;C:\Users\Administrator\.m2\repository\org\glassfish\jersey\media\jersey-media-multipart\2.25.1\jersey-media-multipart-2.25.1.jar;C:\Users\Administrator\.m2\repository\org\jvnet\mimepull\mimepull\1.9.6\mimepull-1.9.6.jar;C:\Users\Administrator\.m2\repository\javax\javaee-api\7.0\javaee-api-7.0.jar;C:\Users\Administrator\.m2\repository\com\sun\mail\javax.mail\1.5.6\javax.mail-1.5.6.jar;C:\Users\Administrator\.m2\repository\javax\activation\activation\1.1\activation-1.1.jar;C:\Users\Administrator\.m2\repository\org\apache\zookeeper\zookeeper\3.4.10\zookeeper-3.4.10.jar;C:\Users\Administrator\.m2\repository\org\slf4j\slf4j-api\1.7.22\slf4j-api-1.7.22.jar;C:\Users\Administrator\.m2\repository\org\slf4j\slf4j-log4j12\1.7.22\slf4j-log4j12-1.7.22.jar;C:\Users\Administrator\.m2\repository\log4j\log4j\1.2.16\log4j-1.2.16.jar;C:\Users\Administrator\.m2\repository\jline\jline\0.9.94\jline-0.9.94.jar;C:\Users\Administrator\.m2\repository\io\netty\netty\3.10.5.Final\netty-3.10.5.Final.jar
[18/02/08 17:51:41:078][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.library.path=D:\Develop\Java\jdk1.7.0_60\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:/Develop/Java/jdk1.7.0_60/bin/../jre/bin/server;D:/Develop/Java/jdk1.7.0_60/bin/../jre/bin;D:/Develop/Java/jdk1.7.0_60/bin/../jre/lib/amd64;C:\Program Files (x86)\Common Files\NetSarang;D:\Develop\apache-tomcat-7.0.54\bin;D:\Develop\maven\bin;D:\Develop\Java\jdk1.7.0_60\bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\Develop\SVN\bin;E:\Python\Scripts\;E:\Python\;D:\Develop\eclipse4.3_x86_64;;.
[18/02/08 17:51:41:078][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.io.tmpdir=C:\Users\ADMINI~1\AppData\Local\Temp\
[18/02/08 17:51:41:078][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:java.compiler=<NA>
[18/02/08 17:51:41:078][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:os.name=Windows 7
[18/02/08 17:51:41:078][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:os.arch=amd64
[18/02/08 17:51:41:078][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:os.version=6.1
[18/02/08 17:51:41:079][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:user.name=Administrator
[18/02/08 17:51:41:079][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:user.home=C:\Users\Administrator
[18/02/08 17:51:41:079][ INFO][org.apache.zookeeper.Environment.logEnv(Environment.java:100)] Client environment:user.dir=D:\workspace_2018\linkdood-disruptor
[18/02/08 17:51:41:081][ INFO][org.apache.zookeeper.ZooKeeper.<init>(ZooKeeper.java:438)] Initiating client connection, connectString=192.168.133.149:2181,192.168.133.149:2182,192.168.133.149:2183,192.168.133.148:2181,192.168.133.158:2181 sessionTimeout=30000 watcher=com.vrv.linkdood_zookeeper.ZKClusterTest$1@6225211b
[18/02/08 17:51:41:088][DEBUG][org.apache.zookeeper.ClientCnxn.<clinit>(ClientCnxn.java:117)] zookeeper.disableAutoWatchReset is false
1. 创建 ZooKeeper 节点 (znode : zoo2, 数据: myData2 ,权限: OPEN_ACL_UNSAFE ,节点类型: Persistent
[18/02/08 17:51:41:119][ INFO][org.apache.zookeeper.ClientCnxn$SendThread.logStartConnect(ClientCnxn.java:1032)] Opening socket connection to server 192.168.133.158/192.168.133.158:2181. Will not attempt to authenticate using SASL (unknown error)
[18/02/08 17:51:41:122][ INFO][org.apache.zookeeper.ClientCnxn$SendThread.primeConnection(ClientCnxn.java:876)] Socket connection established to 192.168.133.158/192.168.133.158:2181, initiating session
[18/02/08 17:51:41:124][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.primeConnection(ClientCnxn.java:949)] Session establishment request sent on 192.168.133.158/192.168.133.158:2181
[18/02/08 17:51:41:170][ INFO][org.apache.zookeeper.ClientCnxn$SendThread.onConnected(ClientCnxn.java:1299)] Session establishment complete on server 192.168.133.158/192.168.133.158:2181, sessionid = 0x4616cb835ff0001, negotiated timeout = 30000
WatchedEvent >>> WatchedEvent state:SyncConnected type:None path:null
[18/02/08 17:51:41:190][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x4616cb835ff0001, packet:: clientPath:null serverPath:null finished:false header:: 1,1 replyHeader:: 1,47244640294,0 request:: '/zoo2,#6d794461746132,v{s{31,s{'world,'anyone}}},0 response:: '/zoo2
2. 查看是否创建成功:
[18/02/08 17:51:41:194][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x4616cb835ff0001, packet:: clientPath:null serverPath:null finished:false header:: 2,4 replyHeader:: 2,47244640294,0 request:: '/zoo2,T response:: #6d794461746132,s{47244640294,47244640294,1518083390801,1518083390801,0,0,0,0,7,0,47244640294}
myData2
3. 修改节点数据
[18/02/08 17:51:41:217][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:766)] Got notification sessionid:0x4616cb835ff0001
[18/02/08 17:51:41:219][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:788)] Got WatchedEvent state:SyncConnected type:NodeDataChanged path:/zoo2 for sessionid 0x4616cb835ff0001
WatchedEvent >>> WatchedEvent state:SyncConnected type:NodeDataChanged path:/zoo2
[18/02/08 17:51:41:220][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x4616cb835ff0001, packet:: clientPath:null serverPath:null finished:false header:: 3,5 replyHeader:: 3,47244640295,0 request:: '/zoo2,#7368616e68793230313630333130,-1 response:: s{47244640294,47244640295,1518083390801,1518083390824,1,0,0,0,14,0,47244640294}
3-1. 再次修改节点数据
[18/02/08 17:51:41:238][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x4616cb835ff0001, packet:: clientPath:null serverPath:null finished:false header:: 4,5 replyHeader:: 4,47244640296,0 request:: '/zoo2,#7368616e687932303136303331302d41424344,-1 response:: s{47244640294,47244640296,1518083390801,1518083390848,2,0,0,0,19,0,47244640294}
4. 查看是否修改成功:
[18/02/08 17:51:41:240][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x4616cb835ff0001, packet:: clientPath:null serverPath:null finished:false header:: 5,4 replyHeader:: 5,47244640296,0 request:: '/zoo2,F response:: #7368616e687932303136303331302d41424344,s{47244640294,47244640296,1518083390801,1518083390848,2,0,0,0,19,0,47244640294}
shanhy20160310-ABCD
5. 删除节点
[18/02/08 17:51:41:267][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x4616cb835ff0001, packet:: clientPath:null serverPath:null finished:false header:: 6,2 replyHeader:: 6,47244640297,0 request:: '/zoo2,-1 response:: null
6. 查看节点是否被删除:
[18/02/08 17:51:41:270][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x4616cb835ff0001, packet:: clientPath:null serverPath:null finished:false header:: 7,3 replyHeader:: 7,47244640297,-101 request:: '/zoo2,F response::
节点状态: [null]
[18/02/08 17:51:41:272][DEBUG][org.apache.zookeeper.ZooKeeper.close(ZooKeeper.java:673)] Closing session: 0x4616cb835ff0001
[18/02/08 17:51:41:273][DEBUG][org.apache.zookeeper.ClientCnxn.close(ClientCnxn.java:1370)] Closing client for session: 0x4616cb835ff0001
[18/02/08 17:51:41:278][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.readResponse(ClientCnxn.java:843)] Reading reply sessionid:0x4616cb835ff0001, packet:: clientPath:null serverPath:null finished:false header:: 8,-11 replyHeader:: 8,47244640298,0 request:: null response:: null
[18/02/08 17:51:41:279][DEBUG][org.apache.zookeeper.ClientCnxn$SendThread.run(ClientCnxn.java:1146)] An exception was thrown while closing send thread for session 0x4616cb835ff0001 : Unable to read additional data from server sessionid 0x4616cb835ff0001, likely server has closed socket
[18/02/08 17:51:41:279][DEBUG][org.apache.zookeeper.ClientCnxn.disconnect(ClientCnxn.java:1354)] Disconnecting client for session: 0x4616cb835ff0001
[18/02/08 17:51:41:279][ INFO][org.apache.zookeeper.ZooKeeper.close(ZooKeeper.java:684)] Session: 0x4616cb835ff0001 closed
当主节点down掉从节点主动升级和接管成为主节点
停掉是leader的158 发现148由follower被选举成为了leader更多推荐
已为社区贡献3条内容
所有评论(0)