作者 crossoverJie
... ... @@ -21,14 +21,19 @@ public enum StatusEnum {
/** 重复登录 */
REPEAT_LOGIN("5000", "Repeat login, log out an account please!"),
/** 请求限流 */
REQUEST_LIMIT("6000", "请求限流"),
/** 账号不在线 */
OFF_LINE("7000", "你选择的账号不在线,请重新选择!"),
SERVER_NOT_AVAILABLE("7100", "CIM server is not available, please try again later!"),
/** 登录信息不匹配 */
ACCOUNT_NOT_MATCH("9100", "The User information you have used is incorrect!"),
/** 请求限流 */
REQUEST_LIMIT("6000", "请求限流"),
;
... ...
package com.crossoverjie.cim.common.route.algorithm.consistenthash;
import com.crossoverjie.cim.common.enums.StatusEnum;
import com.crossoverjie.cim.common.exception.CIMException;
import java.util.SortedMap;
import java.util.TreeMap;
... ... @@ -35,6 +38,9 @@ public class TreeMapConsistentHash extends AbstractConsistentHash {
if (!last.isEmpty()) {
return last.get(last.firstKey());
}
if (treeMap.size() == 0){
throw new CIMException(StatusEnum.SERVER_NOT_AVAILABLE) ;
}
return treeMap.firstEntry().getValue();
}
}
... ...
... ... @@ -3,12 +3,14 @@ package com.crossoverjie.cim.route.controller;
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.pojo.RouteInfo;
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.CommonBizService;
import com.crossoverjie.cim.route.service.UserInfoCacheService;
import com.crossoverjie.cim.route.vo.req.ChatReqVO;
import com.crossoverjie.cim.route.vo.req.LoginReqVO;
... ... @@ -50,6 +52,8 @@ public class RouteController {
@Autowired
private UserInfoCacheService userInfoCacheService ;
@Autowired
private CommonBizService commonBizService ;
@Autowired
private RouteHandle routeHandle ;
... ... @@ -148,17 +152,19 @@ public class RouteController {
public BaseResponse<CIMServerResVO> login(@RequestBody LoginReqVO loginReqVO) throws Exception {
BaseResponse<CIMServerResVO> res = new BaseResponse();
// check server available
String server = routeHandle.routeServer(serverCache.getAll(),String.valueOf(loginReqVO.getUserId()));
RouteInfo routeInfo = RouteInfoParseUtil.parse(server);
commonBizService.checkServerAvailable(routeInfo);
//登录校验
StatusEnum status = accountService.login(loginReqVO);
if (status == StatusEnum.SUCCESS) {
String server = routeHandle.routeServer(serverCache.getAll(),String.valueOf(loginReqVO.getUserId()));
String[] serverInfo = server.split(":");
CIMServerResVO vo = new CIMServerResVO(RouteInfoParseUtil.parse(server));
//保存路由信息
accountService.saveRouteInfo(loginReqVO,server);
CIMServerResVO vo = new CIMServerResVO(routeInfo);
res.setDataBody(vo);
}
... ...
package com.crossoverjie.cim.route.exception;
import com.crossoverjie.cim.common.exception.CIMException;
import com.crossoverjie.cim.common.res.BaseResponse;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Function:
*
* @author crossoverJie
* Date: 2020-04-12 22:13
* @since JDK 1.8
*/
@ControllerAdvice
public class ExceptionHandlingController {
private static Logger logger = LoggerFactory.getLogger(ExceptionHandlingController.class) ;
@ExceptionHandler(CIMException.class)
@ResponseBody()
public BaseResponse handleAllExceptions(CIMException ex) {
logger.error("exception", ex);
BaseResponse baseResponse = new BaseResponse();
baseResponse.setCode(ex.getErrorCode());
baseResponse.setMessage(ex.getMessage());
return baseResponse ;
}
}
\ No newline at end of file
... ...
package com.crossoverjie.cim.route.service;
import com.crossoverjie.cim.common.enums.StatusEnum;
import com.crossoverjie.cim.common.exception.CIMException;
import com.crossoverjie.cim.common.pojo.RouteInfo;
import com.crossoverjie.cim.route.kit.NetAddressIsReachable;
import org.springframework.stereotype.Component;
/**
* Function:
*
* @author crossoverJie
* Date: 2020-04-12 21:40
* @since JDK 1.8
*/
@Component
public class CommonBizService {
/**
* check ip and port
* @param routeInfo
*/
public void checkServerAvailable(RouteInfo routeInfo){
boolean reachable = NetAddressIsReachable.checkAddressReachable(routeInfo.getIp(), routeInfo.getCimServerPort(), 1000);
if (!reachable) {
throw new CIMException(StatusEnum.SERVER_NOT_AVAILABLE) ;
}
}
}
... ...