分组提交
parent
88c481126f
commit
416beb8d33
|
@ -141,7 +141,8 @@ public class TimeUtils {
|
||||||
|
|
||||||
int lastHour = getOverTimeCount(1599602400000L,1600462810000L,5);
|
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)
|
* 判断是否合法的时间格式(HH:mm:ss)
|
||||||
|
@ -1356,6 +1357,19 @@ public class TimeUtils {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
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 hour
|
||||||
|
|
|
@ -6,6 +6,7 @@ import com.global.redis.RedisUtil;
|
||||||
import com.global.thread.ThreadManager;
|
import com.global.thread.ThreadManager;
|
||||||
import com.global.thrift.pool.GlobalRpcService;
|
import com.global.thrift.pool.GlobalRpcService;
|
||||||
import com.global.thrift.pool.RPCServerTask;
|
import com.global.thrift.pool.RPCServerTask;
|
||||||
|
import com.world.logic.GlobalLogic;
|
||||||
import manager.STableManager;
|
import manager.STableManager;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
@ -53,6 +54,8 @@ public class GlobalServerApplication {
|
||||||
RedisUtil.getInstence().init();
|
RedisUtil.getInstence().init();
|
||||||
bean = ConfigurableApplicationContextManager.getBean(CoreSettings.class);
|
bean = ConfigurableApplicationContextManager.getBean(CoreSettings.class);
|
||||||
LOGGER.info("ServerProperties ->{}", bean.toString());
|
LOGGER.info("ServerProperties ->{}", bean.toString());
|
||||||
|
//启动时候分组
|
||||||
|
GlobalLogic.getInstance().doSplit();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -2,6 +2,8 @@ package com.global.handler;
|
||||||
|
|
||||||
import com.global.GlobalServerApplication;
|
import com.global.GlobalServerApplication;
|
||||||
import com.google.protobuf.GeneratedMessage;
|
import com.google.protobuf.GeneratedMessage;
|
||||||
|
import com.world.logic.GlobalLogic;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import rpc.global.GlobalProto;
|
import rpc.global.GlobalProto;
|
||||||
import rpc.net.RpcMessage;
|
import rpc.net.RpcMessage;
|
||||||
import rpc.net.gtGBaseHandler;
|
import rpc.net.gtGBaseHandler;
|
||||||
|
@ -13,6 +15,10 @@ 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 {
|
||||||
|
|
||||||
|
@ -25,7 +31,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(1).build();
|
return GlobalProto.GetWorldServerResponse.newBuilder().setIp(ip).setPort(String.valueOf(port)).setGroup(logic.getSplitInfo(proto.getServerID())).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -15,4 +15,8 @@ public class RedisKey {
|
||||||
|
|
||||||
public static final String MATCHED_SPLIT_KEY = "MATCHED_SPLIT_KEY";
|
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";
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -7,7 +7,9 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||||
|
import org.springframework.data.redis.core.ZSetOperations;
|
||||||
import org.springframework.util.CollectionUtils;
|
import org.springframework.util.CollectionUtils;
|
||||||
|
import util.TimeUtils;
|
||||||
|
|
||||||
import java.util.*;
|
import java.util.*;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
|
@ -2,6 +2,7 @@ package com.global.thread;
|
||||||
|
|
||||||
import com.global.redis.RedisKey;
|
import com.global.redis.RedisKey;
|
||||||
import com.global.redis.RedisUtil;
|
import com.global.redis.RedisUtil;
|
||||||
|
import com.world.logic.GlobalLogic;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -24,7 +25,7 @@ public class MinuteTask extends Thread {
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
LOGGER.info("MinuteTask start...");
|
LOGGER.info("MinuteTask start...");
|
||||||
|
everyMondyZeroClockTask();
|
||||||
|
|
||||||
LOGGER.info("MinuteTask end...");
|
LOGGER.info("MinuteTask end...");
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
|
@ -61,14 +62,15 @@ public class MinuteTask extends Thread {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//每周一凌晨五点执行
|
//每周一凌晨0点执行
|
||||||
public void everyMondyFiveClockTask() throws Exception {
|
public void everyMondyZeroClockTask() throws Exception {
|
||||||
Calendar calendar = Calendar.getInstance();
|
Calendar calendar = Calendar.getInstance();
|
||||||
int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
int hour = calendar.get(Calendar.HOUR_OF_DAY);
|
||||||
int minute = calendar.get(Calendar.MINUTE);
|
int minute = calendar.get(Calendar.MINUTE);
|
||||||
int w = calendar.get(Calendar.DAY_OF_WEEK) - 1;
|
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 ...");
|
LOGGER.info("everyMondyFiveClockTask start ...");
|
||||||
|
GlobalLogic.getInstance().doSplit();
|
||||||
LOGGER.info("everyMondyFiveClockTask end ...");
|
LOGGER.info("everyMondyFiveClockTask end ...");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -360,7 +360,7 @@ public class RedisKey {
|
||||||
familyKey.add(FAMILY_FIGHT_TOTAL_EXP);
|
familyKey.add(FAMILY_FIGHT_TOTAL_EXP);
|
||||||
familyKey.add(FAMILY_FIGHT_FINISH_MATCHING);
|
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)){
|
if(RedisKey.TOKEN.equals(type)||RedisKey.USER_SERVER_INFO.equals(type)||RedisKey.SERVER_WORLDLEVE_INFO.equals(type)){
|
||||||
return type + RedisKey.Delimiter_colon + key;
|
return type + RedisKey.Delimiter_colon + key;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue