公告缓存 服务器状态修改

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;
@ -44,18 +46,9 @@ public class GetNoticeController extends HttpServlet {
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()) {
@ -71,33 +64,23 @@ 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) {
@ -114,7 +97,6 @@ public class GetNoticeController extends HttpServlet {
send = normalList.last(); send = normalList.last();
} }
} }
resMsg.setCode(0); resMsg.setCode(0);
resMsg.setCode(0); resMsg.setCode(0);
resMsg.setMsg(""); resMsg.setMsg("");
@ -132,5 +114,4 @@ public class GetNoticeController extends HttpServlet {
} }
} }

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");
@ -47,26 +50,33 @@ public class GetServerListController extends HttpServlet {
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<>();
long local = System.currentTimeMillis();
if ((local - lastRefreshTime) > 60 * 1000 || serverInfosCache.isEmpty()) {
serverInfosCache.clear();
lastRefreshTime = local;
whiteListCache = BaseGlobal.getInstance().mongoDBPool.findOne(_WHITE_LIST, openId);
List<DBObject> serverInfoList = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, req);
for (DBObject serverInfo : serverInfoList) { 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()); long time = Long.parseLong(serverInfo.get("open_time").toString());
if (System.currentTimeMillis() < time) { if (System.currentTimeMillis() < time) {
continue; continue;
} }
int state = Integer.parseInt(serverInfo.get("state").toString()); if (whiteListCache != null && state == 1) {
if (state == 0 ){
continue;
}
if (whiteList != null && state ==1){
state = 2; state = 2;
} else {
if (state == 5) {
state = getState(serverInfo.get("server_id").toString());
}
} }
int isnew = 0; int isnew = 0;
if (serverInfo.containsField("is_new")) if (serverInfo.containsField("is_new"))
isnew = Integer.parseInt(serverInfo.get("is_new").toString()); isnew = Integer.parseInt(serverInfo.get("is_new").toString());
@ -74,6 +84,7 @@ public class GetServerListController extends HttpServlet {
DBObject res = new BasicDBObject(); DBObject res = new BasicDBObject();
res.put("name", serverInfo.get("name")); res.put("name", serverInfo.get("name"));
res.put("server_id", serverInfo.get("server_id")); res.put("server_id", serverInfo.get("server_id"));
res.put("state", state); res.put("state", state);
res.put("open_time", serverInfo.get("open_time")); res.put("open_time", serverInfo.get("open_time"));
res.put("isnew", isnew); res.put("isnew", isnew);
@ -86,6 +97,10 @@ public class GetServerListController extends HttpServlet {
res.put("ip", ip); res.put("ip", ip);
res.put("port", port); res.put("port", port);
serverList.add(res); serverList.add(res);
serverInfosCache.put(serverInfo.get("server_id").toString(), res);
}
} else {
serverList.addAll(serverInfosCache.values());
} }
//获取我的服务器列表 //获取我的服务器列表
@ -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";
} }