作者 crossoverJie

:recycle: Refactoring code.

... ... @@ -14,7 +14,7 @@ public enum StatusEnum {
/** 成功 */
FALLBACK("8000", "FALL_BACK"),
/** 参数校验失败**/
VALIDATION_FAIL("3000", "参数校验失败"),
VALIDATION_FAIL("3000", "invalid argument"),
/** 失败 */
FAIL("4000", "失败"),
... ...
package com.crossoverjie.cim.common.pojo;
/**
* Function:
*
* @author crossoverJie
* Date: 2020-04-12 20:48
* @since JDK 1.8
*/
public final class RouteInfo {
private String ip ;
private Integer cimServerPort;
private Integer httpPort;
public RouteInfo(String ip, Integer cimServerPort, Integer httpPort) {
this.ip = ip;
this.cimServerPort = cimServerPort;
this.httpPort = httpPort;
}
public String getIp() {
return ip;
}
public void setIp(String ip) {
this.ip = ip;
}
public Integer getCimServerPort() {
return cimServerPort;
}
public void setCimServerPort(Integer cimServerPort) {
this.cimServerPort = cimServerPort;
}
public Integer getHttpPort() {
return httpPort;
}
public void setHttpPort(Integer httpPort) {
this.httpPort = httpPort;
}
}
... ...
package com.crossoverjie.cim.common.util;
import com.crossoverjie.cim.common.exception.CIMException;
import com.crossoverjie.cim.common.pojo.RouteInfo;
import static com.crossoverjie.cim.common.enums.StatusEnum.VALIDATION_FAIL;
/**
* Function:
*
* @author crossoverJie
* Date: 2020-04-12 20:42
* @since JDK 1.8
*/
public class RouteInfoParseUtil {
public static RouteInfo parse(String info){
try {
String[] serverInfo = info.split(":");
RouteInfo routeInfo = new RouteInfo(serverInfo[0], Integer.parseInt(serverInfo[1]),Integer.parseInt(serverInfo[2])) ;
return routeInfo ;
}catch (Exception e){
throw new CIMException(VALIDATION_FAIL) ;
}
}
}
... ...
... ... @@ -6,6 +6,7 @@ import com.crossoverjie.cim.common.pojo.CIMUserInfo;
import com.crossoverjie.cim.common.res.BaseResponse;
import com.crossoverjie.cim.common.res.NULLBody;
import com.crossoverjie.cim.common.route.algorithm.RouteHandle;
import com.crossoverjie.cim.common.util.RouteInfoParseUtil;
import com.crossoverjie.cim.route.cache.ServerCache;
import com.crossoverjie.cim.route.service.AccountService;
import com.crossoverjie.cim.route.service.UserInfoCacheService;
... ... @@ -153,7 +154,7 @@ public class RouteController {
String server = routeHandle.routeServer(serverCache.getAll(),String.valueOf(loginReqVO.getUserId()));
String[] serverInfo = server.split(":");
CIMServerResVO vo = new CIMServerResVO(serverInfo[0], Integer.parseInt(serverInfo[1]),Integer.parseInt(serverInfo[2]));
CIMServerResVO vo = new CIMServerResVO(RouteInfoParseUtil.parse(server));
//保存路由信息
accountService.saveRouteInfo(loginReqVO,server);
... ...
... ... @@ -4,6 +4,7 @@ import com.alibaba.fastjson.JSONObject;
import com.crossoverjie.cim.common.enums.StatusEnum;
import com.crossoverjie.cim.common.exception.CIMException;
import com.crossoverjie.cim.common.pojo.CIMUserInfo;
import com.crossoverjie.cim.common.util.RouteInfoParseUtil;
import com.crossoverjie.cim.route.service.AccountService;
import com.crossoverjie.cim.route.service.UserInfoCacheService;
import com.crossoverjie.cim.route.vo.req.ChatReqVO;
... ... @@ -33,7 +34,7 @@ import static com.crossoverjie.cim.route.constant.Constant.ROUTE_PREFIX;
* Function:
*
* @author crossoverJie
* Date: 2018/12/23 21:58
* Date: 2018/12/23 21:58
* @since JDK 1.8
*/
@Service
... ... @@ -44,7 +45,7 @@ public class AccountServiceRedisImpl implements AccountService {
private RedisTemplate<String, String> redisTemplate;
@Autowired
private UserInfoCacheService userInfoCacheService ;
private UserInfoCacheService userInfoCacheService;
@Autowired
private OkHttpClient okHttpClient;
... ... @@ -84,9 +85,9 @@ public class AccountServiceRedisImpl implements AccountService {
//登录成功,保存登录状态
boolean status = userInfoCacheService.saveAndCheckUserLoginStatus(loginReqVO.getUserId());
if (status == false){
if (status == false) {
//重复登录
return StatusEnum.REPEAT_LOGIN ;
return StatusEnum.REPEAT_LOGIN;
}
return StatusEnum.SUCCESS;
... ... @@ -120,7 +121,7 @@ public class AccountServiceRedisImpl implements AccountService {
try {
scan.close();
} catch (IOException e) {
LOGGER.error("IOException",e);
LOGGER.error("IOException", e);
}
return routes;
... ... @@ -130,20 +131,18 @@ public class AccountServiceRedisImpl implements AccountService {
public CIMServerResVO loadRouteRelatedByUserId(Long userId) {
String value = redisTemplate.opsForValue().get(ROUTE_PREFIX + userId);
if (value == null){
throw new CIMException(OFF_LINE) ;
if (value == null) {
throw new CIMException(OFF_LINE);
}
String[] server = value.split(":");
CIMServerResVO cimServerResVO = new CIMServerResVO(server[0], Integer.parseInt(server[1]), Integer.parseInt(server[2]));
CIMServerResVO cimServerResVO = new CIMServerResVO(RouteInfoParseUtil.parse(value));
return cimServerResVO;
}
private void parseServerInfo(Map<Long, CIMServerResVO> routes, String key) {
long userId = Long.valueOf(key.split(":")[1]);
String value = redisTemplate.opsForValue().get(key);
String[] server = value.split(":");
CIMServerResVO cimServerResVO = new CIMServerResVO(server[0], Integer.parseInt(server[1]), Integer.parseInt(server[2]));
CIMServerResVO cimServerResVO = new CIMServerResVO(RouteInfoParseUtil.parse(value));
routes.put(userId, cimServerResVO);
}
... ... @@ -167,7 +166,7 @@ public class AccountServiceRedisImpl implements AccountService {
if (!response.isSuccessful()) {
throw new IOException("Unexpected code " + response);
}
}finally {
} finally {
response.body().close();
}
}
... ... @@ -178,7 +177,7 @@ public class AccountServiceRedisImpl implements AccountService {
// TODO: 2019-01-21 改为一个原子命令,以防数据一致性
//删除路由
redisTemplate.delete(ROUTE_PREFIX + userId) ;
redisTemplate.delete(ROUTE_PREFIX + userId);
//删除登录状态
userInfoCacheService.removeLoginStatus(userId);
... ...
package com.crossoverjie.cim.route.vo.res;
import com.crossoverjie.cim.common.pojo.RouteInfo;
import java.io.Serializable;
/**
... ... @@ -15,10 +17,10 @@ public class CIMServerResVO implements Serializable {
private Integer cimServerPort;
private Integer httpPort;
public CIMServerResVO(String ip, Integer cimServerPort, Integer httpPort) {
this.ip = ip;
this.cimServerPort = cimServerPort;
this.httpPort = httpPort;
public CIMServerResVO(RouteInfo routeInfo) {
this.ip = routeInfo.getIp();
this.cimServerPort = routeInfo.getCimServerPort();
this.httpPort = routeInfo.getHttpPort();
}
public String getIp() {
... ...