ShopServer.java 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637
  1. package meituan;
  2. import org.apache.zookeeper.*;
  3. import java.io.IOException;
  4. public class ShopServer {
  5. private static String connectString = "192.168.48.128:2181,192.168.48.129:2181,192.168.48.130:2181";
  6. private static int sessionTimeout = 60*1000;
  7. private ZooKeeper zk = null;
  8. public void getConnect() throws IOException {
  9. zk = new ZooKeeper(connectString, sessionTimeout, new Watcher() {
  10. public void process(WatchedEvent watchedEvent) {
  11. }
  12. });
  13. }
  14. public void register(String shopName) throws Exception {
  15. String create = zk.create("/meituan/shop", shopName.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
  16. System.out.println("【"+shopName+"】开始营业!" + create);
  17. }
  18. public void business(String shopName) throws Exception {
  19. System.out.println("【"+shopName+"】正在营业中…");
  20. System.in.read();
  21. }
  22. public static void main(String[] args) throws Exception {
  23. ShopServer shopServer = new ShopServer();
  24. shopServer.getConnect();
  25. shopServer.register(args[0]);
  26. shopServer.business(args[0]);
  27. }
  28. }