Prechádzať zdrojové kódy

模拟商家上线下线通知

helloskyone 2 rokov pred
rodič
commit
f745160fcf

+ 55 - 0
zookeeper/meituan/Customers.java

@@ -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();
+    }
+    
+}

+ 37 - 0
zookeeper/meituan/ShopServer.java

@@ -0,0 +1,37 @@
+package meituan;
+
+import org.apache.zookeeper.*;
+
+import java.io.IOException;
+
+public class ShopServer {
+
+    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 zk = null;
+
+    public void getConnect() throws IOException {
+        zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
+            public void process(WatchedEvent watchedEvent) {
+
+            }
+        });
+    }
+
+    public void register(String shopName) throws Exception {
+        String create = zk.create("/meituan/shop", shopName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
+        System.out.println("【"+shopName+"】开始营业!" + create);
+    }
+
+    public void business(String shopName) throws Exception {
+        System.out.println("【"+shopName+"】正在营业中…");
+        System.in.read();
+    }
+
+    public static void main(String[] args) throws Exception {
+        ShopServer shopServer = new ShopServer();
+        shopServer.getConnect();
+        shopServer.register(args[0]);
+        shopServer.business(args[0]);
+    }
+}