前言
無論我們做什么系統,95%的系統都離不開注冊,登錄;
而游戲更加關鍵,頻繁登錄,並發登錄,導量登錄;如果登錄承載不起來,那么游戲做的再好,都是徒然,進不去啊;
序言
登錄所需要的承載,包含程序和數據存儲瓶頸,統一都可以看成io瓶頸;
我的登錄服務器,操作只是做登錄注冊和返回服務器列表功能(只要其他負載均衡不講解,軟負載,硬負載);
登錄服務器,分不同渠道登錄驗證,本地渠道驗證,如果登錄賬戶不存在,直接注冊賬戶,然后返回token碼;
其他服務器只認token登錄需求;減少其他服務器的數據庫驗證,網絡傳輸驗證,io等開銷;
我的登錄服務器設計只接受 http 登錄請求;
http不是通過web發出的;只是一個http監聽協議而已;
本文,測試結果,
本機測試服務器標准是 I7 8C + 16G,Windows 10,
創建賬號消耗4毫秒左右;理論上登錄和創建賬號是一致結果;
緩存登錄,由於減少了數據庫檢束;
消耗基本是1毫秒左右;
也就說說
登、注的qps= 5000 =1000 / 4 * 20;
緩存登錄 qps= 20000 = 1000 / 1 * 20;
--5000 注冊,5000 登錄,2萬緩存登錄 qps
數據庫設計
userinfo類

1 package net.sz.test; 2 3 import java.io.Serializable; 4 import javax.persistence.Column; 5 import javax.persistence.Id; 6 import javax.persistence.Table; 7 8 /** 9 * 用戶信息表 10 * 11 * <br> 12 * author 失足程序員<br> 13 * blog http://www.cnblogs.com/ty408/<br> 14 * mail 492794628@qq.com<br> 15 * phone 13882122019<br> 16 */ 17 @Table(name = "UserInfo") 18 public class UserInfo implements Serializable { 19 20 private static final long serialVersionUID = -8907709646630947645L; 21 @Id 22 private long id; 23 /*賬戶名*/ 24 private String userName; 25 /*賬戶名小寫副本*/ 26 private String userNameLowerCase; 27 /*密碼*/ 28 @Column(nullable = false) 29 private String userPwd; 30 /*電話*/ 31 @Column(nullable = false) 32 private String userPhone; 33 /*郵件*/ 34 @Column(nullable = false) 35 private String userMail; 36 /*創建時間*/ 37 @Column(nullable = false) 38 private long createTime; 39 /*最后登錄時間*/ 40 @Column(nullable = false) 41 private long lastLoginTime; 42 /*狀態,1正常,2表示不可登錄*/ 43 @Column(nullable = false) 44 private int Status; 45 /*登錄后生成的*/ 46 private transient String token; 47 /*生成 token 的時間*/ 48 private transient long tokenTime; 49 /* 玩家當前登錄服務器ID */ 50 private int loginPlayerServerId; 51 /* 邏輯服務器傳遞過來的同步時間 */ 52 private transient long lastUplogintime; 53 54 public UserInfo() { 55 } 56 57 public long getId() { 58 return id; 59 } 60 61 public void setId(long id) { 62 this.id = id; 63 } 64 65 public String getUserName() { 66 return userName; 67 } 68 69 public void setUserName(String userName) { 70 this.userName = userName; 71 } 72 73 public String getUserNameLowerCase() { 74 return userNameLowerCase; 75 } 76 77 public void setUserNameLowerCase(String userNameLowerCase) { 78 this.userNameLowerCase = userNameLowerCase; 79 } 80 81 public String getUserPwd() { 82 return userPwd; 83 } 84 85 public void setUserPwd(String userPwd) { 86 this.userPwd = userPwd; 87 } 88 89 public String getUserPhone() { 90 return userPhone; 91 } 92 93 public void setUserPhone(String userPhone) { 94 this.userPhone = userPhone; 95 } 96 97 public String getUserMail() { 98 return userMail; 99 } 100 101 public void setUserMail(String userMail) { 102 this.userMail = userMail; 103 } 104 105 public long getCreateTime() { 106 return createTime; 107 } 108 109 public void setCreateTime(long createTime) { 110 this.createTime = createTime; 111 } 112 113 public long getLastLoginTime() { 114 return lastLoginTime; 115 } 116 117 public void setLastLoginTime(long lastLoginTime) { 118 this.lastLoginTime = lastLoginTime; 119 } 120 121 public int getStatus() { 122 return Status; 123 } 124 125 public void setStatus(int Status) { 126 this.Status = Status; 127 } 128 129 public String getToken() { 130 return token; 131 } 132 133 public void setToken(String token) { 134 this.token = token; 135 } 136 137 public long getLastUplogintime() { 138 return lastUplogintime; 139 } 140 141 public void setLastUplogintime(long lastUplogintime) { 142 this.lastUplogintime = lastUplogintime; 143 } 144 145 public long getTokenTime() { 146 return tokenTime; 147 } 148 149 public void setTokenTime(long tokenTime) { 150 this.tokenTime = tokenTime; 151 } 152 153 public int getLoginPlayerServerId() { 154 return loginPlayerServerId; 155 } 156 157 public void setLoginPlayerServerId(int loginPlayerServerId) { 158 this.loginPlayerServerId = loginPlayerServerId; 159 } 160 161 @Override 162 public String toString() { 163 return "UserInfo{" + "id=" + id + ", userName=" + userName + ", userNameLowerCase=" + userNameLowerCase + ", userPwd=" + userPwd + ", userPhone=" + userPhone + ", userMail=" + userMail + ", createTime=" + createTime + ", lastLoginTime=" + lastLoginTime + ", Status=" + Status + ", token=" + token + ", tokenTime=" + tokenTime + ", loginPlayerServerId=" + loginPlayerServerId + ", lastUplogintime=" + lastUplogintime + '}'; 164 } 165 166 }
用來記錄賬戶數據的;
登錄功能划分設計
渠道登錄腳本接口設計

1 package net.sz.game.login.logins.iscript; 2 3 import net.sz.framework.nio.http.NioHttpRequest; 4 import net.sz.framework.scripts.IBaseScript; 5 6 /** 7 * 8 * <br> 9 * author 失足程序員<br> 10 * blog http://www.cnblogs.com/ty408/<br> 11 * mail 492794628@qq.com<br> 12 * phone 13882122019<br> 13 */ 14 public interface ILoginScriptPlatform extends IBaseScript { 15 16 /** 17 * 處理登錄 平台登錄 18 * 19 * @param platform 平台ID 20 * @param channelId 渠道ID 21 * @param request 請求 22 * @return 23 */ 24 boolean login(int platform, int channelId, NioHttpRequest request); 25 }
最終本地登錄腳本接口設計

1 package net.sz.game.login.logins.iscript; 2 3 import net.sz.framework.nio.http.NioHttpRequest; 4 import net.sz.framework.scripts.IBaseScript; 5 6 /** 7 * 8 * <br> 9 * author 失足程序員<br> 10 * blog http://www.cnblogs.com/ty408/<br> 11 * mail 492794628@qq.com<br> 12 * phone 13882122019<br> 13 */ 14 public interface ILoginScript extends IBaseScript { 15 16 /** 17 * 返回錯誤碼 18 * 19 * @param code 20 * @param msg 21 * @return 22 */ 23 String getErrorCode(int code, int msg); 24 25 /** 26 * 最終登錄 27 * 28 * @param username 29 * @param userpwd 30 * @param platform 31 * @param channelId 32 * @param request 33 */ 34 void _login(String username, String userpwd, int platform, int channelId, NioHttpRequest request); 35 36 }
最終登錄腳本需要反向引用,不能通過腳本調用

1 package net.sz.game.login.logins; 2 3 import net.sz.game.login.logins.iscript.ILoginScript; 4 5 /** 6 * 登錄管理類 7 * <br> 8 * author 失足程序員<br> 9 * blog http://www.cnblogs.com/ty408/<br> 10 * mail 492794628@qq.com<br> 11 * phone 13882122019<br> 12 */ 13 public class LoginManager { 14 15 private static final LoginManager instance = new LoginManager(); 16 17 18 public static LoginManager getInstance() { 19 return instance; 20 } 21 22 public ILoginScript loginScript; 23 24 }
在腳本里面加入
@Override public void _init() { //反向注冊 LoginManager.getInstance().loginScript = this; }
直接通過實例對象引用而不再是腳本對象集合調用形式;
腳本登錄區分,
100渠道登錄

package net.sz.game.login.scripts.logins; import net.sz.framework.nio.http.NioHttpRequest; import net.sz.framework.szlog.SzLogger; import net.sz.game.login.logins.LoginManager; import net.sz.game.login.logins.iscript.ILoginScriptPlatform; /** * 100渠道登錄 * <br> * author 失足程序員<br> * blog http://www.cnblogs.com/ty408/<br> * mail 492794628@qq.com<br> * phone 13882122019<br> */ public class LoginScript100 implements ILoginScriptPlatform { private static final SzLogger log = SzLogger.getLogger(); //http://127.0.0.1:7073/login?platform=100&channel=100&username=ROBOTsz111&password=1 //http://192.168.2.235:7073/login?platform=100&channel=100&username=ROBOT111&password=1&version=1&mac64=jdjdjjd&os=ios&fr=0202125 //http://192.168.2.219:7073/login?platform=100&channel=100&username=ROBOT111&password=1&version=1&mac64=jdjdjjd&os=ios&fr=0202125 @Override public boolean login(int platform, int channelId, NioHttpRequest request) { if (100 == platform) { String username = request.getParam("username"); String password = request.getParam("password"); LoginManager.getInstance().loginScript._login(username, password, platform, channelId, request); return true; } return false; } }
200渠道登錄

package net.sz.game.login.scripts.logins; import net.sz.framework.nio.http.NioHttpRequest; import net.sz.framework.szlog.SzLogger; import net.sz.game.login.logins.LoginManager; import net.sz.game.login.logins.iscript.ILoginScriptPlatform; /** * 200渠道登錄 * <br> * author 失足程序員<br> * blog http://www.cnblogs.com/ty408/<br> * mail 492794628@qq.com<br> * phone 13882122019<br> */ public class LoginScript200 implements ILoginScriptPlatform { private static final SzLogger log = SzLogger.getLogger(); //http://127.0.0.1:7073/login?platform=100&username=ROBOT111&userpwd=1 //http://182.150.21.45:7073/login?platform=200&username=ROBOT111&password=1&version=1&mac64=jdjdjjd&os=ios&fr=0202125 @Override public boolean login(int platform, int channelId, NioHttpRequest request) { if (200 == platform) { String username = request.getParam("username"); String password = request.getParam("password"); LoginManager.getInstance().loginScript._login(username, password, platform, channelId, request); return true; } return false; } }
這時模擬以后接取渠道不同處理形式,
比如ios,360,91,豌豆莢等(拒絕廣告);
1 NettyHttpServer nioHttpServer = NettyPool.getInstance().addBindHttpServer("0.0.0.0", ServerHttpPort); 2 //如果需要加入的白名單 3 //nioHttpServer.addWhiteIP("192.168"); 4 nioHttpServer.addHttpBind((url, request) -> { 5 6 ArrayList<IHttpAPIScript> evts = ScriptManager.getInstance().getBaseScriptEntry().getEvts(IHttpAPIScript.class); 7 for (int i = 0; i < evts.size(); i++) { 8 IHttpAPIScript get = evts.get(i); 9 /*判斷監聽*/ 10 if (get.checkUrl(url)) { 11 /*處理監聽*/ 12 get.run(url, request); 13 return; 14 } 15 } 16 17 }, 20, "*");
開啟http監聽狀態;這里可能需要你閱讀之前的文章了解底層庫支持;

1 package net.sz.game.login.scripts.logins; 2 3 import 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; 17 18 /** 19 * 登錄本地系統 操作數據庫 20 * <br> 21 * author 失足程序員<br> 22 * blog http://www.cnblogs.com/ty408/<br> 23 * mail 492794628@qq.com<br> 24 * phone 13882122019<br> 25 */ 26 public class LoginScript implements ILoginScript, IInitBaseScript { 27 28 private static final SzLogger log = SzLogger.getLogger(); 29 30 private static final String LOGINPWDSIGN = "af0ca5ee6203e02ec076aa8b84385d08"; 31 32 @Override 33 public void _init() { 34 //反向注冊 35 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 45 public void _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(); 53 54 if (!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 } 61 62 if (!(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 { 76 77 /*優先獲取緩存狀態*/ 78 UserInfo userinfo = DataManager.getInstance().getUserInfoMap().get(usernameLowerCase); 79 80 if (userinfo == null) { 81 /*數據庫操作之前,加鎖*/ 82 synchronized (this) { 83 if (log.isInfoEnabled()) { 84 log.info("用戶:" + username + " 不存在緩存用戶!!!"); 85 } 86 /*再次獲取緩存狀態,存在並發,那么獲得鎖權限以后有幾率以及得到數據了*/ 87 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 { 104 105 if ("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 } 112 113 if (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 } 144 145 if (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 { 151 //token生成之后3分鍾 152 long md5time = System.currentTimeMillis(); 153 //String token = MD5Util.md5Encode('=', userinfo.getId() + "", username, request.getIp(), md5time + "", MyAttributeKey.TOKENKEY); 154 String token = MD5Util.md5Encode('=', userinfo.getId() + "", username, "", md5time + "", LOGINPWDSIGN); 155 //更新token 156 userinfo.setToken(token); 157 //更新token生成時間 158 userinfo.setTokenTime(md5time); 159 //更新最后同步時間 160 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 } 189 190 public static void 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 } 196 197 static class Ret { 198 199 private int code; 200 private String token; 201 private String userName; 202 private int msg; 203 private long time; 204 private long uid; 205 private ServerInfo[] infos; 206 207 public Ret(int code, int msg) { 208 this.code = code; 209 this.msg = msg; 210 } 211 212 public Ret() { 213 } 214 215 public 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 } 223 224 /** 225 * @return the code 226 */ 227 public int getCode() { 228 return code; 229 } 230 231 /** 232 * @param code the code to set 233 */ 234 public void setCode(int code) { 235 this.code = code; 236 } 237 238 /** 239 * @return the token 240 */ 241 public String getToken() { 242 return token; 243 } 244 245 /** 246 * @param token the token to set 247 */ 248 public void setToken(String token) { 249 this.token = token; 250 } 251 252 /** 253 * @return the userName 254 */ 255 public String getUserName() { 256 return userName; 257 } 258 259 /** 260 * @param userName the userName to set 261 */ 262 public void setUserName(String userName) { 263 this.userName = userName; 264 } 265 266 /** 267 * @return the msg 268 */ 269 public int getMsg() { 270 return msg; 271 } 272 273 /** 274 * @param msg the msg to set 275 */ 276 public void setMsg(int msg) { 277 this.msg = msg; 278 } 279 280 /** 281 * @return the time 282 */ 283 public long getTime() { 284 return time; 285 } 286 287 /** 288 * @param time the time to set 289 */ 290 public void setTime(long time) { 291 this.time = time; 292 } 293 294 /** 295 * @return the uid 296 */ 297 public long getUid() { 298 return uid; 299 } 300 301 /** 302 * @param uid the uid to set 303 */ 304 public void setUid(long uid) { 305 this.uid = uid; 306 } 307 308 /** 309 * @return the infos 310 */ 311 public ServerInfo[] getInfos() { 312 return infos; 313 } 314 315 /** 316 * @param infos the infos to set 317 */ 318 public void setInfos(ServerInfo[] infos) { 319 this.infos = infos; 320 } 321 322 } 323 324 }
整個最后登錄流程。設計;
整個登錄流程
http 請求 -》 流向 http api -》 httploginscript -》 loginscript渠道登錄 -》 loginscript 登錄 -》緩存驗證 -》 數據庫驗證 -》 返回結果;
C#代碼測試調用

1 using Net.Sz.Framework.Netty.Http; 2 using Net.Sz.Framework.Util; 3 using System; 4 using System.Collections.Generic; 5 using System.Linq; 6 using System.Text; 7 using System.Threading; 8 using System.Threading.Tasks; 9 10 11 namespace CApp_CheckLoginTps 12 { 13 14 class Program 15 { 16 17 static List<int> idList = new List<int>(); 18 static IntegerSSId ids = new IntegerSSId(); 19 20 static void Main(string[] args) 21 { 22 Console.WriteLine("准備就緒"); 23 while (true) 24 { 25 Console.ReadLine(); 26 Console.WriteLine("注冊登錄"); 27 test(); 28 Console.ReadLine(); 29 Console.WriteLine("緩存登錄"); 30 test2(); 31 } 32 Console.ReadLine(); 33 } 34 35 36 static void test() 37 { 38 Program.idList.Clear(); 39 int tcount = 2; 40 for (int i = 1; i <= tcount; i++) 41 { 42 new Thread(() => 43 { 44 System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); 45 watch.Start(); 46 int id = ids.GetId(); 47 Program.idList.Add(id); 48 string ret = HttpClient.UrlGet("http://192.168.2.235:7073/login?platform=100&channel=100&username=" + (id) + "&password=1&version=1&mac64=jdjdjjd&os=ios&fr=0202125"); 49 watch.Stop(); 50 Console.WriteLine(watch.ElapsedMilliseconds); 51 }).Start(); 52 } 53 } 54 55 static void test2() 56 { 57 int tcount = Program.idList.Count; 58 59 for (int i = 0; i < tcount; i++) 60 { 61 new Thread(new ParameterizedThreadStart((object obj) => 62 { 63 System.Diagnostics.Stopwatch watch = new System.Diagnostics.Stopwatch(); 64 watch.Start(); 65 string ret = HttpClient.UrlGet("http://192.168.2.235:7073/login?platform=100&channel=100&username=" + (obj) + "&password=1&version=1&mac64=jdjdjjd&os=ios&fr=0202125"); 66 watch.Stop(); 67 Console.WriteLine(watch.ElapsedMilliseconds); 68 })).Start(Program.idList[i]); 69 } 70 } 71 } 72 73 }
測試結果:
[04-12 15:26:06:408:INFO :LoginScript._login():95] 登錄耗時 326060000 4 :5 [04-12 15:26:06:408:INFO :LoginScript._login():114] 用戶:326060000 數據庫不存在!!!創建用戶 [04-12 15:26:06:408:INFO :LoginScript._login():136] 登錄耗時 326060000 5 :5 [04-12 15:26:06:408:INFO :LoginScript._login():164] 登錄耗時 326060000 6 :5 [04-12 15:26:06:408:INFO :LoginScript._login():166] 登錄耗時 326060000 7 :5 [04-12 15:26:06:408:INFO :LoginScript._login():173] 登錄耗時 326060000 8 :5 [04-12 15:26:06:408:INFO :LoginScript._login():178] 登錄耗時 326060000 8 :5 [04-12 15:26:06:408:INFO :LoginScript._login():180] 平台:100, ip:192.168.2.235, 用戶:326060000 用戶登錄完成!!! [04-12 15:26:06:409:INFO :HttpLoginScript.run():50] 處理一個登陸耗時:6 [04-12 15:26:07:043:INFO :LoginScript._login():50] 登錄耗時 326060000 1 :0 [04-12 15:26:07:043:INFO :LoginScript._login():50] 登錄耗時 326060001 1 :0 [04-12 15:26:07:043:INFO :LoginScript._login():73] 登錄耗時 326060000 2 :0 [04-12 15:26:07:043:INFO :LoginScript._login():73] 登錄耗時 326060001 2 :0 [04-12 15:26:07:043:INFO :LoginScript._login():141] 平台:100, ip:192.168.2.235, 用戶:326060000 緩存用戶!!! [04-12 15:26:07:043:INFO :LoginScript._login():141] 平台:100, ip:192.168.2.235, 用戶:326060001 緩存用戶!!! [04-12 15:26:07:043:INFO :LoginScript._login():164] 登錄耗時 326060000 6 :0 [04-12 15:26:07:043:INFO :LoginScript._login():164] 登錄耗時 326060001 6 :0 [04-12 15:26:07:043:INFO :LoginScript._login():166] 登錄耗時 326060000 7 :0 [04-12 15:26:07:043:INFO :LoginScript._login():173] 登錄耗時 326060000 8 :0 [04-12 15:26:07:043:INFO :LoginScript._login():178] 登錄耗時 326060000 8 :0 [04-12 15:26:07:043:INFO :LoginScript._login():166] 登錄耗時 326060001 7 :0 [04-12 15:26:07:043:INFO :LoginScript._login():180] 平台:100, ip:192.168.2.235, 用戶:326060000 用戶登錄完成!!! [04-12 15:26:07:043:INFO :LoginScript._login():173] 登錄耗時 326060001 8 :0 [04-12 15:26:07:043:INFO :LoginScript._login():178] 登錄耗時 326060001 8 :0 [04-12 15:26:07:043:INFO :LoginScript._login():180] 平台:100, ip:192.168.2.235, 用戶:326060001 用戶登錄完成!!! [04-12 15:26:07:043:INFO :HttpLoginScript.run():50] 處理一個登陸耗時:1 [04-12 15:26:07:044:INFO :HttpLoginScript.run():50] 處理一個登陸耗時:2
加到並發效果試試
1 [04-12 15:28:34:648:INFO :LoginScript._login():95] 登錄耗時 328340007 4 :34 2 [04-12 15:28:34:648:INFO :LoginScript._login():114] 用戶:328340007 數據庫不存在!!!創建用戶 3 [04-12 15:28:34:648:INFO :LoginScript._login():136] 登錄耗時 328340007 5 :34 4 [04-12 15:28:34:648:INFO :LoginScript._login():164] 登錄耗時 328340007 6 :34 5 [04-12 15:28:34:648:INFO :LoginScript._login():166] 登錄耗時 328340007 7 :34 6 [04-12 15:28:34:648:INFO :LoginScript._login():173] 登錄耗時 328340007 8 :34 7 [04-12 15:28:34:648:INFO :LoginScript._login():178] 登錄耗時 328340007 8 :34 8 [04-12 15:28:34:648:INFO :LoginScript._login():180] 平台:100, ip:192.168.2.235, 用戶:328340007 用戶登錄完成!!! 9 [04-12 15:28:34:649:INFO :HttpLoginScript.run():50] 處理一個登陸耗時:35
當並發加到10的時候,處理登錄耗時就出現了;
我把數據庫記錄手動加到200多萬條數據庫再次測試一下;
再次嘗試注冊登錄請求的時候
直接導致線程並發死鎖;
[04-12 15:41:03:665:INFO :LoginScript._login():95] 登錄耗時 340290009 4 :34059 [04-12 15:41:03:665:INFO :LoginScript._login():114] 用戶:340290009 數據庫不存在!!!創建用戶 [04-12 15:41:03:665:INFO :LoginScript._login():136] 登錄耗時 340290009 5 :34059 [04-12 15:41:03:666:INFO :LoginScript._login():84] 用戶:340290003 不存在緩存用戶!!! [04-12 15:41:03:666:INFO :LoginScript._login():93] 登錄耗時 340290003 3 :34056 [04-12 15:41:03:667:INFO :LoginScript._login():164] 登錄耗時 340290009 6 :34061 [04-12 15:41:03:668:INFO :LoginScript._login():166] 登錄耗時 340290009 7 :34062 [04-12 15:41:03:668:INFO :LoginScript._login():173] 登錄耗時 340290009 8 :34062 [04-12 15:41:03:668:INFO :LoginScript._login():178] 登錄耗時 340290009 8 :34062 [04-12 15:41:03:668:INFO :LoginScript._login():180] 平台:100, ip:192.168.2.235, 用戶:340290009 用戶登錄完成!!! [04-12 15:41:03:671:INFO :HttpLoginScript.run():50] 處理一個登陸耗時:34065
我們從拋錯和和打印日志情況分析,出現的情況為當操作來了以后,發現緩存不存在,然后進入鎖狀態,去操作數據庫查詢;
我們看到登錄耗時 4 打印,情況發現查詢數據庫直接咯嘣;
查詢數據是否存在居然耗時34秒;
好吧,數據庫原因導致了查詢耗時;
通過軟件查詢,也依然是耗時的,排除程序查詢代碼性能問題;
然后我們通過分析userinfo類
我們通過對userinfo類的分析,我們只對id字段加入了主鍵;那么數據庫默認對id這個字段加入了索引;
然后我們每一次請求登錄的時候數據庫檢索只能通過userNameLowerCase 字段進行檢索;那么考慮對字段加入索引情況;
@Id @Column(nullable = false, unique = true) private long id; /** * */ @Column(nullable = false, unique = true) private String userName; /** * */ @Column(nullable = false, unique = true) private String userNameLowerCase;
我考慮在id,username userNameLowerCase 三個字段都加入唯一鍵索引;
我先刪除掉數據庫,再收到把數據加到200多萬測試
在改造了數據庫索引后我們
並發下我們還是看出了,登錄耗時情況;
看到這里,我們登錄的操作,已經是加入緩存處理,數據庫索引,提供查詢等操作;可並發下還是會耗時呢?
仔細看代碼發現
其實我們登錄操作, 注冊和查詢數據庫的時候,是需要加鎖,保證唯一;
但是我們忽略了一個問題,加鎖的時候,其實值加鎖,賬戶的小寫副本字符串就可以達到效果了;我這里加入了整個對象鎖;鎖的范圍過大;
/*數據庫操作之前,加鎖,鎖定賬戶小寫副本,就一定能針對單賬戶鎖定*/ synchronized (usernameLowerCase) {
[04-12 16:11:58:123:INFO :LoginScript._login():95] 登錄耗時 411580006 4 :3 [04-12 16:11:58:123:INFO :LoginScript._login():114] 用戶:411580006 數據庫不存在!!!創建用戶 [04-12 16:11:58:124:INFO :LoginScript._login():136] 登錄耗時 411580006 5 :4 [04-12 16:11:58:124:INFO :LoginScript._login():95] 登錄耗時 411580009 4 :3 [04-12 16:11:58:124:INFO :LoginScript._login():114] 用戶:411580009 數據庫不存在!!!創建用戶 [04-12 16:11:58:124:INFO :LoginScript._login():164] 登錄耗時 411580006 6 :4 [04-12 16:11:58:124:INFO :LoginScript._login():136] 登錄耗時 411580009 5 :3 [04-12 16:11:58:124:INFO :LoginScript._login():164] 登錄耗時 411580009 6 :3 [04-12 16:11:58:124:INFO :LoginScript._login():166] 登錄耗時 411580006 7 :4 [04-12 16:11:58:124:INFO :LoginScript._login():173] 登錄耗時 411580006 8 :4 [04-12 16:11:58:124:INFO :LoginScript._login():178] 登錄耗時 411580006 8 :4 [04-12 16:11:58:124:INFO :LoginScript._login():166] 登錄耗時 411580009 7 :3 [04-12 16:11:58:124:INFO :LoginScript._login():180] 平台:100, ip:192.168.2.235, 用戶:411580006 用戶登錄完成!!! [04-12 16:11:58:124:INFO :LoginScript._login():173] 登錄耗時 411580009 8 :3 [04-12 16:11:58:124:INFO :LoginScript._login():178] 登錄耗時 411580009 8 :3 [04-12 16:11:58:124:INFO :LoginScript._login():180] 平台:100, ip:192.168.2.235, 用戶:411580009 用戶登錄完成!!! [04-12 16:11:58:124:INFO :HttpLoginScript.run():50] 處理一個登陸耗時:3 [04-12 16:11:58:124:INFO :HttpLoginScript.run():50] 處理一個登陸耗時:4
現在可以看的出來,我們注冊登錄耗時,大約4毫秒了;
[04-12 16:12:55:717:INFO :HttpLoginScript.run():50] 處理一個登陸耗時:2 [04-12 16:12:55:717:INFO :LoginScript._login():166] 登錄耗時 411580009 7 :0 [04-12 16:12:55:717:INFO :LoginScript._login():173] 登錄耗時 411580009 8 :0 [04-12 16:12:55:717:INFO :LoginScript._login():178] 登錄耗時 411580009 8 :0 [04-12 16:12:55:717:INFO :LoginScript._login():180] 平台:100, ip:192.168.2.235, 用戶:411580009 用戶登錄完成!!! [04-12 16:12:55:719:INFO :LoginScript._login():166] 登錄耗時 411580006 7 :3 [04-12 16:12:55:719:INFO :LoginScript._login():173] 登錄耗時 411580006 8 :3 [04-12 16:12:55:719:INFO :LoginScript._login():178] 登錄耗時 411580006 8 :3 [04-12 16:12:55:719:INFO :HttpLoginScript.run():50] 處理一個登陸耗時:2
緩存登錄情況;
總結
本次優化的地方,重點在於;
1、防止重復注冊依賴數據庫檢查的是時候,鎖對象划分;我們正對賬號的小寫副本(String) 加鎖,是一定能鎖定的;
2、加入緩存情況,當前賬號登錄后,加入滑動緩存,2小時候清理對象;
3、優化數據庫方案,加入索引;
4、數據庫寫入操作,上文一直沒講;這里描述。
以上代碼數據庫寫入操作都是異步的,保證了數據在內存驗證通過后,創建對象,異步寫入數據庫一定能通過數據庫驗證寫入數據庫中;
采用集中批量提交數據庫方案,提高寫入優化功能;