HTML5技术

net.sz.framework 框架 登录服务器架构 单服2 万 TPS(QPS) - 失足程序员(4)

字号+ 作者:H5之家 来源:H5之家 2017-04-13 11:05 我要评论( )

1 package net.sz.game.login.scripts.logins; java.util.List; 4 import net.sz.framework.nio.http.NioHttpRequest; 5 import net.sz.framework.scripts.IInitBaseScript; 6 import net.sz.framework.szlog.SzLog

1 package net.sz.game.login.scripts.logins; java.util.List; 4 import net.sz.framework.nio.http.NioHttpRequest; 5 import net.sz.framework.scripts.IInitBaseScript; 6 import net.sz.framework.szlog.SzLogger; 7 import net.sz.framework.utils.GlobalUtil; 8 import net.sz.framework.utils.JsonUtil; 9 import net.sz.framework.utils.MD5Util; 10 import net.sz.framework.utils.StringUtil; 11 import net.sz.game.login.data.DataManager; 12 import net.sz.game.login.logins.LoginManager; 13 import net.sz.game.login.logins.iscript.ILoginScript; 14 import net.sz.game.login.service.ServerManager; 15 import net.sz.game.pmodel.po.loginsr.data.ServerInfo; 16 import net.sz.game.pmodel.po.loginsr.data.UserInfo; * 登录本地系统 操作数据库 20 * <br> 21 * author 失足程序员<br> <br> 23 * mail 492794628@qq.com<br> 24 * phone 13882122019<br> LoginScript implements ILoginScript, IInitBaseScript { SzLogger log = SzLogger.getLogger(); String LOGINPWDSIGN = "af0ca5ee6203e02ec076aa8b84385d08"; 31 32 @Override _init() { LoginManager.getInstance().loginScript = this; 36 } 37 38 @Override 39 public String getErrorCode(int code, int msg) { 40 String ret = "{" + "\"code\":" + code + ", \"msg\":" + msg + "}"; 41 return ret; 42 } 43 44 @Override _login(String username, String userpwd, int platform, int channelId, NioHttpRequest request) { 46 long currentTimeMillis = System.currentTimeMillis(); 47 if (100 != (platform)) { 48 username = platform + "_" + username; 49 } 50 log.info("登录耗时 " + username + " 1 :" + (System.currentTimeMillis() - currentTimeMillis)); 51 boolean flag = true; 52 String usernameLowerCase = username.toLowerCase(); (!StringUtil.checkFilter(username, StringUtil.PATTERN_ABC_0) || !StringUtil.checkFilter(userpwd, StringUtil.PATTERN_ABC_PWD)) { 55 if (log.isInfoEnabled()) { 56 log.info("用户:" + username + " 账号或者密码非法字符!!!"); 57 } 58 request.addContent(getErrorCode(10, 830510)); 59 flag = false; 60 } (!(100 == platform 63 || request.getIp().startsWith("192.168.") 64 || request.getIp().startsWith("127.0.0.1"))) { 65 if (usernameLowerCase.startsWith("robot")) { 66 if (log.isInfoEnabled()) { 67 log.info("用户:" + username + " 并非特殊平台,不允许此账号!!!"); 68 } 69 request.addContent(getErrorCode(10, 830511)); 70 flag = false; 71 } 72 } 73 log.info("登录耗时 " + username + " 2 :" + (System.currentTimeMillis() - currentTimeMillis)); 74 if (flag) { 75 try { UserInfo userinfo = DataManager.getInstance().getUserInfoMap().get(usernameLowerCase); (userinfo == null) { (this) { 83 if (log.isInfoEnabled()) { 84 log.info("用户:" + username + " 不存在缓存用户!!!"); 85 } userinfo = DataManager.getInstance().getUserInfoMap().get(usernameLowerCase); 88 if (userinfo != null) { 89 if (log.isInfoEnabled()) { 90 log.info("平台:" + platform + ", ip:" + request.getIp() + ", 用户:" + username + " 缓存用户!!!"); 91 } 92 } else { 93 log.info("登录耗时 " + username + " 3 :" + (System.currentTimeMillis() - currentTimeMillis)); 94 userinfo = DataManager.getInstance().getDataDao().getObjectByWhere(UserInfo.class, "where `userNameLowerCase` = ?", usernameLowerCase); 95 log.info("登录耗时 " + username + " 4 :" + (System.currentTimeMillis() - currentTimeMillis)); 96 if (userinfo == null) { 97 if (DataManager.getInstance().getUserNameLowerCaseSet().contains(usernameLowerCase)) { 98 request.addContent(getErrorCode(31, 830512)); 99 if (log.isInfoEnabled()) { 100 log.info("平台:" + platform + ", ip:" + request.getIp() + ", 用户:" + username + " 注册用户失败,重名!!!"); 101 } 102 return; 103 } else { ("robottroy".equalsIgnoreCase(usernameLowerCase)) { 106 request.addContent(getErrorCode(31, 830513)); 107 if (log.isInfoEnabled()) { 108 log.info("平台:" + platform + ", ip:" + request.getIp() + ", 用户:" + username + " 注册用户失败,,特殊账号不能注册!!!"); 109 } 110 return; 111 } (log.isInfoEnabled()) { 114 log.info("用户:" + username + " 数据库不存在!!!创建用户"); 115 } 116 117 userinfo = new UserInfo(); 118 userinfo.setId(GlobalUtil.getId()); 119 userinfo.setUserName(username); 120 userinfo.setUserNameLowerCase(usernameLowerCase); 121 userinfo.setUserPwd(userpwd); 122 userinfo.setCreateTime(System.currentTimeMillis()); 123 userinfo.setLastLoginTime(System.currentTimeMillis()); 124 userinfo.setStatus(1); 125 userinfo.setUserMail(""); 126 userinfo.setUserPhone(""); 127 userinfo.setPlatformId(platform); 128 userinfo.setChannelId(channelId); 129 DataManager.getInstance().getcUDThread().insert_Sync(userinfo); 130 } 131 } 132 133 DataManager.getInstance().getUserNameLowerCaseSet().add(usernameLowerCase); 134 135 DataManager.getInstance().getUserInfoMap().put(usernameLowerCase, userinfo); 136 log.info("登录耗时 " + username + " 5 :" + (System.currentTimeMillis() - currentTimeMillis)); 137 } 138 } 139 } else { 140 if (log.isInfoEnabled()) { 141 log.info("平台:" + platform + ", ip:" + request.getIp() + ", 用户:" + username + " 缓存用户!!!"); 142 } 143 } (userinfo == null || !userinfo.getUserName().equals(username) || !userinfo.getUserPwd().equals(userpwd)) { 146 request.addContent(getErrorCode(3, 830514)); 147 if (log.isInfoEnabled()) { 148 log.info("平台:" + platform + ", ip:" + request.getIp() + ", 用户:" + username + " 用户名或密码错误!!!"); 149 } 150 } else { md5time = System.currentTimeMillis(); String token = MD5Util.md5Encode('=', userinfo.getId() + "", username, "", md5time + "", LOGINPWDSIGN); userinfo.setToken(token); userinfo.setTokenTime(md5time); userinfo.setLastUplogintime(md5time); 161 162 userinfo.getLastLoginTime(); 163 userinfo.getLastUplogintime(); 164 log.info("登录耗时 " + username + " 6 :" + (System.currentTimeMillis() - currentTimeMillis)); 165 String serverInfo = ServerManager.getInstance().serverInfoScript.getServerInfo(platform, channelId, request, userinfo); 166 log.info("登录耗时 " + username + " 7 :" + (System.currentTimeMillis() - currentTimeMillis)); 167 Ret ret = new Ret(0, 0); 168 ret.setToken(token); 169 ret.setTime(md5time); 170 ret.setUserName(username); 171 ret.setUid(userinfo.getId()); 172 String toJSONString = ret.showString(serverInfo); 173 log.info("登录耗时 " + username + " 8 :" + (System.currentTimeMillis() - currentTimeMillis)); 174 if (log.isDebugEnabled()) { 175 log.debug("平台:" + platform + ", ip:" + request.getIp() + ", 用户:" + username + " 用户登录完成!!!同步服务器信息:" + toJSONString); 176 } 177 request.addContent(toJSONString); 178 log.info("登录耗时 " + username + " 8 :" + (System.currentTimeMillis() - currentTimeMillis)); 179 if (log.isInfoEnabled()) { 180 log.info("平台:" + platform + ", ip:" + request.getIp() + ", 用户:" + username + " 用户登录完成!!!"); 181 } 182 } 183 } catch (Exception e) { 184 log.error("平台:" + platform + ", ip:" + request.getIp() + ", 用户:" + username + " 登录发生错误信息", e); 185 request.addContent(getErrorCode(500, 830515)); 186 } 187 } 188 } main(String[] args) { 191 String jsonString = "{code:0, token:\"af0ca5ee6203e02ec076aa8b84385d08\", userName:\"ROBOTsz111\", msg:\"\", time:1469087482055, uid:197, infos:[{zoneId:100, serverGroup:\"测试大区\", serverId:\"1003\", serverName:\"服务器(刘富顺)\", tcpIp:\"182.150.21.45\", tcpPort:8084, httpIP:\"182.150.21.45\", httpPort:9094, idenIcon:\"\", startTime:\"1\", otherString:\"\", serverState:\"维护\", nextOpenTime:\"\"},{zoneId:200, serverGroup:\"测试专区\", serverId:\"1\", serverName:\"终焉之时\", tcpIp:\"182.150.21.45\", tcpPort:8083, httpIP:\"182.150.21.45\", httpPort:9093, idenIcon:\"new\", startTime:\"1\", otherString:\" \", serverState:\"维护\", nextOpenTime:\" \"},{zoneId:100, serverGroup:\"测试大区\", serverId:\"1001\", serverName:\"服务器(陈飞)\", tcpIp:\"182.150.21.45\", tcpPort:8084, httpIP:\"182.150.21.45\", httpPort:9094, idenIcon:\"\", startTime:\"1\", otherString:\"\", serverState:\"维护\", nextOpenTime:\"\"},{zoneId:100, serverGroup:\"测试大区\", serverId:\"1002\", serverName:\"服务器(吴复全)\", tcpIp:\"182.150.21.45\", tcpPort:8084, httpIP:\"182.150.21.45\", httpPort:9094, idenIcon:\"\", startTime:\"1\", otherString:\"\", serverState:\"维护\", nextOpenTime:\"\"},{zoneId:100, serverGroup:\"测试大区\", serverId:\"2\", serverName:\"客户端\", tcpIp:\"182.150.21.45\", tcpPort:7075, httpIP:\"182.150.21.45\", httpPort:9094, idenIcon:\"xingxing\", startTime:\"1\", otherString:\"\", serverState:\"维护\", nextOpenTime:\"\"}]}"; 192 jsonString = new LoginScript().getErrorCode(10, 830510); 193 Ret parseObject = JsonUtil.parseObject(jsonString, Ret.class); 194 log.error(parseObject.toString()); 195 } Ret { code; 200 private String token; 201 private String userName; msg; time; uid; 205 private ServerInfo[] infos; Ret(int code, int msg) { 208 this.code = code; 209 this.msg = msg; 210 } Ret() { 213 } String showString(String serverinfos) { 216 return "{" + "\"code\":" + code + ", \"token\":\"" + token + "\", \"userName\":\"" + userName + "\", \"msg\":\"" + msg + "\", \"time\":" + time + ", \"uid\":" + uid + ", \"infos\":" + serverinfos + "}"; 217 } 218 219 @Override 220 public String toString() { 221 return "{" + "code=" + code + ", token=" + token + ", userName=" + userName + ", msg=" + msg + ", time=" + time + ", uid=" + uid + ", infos=" + infos + '}'; 222 } the code getCode() { 228 return code; 229 } code the code to set setCode(int code) { 235 this.code = code; 236 } the token String getToken() { 242 return token; 243 } token the token to set setToken(String token) { 249 this.token = token; 250 } the userName String getUserName() { 256 return userName; 257 } userName the userName to set setUserName(String userName) { 263 this.userName = userName; 264 } the msg getMsg() { 270 return msg; 271 } msg the msg to set setMsg(int msg) { 277 this.msg = msg; 278 } the time getTime() { 284 return time; 285 } time the time to set setTime(long time) { 291 this.time = time; 292 } the uid getUid() { 298 return uid; 299 } uid the uid to set setUid(long uid) { 305 this.uid = uid; 306 } the infos ServerInfo[] getInfos() { 312 return infos; 313 } infos the infos to set setInfos(ServerInfo[] infos) { 319 this.infos = infos; 320 } 321 322 } 323 324 }

View Code

 

 整个最后登录流程。设计;

整个登录流程

http 请求 -》 流向 http api -》 httploginscript -》 loginscript渠道登录 -》 loginscript 登录 -》缓存验证 -》 数据库验证 -》 返回结果;

C#代码测试调用

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
  • 前端开发框架简介:angular和react - 腾讯云技术社区

    前端开发框架简介:angular和react - 腾讯云技术社区

    2017-04-11 18:02

  • WebApp框架 - stumpx

    WebApp框架 - stumpx

    2017-04-06 15:01

  • net.sz.framework 框架 轻松搭建服务---让你更专注逻辑功能---初探 - 失足程序员

    net.sz.framework 框架 轻松搭建服务---让你更专注逻辑功能---初探 -

    2017-04-02 10:11

  • Omi框架Store体系的前世今生 - 【当耐特】

    Omi框架Store体系的前世今生 - 【当耐特】

    2017-03-25 13:00

网友点评