zookeeper事件监听

  原来有两张表,一张是公司的,一张的产品的,项目中用来查询,不需要增删改。现在增删改交给另一项目去维护,由他们变更时同步数据到zk,我们去取。很明显,这里需要一个监听器,每次他项目发起数据变更时,我方必须能实时获取。

  先看测试场景:

  第一步先打开zk服务端,第二步打开zk客户端,第三步准备好要修改的数据并执行上面这个类,第四步通过zk客户端修改数据,观察控制台日志输出。具体输出日志如下:

  zk服务端:

D:Devzookeeper-3.4.10in>call "D:DevJavajdk1.8.0_102"injava "-Dzookeeper.log.dir=D:Devzookeeper-3.4.10in.." "-Dzookeeper.root.logger=INFO,CONSOLE" -cp "D:Devzookeeper-3.4.10in..uildclasses;D:Devzookeeper-3.4.10in..uildlib*;D:Devzookeeper-3.4.10in..*;D:Devzookeeper-3.4.10in..lib*;D:Devzookeeper-3.4.10in..conf" org.apache.zookeeper.server.quorum.QuorumPeerMain "D:Devzookeeper-3.4.10in..confzoo.cfg"
2018-09-12 17:14:29,199 [myid:] - INFO  [main:QuorumPeerConfig@134] - Reading configuration from: D:Devzookeeper-3.4.10in..confzoo.cfg
2018-09-12 17:14:29,206 [myid:] - INFO  [main:DatadirCleanupManager@78] - autopurge.snapRetainCount set to 3
2018-09-12 17:14:29,207 [myid:] - INFO  [main:DatadirCleanupManager@79] - autopurge.purgeInterval set to 0
2018-09-12 17:14:29,207 [myid:] - INFO  [main:DatadirCleanupManager@101] - Purge task is not scheduled.
2018-09-12 17:14:29,209 [myid:] - WARN  [main:QuorumPeerMain@113] - Either no config or no quorum defined in config, running  in standalone mode
2018-09-12 17:14:29,276 [myid:] - INFO  [main:QuorumPeerConfig@134] - Reading configuration from: D:Devzookeeper-3.4.10in..confzoo.cfg
2018-09-12 17:14:29,277 [myid:] - INFO  [main:ZooKeeperServerMain@96] - Starting server
2018-09-12 17:14:29,304 [myid:] - INFO  [main:Environment@100] - Server environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2018-09-12 17:14:29,304 [myid:] - INFO  [main:Environment@100] - Server environment:host.name=windows10.microdone.cn
2018-09-12 17:14:29,304 [myid:] - INFO  [main:Environment@100] - Server environment:java.version=1.8.0_102
2018-09-12 17:14:29,305 [myid:] - INFO  [main:Environment@100] - Server environment:java.vendor=Oracle Corporation
2018-09-12 17:14:29,305 [myid:] - INFO  [main:Environment@100] - Server environment:java.home=D:DevJavajdk1.8.0_102jre
2018-09-12 17:14:29,305 [myid:] - INFO  [main:Environment@100] - Server environment:java.class.path=D:Devzookeeper-3.4.10in..uildclasses;D:Devzookeeper-3.4.10in..uildlib*;D:Devzookeeper-3.4.10in..zookeeper-3.4.10.jar;D:Devzookeeper-3.4.10in..libjline-0.9.94.jar;D:Devzookeeper-3.4.10in..liblog4j-1.2.16.jar;D:Devzookeeper-3.4.10in..lib
etty-3.10.5.Final.jar;D:Devzookeeper-3.4.10in..libslf4j-api-1.6.1.jar;D:Devzookeeper-3.4.10in..libslf4j-log4j12-1.6.1.jar;D:Devzookeeper-3.4.10in..conf
2018-09-12 17:14:29,305 [myid:] - INFO  [main:Environment@100] - Server environment:java.library.path=D:DevJavajdk1.8.0_102in;C:WINDOWSSunJavain;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:WindowsCCM;D:UtilityTortoiseSVNin;D:UtilityPuTTY;D:DevJavajdk1.8.0_102in;D:Devapache-maven-3.3.9in;D:Devapache-tomcat-9.0.0.M26in;D:Devoracleproduct11.2.0dbhome_1in;C:ProgramDataOracleJavajavapath;D:Devoracleinstantclient_12_2;D:Dev
odejs;C:Program FilesMySQLMySQL Utilities 1.6;D:DevGitcmd;C:WINDOWSSystem32OpenSSH;C:Program Files (x86)Windows Kits8.1Windows Performance Toolkit;C:Program FilesMicrosoft SQL Server110ToolsBinn;C:UserswulfAppDataLocalMicrosoftWindowsApps;C:UserswulfAppDataRoaming
pm;;D:UtilityFiddler;%USERPROFILE%AppDataLocalMicrosoftWindowsApps;.
2018-09-12 17:14:29,305 [myid:] - INFO  [main:Environment@100] - Server environment:java.io.tmpdir=C:UserswulfAppDataLocalTemp
2018-09-12 17:14:29,306 [myid:] - INFO  [main:Environment@100] - Server environment:java.compiler=<NA>
2018-09-12 17:14:29,307 [myid:] - INFO  [main:Environment@100] - Server environment:os.name=Windows 10
2018-09-12 17:14:29,307 [myid:] - INFO  [main:Environment@100] - Server environment:os.arch=amd64
2018-09-12 17:14:29,308 [myid:] - INFO  [main:Environment@100] - Server environment:os.version=10.0
2018-09-12 17:14:29,308 [myid:] - INFO  [main:Environment@100] - Server environment:user.name=wulf
2018-09-12 17:14:29,309 [myid:] - INFO  [main:Environment@100] - Server environment:user.home=C:Userswulf
2018-09-12 17:14:29,309 [myid:] - INFO  [main:Environment@100] - Server environment:user.dir=D:Devzookeeper-3.4.10in
2018-09-12 17:14:29,318 [myid:] - INFO  [main:ZooKeeperServer@829] - tickTime set to 2000
2018-09-12 17:14:29,319 [myid:] - INFO  [main:ZooKeeperServer@838] - minSessionTimeout set to -1
2018-09-12 17:14:29,319 [myid:] - INFO  [main:ZooKeeperServer@847] - maxSessionTimeout set to -1
2018-09-12 17:14:29,558 [myid:] - INFO  [main:NIOServerCnxnFactory@89] - binding to port 0.0.0.0/0.0.0.0:2181
2018-09-12 17:14:33,687 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@192] - Accepted socket connection from /0:0:0:0:0:0:0:1:54475
2018-09-12 17:14:33,697 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:ZooKeeperServer@942] - Client attempting to establish new session at /0:0:0:0:0:0:0:1:54475
2018-09-12 17:14:33,699 [myid:] - INFO  [SyncThread:0:FileTxnLog@203] - Creating new log file: log.6d
2018-09-12 17:14:33,712 [myid:] - INFO  [SyncThread:0:ZooKeeperServer@687] - Established session 0x165cd0f7f5b0000 with negotiated timeout 30000 for client /0:0:0:0:0:0:0:1:54475
2018-09-12 17:15:00,004 [myid:] - INFO  [SessionTracker:ZooKeeperServer@358] - Expiring session 0x165cd07fcca0000, timeout of 30000ms exceeded
2018-09-12 17:15:00,018 [myid:] - INFO  [ProcessThread(sid:0 cport:2181)::PrepRequestProcessor@486] - Processed session termination for sessionid: 0x165cd07fcca0000
2018-09-12 17:15:02,298 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@192] - Accepted socket connection from /127.0.0.1:54487
2018-09-12 17:15:02,303 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:ZooKeeperServer@942] - Client attempting to establish new session at /127.0.0.1:54487
2018-09-12 17:15:02,312 [myid:] - INFO  [SyncThread:0:ZooKeeperServer@687] - Established session 0x165cd0f7f5b0001 with negotiated timeout 4000 for client /127.0.0.1:54487
2018-09-12 17:16:26,964 [myid:] - WARN  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@373] - Exception causing close of session 0x165cd0f7f5b0000 due to java.io.IOException: 远程主机强迫关闭了一个现有的连接。
2018-09-12 17:16:26,965 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1044] - Closed socket connection for client /0:0:0:0:0:0:0:1:54475 which had sessionid 0x165cd0f7f5b0000
2018-09-12 17:16:32,008 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxnFactory@192] - Accepted socket connection from /0:0:0:0:0:0:0:1:54513
2018-09-12 17:16:32,015 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:ZooKeeperServer@942] - Client attempting to establish new session at /0:0:0:0:0:0:0:1:54513
2018-09-12 17:16:32,019 [myid:] - INFO  [SyncThread:0:ZooKeeperServer@687] - Established session 0x165cd0f7f5b0002 with negotiated timeout 30000 for client /0:0:0:0:0:0:0:1:54513
2018-09-12 17:16:54,001 [myid:] - INFO  [SessionTracker:ZooKeeperServer@358] - Expiring session 0x165cd0f7f5b0000, timeout of 30000ms exceeded
2018-09-12 17:16:54,002 [myid:] - INFO  [ProcessThread(sid:0 cport:2181)::PrepRequestProcessor@486] - Processed session termination for sessionid: 0x165cd0f7f5b0000
2018-09-12 17:17:33,963 [myid:] - WARN  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@373] - Exception causing close of session 0x165cd0f7f5b0001 due to java.io.IOException: 远程主机强迫关闭了一个现有的连接。
2018-09-12 17:17:33,964 [myid:] - INFO  [NIOServerCxn.Factory:0.0.0.0/0.0.0.0:2181:NIOServerCnxn@1044] - Closed socket connection for client /127.0.0.1:54487 which had sessionid 0x165cd0f7f5b0001
2018-09-12 17:17:38,000 [myid:] - INFO  [SessionTracker:ZooKeeperServer@358] - Expiring session 0x165cd0f7f5b0001, timeout of 4000ms exceeded
2018-09-12 17:17:38,001 [myid:] - INFO  [ProcessThread(sid:0 cport:2181)::PrepRequestProcessor@486] - Processed session termination for sessionid: 0x165cd0f7f5b0001

  zk客户端:

Connecting to localhost:2181
2018-09-12 17:16:31,710 [myid:] - INFO  [main:Environment@100] - Client environment:zookeeper.version=3.4.10-39d3a4f269333c922ed3db283be479f9deacaa0f, built on 03/23/2017 10:13 GMT
2018-09-12 17:16:31,713 [myid:] - INFO  [main:Environment@100] - Client environment:host.name=windows10.microdone.cn
2018-09-12 17:16:31,713 [myid:] - INFO  [main:Environment@100] - Client environment:java.version=1.8.0_102
2018-09-12 17:16:31,716 [myid:] - INFO  [main:Environment@100] - Client environment:java.vendor=Oracle Corporation
2018-09-12 17:16:31,716 [myid:] - INFO  [main:Environment@100] - Client environment:java.home=D:DevJavajdk1.8.0_102jre
2018-09-12 17:16:31,716 [myid:] - INFO  [main:Environment@100] - Client environment:java.class.path=D:Devzookeeper-3.4.10in..uildclasses;D:Devzookeeper-3.4.10in..uildlib*;D:Devzookeeper-3.4.10in..zookeeper-3.4.10.jar;D:Devzookeeper-3.4.10in..libjline-0.9.94.jar;D:Devzookeeper-3.4.10in..liblog4j-1.2.16.jar;D:Devzookeeper-3.4.10in..lib
etty-3.10.5.Final.jar;D:Devzookeeper-3.4.10in..libslf4j-api-1.6.1.jar;D:Devzookeeper-3.4.10in..libslf4j-log4j12-1.6.1.jar;D:Devzookeeper-3.4.10in..conf
2018-09-12 17:16:31,716 [myid:] - INFO  [main:Environment@100] - Client environment:java.library.path=D:DevJavajdk1.8.0_102in;C:WINDOWSSunJavain;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:WindowsCCM;D:UtilityTortoiseSVNin;D:UtilityPuTTY;D:DevJavajdk1.8.0_102in;D:Devapache-maven-3.3.9in;D:Devapache-tomcat-9.0.0.M26in;D:Devoracleproduct11.2.0dbhome_1in;C:ProgramDataOracleJavajavapath;D:Devoracleinstantclient_12_2;D:Dev
odejs;C:Program FilesMySQLMySQL Utilities 1.6;D:DevGitcmd;C:WINDOWSSystem32OpenSSH;C:Program Files (x86)Windows Kits8.1Windows Performance Toolkit;C:Program FilesMicrosoft SQL Server110ToolsBinn;C:UserswulfAppDataLocalMicrosoftWindowsApps;C:UserswulfAppDataRoaming
pm;;D:UtilityFiddler;%USERPROFILE%AppDataLocalMicrosoftWindowsApps;.
2018-09-12 17:16:31,716 [myid:] - INFO  [main:Environment@100] - Client environment:java.io.tmpdir=C:UserswulfAppDataLocalTemp
2018-09-12 17:16:31,716 [myid:] - INFO  [main:Environment@100] - Client environment:java.compiler=<NA>
2018-09-12 17:16:31,717 [myid:] - INFO  [main:Environment@100] - Client environment:os.name=Windows 10
2018-09-12 17:16:31,717 [myid:] - INFO  [main:Environment@100] - Client environment:os.arch=amd64
2018-09-12 17:16:31,717 [myid:] - INFO  [main:Environment@100] - Client environment:os.version=10.0
2018-09-12 17:16:31,717 [myid:] - INFO  [main:Environment@100] - Client environment:user.name=wulf
2018-09-12 17:16:31,717 [myid:] - INFO  [main:Environment@100] - Client environment:user.home=C:Userswulf
2018-09-12 17:16:31,717 [myid:] - INFO  [main:Environment@100] - Client environment:user.dir=D:Devzookeeper-3.4.10in
2018-09-12 17:16:31,720 [myid:] - INFO  [main:ZooKeeper@438] - Initiating client connection, connectString=localhost:2181 sessionTimeout=30000 watcher=org.apache.zookeeper.ZooKeeperMain$MyWatcher@799f7e29
Welcome to ZooKeeper!
2018-09-12 17:16:32,007 [myid:] - INFO  [main-SendThread(0:0:0:0:0:0:0:1:2181):ClientCnxn$SendThread@1032] - Opening socket connection to server 0:0:0:0:0:0:0:1/0:0:0:0:0:0:0:1:2181. Will not attempt to authenticate using SASL (unknown error)
2018-09-12 17:16:32,010 [myid:] - INFO  [main-SendThread(0:0:0:0:0:0:0:1:2181):ClientCnxn$SendThread@876] - Socket connection established to 0:0:0:0:0:0:0:1/0:0:0:0:0:0:0:1:2181, initiating session
JLine support is enabled
2018-09-12 17:16:32,020 [myid:] - INFO  [main-SendThread(0:0:0:0:0:0:0:1:2181):ClientCnxn$SendThread@1299] - Session establishment complete on server 0:0:0:0:0:0:0:1/0:0:0:0:0:0:0:1:2181, sessionid = 0x165cd0f7f5b0002, negotiated timeout = 30000

WATCHER::

WatchedEvent state:SyncConnected type:None path:null
[zk: localhost:2181(CONNECTED) 0] get /config/wlf/accessCompany/333333
{"mchId":"333333","mchName":"C鍏�徃","mchType":"1","spId":"AAA","key":"BBB","remark":""}
cZxid = 0x73
ctime = Wed Sep 12 17:15:02 CST 2018
mZxid = 0x73
mtime = Wed Sep 12 17:15:02 CST 2018
pZxid = 0x73
cversion = 0
dataVersion = 0
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 89
numChildren = 0
[zk: localhost:2181(CONNECTED) 1] set /config/wlf/accessCompany/333333 {"mchId":"333333","mchName":"wlf????","mchType":"2","spId":"wlf","key":"wlf","remark":""}
cZxid = 0x73
ctime = Wed Sep 12 17:15:02 CST 2018
mZxid = 0x7c
mtime = Wed Sep 12 17:17:28 CST 2018
pZxid = 0x73
cversion = 0
dataVersion = 1
aclVersion = 0
ephemeralOwner = 0x0
dataLength = 89
numChildren = 0
[zk: localhost:2181(CONNECTED) 2]

  可以看到我们改的是/config/wlf/accessCompany/333333的mchName、mchType、spId和key的值。

  控制台输出:

ERROR StatusLogger Unable to locate appender "httpClient-log" for logger config "org.asynchttpclient"
[2018-09-12 17:15:02] DEBUG ZKUtils:95 - start connect zk, zk server:127.0.0.1:2181
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/E:/Users/wulf/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.8/log4j-slf4j-impl-2.8.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/E:/Users/wulf/.m2/repository/org/slf4j/slf4j-log4j12/1.7.25/slf4j-log4j12-1.7.25.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.apache.logging.slf4j.Log4jLoggerFactory]
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:zookeeper.version=3.4.13-2d71af4dbe22557fda74f9a9b4309b15a7487f03, built on 06/29/2018 00:39 GMT
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:host.name=windows10.microdone.cn
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:java.version=1.8.0_102
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:java.vendor=Oracle Corporation
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:java.home=D:DevJavajdk1.8.0_102jre
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:java.class.path=E:workspaceWireless-Router	est-pilling	argetclasses;E:Userswulf.m2
epositoryorgspringframeworkspring-core4.3.11.RELEASEspring-core-4.3.11.RELEASE.jar;E:Userswulf.m2
epositorycommons-loggingcommons-logging1.2commons-logging-1.2.jar;E:Userswulf.m2
epositoryorgspringframeworkspring-context-support4.3.11.RELEASEspring-context-support-4.3.11.RELEASE.jar;E:Userswulf.m2
epositoryorgspringframeworkspring-beans4.3.11.RELEASEspring-beans-4.3.11.RELEASE.jar;E:Userswulf.m2
epositoryorgspringframeworkspring-context4.3.11.RELEASEspring-context-4.3.11.RELEASE.jar;E:Userswulf.m2
epositoryorgspringframeworkspring-aop4.3.11.RELEASEspring-aop-4.3.11.RELEASE.jar;E:Userswulf.m2
epositoryorgspringframeworkspring-expression4.3.11.RELEASEspring-expression-4.3.11.RELEASE.jar;E:Userswulf.m2
epositoryorgspringframeworkspring-web4.3.11.RELEASEspring-web-4.3.11.RELEASE.jar;E:Userswulf.m2
epositoryorgspringframeworkspring-webmvc4.3.11.RELEASEspring-webmvc-4.3.11.RELEASE.jar;E:Userswulf.m2
epositorycomfasterxmljacksoncorejackson-annotations2.8.5jackson-annotations-2.8.5.jar;E:Userswulf.m2
epositorycomfasterxmljacksoncorejackson-core2.8.5jackson-core-2.8.5.jar;E:Userswulf.m2
epositorycomfasterxmljacksoncorejackson-databind2.8.5jackson-databind-2.8.5.jar;E:Userswulf.m2
epositoryorgapachelogginglog4jlog4j-api2.8log4j-api-2.8.jar;E:Userswulf.m2
epositoryorgapachelogginglog4jlog4j-core2.8log4j-core-2.8.jar;E:Userswulf.m2
epositoryorgapachelogginglog4jlog4j-slf4j-impl2.8log4j-slf4j-impl-2.8.jar;E:Userswulf.m2
epositoryorgapachelogginglog4jlog4j-web2.8log4j-web-2.8.jar;E:Userswulf.m2
epositoryorgslf4jslf4j-api1.7.5slf4j-api-1.7.5.jar;E:Userswulf.m2
epositoryorgapachevelocityvelocity1.7velocity-1.7.jar;E:Userswulf.m2
epositorycommons-collectionscommons-collections3.2.1commons-collections-3.2.1.jar;E:Userswulf.m2
epositoryorgapachevelocityvelocity-tools2.0velocity-tools-2.0.jar;E:Userswulf.m2
epositorycommons-beanutilscommons-beanutils1.7.0commons-beanutils-1.7.0.jar;E:Userswulf.m2
epositorycommons-digestercommons-digester1.8commons-digester-1.8.jar;E:Userswulf.m2
epositorycommons-chaincommons-chain1.1commons-chain-1.1.jar;E:Userswulf.m2
epositorycommons-validatorcommons-validator1.3.1commons-validator-1.3.1.jar;E:Userswulf.m2
epositorydom4jdom4j1.1dom4j-1.1.jar;E:Userswulf.m2
epositoryorooro2.0.8oro-2.0.8.jar;E:Userswulf.m2
epositorysslextsslext1.2-0sslext-1.2-0.jar;E:Userswulf.m2
epositoryorgapachestrutsstruts-core1.3.8struts-core-1.3.8.jar;E:Userswulf.m2
epositoryantlrantlr2.7.2antlr-2.7.2.jar;E:Userswulf.m2
epositoryorgapachestrutsstruts-taglib1.3.8struts-taglib-1.3.8.jar;E:Userswulf.m2
epositoryorgapachestrutsstruts-tiles1.3.8struts-tiles-1.3.8.jar;E:Userswulf.m2
epositorycommons-langcommons-lang2.6commons-lang-2.6.jar;E:Userswulf.m2
epositoryorgapachezookeeperzookeeper3.4.13zookeeper-3.4.13.jar;E:Userswulf.m2
epositoryorgslf4jslf4j-log4j121.7.25slf4j-log4j12-1.7.25.jar;E:Userswulf.m2
epositorylog4jlog4j1.2.17log4j-1.2.17.jar;E:Userswulf.m2
epositoryjlinejline0.9.94jline-0.9.94.jar;E:Userswulf.m2
epositoryorgapacheyetusaudience-annotations0.5.0audience-annotations-0.5.0.jar;E:Userswulf.m2
epositoryio
etty
etty3.10.6.Final
etty-3.10.6.Final.jar;E:Userswulf.m2
epositoryjavaxservletjavax.servlet-api3.1.0javax.servlet-api-3.1.0.jar;E:Userswulf.m2
epositoryorgspringframeworkspring-test4.3.11.RELEASEspring-test-4.3.11.RELEASE.jar
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:java.library.path=D:DevJavajdk1.8.0_102in;C:WINDOWSSunJavain;C:WINDOWSsystem32;C:WINDOWS;D:/Dev/Java/jdk1.8.0_102/bin/../jre/bin/server;D:/Dev/Java/jdk1.8.0_102/bin/../jre/bin;D:/Dev/Java/jdk1.8.0_102/bin/../jre/lib/amd64;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:WindowsCCM;D:UtilityTortoiseSVNin;D:UtilityPuTTY;D:DevJavajdk1.8.0_102in;D:Devapache-maven-3.3.9in;D:Devapache-tomcat-9.0.0.M26in;D:Devoracleproduct11.2.0dbhome_1in;C:ProgramDataOracleJavajavapath;D:Devoracleinstantclient_12_2;D:Dev
odejs;C:Program FilesMySQLMySQL Utilities 1.6;D:DevGitcmd;C:WINDOWSSystem32OpenSSH;C:Program Files (x86)Windows Kits8.1Windows Performance Toolkit;C:Program FilesMicrosoft SQL Server110ToolsBinn;C:UserswulfAppDataLocalMicrosoftWindowsApps;C:UserswulfAppDataRoaming
pm;;D:UtilityFiddler;%USERPROFILE%AppDataLocalMicrosoftWindowsApps;D:Deveclipse;;.
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:java.io.tmpdir=C:UserswulfAppDataLocalTemp
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:java.compiler=<NA>
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:os.name=Windows 10
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:os.arch=amd64
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:os.version=10.0
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:user.name=wulf
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:user.home=C:Userswulf
[2018-09-12 17:15:02] INFO  ZooKeeper:100 - Client environment:user.dir=E:workspaceWireless-Router	est-pilling
[2018-09-12 17:15:02] INFO  ZooKeeper:442 - Initiating client connection, connectString=127.0.0.1:2181 sessionTimeout=3000 watcher=com.wulinfeng.test.testpilling.util.ZKUtils$1@52e6fdee
[2018-09-12 17:15:02] DEBUG ClientCnxn:120 - zookeeper.disableAutoWatchReset is false
[2018-09-12 17:15:02] INFO  ClientCnxn:1029 - Opening socket connection to server 127.0.0.1/127.0.0.1:2181. Will not attempt to authenticate using SASL (unknown error)
[2018-09-12 17:15:02] INFO  ClientCnxn:879 - Socket connection established to 127.0.0.1/127.0.0.1:2181, initiating session
[2018-09-12 17:15:02] DEBUG ClientCnxn:952 - Session establishment request sent on 127.0.0.1/127.0.0.1:2181
[2018-09-12 17:15:02] INFO  ClientCnxn:1303 - Session establishment complete on server 127.0.0.1/127.0.0.1:2181, sessionid = 0x165cd0f7f5b0001, negotiated timeout = 4000
[2018-09-12 17:15:02] INFO  ZKUtils:302 - enter handleServiceAccessCompany...
[2018-09-12 17:15:02] INFO  ZKUtils:285 - enter handelServiceAccessProduct...
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 1,3  replyHeader:: 1,111,-101  request:: '/config/wlf,F  response::  
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 2,3  replyHeader:: 2,111,0  request:: '/config,F  response:: s{7,7,1536670545037,1536670545037,0,5,0,0,0,1,108} 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 3,3  replyHeader:: 3,111,-101  request:: '/config/wlf,F  response::  
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 4,1  replyHeader:: 4,112,0  request:: '/config/wlf,,v{s{31,s{'world,'anyone}}},0  response:: '/config/wlf 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 5,1  replyHeader:: 5,113,0  request:: '/config/wlf/accessCompany,,v{s{31,s{'world,'anyone}}},0  response:: '/config/wlf/accessCompany 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 6,3  replyHeader:: 6,113,0  request:: '/config/wlf,F  response:: s{112,112,1536743702334,1536743702334,0,1,0,0,0,1,113} 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 7,3  replyHeader:: 7,113,-101  request:: '/config/wlf/accessProduct,F  response::  
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 8,1  replyHeader:: 8,114,0  request:: '/config/wlf/accessProduct,,v{s{31,s{'world,'anyone}}},0  response:: '/config/wlf/accessProduct 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 9,8  replyHeader:: 9,114,0  request:: '/config/wlf/accessProduct,T  response:: v{} 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 10,8  replyHeader:: 10,114,0  request:: '/config/wlf/accessCompany,T  response:: v{} 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 11,8  replyHeader:: 11,114,0  request:: '/,F  response:: v{'zk,'zookeeper,'config} 
[2018-09-12 17:15:02] DEBUG ZKUtils:69 - zk server: [ 127.0.0.1:2181 ]
zk path: [/] 

zk
zookeeper
config

[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 12,3  replyHeader:: 12,114,-101  request:: '/config/wlf/accessCompany/333333,F  response::  
[2018-09-12 17:15:02] DEBUG ClientCnxn:769 - Got notification sessionid:0x165cd0f7f5b0001
[2018-09-12 17:15:02] DEBUG ClientCnxn:791 - Got WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/config/wlf/accessCompany for sessionid 0x165cd0f7f5b0001
[2018-09-12 17:15:02] INFO  ZKUtils:302 - enter handleServiceAccessCompany...
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 13,1  replyHeader:: 13,115,0  request:: '/config/wlf/accessCompany/333333,#7b226d63684964223a22333333333333222c226d63684e616d65223a2243ffffffe5ffffff85ffffffacffffffe5ffffff8fffffffb8222c226d636854797065223a2231222c2273704964223a22414141222c226b6579223a22424242222c2272656d61726b223a22227d,v{s{31,s{'world,'anyone}}},0  response:: '/config/wlf/accessCompany/333333 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 14,8  replyHeader:: 14,115,0  request:: '/config/wlf/accessCompany,T  response:: v{'333333} 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 15,3  replyHeader:: 15,115,-101  request:: '/config/wlf/accessCompany/111111,F  response::  
[2018-09-12 17:15:02] DEBUG ClientCnxn:769 - Got notification sessionid:0x165cd0f7f5b0001
[2018-09-12 17:15:02] DEBUG ClientCnxn:791 - Got WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/config/wlf/accessCompany for sessionid 0x165cd0f7f5b0001
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 16,1  replyHeader:: 16,116,0  request:: '/config/wlf/accessCompany/111111,#7b226d63684964223a22313131313131222c226d63684e616d65223a2241ffffffe5ffffff85ffffffacffffffe5ffffff8fffffffb8222c226d636854797065223a2231222c2273704964223a22414141222c226b6579223a22424242222c2272656d61726b223a22227d,v{s{31,s{'world,'anyone}}},0  response:: '/config/wlf/accessCompany/111111 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 17,3  replyHeader:: 17,116,-101  request:: '/config/wlf/accessCompany/222222,F  response::  
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 18,1  replyHeader:: 18,117,0  request:: '/config/wlf/accessCompany/222222,#7b226d63684964223a22323232323232222c226d63684e616d65223a2242ffffffe5ffffff85ffffffacffffffe5ffffff8fffffffb8222c226d636854797065223a2231222c2273704964223a22414141222c226b6579223a22424242222c2272656d61726b223a22227d,v{s{31,s{'world,'anyone}}},0  response:: '/config/wlf/accessCompany/222222 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 19,4  replyHeader:: 19,117,0  request:: '/config/wlf/accessCompany/333333,T  response:: #7b226d63684964223a22333333333333222c226d63684e616d65223a2243ffffffe5ffffff85ffffffacffffffe5ffffff8fffffffb8222c226d636854797065223a2231222c2273704964223a22414141222c226b6579223a22424242222c2272656d61726b223a22227d,s{115,115,1536743702395,1536743702395,0,0,0,0,89,0,115} 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 20,3  replyHeader:: 20,117,-101  request:: '/config/wlf/accessProduct/1111111,F  response::  
[2018-09-12 17:15:02] DEBUG ClientCnxn:769 - Got notification sessionid:0x165cd0f7f5b0001
[2018-09-12 17:15:02] DEBUG ClientCnxn:791 - Got WatchedEvent state:SyncConnected type:NodeChildrenChanged path:/config/wlf/accessProduct for sessionid 0x165cd0f7f5b0001
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 21,1  replyHeader:: 21,118,0  request:: '/config/wlf/accessProduct/1111111,#7b2270726f647563744964223a2231313131313131222c226d63684964223a22313131313131222c2270726f6475637454797065223a2232222c227370536572766963654964223a22222c2263616d706169676e4964223a22222c227469636b65744964223a2231313131313131222c22737461727454696d65223a22323031382d30342d323020383a30303a3030222c22656e6454696d65223a22323031382d30362d33302032333a35393a3539222c22697342616c616e6365223a2230227d,v{s{31,s{'world,'anyone}}},0  response:: '/config/wlf/accessProduct/1111111 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 22,3  replyHeader:: 22,118,-101  request:: '/config/wlf/accessProduct/333333333,F  response::  
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 23,1  replyHeader:: 23,119,0  request:: '/config/wlf/accessProduct/333333333,#7b2270726f647563744964223a22333333333333333333222c226d63684964223a22313131313131222c2270726f6475637454797065223a2231222c227370536572766963654964223a22222c2263616d706169676e4964223a22222c227469636b65744964223a22222c22737461727454696d65223a22323031382d30382d30332030303a30303a3030222c22656e6454696d65223a22323031392d30382d30322032333a35393a3539222c22697342616c616e6365223a2231227d,v{s{31,s{'world,'anyone}}},0  response:: '/config/wlf/accessProduct/333333333 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 24,3  replyHeader:: 24,119,-101  request:: '/config/wlf/accessProduct/2222222,F  response::  
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 25,1  replyHeader:: 25,120,0  request:: '/config/wlf/accessProduct/2222222,#7b2270726f647563744964223a2232323232323232222c226d63684964223a22323232323232222c2270726f6475637454797065223a2231222c227370536572766963654964223a22222c2263616d706169676e4964223a22222c227469636b65744964223a22222c22737461727454696d65223a22323031382d30342d32302031323a32363a3434222c22656e6454696d65223a22323031382d30362d32302031323a32363a3434222c22697342616c616e6365223a2230227d,v{s{31,s{'world,'anyone}}},0  response:: '/config/wlf/accessProduct/2222222 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 26,3  replyHeader:: 26,120,-101  request:: '/config/wlf/accessProduct/4444444444,F  response::  
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 27,1  replyHeader:: 27,121,0  request:: '/config/wlf/accessProduct/4444444444,#7b2270726f647563744964223a2234343434343434343434222c226d63684964223a22323232323232222c2270726f6475637454797065223a2231222c227370536572766963654964223a22222c2263616d706169676e4964223a22222c227469636b65744964223a22222c22737461727454696d65223a22323031382d30382d30332030303a30303a3030222c22656e6454696d65223a22323031382d31322d33312032333a35393a3539222c22697342616c616e6365223a2230227d,v{s{31,s{'world,'anyone}}},0  response:: '/config/wlf/accessProduct/4444444444 
[2018-09-12 17:15:02] DEBUG ZKUtils:426 - newServiceAccessCompanyMap:{"333333":{"mchId":"333333","mchName":"C公司","mchType":"1","spId":"AAA","key":"BBB","remark":""}}
[2018-09-12 17:15:02] INFO  ZKUtils:285 - enter handelServiceAccessProduct...
[2018-09-12 17:15:02] INFO  ZKUtils:302 - enter handleServiceAccessCompany...
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 28,8  replyHeader:: 28,121,0  request:: '/config/wlf/accessCompany,T  response:: v{'222222,'333333,'111111} 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 29,4  replyHeader:: 29,121,0  request:: '/config/wlf/accessCompany/222222,T  response:: #7b226d63684964223a22323232323232222c226d63684e616d65223a2242ffffffe5ffffff85ffffffacffffffe5ffffff8fffffffb8222c226d636854797065223a2231222c2273704964223a22414141222c226b6579223a22424242222c2272656d61726b223a22227d,s{117,117,1536743702412,1536743702412,0,0,0,0,89,0,117} 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 30,4  replyHeader:: 30,121,0  request:: '/config/wlf/accessCompany/333333,T  response:: #7b226d63684964223a22333333333333222c226d63684e616d65223a2243ffffffe5ffffff85ffffffacffffffe5ffffff8fffffffb8222c226d636854797065223a2231222c2273704964223a22414141222c226b6579223a22424242222c2272656d61726b223a22227d,s{115,115,1536743702395,1536743702395,0,0,0,0,89,0,115} 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 31,4  replyHeader:: 31,121,0  request:: '/config/wlf/accessCompany/111111,T  response:: #7b226d63684964223a22313131313131222c226d63684e616d65223a2241ffffffe5ffffff85ffffffacffffffe5ffffff8fffffffb8222c226d636854797065223a2231222c2273704964223a22414141222c226b6579223a22424242222c2272656d61726b223a22227d,s{116,116,1536743702405,1536743702405,0,0,0,0,89,0,116} 
[2018-09-12 17:15:02] DEBUG ZKUtils:426 - newServiceAccessCompanyMap:{"333333":{"mchId":"333333","mchName":"C公司","mchType":"1","spId":"AAA","key":"BBB","remark":""},"222222":{"mchId":"222222","mchName":"B公司","mchType":"1","spId":"AAA","key":"BBB","remark":""},"111111":{"mchId":"111111","mchName":"A公司","mchType":"1","spId":"AAA","key":"BBB","remark":""}}
[2018-09-12 17:15:02] INFO  ZKUtils:285 - enter handelServiceAccessProduct...
[2018-09-12 17:15:02] INFO  ZKUtils:302 - enter handleServiceAccessCompany...
[2018-09-12 17:15:02] INFO  ZKUtils:285 - enter handelServiceAccessProduct...
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 32,8  replyHeader:: 32,121,0  request:: '/config/wlf/accessProduct,T  response:: v{'1111111,'333333333,'2222222,'4444444444} 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 33,4  replyHeader:: 33,121,0  request:: '/config/wlf/accessProduct/1111111,T  response:: #7b2270726f647563744964223a2231313131313131222c226d63684964223a22313131313131222c2270726f6475637454797065223a2232222c227370536572766963654964223a22222c2263616d706169676e4964223a22222c227469636b65744964223a2231313131313131222c22737461727454696d65223a22323031382d30342d323020383a30303a3030222c22656e6454696d65223a22323031382d30362d33302032333a35393a3539222c22697342616c616e6365223a2230227d,s{118,118,1536743702422,1536743702422,0,0,0,0,193,0,118} 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 34,4  replyHeader:: 34,121,0  request:: '/config/wlf/accessProduct/333333333,T  response:: #7b2270726f647563744964223a22333333333333333333222c226d63684964223a22313131313131222c2270726f6475637454797065223a2231222c227370536572766963654964223a22222c2263616d706169676e4964223a22222c227469636b65744964223a22222c22737461727454696d65223a22323031382d30382d30332030303a30303a3030222c22656e6454696d65223a22323031392d30382d30322032333a35393a3539222c22697342616c616e6365223a2231227d,s{119,119,1536743702429,1536743702429,0,0,0,0,189,0,119} 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 35,4  replyHeader:: 35,121,0  request:: '/config/wlf/accessProduct/2222222,T  response:: #7b2270726f647563744964223a2232323232323232222c226d63684964223a22323232323232222c2270726f6475637454797065223a2231222c227370536572766963654964223a22222c2263616d706169676e4964223a22222c227469636b65744964223a22222c22737461727454696d65223a22323031382d30342d32302031323a32363a3434222c22656e6454696d65223a22323031382d30362d32302031323a32363a3434222c22697342616c616e6365223a2230227d,s{120,120,1536743702435,1536743702435,0,0,0,0,187,0,120} 
[2018-09-12 17:15:02] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 36,4  replyHeader:: 36,121,0  request:: '/config/wlf/accessProduct/4444444444,T  response:: #7b2270726f647563744964223a2234343434343434343434222c226d63684964223a22323232323232222c2270726f6475637454797065223a2231222c227370536572766963654964223a22222c2263616d706169676e4964223a22222c227469636b65744964223a22222c22737461727454696d65223a22323031382d30382d30332030303a30303a3030222c22656e6454696d65223a22323031382d31322d33312032333a35393a3539222c22697342616c616e6365223a2230227d,s{121,121,1536743702439,1536743702439,0,0,0,0,190,0,121} 
[2018-09-12 17:15:02] DEBUG ZKUtils:357 - newServiceAccessProductMap:{"1111111":{"productId":"1111111","mchId":"111111","productType":"2","spServiceId":"","campaignId":"","ticketId":"1111111","startTime":"2018-04-20 8:00:00","endTime":"2018-06-30 23:59:59","isBalance":"0"},"333333333":{"productId":"333333333","mchId":"111111","productType":"1","spServiceId":"","campaignId":"","ticketId":"","startTime":"2018-08-03 00:00:00","endTime":"2019-08-02 23:59:59","isBalance":"1"},"2222222":{"productId":"2222222","mchId":"222222","productType":"1","spServiceId":"","campaignId":"","ticketId":"","startTime":"2018-04-20 12:26:44","endTime":"2018-06-20 12:26:44","isBalance":"0"},"4444444444":{"productId":"4444444444","mchId":"222222","productType":"1","spServiceId":"","campaignId":"","ticketId":"","startTime":"2018-08-03 00:00:00","endTime":"2018-12-31 23:59:59","isBalance":"0"}}
[2018-09-12 17:15:03] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:05] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:06] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:07] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 2ms
[2018-09-12 17:15:09] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:10] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:11] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:13] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:14] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:15] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:17] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 2ms
[2018-09-12 17:15:18] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:19] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:21] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:22] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:23] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:25] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:26] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:27] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:29] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 2ms
[2018-09-12 17:15:30] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:31] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 2ms
[2018-09-12 17:15:33] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:34] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:35] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:37] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 2ms
[2018-09-12 17:15:38] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:39] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:41] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:42] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:43] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:45] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 2ms
[2018-09-12 17:15:46] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:47] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:49] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:50] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:51] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 2ms
[2018-09-12 17:15:53] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:54] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:55] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:57] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:58] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:15:59] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:01] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 0ms
[2018-09-12 17:16:02] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:03] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:05] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 3ms
[2018-09-12 17:16:06] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:07] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:09] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:10] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:11] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:13] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:14] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:15] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:17] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:18] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:19] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:21] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:22] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 2ms
[2018-09-12 17:16:23] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:25] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:26] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:27] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:29] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:30] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:31] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 0ms
[2018-09-12 17:16:33] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:34] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 0ms
[2018-09-12 17:16:35] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:37] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:38] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:39] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:41] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 2ms
[2018-09-12 17:16:42] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:43] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:45] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:46] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:47] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:49] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:50] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:51] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:53] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:54] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:55] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:57] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:58] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:16:59] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:01] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:02] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:03] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:05] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:06] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:07] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:09] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:10] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:11] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:13] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:14] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:15] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:17] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:18] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:19] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:21] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:22] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:23] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:25] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:26] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:27] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:28] DEBUG ClientCnxn:769 - Got notification sessionid:0x165cd0f7f5b0001
[2018-09-12 17:17:28] DEBUG ClientCnxn:791 - Got WatchedEvent state:SyncConnected type:NodeDataChanged path:/config/wlf/accessCompany/333333 for sessionid 0x165cd0f7f5b0001
[2018-09-12 17:17:28] INFO  ZKUtils:302 - enter handleServiceAccessCompany...
[2018-09-12 17:17:28] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 37,8  replyHeader:: 37,124,0  request:: '/config/wlf/accessCompany,T  response:: v{'222222,'333333,'111111} 
[2018-09-12 17:17:28] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 38,4  replyHeader:: 38,124,0  request:: '/config/wlf/accessCompany/222222,T  response:: #7b226d63684964223a22323232323232222c226d63684e616d65223a2242ffffffe5ffffff85ffffffacffffffe5ffffff8fffffffb8222c226d636854797065223a2231222c2273704964223a22414141222c226b6579223a22424242222c2272656d61726b223a22227d,s{117,117,1536743702412,1536743702412,0,0,0,0,89,0,117} 
[2018-09-12 17:17:28] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 39,4  replyHeader:: 39,124,0  request:: '/config/wlf/accessCompany/333333,T  response:: #7b226d63684964223a22333333333333222c226d63684e616d65223a22776c663f3f3f3f222c226d636854797065223a2232222c2273704964223a22776c66222c226b6579223a22776c66222c2272656d61726b223a22227d,s{115,124,1536743702395,1536743848084,1,0,0,0,89,0,115} 
[2018-09-12 17:17:28] DEBUG ClientCnxn:846 - Reading reply sessionid:0x165cd0f7f5b0001, packet:: clientPath:null serverPath:null finished:false header:: 40,4  replyHeader:: 40,124,0  request:: '/config/wlf/accessCompany/111111,T  response:: #7b226d63684964223a22313131313131222c226d63684e616d65223a2241ffffffe5ffffff85ffffffacffffffe5ffffff8fffffffb8222c226d636854797065223a2231222c2273704964223a22414141222c226b6579223a22424242222c2272656d61726b223a22227d,s{116,116,1536743702405,1536743702405,0,0,0,0,89,0,116} 
[2018-09-12 17:17:28] DEBUG ZKUtils:426 - newServiceAccessCompanyMap:{"333333":{"mchId":"333333","mchName":"wlf????","mchType":"2","spId":"wlf","key":"wlf","remark":""},"222222":{"mchId":"222222","mchName":"B公司","mchType":"1","spId":"AAA","key":"BBB","remark":""},"111111":{"mchId":"111111","mchName":"A公司","mchType":"1","spId":"AAA","key":"BBB","remark":""}}
[2018-09-12 17:17:28] INFO  ZKUtils:285 - enter handelServiceAccessProduct...
[2018-09-12 17:17:29] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:30] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:32] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms
[2018-09-12 17:17:33] DEBUG ClientCnxn:745 - Got ping response for sessionid: 0x165cd0f7f5b0001 after 1ms

  上面最后打印的newServiceAccessCompanyMap就是监听到变更后同步过来的最新数据。

  最后看代码实现:

package com.wulinfeng.test.testpilling.util;

import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.KeeperException.Code;
import org.apache.zookeeper.WatchedEvent;
import org.apache.zookeeper.Watcher;
import org.apache.zookeeper.Watcher.Event.EventType;
import org.apache.zookeeper.ZooDefs.Ids;
import org.apache.zookeeper.ZooKeeper;

import com.fasterxml.jackson.annotation.JsonInclude.Include;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wulinfeng.test.testpilling.bean.ServiceAccessCompany;
import com.wulinfeng.test.testpilling.bean.ServiceAccessProduct;

/**
 * ZK工具类
 *
 * @author wulinfeng
 * @version C10 2018年9月11日
 * @since SDP V300R003C10
 */
public class ZKUtils
{
    private static Logger LOGGER = LogManager.getLogger(ZKUtils.class);
    
    public static final int ZK_SESSION_TIMEOUT = PropertiesConfigUtil.getPropertyInt("zk.session.timeout", 3000);
    
    public static final String ZK_SERVER_URL = PropertiesConfigUtil.getProperty("zk.server.url", "127.0.0.1:2181");
    
    private static ZooKeeper zk;
    
    private static final String ACCESS_PRODUCT_PATH =
        PropertiesConfigUtil.getProperty("access.product.path", "/config/wlf/accessProduct");;
    
    private static final String ACCESS_COMPANY_PATH =
        PropertiesConfigUtil.getProperty("access.company.path", "/config/wlf/accessCompany");
    
    /**
     * 存放接入方信息的map
     */
    public static volatile Map<String, ServiceAccessCompany> serviceAccessCompanyMap;
    
    /**
     * 存放产品信息的map
     */
    public static volatile Map<String, ServiceAccessProduct> serviceAccessProductMap;
    
    private static final ObjectMapper MAPPER = new ObjectMapper();
    
    public static void main(String[] args)
    {
        // 初始化
        ZKUtils zk = new ZKUtils();
        zk.init();
        
        // 看看跟目录下有啥
        LOGGER.debug(getZKServices("/"));
        
        // 导入测试数据
        zk.importData();
        
        // 主进程休眠5分钟,方便操作后台zk,看能否监听到操作
        try
        {
            Thread.sleep(ZK_SESSION_TIMEOUT * 100);
        }
        catch (Exception e)
        {
            LOGGER.error("error :{} ", e);
        }
    }
    
    /**
     * 初始化
     *
     * @author wulinfeng
     */
    public void init()
    {
        // 忽略不存在的字段
        MAPPER.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
        MAPPER.setSerializationInclusion(Include.NON_NULL);
        LOGGER.debug(String.format("start connect zk, zk server:%s", ZK_SERVER_URL));
        connect();
    }
    
    /**
     * 导入数据
     *
     * @author wulinfeng
     */
    public void importData()
    {
        // 构造数据
        Map<String, byte[]> companys = createCompany();
        Map<String, byte[]> products = createProduct();
        
        // 批量导入zk
        try
        {
            if (companys != null && companys.size() > 0)
            {
                for (Map.Entry<String, byte[]> entry : companys.entrySet())
                {
                    String key = ACCESS_COMPANY_PATH.concat("/").concat(entry.getKey());
                    if (null == zk.exists(key, false))
                    {
                        zk.create(key, entry.getValue(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                    }
                }
            }
            
            if (products != null && products.size() > 0)
            {
                for (Map.Entry<String, byte[]> entry : products.entrySet())
                {
                    String key = ACCESS_PRODUCT_PATH.concat("/").concat(entry.getKey());
                    if (null == zk.exists(key, false))
                    {
                        zk.create(key, entry.getValue(), Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                    }
                }
            }
        }
        catch (KeeperException e)
        {
            Code code = e.code();
            if (code != Code.NODEEXISTS)
            {
                LOGGER.error("ZKUtilss.importData has error, e:{}", e);
            }
            if (code == Code.SESSIONEXPIRED || code == Code.SESSIONMOVED)
            {
                connect();
            }
        }
        catch (InterruptedException e)
        {
            LOGGER.error("ZKUtilss.importData has error, e:{}", e);
        }
    }
    
    /**
     * 连接zk服务器
     *
     * @author wulinfeng
     */
    public void connect()
    {
        try
        {
            synchronized (MAPPER)
            {
                try
                {
                    if (null != zk)
                    {
                        LOGGER.error("zk retry connect...");
                        zk.close();
                    }
                }
                catch (InterruptedException e)
                {
                    LOGGER.error("ZKUtils.connect has error, when zk close, e: {}", e);
                }
                // 连接zk并注册监听
                zk = new ZooKeeper(ZK_SERVER_URL, ZK_SESSION_TIMEOUT, new Watcher()
                {
                    // 监控所有被触发的事件
                    public void process(WatchedEvent event)
                    {
                        handel(event);
                    }
                });
            }
            
            // 监听的目录不存在,默认创建目录
            createIfAbsent();
            // 更新产品信息
            updateServiceAccessProduct();
            // 更新接入方信息
            updateServiceAccessCompany();
        }
        catch (IOException e)
        {
            LOGGER.error("ZKUtils.connect has error, connect failed, zkServerUrl: {}, e: {}", ZK_SERVER_URL, e);
        }
    }
    
    /**
     * 目录不存在则创建
     *
     * @author wulinfeng
     */
    private void createIfAbsent()
    {
        createIfAbsent(ACCESS_COMPANY_PATH);
        createIfAbsent(ACCESS_PRODUCT_PATH);
    }
    
    /**
     * 递归创建目录
     *
     * @param path
     * @author fengjiangtao
     */
    private void createIfAbsent(String path)
    {
        try
        {
            int index = path.lastIndexOf("/");
            if (index < 0)
            {
                return;
            }
            else if (index == 0)
            {
                // 根目录
                zk.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            }
            else
            {
                String sonPath = path.substring(0, index);
                if (null == zk.exists(sonPath, false))
                {
                    createIfAbsent(sonPath);
                    zk.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                }
                else if (null == zk.exists(path, false))
                {
                    zk.create(path, null, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
                }
            }
        }
        catch (KeeperException e)
        {
            Code code = e.code();
            if (code != Code.NODEEXISTS)
            {
                LOGGER.error("ZKUtilss.createIfAbsent has error, e:{}", e);
            }
            if (code == Code.SESSIONEXPIRED || code == Code.SESSIONMOVED)
            {
                connect();
            }
        }
        catch (InterruptedException e)
        {
            LOGGER.error("ZKUtilss.createIfAbsent has error, e:{}", e);
        }
    }
    
    /**
     * zk监听处理类
     *
     * @author wulinfeng
     * @param event
     */
    private void handel(WatchedEvent event)
    {
        handleServiceAccessCompany(event);
        handelServiceAccessProduct(event);
    }
    
    /**
     * 把zk节点变化的数据同步到内存中
     *
     * @author wulinfeng
     * @param event
     */
    private void handelServiceAccessProduct(WatchedEvent event)
    {
        LOGGER.info("enter handelServiceAccessProduct...");
        EventType type = event.getType();
        if (event.getPath() != null && event.getPath().startsWith(ACCESS_PRODUCT_PATH)
            && (type == EventType.NodeChildrenChanged || type == EventType.NodeDataChanged))
        {
            updateServiceAccessProduct();
        }
    }
    
    /**
     * 监听接入方事件
     *
     * @author wulinfeng
     * @param event
     */
    private void handleServiceAccessCompany(WatchedEvent event)
    {
        LOGGER.info("enter handleServiceAccessCompany...");
        EventType type = event.getType();
        if (event.getPath() != null && event.getPath().startsWith(ACCESS_COMPANY_PATH)
            && (type == EventType.NodeChildrenChanged || type == EventType.NodeDataChanged))
        {
            updateServiceAccessCompany();
        }
    }
    
    /**
     * 更新产品
     *
     * @author wulinfeng
     */
    private void updateServiceAccessProduct()
    {
        Map<String, ServiceAccessProduct> accessProductMap = getServiceAccessProduct();
        if (accessProductMap != null && accessProductMap.size() > 0)
        {
            serviceAccessProductMap = accessProductMap;
        }
    }
    
    /**
     * 获取产品数据
     *
     * @author wulinfeng
     * @return
     */
    private Map<String, ServiceAccessProduct> getServiceAccessProduct()
    {
        try
        {
            List<String> products = zk.getChildren(ACCESS_PRODUCT_PATH, true);
            if (products != null && products.size() > 0)
            {
                Map<String, ServiceAccessProduct> newServiceAccessProductMap =
                    new HashMap<String, ServiceAccessProduct>(products.size());
                for (String product : products)
                {
                    if (StringUtils.isBlank(product))
                    {
                        continue;
                    }
                    String serviceConfigPath = ACCESS_PRODUCT_PATH.concat("/").concat(product);
                    ServiceAccessProduct newServiceAccessProduct =
                        getData(serviceConfigPath, ServiceAccessProduct.class);
                    
                    if (newServiceAccessProduct != null)
                    {
                        newServiceAccessProductMap.put(newServiceAccessProduct.getProductId(), newServiceAccessProduct);
                    }
                }
                if (newServiceAccessProductMap.size() > 0)
                {
                    LOGGER.debug(String.format("newServiceAccessProductMap:%s", getJson(newServiceAccessProductMap)));
                    return newServiceAccessProductMap;
                }
            }
        }
        catch (KeeperException e)
        {
            Code code = e.code();
            if (code != Code.NONODE)
            {
                LOGGER.error("ZKUtils.getServiceAccessProduct has error, e: {}", e);
            }
            if (code == Code.SESSIONEXPIRED || code == Code.SESSIONMOVED)
            {
                connect();
            }
        }
        catch (InterruptedException e)
        {
            LOGGER.error("ZKUtils.getServiceAccessProduct has error, e: {}", e);
        }
        return null;
    }
    
    /**
     * 更新接入方
     *
     * @author wulinfeng
     */
    private void updateServiceAccessCompany()
    {
        Map<String, ServiceAccessCompany> accessCompanyMap = getServiceAccessCompany();
        if (accessCompanyMap != null && accessCompanyMap.size() > 0)
        {
            serviceAccessCompanyMap = accessCompanyMap;
        }
    }
    
    /**
     * 获取接入方数据
     *
     * @author wulinfeng
     * @return
     */
    private Map<String, ServiceAccessCompany> getServiceAccessCompany()
    {
        try
        {
            List<String> mchids = zk.getChildren(ACCESS_COMPANY_PATH, true);
            if (mchids != null && mchids.size() > 0)
            {
                Map<String, ServiceAccessCompany> newServiceAccessCompanyMap =
                    new HashMap<String, ServiceAccessCompany>(mchids.size());
                for (String mchid : mchids)
                {
                    if (StringUtils.isBlank(mchid))
                    {
                        continue;
                    }
                    String serviceConfigPath = ACCESS_COMPANY_PATH.concat("/").concat(mchid);
                    ServiceAccessCompany newServiceAccessCompany =
                        getData(serviceConfigPath, ServiceAccessCompany.class);
                    if (newServiceAccessCompany != null)
                    {
                        newServiceAccessCompanyMap.put(newServiceAccessCompany.getMchId(), newServiceAccessCompany);
                    }
                }
                if (newServiceAccessCompanyMap.size() > 0)
                {
                    LOGGER.debug(String.format("newServiceAccessCompanyMap:%s", getJson(newServiceAccessCompanyMap)));
                    return newServiceAccessCompanyMap;
                }
            }
        }
        catch (KeeperException e)
        {
            Code code = e.code();
            if (code != Code.NONODE)
            {
                LOGGER.error("ZKUtils.updateServiceAccessCompany has error, e: {}", e);
            }
            if (code == Code.SESSIONEXPIRED || code == Code.SESSIONMOVED)
            {
                connect();
            }
        }
        catch (InterruptedException e)
        {
            LOGGER.error("ZKUtils.updateServiceAccessCompany has error, e: {}", e);
        }
        return null;
    }
    
    /**
     * 对象转json
     *
     * @author wulinfeng
     * @param object
     * @return
     */
    private String getJson(Object object)
    {
        if (null == object)
        {
            return null;
        }
        try
        {
            return MAPPER.writeValueAsString(object);
        }
        catch (JsonProcessingException e)
        {
            LOGGER.error("ZKUtils.getJson has error, object: {}, e: {}", object, e);
        }
        return null;
    }
    
    /**
     * 创建zk节点
     *
     * @author wulinfeng
     * @param path
     * @param data
     * @throws KeeperException
     * @throws InterruptedException
     */
    public static void create(String path, byte[] data)
        throws KeeperException, InterruptedException
    {
        /**
         * 此处采用的是CreateMode EPHEMERAL 表示The znode will be deleted upon the client's disconnect.
         */
        zk.create(path, data, Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);
    }
    
    /**
     * 获取发布到zk上的服务
     *
     * @author wulinfeng
     * @param path
     * @return
     * @throws KeeperException
     * @throws InterruptedException
     */
    public static String getZKServices(String path)
    {
        if (path == null)
        {
            return "zk path is null!";
        }
        StringBuilder childsStr = new StringBuilder("zk server: [ ");
        childsStr.append(ZK_SERVER_URL);
        childsStr.append(" ]
");
        childsStr.append("zk path: [");
        childsStr.append(path);
        childsStr.append("] 

");
        try
        {
            List<String> list = zk.getChildren(path, false);
            if (list.isEmpty())
            {
                childsStr.append("no zk service!");
            }
            else
            {
                for (String child : list)
                {
                    childsStr.append(child);
                    childsStr.append("
");
                }
            }
        }
        catch (KeeperException e)
        {
            Code code = e.code();
            if (code != Code.NONODE)
            {
                LOGGER.error("ZKUtils.getServiceAccessProduct has error, e: {}", e);
            }
        }
        catch (InterruptedException e)
        {
            LOGGER.error("ZKUtils.getServiceAccessProduct has error, e: {}", e);
        }
        return childsStr.toString();
    }
    
    /**
     * 获取zk节点下的数据
     *
     * @author wulinfeng
     * @param znodePath
     * @param clazz
     * @return
     */
    private <T> T getData(String znodePath, Class<T> clazz)
    {
        try
        {
            byte[] data = zk.getData(znodePath, true, null);
            String jsonData = new String(data, "utf-8");
            if (clazz == String.class)
            {
                return (T)jsonData;
            }
            if (jsonData != null)
            {
                return MAPPER.readValue(jsonData, clazz);
            }
        }
        catch (KeeperException e)
        {
            Code code = e.code();
            if (code != Code.NONODE)
            {
                LOGGER.error("ZKUtils.updateDate has error, class:{}, e:{}", clazz.getName(), e);
            }
            if (code == Code.SESSIONEXPIRED || code == Code.SESSIONMOVED)
            {
                connect();
            }
        }
        catch (Exception e)
        {
            LOGGER.error("ZKUtils.updateDate has error, class:{}, e:{}", clazz.getName(), e);
        }
        
        return null;
    }
    
    /**
     * 构造测试数据
     *
     * @author wulinfeng
     * @return
     */
    private Map<String, byte[]> createCompany()
    {
        Map<String, byte[]> companyMap = new HashMap<>(3);
        ServiceAccessCompany sac = new ServiceAccessCompany();
        
        sac.setMchId("111111");
        sac.setMchName("A公司");
        sac.setMchType("1");
        sac.setSpId("AAA");
        sac.setKey("BBB");
        sac.setRemark("");
        companyMap.put(sac.getMchId(), getJson(sac).getBytes());
        
        sac.setMchId("222222");
        sac.setMchName("B公司");
        companyMap.put(sac.getMchId(), getJson(sac).getBytes());
        
        sac.setMchId("333333");
        sac.setMchName("C公司");
        companyMap.put(sac.getMchId(), getJson(sac).getBytes());
        
        return companyMap;
    }
    
    /**
     * 构造测试数据
     *
     * @author wulinfeng
     * @return
     */
    private Map<String, byte[]> createProduct()
    {
        Map<String, byte[]> productMap = new HashMap<>(4);
        ServiceAccessProduct sap = new ServiceAccessProduct();
        
        sap.setProductId("1111111");
        sap.setProductType("2");
        sap.setMchId("111111");
        sap.setCampaignId("");
        sap.setSpServiceId("");
        sap.setTicketId("1111111");
        sap.setIsBalance("0");
        sap.setStartTime("2018-04-20 8:00:00");
        sap.setEndTime("2018-06-30 23:59:59");
        productMap.put(sap.getProductId(), getJson(sap).getBytes());
        
        sap.setProductId("2222222");
        sap.setProductType("1");
        sap.setMchId("222222");
        sap.setCampaignId("");
        sap.setSpServiceId("");
        sap.setTicketId("");
        sap.setIsBalance("0");
        sap.setStartTime("2018-04-20 12:26:44");
        sap.setEndTime("2018-06-20 12:26:44");
        productMap.put(sap.getProductId(), getJson(sap).getBytes());
        
        sap.setProductId("333333333");
        sap.setMchId("111111");
        sap.setCampaignId("");
        sap.setSpServiceId("");
        sap.setIsBalance("1");
        sap.setStartTime("2018-08-03 00:00:00");
        sap.setEndTime("2019-08-02 23:59:59");
        productMap.put(sap.getProductId(), getJson(sap).getBytes());
        
        sap.setProductId("4444444444");
        sap.setMchId("222222");
        sap.setCampaignId("");
        sap.setSpServiceId("");
        sap.setIsBalance("0");
        sap.setEndTime("2018-12-31 23:59:59");
        productMap.put(sap.getProductId(), getJson(sap).getBytes());
        
        return productMap;
    }
}
原文地址:https://www.cnblogs.com/wuxun1997/p/9636135.html