Customers.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. package meituan;
  2. import org.apache.zookeeper.KeeperException;
  3. import org.apache.zookeeper.WatchedEvent;
  4. import org.apache.zookeeper.Watcher;
  5. import org.apache.zookeeper.ZooKeeper;
  6. import org.apache.zookeeper.data.Stat;
  7. import java.io.IOException;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10. public class Customers {
  11. private static String connectString = "192.168.48.128:2181,192.168.48.129:2181,192.168.48.130:2181";
  12. private static int sessionTimeout = 60*1000;
  13. private ZooKeeper zooKeeper;
  14. public void getConnect() throws IOException {
  15. zooKeeper = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
  16. public void process(WatchedEvent watchedEvent) {
  17. try {
  18. getShopList();
  19. } catch (Exception e) {
  20. e.printStackTrace();
  21. }
  22. }
  23. });
  24. }
  25. public void getShopList() throws Exception {
  26. List<String> shops = zooKeeper.getChildren("/meituan", true);
  27. ArrayList<String> shopList = new ArrayList<String>();
  28. for (String shop : shops) {
  29. byte[] data = zooKeeper.getData("/meituan/" + shop, false, new Stat());
  30. shopList.add(new String(data));
  31. }
  32. System.out.println(shopList);
  33. }
  34. public void business() throws Exception {
  35. System.out.println("客户正在浏览商家…");
  36. System.in.read();
  37. }
  38. public static void main(String[] args) throws Exception {
  39. Customers customers = new Customers();
  40. customers.getConnect();
  41. customers.getShopList();
  42. customers.business();
  43. }
  44. }