添加服务器状态的redis的缓存

master
duhui 2022-07-13 16:32:19 +08:00
parent 9575b330f0
commit 940740d8bc
5 changed files with 57 additions and 12 deletions

View File

@ -35,6 +35,8 @@ public class LoginController {
private CUserDao cUserDao;
@Resource
private ChannelInfoDao channelInfoDao;
@Resource
private ServerInfoController serverInfo;
static final String ROOT = "root";
@ -142,7 +144,7 @@ public class LoginController {
}
// 服务器数据同步
ServerInfoController.oneRunServerGroup();
serverInfo.oneRunServerGroup();
// 登陆成功,并添加session
if (verify) {

View File

@ -108,6 +108,7 @@ public class ServerInfoController {
public void onstart(){
// 定时任务延迟1分钟执行每隔俩小时执行一次
TaskKit.scheduleAtFixedRate(this::refreshCache,10,7200,TimeUnit.SECONDS);
LOGGER.info("服务器缓存刷新定时器启动");
}
/**
@ -600,14 +601,28 @@ public class ServerInfoController {
}
/**
* gmgm
*
*/
public static void oneRunServerGroup(){
Map<String, Integer> map = RedisUtil.getInstence().getMap(RedisUserKey.CROSS_SERVER_GROUP + ":", Integer.class, -1);
if (map == null || map.isEmpty()){
Map<String, Integer> map1 = RedisUtil.getInstence().getMap(RedisUserKey.SERVER_SPLIT_INFO + ":", Integer.class, -1);
RedisUtil.getInstence().putMap(RedisUserKey.CROSS_SERVER_GROUP,"",map1);
LOGGER.info("同步跨服id完成");
public void oneRunServerGroup(){
try {
// 服务器状态
List<ServerInfo> serverInfo = serverInfoDao.getAllServerInfo();
RedisUtil.getInstence().del(RedisUserKey.getKey(RedisUserKey.SERVER_STATE,"",0));
for (ServerInfo info : serverInfo) {
registerServerState(info.getServer_id(),info.getStatus());
}
LOGGER.info("同步服务器状态完成");
// 跨服id
Map<String, Integer> map = RedisUtil.getInstence().getMap(RedisUserKey.CROSS_SERVER_GROUP + ":", Integer.class, -1);
if (map == null || map.isEmpty()){
Map<String, Integer> map1 = RedisUtil.getInstence().getMap(RedisUserKey.SERVER_SPLIT_INFO + ":", Integer.class, -1);
RedisUtil.getInstence().putMap(RedisUserKey.CROSS_SERVER_GROUP,"",map1);
LOGGER.info("同步跨服id完成");
}
} catch (Exception e) {
e.printStackTrace();
}
}
@ -630,4 +645,13 @@ public class ServerInfoController {
public static void updateGmGroupId(String serverId, int groupId){
RedisUtil.getInstence().putMapEntry(RedisUserKey.CROSS_SERVER_GROUP,"",serverId,String.valueOf(groupId),-1);
}
/**
*
* @param serverId
* @param state
*/
public static void registerServerState(String serverId, String state){
RedisUtil.getInstence().putMapEntry(RedisUserKey.SERVER_STATE,"",serverId,state,-1);
}
}

View File

@ -118,6 +118,8 @@ public class ServerInfoDaoImpl implements ServerInfoDao {
MongoTemplate mongoTemplate = connect.getMongoTemplete(dbName);
Query query = new Query(Criteria.where("server_id").is(serverInfo.getServer_id()));
mongoTemplate.updateMulti(query, update, ServerInfo.class);
ServerInfoController.registerServerState(serverInfo.getServer_id(),serverInfo.getStatus());
}
@Override
@ -125,6 +127,8 @@ public class ServerInfoDaoImpl implements ServerInfoDao {
MongoTemplate mongoTemplate = connect.getMongoTemplete(dbName);
//有则更新,没有则新增
mongoTemplate.insert(serverInfo, "server_info");
ServerInfoController.registerServerState(serverInfo.getServer_id(),serverInfo.getStatus());
}
/**
@ -171,6 +175,8 @@ public class ServerInfoDaoImpl implements ServerInfoDao {
MongoTemplate mongoTemplate = connect.getMongoTemplete(dbName);
Query query = new Query(Criteria.where("server_id").is(server_id));
mongoTemplate.updateMulti(query, update, ServerInfo.class);
ServerInfoController.registerServerState(server_id,status);
}
@Override

View File

@ -35,6 +35,8 @@ public class RedisUserKey {
public static final String SERVER_SPLIT_INFO = "SERVER_SPLIT_INFO";//游戏服跨服分组实时生效gm不要修改这个key参数
public static final String CROSS_SERVER_GROUP = "CROSS_SERVER_GROUP";//gm跨服分组每周一重新分组生效
public static final String SERVER_STATE = "SERVER_STATE";//服务器状态
/**
* 线 updata/min
*/
@ -45,11 +47,10 @@ public class RedisUserKey {
public static final String GAME_PAY_URL = "GAME_PAY_URL:";
public static String getKey(String type, String key, int serverId) {
if (serverId!=0) {
return serverId + Delimiter_colon + type + Delimiter_colon + key;
if (serverId==0) {
return type + Delimiter_colon + key;
}
return type + Delimiter_colon + key;
return serverId + Delimiter_colon + type + Delimiter_colon + key;
}
}

View File

@ -8,6 +8,7 @@ import org.slf4j.LoggerFactory;
import org.springframework.data.redis.RedisConnectionFailureException;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.List;
@ -194,6 +195,17 @@ public class RedisUtil {
}
}
/**
*
* @param key
*/
public void del(String key) {
Boolean bol = redisObjectTemplate.hasKey(key);
if (bol){
redisObjectTemplate.delete(key);
}
}
/**
* Map
*