公告缓存 服务器状态修改

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.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<String, NoticeInfo> noticeInfoMap = BaseGlobal.getInstance().redisApp.hgetAll(RedisKey.NOTICE, "", NoticeInfo.class, -1, false);
List<DBObject> 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<NoticeInfo> 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<String> 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<String,String> parms = new HashMap<>();
parms.put("title",send.getTitle());
parms.put("content",send.getContent());
if (send != null) {
Map<String, String> 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();
}
}
}

View File

@ -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<String, DBObject> 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<DBObject> serverInfoList = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, req);
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) {
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;
}
int state = Integer.parseInt(serverInfo.get("state").toString());
if (state == 0 ){
continue;
}
if (whiteList != null && state ==1){
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"))
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端口
if (state == 1) { // 服务器状态为维护的话不传给前端ip端口
ip = "";
port = "";
}
res.put("ip",ip );
res.put("port",port);
res.put("ip", ip);
res.put("port", port);
serverList.add(res);
serverInfosCache.put(serverInfo.get("server_id").toString(), res);
}
} else {
serverList.addAll(serverInfosCache.values());
}
//获取我的服务器列表
@ -95,15 +110,15 @@ public class GetServerListController extends HttpServlet {
int time = 0;
for (Map.Entry<String, UserRecentLoginInfo> 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<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 NOTICE = "NOTICE";
String GLOBAL_SYS_PRO= "GLOBAL_SYS_PRO";
}