From ab008e8da11710061e028b5640df9568fe3a38c8 Mon Sep 17 00:00:00 2001 From: zhangshanxue Date: Mon, 4 Nov 2019 10:36:32 +0800 Subject: [PATCH] =?UTF-8?q?=E5=85=AC=E5=91=8A=E7=BC=93=E5=AD=98=20?= =?UTF-8?q?=E6=9C=8D=E5=8A=A1=E5=99=A8=E7=8A=B6=E6=80=81=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ljsd/controller/GetNoticeController.java | 69 ++++------ .../controller/GetServerListController.java | 123 ++++++++++++------ src/main/java/com/ljsd/redis/RedisKey.java | 6 + 3 files changed, 112 insertions(+), 86 deletions(-) diff --git a/src/main/java/com/ljsd/controller/GetNoticeController.java b/src/main/java/com/ljsd/controller/GetNoticeController.java index 3c61896..ead4d78 100644 --- a/src/main/java/com/ljsd/controller/GetNoticeController.java +++ b/src/main/java/com/ljsd/controller/GetNoticeController.java @@ -3,6 +3,8 @@ package com.ljsd.controller; import com.google.gson.Gson; import com.ljsd.pojo.NoticeInfo; import com.ljsd.pojo.ResMsg; +import com.ljsd.pojo.UserRecentLoginInfo; +import com.ljsd.redis.RedisKey; import com.ljsd.util.BaseGlobal; import com.ljsd.util.MD5Util; import com.ljsd.util.StringUtils; @@ -31,36 +33,27 @@ public class GetNoticeController extends HttpServlet { String sign = req.getParameter("sign"); ResMsg resMsg = new ResMsg(); resMsg.setCode(1); - try (PrintWriter out = response.getWriter();){ - if(StringUtils.checkIsEmpty(timestamp) || StringUtils.checkIsEmpty(sign)){ + try (PrintWriter out = response.getWriter();) { + if (StringUtils.checkIsEmpty(timestamp) || StringUtils.checkIsEmpty(sign)) { resMsg.setMsg("参数不能为空"); out.print(gson.toJson(resMsg)); return; } String mySign = MD5Util.encrypByMd5(timestamp + MD5Util.salt); - if(!mySign.equals(sign)){ + if (!mySign.equals(sign)) { resMsg.setMsg("签名错误"); out.print(gson.toJson(resMsg)); return; } + Map noticeInfoMap = BaseGlobal.getInstance().redisApp.hgetAll(RedisKey.NOTICE, "", NoticeInfo.class, -1, false); - List notices = BaseGlobal.getInstance().mongoDBPool.findAll("notice_info"); - - - NoticeInfo send = new NoticeInfo(); - DBObject notice = BaseGlobal.getInstance().mongoDBPool.findOne("notice_info", "1"); - if(notice!=null){ - Object content = notice.get("content"); - Object title = notice.get("title"); - send.setContent(content.toString()); - send.setTitle(title.toString()); - } + NoticeInfo send = noticeInfoMap.get("1"); Comparator comparator = (o1, o2) -> { if (o1.getStartTime() > o2.getStartTime()) { return -1; - } else if (o1.getStartTime()==o2.getStartTime()) { + } else if (o1.getStartTime() == o2.getStartTime()) { return 0; } else { return 1; @@ -71,66 +64,54 @@ public class GetNoticeController extends HttpServlet { Set remove = new HashSet<>(); long localtime = System.currentTimeMillis() / 1000; - for (DBObject dbObject:notices) { - String id = dbObject.get("_id").toString(); - if(id.equals("1")) + for (NoticeInfo noticeInfo : noticeInfoMap.values()) { + if (noticeInfo.getLeve() == 0) continue; try { - NoticeInfo noticeInfo = new NoticeInfo(); - noticeInfo.set_id(id); - Object content = dbObject.get("content"); - Object title = dbObject.get("title"); - Object start_time = dbObject.get("start_time"); - Object end_time = dbObject.get("end_time"); - noticeInfo.setContent(content.toString()); - noticeInfo.setTitle(title.toString()); - - long start = (long)start_time/1000; - long end = (long)end_time/1000; + long start = noticeInfo.getStartTime() / 1000; + long end = noticeInfo.getEndTime() / 1000; if (start > localtime) { continue; } //移除当前时间以前的 if (end < localtime) { - remove.add(id); + remove.add(noticeInfo.get_id()); continue; } - int leve = (int)dbObject.get("leve"); - if(leve==1) + int leve =noticeInfo.getLeve(); + if (leve == 1) normalList.add(noticeInfo); - else if(leve==2){ + else if (leve == 2) { highList.add(noticeInfo); } - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } } - if(highList.size()!=0){ + if (highList.size() != 0) { send = highList.last(); - }else { - if(normalList.size()!=0){ + } else { + if (normalList.size() != 0) { send = normalList.last(); } } - resMsg.setCode(0); resMsg.setCode(0); resMsg.setMsg(""); - if(send!=null){ - Map parms = new HashMap<>(); - parms.put("title",send.getTitle()); - parms.put("content",send.getContent()); + if (send != null) { + Map parms = new HashMap<>(); + parms.put("title", send.getTitle()); + parms.put("content", send.getContent()); resMsg.setParms(parms); } out.print(gson.toJson(resMsg)); - }catch (Exception e){ + } catch (Exception e) { e.printStackTrace(); } } - } diff --git a/src/main/java/com/ljsd/controller/GetServerListController.java b/src/main/java/com/ljsd/controller/GetServerListController.java index 882abaa..ab7bfcb 100644 --- a/src/main/java/com/ljsd/controller/GetServerListController.java +++ b/src/main/java/com/ljsd/controller/GetServerListController.java @@ -1,13 +1,11 @@ package com.ljsd.controller; import com.ljsd.pojo.UserRecentLoginInfo; -import com.ljsd.pojo.UserServerInfo; import com.ljsd.redis.RedisKey; import com.ljsd.util.BaseGlobal; import com.ljsd.util.StringUtils; import com.mongodb.BasicDBObject; import com.mongodb.DBObject; -import com.mongodb.util.JSON; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; @@ -18,10 +16,15 @@ import java.io.PrintWriter; import java.util.ArrayList; import java.util.List; import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; public class GetServerListController extends HttpServlet { private final static String _COLLECTION_NAME = "server_info"; private final static String _WHITE_LIST = "white_list"; + private static ConcurrentHashMap serverInfosCache = new ConcurrentHashMap<>(); + private static volatile DBObject whiteListCache = new BasicDBObject(); + private static volatile long lastRefreshTime = 0; + public GetServerListController() { super(); } @@ -30,7 +33,7 @@ public class GetServerListController extends HttpServlet { super.destroy(); } - // 服务器状态 0:不可见 1:维护 2:流畅 3:爆满 + // 服务器状态 0:不可见 -1 准备状态 1:维护 2:流畅 3拥挤 4爆满 5状态根据在线人数改变 public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setCharacterEncoding("UTF-8"); @@ -41,51 +44,63 @@ public class GetServerListController extends HttpServlet { String channel = request.getParameter("channel"); String sub_channel = request.getParameter("sub_channel"); String plat = request.getParameter("plat"); - if(StringUtils.checkIsEmpty(openId) || StringUtils.checkIsEmpty(channel) || - StringUtils.checkIsEmpty(sub_channel) || StringUtils.checkIsEmpty(plat) ){ + if (StringUtils.checkIsEmpty(openId) || StringUtils.checkIsEmpty(channel) || + StringUtils.checkIsEmpty(sub_channel) || StringUtils.checkIsEmpty(plat)) { response.sendError(400, "parm is wrong"); return; } DBObject req = new BasicDBObject(); - DBObject whiteList = BaseGlobal.getInstance().mongoDBPool.findOne(_WHITE_LIST,openId); req.put("channel", channel); req.put("sub_channel", sub_channel); req.put("plat", plat); - List serverInfoList = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, req); + List serverList = new ArrayList<>(); - for (DBObject serverInfo : serverInfoList) { - long time = Long.parseLong(serverInfo.get("open_time").toString()); - if (System.currentTimeMillis() < time) { - continue; - } - int state = Integer.parseInt(serverInfo.get("state").toString()); + long local = System.currentTimeMillis(); + if ((local - lastRefreshTime) > 60 * 1000 || serverInfosCache.isEmpty()) { + serverInfosCache.clear(); + lastRefreshTime = local; + whiteListCache = BaseGlobal.getInstance().mongoDBPool.findOne(_WHITE_LIST, openId); + List serverInfoList = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, req); + for (DBObject serverInfo : serverInfoList) { + int state = Integer.parseInt(serverInfo.get("state").toString()); + if (state == 0 || state == -1) { + continue; + } + long time = Long.parseLong(serverInfo.get("open_time").toString()); + if (System.currentTimeMillis() < time) { + continue; + } + if (whiteListCache != null && state == 1) { + state = 2; + } else { + if (state == 5) { + state = getState(serverInfo.get("server_id").toString()); + } + } + int isnew = 0; + if (serverInfo.containsField("is_new")) + isnew = Integer.parseInt(serverInfo.get("is_new").toString()); - if (state == 0 ){ - continue; - } - if (whiteList != null && state ==1){ - state = 2; - } + DBObject res = new BasicDBObject(); + res.put("name", serverInfo.get("name")); + res.put("server_id", serverInfo.get("server_id")); - int isnew = 0; - if(serverInfo.containsField("is_new")) - isnew = Integer.parseInt(serverInfo.get("is_new").toString()); - - DBObject res = new BasicDBObject(); - res.put("name", serverInfo.get("name")); - res.put("server_id", serverInfo.get("server_id")); - res.put("state", state); - res.put("open_time", serverInfo.get("open_time")); - res.put("isnew", isnew); - Object ip = serverInfo.get("ip"); - Object port = serverInfo.get("port"); - if (state ==1){ // 服务器状态为维护的话不传给前端ip端口 - ip = ""; - port = ""; + res.put("state", state); + res.put("open_time", serverInfo.get("open_time")); + res.put("isnew", isnew); + Object ip = serverInfo.get("ip"); + Object port = serverInfo.get("port"); + if (state == 1) { // 服务器状态为维护的话不传给前端ip端口 + ip = ""; + port = ""; + } + res.put("ip", ip); + res.put("port", port); + serverList.add(res); + serverInfosCache.put(serverInfo.get("server_id").toString(), res); } - res.put("ip",ip ); - res.put("port",port); - serverList.add(res); + } else { + serverList.addAll(serverInfosCache.values()); } //获取我的服务器列表 @@ -95,15 +110,15 @@ public class GetServerListController extends HttpServlet { int time = 0; for (Map.Entry entry : userServerInfoMap.entrySet()) { myServerList.add(entry.getKey()); - if(entry.getValue().getLoginTime()>time){ - myLastServer=getDBObject(entry.getKey(),entry.getValue()); + if (entry.getValue().getLoginTime() > time) { + myLastServer = getDBObject(entry.getKey(), entry.getValue()); } } DBObject resp = new BasicDBObject(); resp.put("serverList", serverList); resp.put("myServerList", myServerList); - if(null!=myLastServer) + if (null != myLastServer) resp.put("lastServer", myLastServer); out.print(resp.toString()); } catch (Exception e) { @@ -115,9 +130,9 @@ public class GetServerListController extends HttpServlet { } - private DBObject getDBObject(String serverId,UserRecentLoginInfo userRecentLoginInfo){ + private DBObject getDBObject(String serverId, UserRecentLoginInfo userRecentLoginInfo) { DBObject res = new BasicDBObject(); - res.put("serverid",serverId); + res.put("serverid", serverId); res.put("name", userRecentLoginInfo.getName()); res.put("level", userRecentLoginInfo.getLevel()); return res; @@ -127,4 +142,28 @@ public class GetServerListController extends HttpServlet { throws ServletException, IOException { this.doGet(request, response); } + + private int getState(String server_id) throws Exception { + int num = getOnlineNum(server_id); + int state = 2; + Map globalCfgMap = BaseGlobal.getInstance().redisApp.hgetAll(RedisKey.GLOBAL_SYS_PRO, "", String.class, -1, false); + int crowdnum = Integer.valueOf(globalCfgMap.getOrDefault("crowdnum", "0")); + int highnum = Integer.valueOf(globalCfgMap.getOrDefault("highnum", "0")); + if (crowdnum != 0 && highnum != 0) { + if (num >= crowdnum) { + state = 3; + if (num >= highnum) + state = 4; + } + } + return state; + } + + private int getOnlineNum(String server_id) throws Exception { + Integer num = BaseGlobal.getInstance().redisApp.get("ONLINE_NUM", server_id, Integer.class, -1); + if (num == null) { + num = 0; + } + return num; + } } diff --git a/src/main/java/com/ljsd/redis/RedisKey.java b/src/main/java/com/ljsd/redis/RedisKey.java index e5118b9..0d4dd1a 100644 --- a/src/main/java/com/ljsd/redis/RedisKey.java +++ b/src/main/java/com/ljsd/redis/RedisKey.java @@ -8,4 +8,10 @@ public interface RedisKey { * 最近登录服务器列表 */ String USER_SERVER_INFO = "JL_USER_SERVER_INFO"; + + /** + * 公告列表 + */ + String NOTICE = "NOTICE"; + String GLOBAL_SYS_PRO= "GLOBAL_SYS_PRO"; }