公告缓存 服务器状态修改

master
zhangshanxue 2019-11-04 10:36:32 +08:00
parent bf8b754487
commit ab008e8da1
3 changed files with 112 additions and 86 deletions

View File

@ -3,6 +3,8 @@ package com.ljsd.controller;
import com.google.gson.Gson; import com.google.gson.Gson;
import com.ljsd.pojo.NoticeInfo; import com.ljsd.pojo.NoticeInfo;
import com.ljsd.pojo.ResMsg; import com.ljsd.pojo.ResMsg;
import com.ljsd.pojo.UserRecentLoginInfo;
import com.ljsd.redis.RedisKey;
import com.ljsd.util.BaseGlobal; import com.ljsd.util.BaseGlobal;
import com.ljsd.util.MD5Util; import com.ljsd.util.MD5Util;
import com.ljsd.util.StringUtils; import com.ljsd.util.StringUtils;
@ -31,36 +33,27 @@ public class GetNoticeController extends HttpServlet {
String sign = req.getParameter("sign"); String sign = req.getParameter("sign");
ResMsg resMsg = new ResMsg(); ResMsg resMsg = new ResMsg();
resMsg.setCode(1); resMsg.setCode(1);
try (PrintWriter out = response.getWriter();){ try (PrintWriter out = response.getWriter();) {
if(StringUtils.checkIsEmpty(timestamp) || StringUtils.checkIsEmpty(sign)){ if (StringUtils.checkIsEmpty(timestamp) || StringUtils.checkIsEmpty(sign)) {
resMsg.setMsg("参数不能为空"); resMsg.setMsg("参数不能为空");
out.print(gson.toJson(resMsg)); out.print(gson.toJson(resMsg));
return; return;
} }
String mySign = MD5Util.encrypByMd5(timestamp + MD5Util.salt); String mySign = MD5Util.encrypByMd5(timestamp + MD5Util.salt);
if(!mySign.equals(sign)){ if (!mySign.equals(sign)) {
resMsg.setMsg("签名错误"); resMsg.setMsg("签名错误");
out.print(gson.toJson(resMsg)); out.print(gson.toJson(resMsg));
return; return;
} }
Map<String, NoticeInfo> noticeInfoMap = BaseGlobal.getInstance().redisApp.hgetAll(RedisKey.NOTICE, "", NoticeInfo.class, -1, false);
List<DBObject> notices = BaseGlobal.getInstance().mongoDBPool.findAll("notice_info"); NoticeInfo send = noticeInfoMap.get("1");
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());
}
Comparator<NoticeInfo> comparator = (o1, o2) -> { Comparator<NoticeInfo> comparator = (o1, o2) -> {
if (o1.getStartTime() > o2.getStartTime()) { if (o1.getStartTime() > o2.getStartTime()) {
return -1; return -1;
} else if (o1.getStartTime()==o2.getStartTime()) { } else if (o1.getStartTime() == o2.getStartTime()) {
return 0; return 0;
} else { } else {
return 1; return 1;
@ -71,66 +64,54 @@ public class GetNoticeController extends HttpServlet {
Set<String> remove = new HashSet<>(); Set<String> remove = new HashSet<>();
long localtime = System.currentTimeMillis() / 1000; long localtime = System.currentTimeMillis() / 1000;
for (DBObject dbObject:notices) { for (NoticeInfo noticeInfo : noticeInfoMap.values()) {
String id = dbObject.get("_id").toString(); if (noticeInfo.getLeve() == 0)
if(id.equals("1"))
continue; continue;
try { 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 = noticeInfo.getStartTime() / 1000;
long start = (long)start_time/1000; long end = noticeInfo.getEndTime() / 1000;
long end = (long)end_time/1000;
if (start > localtime) { if (start > localtime) {
continue; continue;
} }
//移除当前时间以前的 //移除当前时间以前的
if (end < localtime) { if (end < localtime) {
remove.add(id); remove.add(noticeInfo.get_id());
continue; continue;
} }
int leve = (int)dbObject.get("leve"); int leve =noticeInfo.getLeve();
if(leve==1) if (leve == 1)
normalList.add(noticeInfo); normalList.add(noticeInfo);
else if(leve==2){ else if (leve == 2) {
highList.add(noticeInfo); highList.add(noticeInfo);
} }
}catch (Exception e){ } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
if(highList.size()!=0){ if (highList.size() != 0) {
send = highList.last(); send = highList.last();
}else { } else {
if(normalList.size()!=0){ if (normalList.size() != 0) {
send = normalList.last(); send = normalList.last();
} }
} }
resMsg.setCode(0); resMsg.setCode(0);
resMsg.setCode(0); resMsg.setCode(0);
resMsg.setMsg(""); resMsg.setMsg("");
if(send!=null){ if (send != null) {
Map<String,String> parms = new HashMap<>(); Map<String, String> parms = new HashMap<>();
parms.put("title",send.getTitle()); parms.put("title", send.getTitle());
parms.put("content",send.getContent()); parms.put("content", send.getContent());
resMsg.setParms(parms); resMsg.setParms(parms);
} }
out.print(gson.toJson(resMsg)); out.print(gson.toJson(resMsg));
}catch (Exception e){ } catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
} }
} }

View File

@ -1,13 +1,11 @@
package com.ljsd.controller; package com.ljsd.controller;
import com.ljsd.pojo.UserRecentLoginInfo; import com.ljsd.pojo.UserRecentLoginInfo;
import com.ljsd.pojo.UserServerInfo;
import com.ljsd.redis.RedisKey; import com.ljsd.redis.RedisKey;
import com.ljsd.util.BaseGlobal; import com.ljsd.util.BaseGlobal;
import com.ljsd.util.StringUtils; import com.ljsd.util.StringUtils;
import com.mongodb.BasicDBObject; import com.mongodb.BasicDBObject;
import com.mongodb.DBObject; import com.mongodb.DBObject;
import com.mongodb.util.JSON;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServlet;
@ -18,10 +16,15 @@ import java.io.PrintWriter;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
public class GetServerListController extends HttpServlet { public class GetServerListController extends HttpServlet {
private final static String _COLLECTION_NAME = "server_info"; private final static String _COLLECTION_NAME = "server_info";
private final static String _WHITE_LIST = "white_list"; private final static String _WHITE_LIST = "white_list";
private static ConcurrentHashMap<String, DBObject> serverInfosCache = new ConcurrentHashMap<>();
private static volatile DBObject whiteListCache = new BasicDBObject();
private static volatile long lastRefreshTime = 0;
public GetServerListController() { public GetServerListController() {
super(); super();
} }
@ -30,7 +33,7 @@ public class GetServerListController extends HttpServlet {
super.destroy(); super.destroy();
} }
// 服务器状态 0:不可见 1:维护 2:流畅 3:爆满 // 服务器状态 0:不可见 -1 准备状态 1:维护 2:流畅 3拥挤 4爆满 5状态根据在线人数改变
public void doGet(HttpServletRequest request, HttpServletResponse response) public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { throws ServletException, IOException {
response.setCharacterEncoding("UTF-8"); response.setCharacterEncoding("UTF-8");
@ -41,51 +44,63 @@ public class GetServerListController extends HttpServlet {
String channel = request.getParameter("channel"); String channel = request.getParameter("channel");
String sub_channel = request.getParameter("sub_channel"); String sub_channel = request.getParameter("sub_channel");
String plat = request.getParameter("plat"); String plat = request.getParameter("plat");
if(StringUtils.checkIsEmpty(openId) || StringUtils.checkIsEmpty(channel) || if (StringUtils.checkIsEmpty(openId) || StringUtils.checkIsEmpty(channel) ||
StringUtils.checkIsEmpty(sub_channel) || StringUtils.checkIsEmpty(plat) ){ StringUtils.checkIsEmpty(sub_channel) || StringUtils.checkIsEmpty(plat)) {
response.sendError(400, "parm is wrong"); response.sendError(400, "parm is wrong");
return; return;
} }
DBObject req = new BasicDBObject(); DBObject req = new BasicDBObject();
DBObject whiteList = BaseGlobal.getInstance().mongoDBPool.findOne(_WHITE_LIST,openId);
req.put("channel", channel); req.put("channel", channel);
req.put("sub_channel", sub_channel); req.put("sub_channel", sub_channel);
req.put("plat", plat); req.put("plat", plat);
List<DBObject> serverInfoList = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, req);
List<DBObject> serverList = new ArrayList<>(); List<DBObject> serverList = new ArrayList<>();
for (DBObject serverInfo : serverInfoList) { long local = System.currentTimeMillis();
long time = Long.parseLong(serverInfo.get("open_time").toString()); if ((local - lastRefreshTime) > 60 * 1000 || serverInfosCache.isEmpty()) {
if (System.currentTimeMillis() < time) { serverInfosCache.clear();
continue; lastRefreshTime = local;
} whiteListCache = BaseGlobal.getInstance().mongoDBPool.findOne(_WHITE_LIST, openId);
int state = Integer.parseInt(serverInfo.get("state").toString()); List<DBObject> 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 ){ DBObject res = new BasicDBObject();
continue; res.put("name", serverInfo.get("name"));
} res.put("server_id", serverInfo.get("server_id"));
if (whiteList != null && state ==1){
state = 2;
}
int isnew = 0; res.put("state", state);
if(serverInfo.containsField("is_new")) res.put("open_time", serverInfo.get("open_time"));
isnew = Integer.parseInt(serverInfo.get("is_new").toString()); res.put("isnew", isnew);
Object ip = serverInfo.get("ip");
DBObject res = new BasicDBObject(); Object port = serverInfo.get("port");
res.put("name", serverInfo.get("name")); if (state == 1) { // 服务器状态为维护的话不传给前端ip端口
res.put("server_id", serverInfo.get("server_id")); ip = "";
res.put("state", state); port = "";
res.put("open_time", serverInfo.get("open_time")); }
res.put("isnew", isnew); res.put("ip", ip);
Object ip = serverInfo.get("ip"); res.put("port", port);
Object port = serverInfo.get("port"); serverList.add(res);
if (state ==1){ // 服务器状态为维护的话不传给前端ip端口 serverInfosCache.put(serverInfo.get("server_id").toString(), res);
ip = "";
port = "";
} }
res.put("ip",ip ); } else {
res.put("port",port); serverList.addAll(serverInfosCache.values());
serverList.add(res);
} }
//获取我的服务器列表 //获取我的服务器列表
@ -95,15 +110,15 @@ public class GetServerListController extends HttpServlet {
int time = 0; int time = 0;
for (Map.Entry<String, UserRecentLoginInfo> entry : userServerInfoMap.entrySet()) { for (Map.Entry<String, UserRecentLoginInfo> entry : userServerInfoMap.entrySet()) {
myServerList.add(entry.getKey()); myServerList.add(entry.getKey());
if(entry.getValue().getLoginTime()>time){ if (entry.getValue().getLoginTime() > time) {
myLastServer=getDBObject(entry.getKey(),entry.getValue()); myLastServer = getDBObject(entry.getKey(), entry.getValue());
} }
} }
DBObject resp = new BasicDBObject(); DBObject resp = new BasicDBObject();
resp.put("serverList", serverList); resp.put("serverList", serverList);
resp.put("myServerList", myServerList); resp.put("myServerList", myServerList);
if(null!=myLastServer) if (null != myLastServer)
resp.put("lastServer", myLastServer); resp.put("lastServer", myLastServer);
out.print(resp.toString()); out.print(resp.toString());
} catch (Exception e) { } 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(); DBObject res = new BasicDBObject();
res.put("serverid",serverId); res.put("serverid", serverId);
res.put("name", userRecentLoginInfo.getName()); res.put("name", userRecentLoginInfo.getName());
res.put("level", userRecentLoginInfo.getLevel()); res.put("level", userRecentLoginInfo.getLevel());
return res; return res;
@ -127,4 +142,28 @@ public class GetServerListController extends HttpServlet {
throws ServletException, IOException { throws ServletException, IOException {
this.doGet(request, response); this.doGet(request, response);
} }
private int getState(String server_id) throws Exception {
int num = getOnlineNum(server_id);
int state = 2;
Map<String, String> 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;
}
} }

View File

@ -8,4 +8,10 @@ public interface RedisKey {
* *
*/ */
String USER_SERVER_INFO = "JL_USER_SERVER_INFO"; String USER_SERVER_INFO = "JL_USER_SERVER_INFO";
/**
*
*/
String NOTICE = "NOTICE";
String GLOBAL_SYS_PRO= "GLOBAL_SYS_PRO";
} }