作者 crossoverJie

:recycle: Refactoring code.

@@ -14,7 +14,7 @@ public enum StatusEnum { @@ -14,7 +14,7 @@ public enum StatusEnum {
14 /** 成功 */ 14 /** 成功 */
15 FALLBACK("8000", "FALL_BACK"), 15 FALLBACK("8000", "FALL_BACK"),
16 /** 参数校验失败**/ 16 /** 参数校验失败**/
17 - VALIDATION_FAIL("3000", "参数校验失败"), 17 + VALIDATION_FAIL("3000", "invalid argument"),
18 /** 失败 */ 18 /** 失败 */
19 FAIL("4000", "失败"), 19 FAIL("4000", "失败"),
20 20
  1 +package com.crossoverjie.cim.common.pojo;
  2 +
  3 +/**
  4 + * Function:
  5 + *
  6 + * @author crossoverJie
  7 + * Date: 2020-04-12 20:48
  8 + * @since JDK 1.8
  9 + */
  10 +public final class RouteInfo {
  11 +
  12 + private String ip ;
  13 + private Integer cimServerPort;
  14 + private Integer httpPort;
  15 +
  16 + public RouteInfo(String ip, Integer cimServerPort, Integer httpPort) {
  17 + this.ip = ip;
  18 + this.cimServerPort = cimServerPort;
  19 + this.httpPort = httpPort;
  20 + }
  21 +
  22 + public String getIp() {
  23 + return ip;
  24 + }
  25 +
  26 + public void setIp(String ip) {
  27 + this.ip = ip;
  28 + }
  29 +
  30 + public Integer getCimServerPort() {
  31 + return cimServerPort;
  32 + }
  33 +
  34 + public void setCimServerPort(Integer cimServerPort) {
  35 + this.cimServerPort = cimServerPort;
  36 + }
  37 +
  38 + public Integer getHttpPort() {
  39 + return httpPort;
  40 + }
  41 +
  42 + public void setHttpPort(Integer httpPort) {
  43 + this.httpPort = httpPort;
  44 + }
  45 +}
  1 +package com.crossoverjie.cim.common.util;
  2 +
  3 +import com.crossoverjie.cim.common.exception.CIMException;
  4 +import com.crossoverjie.cim.common.pojo.RouteInfo;
  5 +
  6 +import static com.crossoverjie.cim.common.enums.StatusEnum.VALIDATION_FAIL;
  7 +
  8 +/**
  9 + * Function:
  10 + *
  11 + * @author crossoverJie
  12 + * Date: 2020-04-12 20:42
  13 + * @since JDK 1.8
  14 + */
  15 +public class RouteInfoParseUtil {
  16 +
  17 + public static RouteInfo parse(String info){
  18 + try {
  19 + String[] serverInfo = info.split(":");
  20 + RouteInfo routeInfo = new RouteInfo(serverInfo[0], Integer.parseInt(serverInfo[1]),Integer.parseInt(serverInfo[2])) ;
  21 + return routeInfo ;
  22 + }catch (Exception e){
  23 + throw new CIMException(VALIDATION_FAIL) ;
  24 + }
  25 + }
  26 +}
@@ -6,6 +6,7 @@ import com.crossoverjie.cim.common.pojo.CIMUserInfo; @@ -6,6 +6,7 @@ import com.crossoverjie.cim.common.pojo.CIMUserInfo;
6 import com.crossoverjie.cim.common.res.BaseResponse; 6 import com.crossoverjie.cim.common.res.BaseResponse;
7 import com.crossoverjie.cim.common.res.NULLBody; 7 import com.crossoverjie.cim.common.res.NULLBody;
8 import com.crossoverjie.cim.common.route.algorithm.RouteHandle; 8 import com.crossoverjie.cim.common.route.algorithm.RouteHandle;
  9 +import com.crossoverjie.cim.common.util.RouteInfoParseUtil;
9 import com.crossoverjie.cim.route.cache.ServerCache; 10 import com.crossoverjie.cim.route.cache.ServerCache;
10 import com.crossoverjie.cim.route.service.AccountService; 11 import com.crossoverjie.cim.route.service.AccountService;
11 import com.crossoverjie.cim.route.service.UserInfoCacheService; 12 import com.crossoverjie.cim.route.service.UserInfoCacheService;
@@ -153,7 +154,7 @@ public class RouteController { @@ -153,7 +154,7 @@ public class RouteController {
153 154
154 String server = routeHandle.routeServer(serverCache.getAll(),String.valueOf(loginReqVO.getUserId())); 155 String server = routeHandle.routeServer(serverCache.getAll(),String.valueOf(loginReqVO.getUserId()));
155 String[] serverInfo = server.split(":"); 156 String[] serverInfo = server.split(":");
156 - CIMServerResVO vo = new CIMServerResVO(serverInfo[0], Integer.parseInt(serverInfo[1]),Integer.parseInt(serverInfo[2])); 157 + CIMServerResVO vo = new CIMServerResVO(RouteInfoParseUtil.parse(server));
157 158
158 //保存路由信息 159 //保存路由信息
159 accountService.saveRouteInfo(loginReqVO,server); 160 accountService.saveRouteInfo(loginReqVO,server);
@@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject; @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
4 import com.crossoverjie.cim.common.enums.StatusEnum; 4 import com.crossoverjie.cim.common.enums.StatusEnum;
5 import com.crossoverjie.cim.common.exception.CIMException; 5 import com.crossoverjie.cim.common.exception.CIMException;
6 import com.crossoverjie.cim.common.pojo.CIMUserInfo; 6 import com.crossoverjie.cim.common.pojo.CIMUserInfo;
  7 +import com.crossoverjie.cim.common.util.RouteInfoParseUtil;
7 import com.crossoverjie.cim.route.service.AccountService; 8 import com.crossoverjie.cim.route.service.AccountService;
8 import com.crossoverjie.cim.route.service.UserInfoCacheService; 9 import com.crossoverjie.cim.route.service.UserInfoCacheService;
9 import com.crossoverjie.cim.route.vo.req.ChatReqVO; 10 import com.crossoverjie.cim.route.vo.req.ChatReqVO;
@@ -33,7 +34,7 @@ import static com.crossoverjie.cim.route.constant.Constant.ROUTE_PREFIX; @@ -33,7 +34,7 @@ import static com.crossoverjie.cim.route.constant.Constant.ROUTE_PREFIX;
33 * Function: 34 * Function:
34 * 35 *
35 * @author crossoverJie 36 * @author crossoverJie
36 - * Date: 2018/12/23 21:58 37 + * Date: 2018/12/23 21:58
37 * @since JDK 1.8 38 * @since JDK 1.8
38 */ 39 */
39 @Service 40 @Service
@@ -44,7 +45,7 @@ public class AccountServiceRedisImpl implements AccountService { @@ -44,7 +45,7 @@ public class AccountServiceRedisImpl implements AccountService {
44 private RedisTemplate<String, String> redisTemplate; 45 private RedisTemplate<String, String> redisTemplate;
45 46
46 @Autowired 47 @Autowired
47 - private UserInfoCacheService userInfoCacheService ; 48 + private UserInfoCacheService userInfoCacheService;
48 49
49 @Autowired 50 @Autowired
50 private OkHttpClient okHttpClient; 51 private OkHttpClient okHttpClient;
@@ -84,9 +85,9 @@ public class AccountServiceRedisImpl implements AccountService { @@ -84,9 +85,9 @@ public class AccountServiceRedisImpl implements AccountService {
84 85
85 //登录成功,保存登录状态 86 //登录成功,保存登录状态
86 boolean status = userInfoCacheService.saveAndCheckUserLoginStatus(loginReqVO.getUserId()); 87 boolean status = userInfoCacheService.saveAndCheckUserLoginStatus(loginReqVO.getUserId());
87 - if (status == false){ 88 + if (status == false) {
88 //重复登录 89 //重复登录
89 - return StatusEnum.REPEAT_LOGIN ; 90 + return StatusEnum.REPEAT_LOGIN;
90 } 91 }
91 92
92 return StatusEnum.SUCCESS; 93 return StatusEnum.SUCCESS;
@@ -120,7 +121,7 @@ public class AccountServiceRedisImpl implements AccountService { @@ -120,7 +121,7 @@ public class AccountServiceRedisImpl implements AccountService {
120 try { 121 try {
121 scan.close(); 122 scan.close();
122 } catch (IOException e) { 123 } catch (IOException e) {
123 - LOGGER.error("IOException",e); 124 + LOGGER.error("IOException", e);
124 } 125 }
125 126
126 return routes; 127 return routes;
@@ -130,20 +131,18 @@ public class AccountServiceRedisImpl implements AccountService { @@ -130,20 +131,18 @@ public class AccountServiceRedisImpl implements AccountService {
130 public CIMServerResVO loadRouteRelatedByUserId(Long userId) { 131 public CIMServerResVO loadRouteRelatedByUserId(Long userId) {
131 String value = redisTemplate.opsForValue().get(ROUTE_PREFIX + userId); 132 String value = redisTemplate.opsForValue().get(ROUTE_PREFIX + userId);
132 133
133 - if (value == null){  
134 - throw new CIMException(OFF_LINE) ; 134 + if (value == null) {
  135 + throw new CIMException(OFF_LINE);
135 } 136 }
136 137
137 - String[] server = value.split(":");  
138 - CIMServerResVO cimServerResVO = new CIMServerResVO(server[0], Integer.parseInt(server[1]), Integer.parseInt(server[2])); 138 + CIMServerResVO cimServerResVO = new CIMServerResVO(RouteInfoParseUtil.parse(value));
139 return cimServerResVO; 139 return cimServerResVO;
140 } 140 }
141 141
142 private void parseServerInfo(Map<Long, CIMServerResVO> routes, String key) { 142 private void parseServerInfo(Map<Long, CIMServerResVO> routes, String key) {
143 long userId = Long.valueOf(key.split(":")[1]); 143 long userId = Long.valueOf(key.split(":")[1]);
144 String value = redisTemplate.opsForValue().get(key); 144 String value = redisTemplate.opsForValue().get(key);
145 - String[] server = value.split(":");  
146 - CIMServerResVO cimServerResVO = new CIMServerResVO(server[0], Integer.parseInt(server[1]), Integer.parseInt(server[2])); 145 + CIMServerResVO cimServerResVO = new CIMServerResVO(RouteInfoParseUtil.parse(value));
147 routes.put(userId, cimServerResVO); 146 routes.put(userId, cimServerResVO);
148 } 147 }
149 148
@@ -167,7 +166,7 @@ public class AccountServiceRedisImpl implements AccountService { @@ -167,7 +166,7 @@ public class AccountServiceRedisImpl implements AccountService {
167 if (!response.isSuccessful()) { 166 if (!response.isSuccessful()) {
168 throw new IOException("Unexpected code " + response); 167 throw new IOException("Unexpected code " + response);
169 } 168 }
170 - }finally { 169 + } finally {
171 response.body().close(); 170 response.body().close();
172 } 171 }
173 } 172 }
@@ -178,7 +177,7 @@ public class AccountServiceRedisImpl implements AccountService { @@ -178,7 +177,7 @@ public class AccountServiceRedisImpl implements AccountService {
178 // TODO: 2019-01-21 改为一个原子命令,以防数据一致性 177 // TODO: 2019-01-21 改为一个原子命令,以防数据一致性
179 178
180 //删除路由 179 //删除路由
181 - redisTemplate.delete(ROUTE_PREFIX + userId) ; 180 + redisTemplate.delete(ROUTE_PREFIX + userId);
182 181
183 //删除登录状态 182 //删除登录状态
184 userInfoCacheService.removeLoginStatus(userId); 183 userInfoCacheService.removeLoginStatus(userId);
1 package com.crossoverjie.cim.route.vo.res; 1 package com.crossoverjie.cim.route.vo.res;
2 2
  3 +import com.crossoverjie.cim.common.pojo.RouteInfo;
  4 +
3 import java.io.Serializable; 5 import java.io.Serializable;
4 6
5 /** 7 /**
@@ -15,10 +17,10 @@ public class CIMServerResVO implements Serializable { @@ -15,10 +17,10 @@ public class CIMServerResVO implements Serializable {
15 private Integer cimServerPort; 17 private Integer cimServerPort;
16 private Integer httpPort; 18 private Integer httpPort;
17 19
18 - public CIMServerResVO(String ip, Integer cimServerPort, Integer httpPort) {  
19 - this.ip = ip;  
20 - this.cimServerPort = cimServerPort;  
21 - this.httpPort = httpPort; 20 + public CIMServerResVO(RouteInfo routeInfo) {
  21 + this.ip = routeInfo.getIp();
  22 + this.cimServerPort = routeInfo.getCimServerPort();
  23 + this.httpPort = routeInfo.getHttpPort();
22 } 24 }
23 25
24 public String getIp() { 26 public String getIp() {