package happy; import org.apache.zookeeper.CreateMode; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooDefs.Ids; import org.apache.zookeeper.ZooKeeper; public class HappyRoom { //zookeeper集群地址 private static String connectServerList = "192.168.220.130:2181,192.168.220.131:2181,192.168.220.132:2181"; //连接超时时间 private static int sessionTimeout = 20000; //zookeeper客户端 public static ZooKeeper zk = null; //天上人间主播平台 private static final String anchor = "/anchor"; /** * 美女主播签到 */ public void singIn(String BeautyAnchorName) throws Exception{ ZooKeeper happyOnline = new ZooKeeper(connectServerList, sessionTimeout, null); //签到 happyOnline.create(anchor +"/BeautyAnchor", BeautyAnchorName.getBytes(), Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL); System.out.println("美女主播------>"+BeautyAnchorName +"<-----姗姗来迟,妩媚动人、艳惊四座...开启直播不归路......"); } /** * 直播 */ public void liveBroadcast(String BeautyAnchorName) throws InterruptedException{ System.out.println("美女主播------->"+BeautyAnchorName+"<--------"+"开始直播LOL兔女狼上单5杀...."); Thread.sleep(Long.MAX_VALUE); } /** * 开启直播 */ public static void main(String[] args) throws Exception{ HappyRoom happyRoom = new HappyRoom(); happyRoom.singIn(args[0]); happyRoom.liveBroadcast(args[0]); } }
package happy; import org.apache.zookeeper.WatchedEvent; import org.apache.zookeeper.Watcher; import org.apache.zookeeper.ZooKeeper; import java.util.ArrayList; import java.util.Collection; import java.util.List; import java.util.Random; /** * 天上人间直播VIP客户 */ public class Player { /** * * * 采帅哥的小蘑菇 * ゛男人不狠、江山不稳↘ * 百厮不得骑姐 * * 爱上 我、你会上瘾 * 朕要翻你的牌 * 你姐我Gay你咋的了 ? * * 尼爸帅得像犀利哥 * * 帅先森 * * 别动姐滴爷△ * * 社会哥 @ * * 韩国技美小璐姐 * * 哼,姐就這樣,誰都管不著 * * 男人千千万姐要不爽天天换 */ volatile List<String> BeautyAnchor = null; ArrayList<String> VIP = new ArrayList<String>(); //zookeeper集群地址 private static String connectServerList = "192.168.220.130:2181,192.168.220.131:2181,192.168.220.132:2181"; //连接超时时间 private static int sessionTimeout = 20000; //zookeeper客户端 public static ZooKeeper zk = null; //天上人间主播平台 private static final String anchor = "/anchor"; public Player(){ VIP.add("゛男人不狠、江山不稳↘"); VIP.add("百厮不得骑姐"); VIP.add("爱上我、你会上瘾"); VIP.add("朕要翻你的牌"); VIP.add("你姐我Gay你咋的了?"); VIP.add("尼爸帅得像犀利哥"); VIP.add("帅先森"); VIP.add("别动姐滴爷△"); VIP.add("社会哥@"); VIP.add("韩国技美小璐姐"); VIP.add("哼,姐就這樣,誰都管不著"); VIP.add("男人千千万姐要不爽天天换"); } /** * 查看在线美女主播 */ public void viewBeautyAnchor() throws Exception{ zk = new ZooKeeper(connectServerList, sessionTimeout, new Watcher() { public void process(WatchedEvent watchedEvent) { try { refresh(); } catch (Exception e) { e.printStackTrace(); } } }); refresh(); } /** * VIP客户打赏主播 */ public void VIPReward(){ Random random = new Random(); int n = random.nextInt(VIP.size()); String VIPName = VIP.get(n); int m = random.nextInt(BeautyAnchor.size()); String BeautyAnchorName = BeautyAnchor.get(m); System.out.println("VIP客户--> "+VIPName+" ======> 打赏主播--> "+BeautyAnchorName+" ---> 一架飞机,一辆跑车,一部游艇"); } /** * 刷新在线美女主播列表 */ public void refresh() throws Exception{ ArrayList<String> onlineBeautyAnchor = new ArrayList<String>(); List<String> AnchorList = zk.getChildren(anchor, true); for (String Anchor: AnchorList ) { byte[] BeautyAnchor = zk.getData(anchor + "/" + Anchor, false, null); String BeautyAnchorName = new String(BeautyAnchor,"GBK"); onlineBeautyAnchor.add(BeautyAnchorName); System.out.println("<---------------当前在线美女主播有----->"+BeautyAnchorName); } BeautyAnchor = onlineBeautyAnchor; System.out.println("=嗨翻世界!i===嗨翻世界!i======嗨翻世界!i==========嗨翻世界!i====嗨翻世界!i=="); } public static void main(String[] args) throws Exception { Player player = new Player(); player.viewBeautyAnchor(); player.VIPReward(); Thread.sleep(Long.MAX_VALUE); } }
启动多台服务器:
D:\>java -jar BeautyAnchor.jar 甜馨儿
2019-07-30 00:05:48,760 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
2019-07-30 00:05:48,760 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:host.name=DESKTOP-RALNSM7
2019-07-30 00:05:48,760 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.version=1.8.0_111
2019-07-30 00:05:48,761 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.vendor=Oracle Corporation
2019-07-30 00:05:48,761 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.home=D:\JAVA\jre
2019-07-30 00:05:48,761 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.class.path=BeautyAnchor.jar
2019-07-30 00:05:48,761 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.library.path=D:\JAVA\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\JAVA\bin;D:\JAVA\jre\binC:\ProgramData\Oracle\Java\javapath;D:\Hadoop_2.7.2dev\hadoop-2.7.2\bin;D:\Hadoop_2.7.2dev\hadoop-2.7.2\sbin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\devSoftware\python27;D:\devSoftware\python36\;D:\devSoftware\bin;D:\MAVEN\apache-maven-3.5.0\bin;D:\devSoftware\Scripts\;D:\devSoftware\;D:\devSoftware\python36\Scripts\;D:\osSoftware\;.
2019-07-30 00:05:48,761 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.io.tmpdir=C:\Users\lsh\AppData\Local\Temp\
2019-07-30 00:05:48,761 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.compiler=<NA>
2019-07-30 00:05:48,762 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:os.name=Windows 10
2019-07-30 00:05:48,762 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:os.arch=amd64
2019-07-30 00:05:48,762 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:os.version=10.0
2019-07-30 00:05:48,762 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:user.name=lsh
2019-07-30 00:05:48,762 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:user.home=C:\Users\lsh
2019-07-30 00:05:48,762 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:user.dir=D:\
2019-07-30 00:05:48,763 INFO [org.apache.zookeeper.ZooKeeper] - Initiating client connection, connectString=192.168.220.130:2181,192.168.220.131:2181,192.168.220.132:2181 sessionTimeout=20000 watcher=null
2019-07-30 00:05:48,865 INFO [org.apache.zookeeper.ClientCnxn] - Opening socket connection to server 192.168.220.130/192.168.220.130:2181. Will not attempt to authenticate using SASL (unknown error)
2019-07-30 00:05:48,868 INFO [org.apache.zookeeper.ClientCnxn] - Socket connection established to 192.168.220.130/192.168.220.130:2181, initiating session
2019-07-30 00:05:48,880 INFO [org.apache.zookeeper.ClientCnxn] - Session establishment complete on server 192.168.220.130/192.168.220.130:2181, sessionid = 0x16b29c2ee08000b, negotiated timeout = 20000
2019-07-30 00:05:48,882 ERROR [org.apache.zookeeper.ClientCnxn] - Error while calling watcher
java.lang.NullPointerException
at org.apache.zookeeper.ClientCnxn$EventThread.processEvent(ClientCnxn.java:530)
at org.apache.zookeeper.ClientCnxn$EventThread.run(ClientCnxn.java:505)
美女主播------>甜馨儿<-----姗姗来迟,妩媚动人、艳惊四座...开启直播不归路......
美女主播------->甜馨儿<--------开始直播LOL兔女狼上单5杀....
D:\>java -jar BeautyAnchor.jar 向未来奋斗的励志人
2019-07-30 00:08:52,252 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
2019-07-30 00:08:52,253 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:host.name=DESKTOP-RALNSM7
2019-07-30 00:08:52,253 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.version=1.8.0_111
2019-07-30 00:08:52,253 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.vendor=Oracle Corporation
2019-07-30 00:08:52,253 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.home=D:\JAVA\jre
2019-07-30 00:08:52,253 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.class.path=BeautyAnchor.jar
2019-07-30 00:08:52,253 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.library.path=D:\JAVA\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\JAVA\bin;D:\JAVA\jre\binC:\ProgramData\Oracle\Java\javapath;D:\Hadoop_2.7.2dev\hadoop-2.7.2\bin;D:\Hadoop_2.7.2dev\hadoop-2.7.2\sbin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\devSoftware\python27;D:\devSoftware\python36\;D:\devSoftware\bin;D:\MAVEN\apache-maven-3.5.0\bin;D:\devSoftware\Scripts\;D:\devSoftware\;D:\devSoftware\python36\Scripts\;D:\osSoftware\;.
2019-07-30 00:08:52,254 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.io.tmpdir=C:\Users\lsh\AppData\Local\Temp\
2019-07-30 00:08:52,254 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.compiler=<NA>
2019-07-30 00:08:52,254 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:os.name=Windows 10
2019-07-30 00:08:52,254 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:os.arch=amd64
2019-07-30 00:08:52,254 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:os.version=10.0
2019-07-30 00:08:52,254 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:user.name=lsh
2019-07-30 00:08:52,255 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:user.home=C:\Users\lsh
2019-07-30 00:08:52,255 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:user.dir=D:\
2019-07-30 00:08:52,255 INFO [org.apache.zookeeper.ZooKeeper] - Initiating client connection, connectString=192.168.220.130:2181,192.168.220.131:2181,192.168.220.132:2181 sessionTimeout=20000 watcher=null
2019-07-30 00:08:52,354 INFO [org.apache.zookeeper.ClientCnxn] - Opening socket connection to server 192.168.220.130/192.168.220.130:2181. Will not attempt to authenticate using SASL (unknown error)
2019-07-30 00:08:52,356 INFO [org.apache.zookeeper.ClientCnxn] - Socket connection established to 192.168.220.130/192.168.220.130:2181, initiating session
2019-07-30 00:08:52,364 INFO [org.apache.zookeeper.ClientCnxn] - Session establishment complete on server 192.168.220.130/192.168.220.130:2181, sessionid = 0x16b29c2ee08000c, negotiated timeout = 20000
美女主播------>向未来奋斗的励志人<-----姗姗来迟,妩媚动人、艳惊四座...开启直播不归路......
美女主播------->向未来奋斗的励志人<--------开始直播LOL兔女狼上单5杀....
2019-07-30 00:08:52,366 ERROR [org.apache.zookeeper.ClientCnxn] - Error while calling watcher
java.lang.NullPointerException
at org.apache.zookeeper.ClientCnxn$EventThread.processEvent(ClientCnxn.java:530)
at org.apache.zookeeper.ClientCnxn$EventThread.run(ClientCnxn.java:505)
D:\>java -jar BeautyAnchor.jar 恋爱请排队
2019-07-30 00:10:21,794 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:zookeeper.version=3.4.8--1, built on 02/06/2016 03:18 GMT
2019-07-30 00:10:21,794 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:host.name=DESKTOP-RALNSM7
2019-07-30 00:10:21,794 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.version=1.8.0_111
2019-07-30 00:10:21,794 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.vendor=Oracle Corporation
2019-07-30 00:10:21,794 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.home=D:\JAVA\jre
2019-07-30 00:10:21,795 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.class.path=BeautyAnchor.jar
2019-07-30 00:10:21,795 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.library.path=D:\JAVA\bin;C:\Windows\Sun\Java\bin;C:\Windows\system32;C:\Windows;D:\JAVA\bin;D:\JAVA\jre\binC:\ProgramData\Oracle\Java\javapath;D:\Hadoop_2.7.2dev\hadoop-2.7.2\bin;D:\Hadoop_2.7.2dev\hadoop-2.7.2\sbin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;D:\devSoftware\python27;D:\devSoftware\python36\;D:\devSoftware\bin;D:\MAVEN\apache-maven-3.5.0\bin;D:\devSoftware\Scripts\;D:\devSoftware\;D:\devSoftware\python36\Scripts\;D:\osSoftware\;.
2019-07-30 00:10:21,795 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.io.tmpdir=C:\Users\lsh\AppData\Local\Temp\
2019-07-30 00:10:21,795 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:java.compiler=<NA>
2019-07-30 00:10:21,795 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:os.name=Windows 10
2019-07-30 00:10:21,795 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:os.arch=amd64
2019-07-30 00:10:21,795 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:os.version=10.0
2019-07-30 00:10:21,795 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:user.name=lsh
2019-07-30 00:10:21,795 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:user.home=C:\Users\lsh
2019-07-30 00:10:21,795 INFO [org.apache.zookeeper.ZooKeeper] - Client environment:user.dir=D:\
2019-07-30 00:10:21,796 INFO [org.apache.zookeeper.ZooKeeper] - Initiating client connection, connectString=192.168.220.130:2181,192.168.220.131:2181,192.168.220.132:2181 sessionTimeout=20000 watcher=null
2019-07-30 00:10:21,894 INFO [org.apache.zookeeper.ClientCnxn] - Opening socket connection to server 192.168.220.132/192.168.220.132:2181. Will not attempt to authenticate using SASL (unknown error)
2019-07-30 00:10:21,895 INFO [org.apache.zookeeper.ClientCnxn] - Socket connection established to 192.168.220.132/192.168.220.132:2181, initiating session
2019-07-30 00:10:21,903 INFO [org.apache.zookeeper.ClientCnxn] - Session establishment complete on server 192.168.220.132/192.168.220.132:2181, sessionid = 0x36b28a6cb09000b, negotiated timeout = 20000
2019-07-30 00:10:21,905 ERROR [org.apache.zookeeper.ClientCnxn] - Error while calling watcher
java.lang.NullPointerException
at org.apache.zookeeper.ClientCnxn$EventThread.processEvent(ClientCnxn.java:530)
at org.apache.zookeeper.ClientCnxn$EventThread.run(ClientCnxn.java:505)
美女主播------>恋爱请排队<-----姗姗来迟,妩媚动人、艳惊四座...开启直播不归路......
美女主播------->恋爱请排队<--------开始直播LOL兔女狼上单5杀....
客户端:
<---------------当前在线美女主播有----->閲囧竻鍝ョ殑灏忚槕鑿�
<---------------当前在线美女主播有----->閲囧竻鍝ョ殑灏忚槕鑿�
<---------------当前在线美女主播有----->甜馨儿
=嗨翻世界!i===嗨翻世界!i======嗨翻世界!i==========嗨翻世界!i====嗨翻世界!i==
<---------------当前在线美女主播有----->甜馨儿
=嗨翻世界!i===嗨翻世界!i======嗨翻世界!i==========嗨翻世界!i====嗨翻世界!i==
VIP客户--> 社会哥@ ======> 打赏主播--> 甜馨儿 ---> 一架飞机,一辆跑车,一部游艇
<---------------当前在线美女主播有----->向未来奋斗的励志人
<---------------当前在线美女主播有----->閲囧竻鍝ョ殑灏忚槕鑿�
<---------------当前在线美女主播有----->甜馨儿
=嗨翻世界!i===嗨翻世界!i======嗨翻世界!i==========嗨翻世界!i====嗨翻世界!i==
<---------------当前在线美女主播有----->向未来奋斗的励志人
<---------------当前在线美女主播有----->无辜的黄瓜
<---------------当前在线美女主播有----->閲囧竻鍝ョ殑灏忚槕鑿�
<---------------当前在线美女主播有----->甜馨儿
=嗨翻世界!i===嗨翻世界!i======嗨翻世界!i==========嗨翻世界!i====嗨翻世界!i==
<---------------当前在线美女主播有----->向未来奋斗的励志人
<---------------当前在线美女主播有----->无辜的黄瓜
<---------------当前在线美女主播有----->恋爱请排队
<---------------当前在线美女主播有----->閲囧竻鍝ョ殑灏忚槕鑿�
<---------------当前在线美女主播有----->甜馨儿
=嗨翻世界!i===嗨翻世界!i======嗨翻世界!i==========嗨翻世界!i====嗨翻世界!i==
############无辜的黄瓜 退出后客户端马上获取到最新的主播列表 #######################
<---------------当前在线美女主播有----->向未来奋斗的励志人
<---------------当前在线美女主播有----->恋爱请排队
<---------------当前在线美女主播有----->閲囧竻鍝ョ殑灏忚槕鑿�
<---------------当前在线美女主播有----->甜馨儿
=嗨翻世界!i===嗨翻世界!i======嗨翻世界!i==========嗨翻世界!i====嗨翻世界!i==