跨服分布式锁添加

lvxinran 2021-07-02 18:21:40 +08:00
parent 9c3e803af5
commit 2fe4049b55
6 changed files with 52 additions and 6 deletions

View File

@ -21,6 +21,7 @@ import redis.clients.jedis.Protocol;
import java.lang.reflect.Type;
import java.util.*;
import java.util.concurrent.TimeUnit;
import java.util.function.BiFunction;
public class RedisUtil {
@ -272,6 +273,30 @@ public class RedisUtil {
redisTemplate.opsForValue().set(key, gson.toJson(t));
}
public boolean lock(String lockKey, long lockExpireMils) {
return (Boolean) redisTemplate.execute((RedisCallback) connection -> {
long nowTime = System.currentTimeMillis();
Boolean acquire = connection.setNX(lockKey.getBytes(), String.valueOf(nowTime + lockExpireMils + 1).getBytes());
if (acquire) {
return Boolean.TRUE;
} else {
byte[] value = connection.get(lockKey.getBytes());
if (Objects.nonNull(value) && value.length > 0) {
long oldTime = Long.parseLong(new String(value));
if (oldTime < nowTime) {
//connection.getSet返回这个key的旧值并设置新值。
byte[] oldValue = connection.getSet(lockKey.getBytes(), String.valueOf(nowTime + lockExpireMils + 1).getBytes());
//当key不存时会返回空表示key不存在或者已在管道中使用
return oldValue == null ? false : Long.parseLong(new String(oldValue)) < nowTime;
}
}
}
return Boolean.FALSE;
});
}
/**
*
*

View File

@ -20,7 +20,7 @@ public class GetPlayerOneTeamInfoRequestHandler extends gtGBaseHandler<WorldPro
PlayerInfoProto.GetPlayerOneTeamInfoResponse oneTeamInfo = PlayerLogic.getInstance().getOneTeamInfo( proto.getPlayerId(), proto.getTeamId());
// WorldProto.ViewHeroInfoResponse build = WorldProto.ViewHeroInfoResponse.newBuilder().addAllEquip(viewHeroInfoResponse.getEquipList())
// .setHero(viewHeroInfoResponse.getHero()).setForce(viewHeroInfoResponse.getForce())
// .setGuildSkill(viewHeroInfoResponse.getGuildSkill()).addAllSpecialEffects(viewHeroInfoResponse.getSpecialEffectsList()).build();
// .setGuildSkill(viewHeroInfoResponse.getGuildSkill()).addAllSpecialEffects(viewHeroInfoResponse.getSpecialEffectsList()).build();s
return oneTeamInfo;
}

View File

@ -88,10 +88,10 @@ public class GetWorldArenaChallengeRequestHandler extends BaseHandler<WorldProto
if (!ArenaWorldLogic.checkJoin(String.valueOf(iSession.getUid()))) {
throw new ErrorCodeException(ErrorCode.WORLD_LEVELIMIT);
}
// boolean check = PlayerLogic.getInstance().checkAndUpdate(user, VipPrivilegeType.WORLD_ARENA_CHALLENGE_TIMES, 1);
// if(!check){
// throw new ErrorCodeException(ErrorCode.SERVER_SELF_DEFINE);
// }
boolean check = PlayerLogic.getInstance().checkAndUpdate(user, VipPrivilegeType.WORLD_ARENA_CHALLENGE_TIMES, 1);
if(!check){
throw new ErrorCodeException(ErrorCode.SERVER_SELF_DEFINE);
}
user.getArenaManager().setCostDallyTime(user.getArenaManager().getCostDallyTime()+1);

View File

@ -27,6 +27,15 @@ public class ViewHeroInfoHandler extends BaseHandler<PlayerInfoProto.ViewHeroInf
@Override
public void processWithProto(ISession iSession, PlayerInfoProto.ViewHeroInfoRequest proto) throws Exception {
if(proto.getServerId()==0){
}
SArenaRobotConfig sArenaRobotConfig = SArenaRobotConfig.getsArenaRobotConfigById(proto.getTargetUid());
if(sArenaRobotConfig!=null){
PlayerLogic.getInstance().viewRobotHeroInfo(iSession,sArenaRobotConfig,proto.getHeroId());

View File

@ -224,6 +224,12 @@ public class GlobleSystemLogic implements IEventHandler {
}
SMServerArenaSetting setting = STableManager.getConfig(SMServerArenaSetting.class).get(1);
RedisUtil redisUtil = RedisUtil.getInstence();
//使用分布式锁,保证只有一个服来分组
boolean lock = redisUtil.lock("SERVER_SPLIT_REDIS_LOCK", TimeUtils.ONE_MINUTE * 2);
if(!lock){
//没有拿到锁直接返回
return;
}
//这里为所有服务器的世界等级
Map<String,Integer> worldLevelMap = redisUtil.getMapValues(RedisKey.SERVER_WORLDLEVE_INFO,"",String.class,Integer.class);
Object matched = redisUtil.get(RedisKey.MATCHED_SPLIT_KEY);

View File

@ -795,8 +795,14 @@ public class ArenaLogic {
}
//初始化机器人信息
private void initRank(){
//todo 分布式锁
int crossGroup = GlobleSystemLogic.getInstence().getCrossGroup();
//分布式锁,保证一个组只有一个服进行初始化
boolean lock = RedisUtil.getInstence().lock("WORLD_ARENA_INIT_RANK:" + crossGroup, TimeUtils.ONE_MINUTE * 5);
if(!lock){
return;
}
String key1 = RedisUtil.getInstence().getKey(RedisKey.CROSS_ARENA_ROBOT_INFO, String.valueOf(crossGroup));
String key2 = RedisUtil.getInstence().getKey(RedisKey.CROSS_SERVICE_ARENA, String.valueOf(crossGroup));
RedisUtil.getInstence().del(key1,key2);