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]); } }