程序不能退出,因而thread.sleep(long.max)
zk getchildren(true)相当与ls / watch 监听字节点变化,getdata(true)相当与get / watcher监听节点数据的变化,但zk只监听一次就不监听了,所以,在触发监听时也就是process()方法中再重新getchildren(true)相当与又一次新的监听,因为有节点变化就会触发process,所以循环,会持续监听
server
一启动就去zk集群注册信息,即创建ephemeral_sequence节点,因为服务器一下线节点就会消失,从而触发监听
package zkp; import org.apache.zookeeper.*; public class DistributedServer { private static final String parentNode = "/servers"; private static final String connectString = "mini01:2181,mini02:2181,mini03:2181"; private static final int sessionTimeout = 2000; private ZooKeeper zk = null; //创建到zk的客户端链接 public void getConnect() throws Exception { zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() { public void process(WatchedEvent event) { try { zk.getChildren("/", true);//持续监听,否则zk只监听一次 } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }); } //向zk集群注册服务器信息 public void registerServer(String hostname) throws Exception { String create = zk.create(parentNode + "/server", hostname.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); System.out.println(hostname + "is online.." + create); } //业务功能 public void handleBusiness(String hostname) throws InterruptedException { System.out.println(hostname + "starting working"); Thread.sleep(Long.MAX_VALUE);//保证程序不退出 } public static void main(String[] args) throws Exception { //获取zk链接 DistributedServer server = new DistributedServer(); server.getConnect(); //利用zk链接注册服务器信息 server.registerServer(args[0]); //启动业务功能 server.handleBusiness(args[0]); } }client
持续监听,触发事件打印节点信息
package zkp; import org.apache.zookeeper.KeeperException; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import java.io.IOException; import java.util.ArrayList; import java.util.List; public class DistributedClient { private static final String connectString = "mini01:2181,mini02:2181,mini03:2181"; private static final int sessionTimeout = 2000; private static final String parentNode = "/servers"; private volatile List<String> serverList;//volatile全局共享 ZooKeeper zk = null; public void getConnect() throws IOException { zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() { public void process(WatchedEvent event) { try { getServerList(); } catch (KeeperException e) { e.printStackTrace(); } catch (InterruptedException e) { e.printStackTrace(); } } }); } public void getServerList() throws KeeperException, InterruptedException { //获取服务器字节点信息,并且对副节点监听 List<String> children = zk.getChildren(parentNode, true); List<String> servers = new ArrayList<String>(); for (String child : children) { byte[] data = zk.getData(parentNode + "/" + child, false, null); servers.add(new String(data)); } serverList = servers; System.out.println(serverList); } //业务线程 public void handleBusiness() throws InterruptedException { System.out.println("client starting"); Thread.sleep(Long.MAX_VALUE); } public static void main(String[] args) throws IOException, KeeperException, InterruptedException { //获取链接 DistributedClient client = new DistributedClient(); client.getConnect(); //获取servers的字节点,从中获取服务器信息列表(并监听) client.getServerList(); client.handleBusiness(); } }
pom
<dependencies> <!-- https://mvnrepository.com/artifact/org.apache.zookeeper/zookeeper --> <dependency> <groupId>org.apache.zookeeper</groupId> <artifactId>zookeeper</artifactId> <version>3.4.11</version> </dependency> <dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>RELEASE</version> </dependency> </dependencies>quickstart