服务器分组修改
parent
416beb8d33
commit
8ed8485114
|
@ -1,8 +1,8 @@
|
||||||
package com.global.handler;
|
package com.global.handler;
|
||||||
|
|
||||||
import com.global.GlobalServerApplication;
|
import com.global.GlobalServerApplication;
|
||||||
|
import com.global.logic.GlobalLogic;
|
||||||
import com.google.protobuf.GeneratedMessage;
|
import com.google.protobuf.GeneratedMessage;
|
||||||
import com.world.logic.GlobalLogic;
|
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import rpc.global.GlobalProto;
|
import rpc.global.GlobalProto;
|
||||||
import rpc.net.RpcMessage;
|
import rpc.net.RpcMessage;
|
||||||
|
@ -16,9 +16,6 @@ import rpc.net.gtGBaseHandler;
|
||||||
public class GetWorldServerRequestHandler extends gtGBaseHandler<GlobalProto.GetWorldServerRequest> {
|
public class GetWorldServerRequestHandler extends gtGBaseHandler<GlobalProto.GetWorldServerRequest> {
|
||||||
|
|
||||||
|
|
||||||
@Autowired
|
|
||||||
public GlobalLogic logic;
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public GeneratedMessage processWithProto(RpcMessage rpcMessage, GlobalProto.GetWorldServerRequest proto) throws Exception {
|
public GeneratedMessage processWithProto(RpcMessage rpcMessage, GlobalProto.GetWorldServerRequest proto) throws Exception {
|
||||||
|
|
||||||
|
@ -31,7 +28,7 @@ public class GetWorldServerRequestHandler extends gtGBaseHandler<GlobalProto.Get
|
||||||
//第一版固定一个世界服
|
//第一版固定一个世界服
|
||||||
String ip = GlobalServerApplication.bean.getWorldip();
|
String ip = GlobalServerApplication.bean.getWorldip();
|
||||||
int port = GlobalServerApplication.bean.getPort();
|
int port = GlobalServerApplication.bean.getPort();
|
||||||
return GlobalProto.GetWorldServerResponse.newBuilder().setIp(ip).setPort(String.valueOf(port)).setGroup(logic.getSplitInfo(proto.getServerID())).build();
|
return GlobalProto.GetWorldServerResponse.newBuilder().setIp(ip).setPort(String.valueOf(port)).setGroup(GlobalLogic.getInstance().getSplitInfo(proto.getServerID())).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,88 @@
|
||||||
|
package com.global.logic;
|
||||||
|
|
||||||
|
|
||||||
|
import com.global.redis.RedisKey;
|
||||||
|
import com.global.redis.RedisUtil;
|
||||||
|
import config.SMServerArenaSetting;
|
||||||
|
import manager.STableManager;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.data.redis.core.ZSetOperations;
|
||||||
|
import util.TimeUtils;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @author lvxinran
|
||||||
|
* @date 2020/12/1
|
||||||
|
* @discribe
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class GlobalLogic {
|
||||||
|
|
||||||
|
|
||||||
|
public static final Logger LOGGER = LoggerFactory.getLogger(GlobalLogic.class);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 获取单例
|
||||||
|
*
|
||||||
|
* @return
|
||||||
|
*/
|
||||||
|
public static GlobalLogic getInstance() {
|
||||||
|
return Instance.instance;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static class Instance {
|
||||||
|
public final static GlobalLogic instance = new GlobalLogic();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static Map<String,Integer> splitInfo;
|
||||||
|
/**
|
||||||
|
* 分组
|
||||||
|
*/
|
||||||
|
public void doSplit(){
|
||||||
|
SMServerArenaSetting setting = STableManager.getConfig(SMServerArenaSetting.class).get(1);
|
||||||
|
RedisUtil redisUtil = RedisUtil.getInstence();
|
||||||
|
//这里为所有服务器的世界等级
|
||||||
|
Map<String,Integer> worldLevelMap = redisUtil.getMapValues(RedisKey.SERVER_WORLDLEVE_INFO+":",String.class,Integer.class);
|
||||||
|
Object matched = redisUtil.get(RedisKey.MATCHED_SPLIT_KEY);
|
||||||
|
if(matched!=null){
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
LOGGER.info("Global开始分组");
|
||||||
|
long lastHour = TimeUtils.getLastOrUnderHour(TimeUtils.now(), 1, 0, 0, true);
|
||||||
|
lastHour+=(2*TimeUtils.WEEK);
|
||||||
|
int expireTime =(int)((lastHour-TimeUtils.now())/1000);
|
||||||
|
redisUtil.set(RedisKey.MATCHED_SPLIT_KEY,String.valueOf(1),expireTime-60);
|
||||||
|
worldLevelMap.forEach((k,v)->redisUtil.zsetAddOne(RedisKey.SERVER_LEVEL_RANK,k,v));
|
||||||
|
Set<ZSetOperations.TypedTuple<String>> zsetRevRangeWithScore = redisUtil.getZsetRevRangeWithScore(RedisKey.SERVER_LEVEL_RANK, setting.getWorldLevel(), 999);
|
||||||
|
// int remain = zsetRevRangeWithScore.size() % 8;
|
||||||
|
int index = 0;
|
||||||
|
int areaId = 1;
|
||||||
|
Map<String,Integer> split = new HashMap<>(zsetRevRangeWithScore.size());
|
||||||
|
for(ZSetOperations.TypedTuple<String> data:zsetRevRangeWithScore){
|
||||||
|
if(index>=setting.getServerNum()){
|
||||||
|
if((zsetRevRangeWithScore.size()-areaId*setting.getServerNum())>setting.getServerNum()/2){
|
||||||
|
areaId++;
|
||||||
|
}
|
||||||
|
index=0;
|
||||||
|
}
|
||||||
|
split.put(data.getValue(),areaId);
|
||||||
|
|
||||||
|
index++;
|
||||||
|
}
|
||||||
|
redisUtil.putMapEntrys(RedisKey.SERVER_SPLIT_INFO,split);
|
||||||
|
LOGGER.info("Global分组结束");
|
||||||
|
splitInfo = RedisUtil.getInstence().getMapValues(RedisKey.SERVER_SPLIT_INFO,String.class,Integer.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
public int getSplitInfo(int serverId){
|
||||||
|
|
||||||
|
if(splitInfo==null){
|
||||||
|
splitInfo = RedisUtil.getInstence().getMapValues(RedisKey.SERVER_SPLIT_INFO,String.class,Integer.class);
|
||||||
|
}
|
||||||
|
return splitInfo.getOrDefault(String.valueOf(serverId),-1);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue