generated from root/miduo_server
注册分布式锁添加
parent
70f21cea83
commit
f28de9e226
|
@ -149,25 +149,34 @@ public class GetUserController extends HttpServlet {
|
||||||
}
|
}
|
||||||
|
|
||||||
int uid = 0;
|
int uid = 0;
|
||||||
|
//加分布式锁
|
||||||
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
List<DBObject> userInfos = BaseGlobal.getInstance().mongoDBPool.find(_COLLECTION_NAME, dbObject);
|
||||||
if (userInfos.size() == 0) {
|
try{
|
||||||
DBObject next = server_info.iterator().next();
|
boolean getLock=false;
|
||||||
Object register_state = next.get("register_state");
|
while(!getLock){
|
||||||
if(register_state!=null&& register_state.equals("0")){
|
getLock = BaseGlobal.getInstance().redisApp.tryGetDistributedLock(RedisKey.REGISTER_LOCK, "", "get", 800);
|
||||||
returnErrorToFront(res,response,"该服务器已停止注册,请选择其他服务器游戏");
|
if (getLock&&userInfos.size() == 0) {
|
||||||
return;
|
DBObject next = server_info.iterator().next();
|
||||||
|
Object register_state = next.get("register_state");
|
||||||
|
if(register_state!=null&& register_state.equals("0")){
|
||||||
|
returnErrorToFront(res,response,"该服务器已停止注册,请选择其他服务器游戏");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
uid = BaseGlobal.getInstance().mongoDBPool.inc("uid") + 10000000;
|
||||||
|
dbObject.put("uid", uid);
|
||||||
|
dbObject.put("_id", uid);
|
||||||
|
userInfos.add(dbObject);
|
||||||
|
if (gid != null && !gid.isEmpty()) {
|
||||||
|
dbObject.put("gid", gid);
|
||||||
|
}
|
||||||
|
if (pid != null && !pid.isEmpty()) {
|
||||||
|
dbObject.put("pid", pid);
|
||||||
|
}
|
||||||
|
BaseGlobal.getInstance().mongoDBPool.save(_COLLECTION_NAME, dbObject);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
uid = BaseGlobal.getInstance().mongoDBPool.inc("uid") + 10000000;
|
}finally {
|
||||||
dbObject.put("uid", uid);
|
BaseGlobal.getInstance().redisApp.releaseDistributedLock(RedisKey.REGISTER_LOCK,"","get");
|
||||||
dbObject.put("_id", uid);
|
|
||||||
userInfos.add(dbObject);
|
|
||||||
if (gid != null && !gid.isEmpty()) {
|
|
||||||
dbObject.put("gid", gid);
|
|
||||||
}
|
|
||||||
if (pid != null && !pid.isEmpty()) {
|
|
||||||
dbObject.put("pid", pid);
|
|
||||||
}
|
|
||||||
BaseGlobal.getInstance().mongoDBPool.save(_COLLECTION_NAME, dbObject);
|
|
||||||
}
|
}
|
||||||
Random random = new Random();
|
Random random = new Random();
|
||||||
int utoken = Math.abs(random.nextInt());
|
int utoken = Math.abs(random.nextInt());
|
||||||
|
|
|
@ -19,4 +19,6 @@ public interface RedisKey {
|
||||||
* 根据openId 封号
|
* 根据openId 封号
|
||||||
*/
|
*/
|
||||||
String Ban_Open_Id = "Ban_Open_Id";
|
String Ban_Open_Id = "Ban_Open_Id";
|
||||||
|
|
||||||
|
String REGISTER_LOCK = "REGISTER_LOCK";
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue