|
@@ -0,0 +1,55 @@
|
|
|
+package meituan;
|
|
|
+
|
|
|
+import org.apache.zookeeper.KeeperException;
|
|
|
+import org.apache.zookeeper.WatchedEvent;
|
|
|
+import org.apache.zookeeper.Watcher;
|
|
|
+import org.apache.zookeeper.ZooKeeper;
|
|
|
+import org.apache.zookeeper.data.Stat;
|
|
|
+
|
|
|
+import java.io.IOException;
|
|
|
+import java.util.ArrayList;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+public class Customers {
|
|
|
+ private static String connectString = "192.168.48.128:2181,192.168.48.129:2181,192.168.48.130:2181";
|
|
|
+ private static int sessionTimeout = 60*1000;
|
|
|
+
|
|
|
+ private ZooKeeper zooKeeper;
|
|
|
+
|
|
|
+ public void getConnect() throws IOException {
|
|
|
+ zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
|
|
|
+ public void process(WatchedEvent watchedEvent) {
|
|
|
+
|
|
|
+ try {
|
|
|
+ getShopList();
|
|
|
+ } catch (Exception e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public void getShopList() throws Exception {
|
|
|
+ List<String> shops = zooKeeper.getChildren("/meituan", true);
|
|
|
+
|
|
|
+ ArrayList<String> shopList = new ArrayList<String>();
|
|
|
+ for (String shop : shops) {
|
|
|
+ byte[] data = zooKeeper.getData("/meituan/" + shop, false, new Stat());
|
|
|
+ shopList.add(new String(data));
|
|
|
+ }
|
|
|
+ System.out.println(shopList);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void business() throws Exception {
|
|
|
+ System.out.println("客户正在浏览商家…");
|
|
|
+ System.in.read();
|
|
|
+ }
|
|
|
+
|
|
|
+ public static void main(String[] args) throws Exception {
|
|
|
+ Customers customers = new Customers();
|
|
|
+ customers.getConnect();
|
|
|
+ customers.getShopList();
|
|
|
+ customers.business();
|
|
|
+ }
|
|
|
+
|
|
|
+}
|