分组提交

lvxinran 2020-12-01 10:55:37 +08:00
parent 88c481126f
commit 416beb8d33
8 changed files with 129 additions and 13 deletions

View File

@ -141,7 +141,8 @@ public class TimeUtils {
int lastHour = getOverTimeCount(1599602400000L,1600462810000L,5);
System.out.println(lastHour);
long lastOrUnderHour = getLastOrUnderHour(TimeUtils.now(), 1, 0, 0, true);
System.out.println(lastOrUnderHour);
}
/**
* (HH:mm:ss)
@ -1356,12 +1357,25 @@ public class TimeUtils {
}
}
/**
*
* @param hour
* @param lastOrUnder truefalse
* @return
*/
public static long getLastOrUnderHour(long time,int week,int hour,int minute,boolean lastOrUnder) {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);
cal.set(Calendar.DAY_OF_WEEK,week==7?0:week+1);
time = cal.getTimeInMillis();
long lastOrUnderHour = getLastOrUnderHour(time, hour, minute, true);
if(!lastOrUnder){
lastOrUnderHour+=TimeUtils.WEEK;
}
return lastOrUnderHour;
}
/**
*
* @param hour
* @param lastOrUnder truefalse
* @return
*/
public static long getLastOrUnderHour(long time,int hour,int minute,boolean lastOrUnder){
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(time);

View File

@ -6,6 +6,7 @@ import com.global.redis.RedisUtil;
import com.global.thread.ThreadManager;
import com.global.thrift.pool.GlobalRpcService;
import com.global.thrift.pool.RPCServerTask;
import com.world.logic.GlobalLogic;
import manager.STableManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -53,6 +54,8 @@ public class GlobalServerApplication {
RedisUtil.getInstence().init();
bean = ConfigurableApplicationContextManager.getBean(CoreSettings.class);
LOGGER.info("ServerProperties ->{}", bean.toString());
//启动时候分组
GlobalLogic.getInstance().doSplit();
}

View File

@ -2,6 +2,8 @@ package com.global.handler;
import com.global.GlobalServerApplication;
import com.google.protobuf.GeneratedMessage;
import com.world.logic.GlobalLogic;
import org.springframework.beans.factory.annotation.Autowired;
import rpc.global.GlobalProto;
import rpc.net.RpcMessage;
import rpc.net.gtGBaseHandler;
@ -13,6 +15,10 @@ import rpc.net.gtGBaseHandler;
*/
public class GetWorldServerRequestHandler extends gtGBaseHandler<GlobalProto.GetWorldServerRequest> {
@Autowired
public GlobalLogic logic;
@Override
public GeneratedMessage processWithProto(RpcMessage rpcMessage, GlobalProto.GetWorldServerRequest proto) throws Exception {
@ -25,7 +31,7 @@ public class GetWorldServerRequestHandler extends gtGBaseHandler<GlobalProto.Get
//第一版固定一个世界服
String ip = GlobalServerApplication.bean.getWorldip();
int port = GlobalServerApplication.bean.getPort();
return GlobalProto.GetWorldServerResponse.newBuilder().setIp(ip).setPort(String.valueOf(port)).setGroup(1).build();
return GlobalProto.GetWorldServerResponse.newBuilder().setIp(ip).setPort(String.valueOf(port)).setGroup(logic.getSplitInfo(proto.getServerID())).build();
}

View File

@ -15,4 +15,8 @@ public class RedisKey {
public static final String MATCHED_SPLIT_KEY = "MATCHED_SPLIT_KEY";
public static final String SERVER_LEVEL_RANK = "SERVER_LEVEL_RANK";
public static final String SERVER_SPLIT_INFO = "SERVER_SPLIT_INFO";
}

View File

@ -7,7 +7,9 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ZSetOperations;
import org.springframework.util.CollectionUtils;
import util.TimeUtils;
import java.util.*;
import java.util.concurrent.TimeUnit;

View File

@ -2,6 +2,7 @@ package com.global.thread;
import com.global.redis.RedisKey;
import com.global.redis.RedisUtil;
import com.world.logic.GlobalLogic;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -24,7 +25,7 @@ public class MinuteTask extends Thread {
public void run() {
try {
LOGGER.info("MinuteTask start...");
everyMondyZeroClockTask();
LOGGER.info("MinuteTask end...");
} catch (Exception e) {
@ -61,14 +62,15 @@ public class MinuteTask extends Thread {
}
}
//每周一凌晨点执行
public void everyMondyFiveClockTask() throws Exception {
//每周一凌晨0点执行
public void everyMondyZeroClockTask() throws Exception {
Calendar calendar = Calendar.getInstance();
int hour = calendar.get(Calendar.HOUR_OF_DAY);
int minute = calendar.get(Calendar.MINUTE);
int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
if(w == 1 &&hour == 5 && minute ==0){
if(w == 1 &&hour == 0 && minute ==0){
LOGGER.info("everyMondyFiveClockTask start ...");
GlobalLogic.getInstance().doSplit();
LOGGER.info("everyMondyFiveClockTask end ...");
}
}

View File

@ -0,0 +1,85 @@
package com.world.logic;
import com.global.redis.RedisKey;
import com.global.redis.RedisUtil;
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(){
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, 10, 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>=8){
if(zsetRevRangeWithScore.size()-areaId*8>4){
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);
}
}

View File

@ -360,7 +360,7 @@ public class RedisKey {
familyKey.add(FAMILY_FIGHT_TOTAL_EXP);
familyKey.add(FAMILY_FIGHT_FINISH_MATCHING);
}
public static String getKey(String type, String key, boolean withoutServerId)
public static String getKey(String type, String key, boolean withoutServerId){
if(RedisKey.TOKEN.equals(type)||RedisKey.USER_SERVER_INFO.equals(type)||RedisKey.SERVER_WORLDLEVE_INFO.equals(type)){
return type + RedisKey.Delimiter_colon + key;
}