generated from root/miduo_server
添加服务器状态的redis的缓存
parent
9575b330f0
commit
940740d8bc
|
|
@ -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) {
|
||||
|
|
|
|||
|
|
@ -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 {
|
|||
}
|
||||
|
||||
/**
|
||||
* 只会执行一次,同步游戏服跨服分组到gm,之后会以gm的为准
|
||||
* 登陆账号执行
|
||||
*/
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in New Issue