Fixing a bug.
https://github.com/crossoverJie/cim/issues/28
正在显示
5 个修改的文件
包含
86 行增加
和
6 行删除
| @@ -21,14 +21,19 @@ public enum StatusEnum { | @@ -21,14 +21,19 @@ public enum StatusEnum { | ||
| 21 | /** 重复登录 */ | 21 | /** 重复登录 */ |
| 22 | REPEAT_LOGIN("5000", "Repeat login, log out an account please!"), | 22 | REPEAT_LOGIN("5000", "Repeat login, log out an account please!"), |
| 23 | 23 | ||
| 24 | + /** 请求限流 */ | ||
| 25 | + REQUEST_LIMIT("6000", "请求限流"), | ||
| 26 | + | ||
| 24 | /** 账号不在线 */ | 27 | /** 账号不在线 */ |
| 25 | OFF_LINE("7000", "你选择的账号不在线,请重新选择!"), | 28 | OFF_LINE("7000", "你选择的账号不在线,请重新选择!"), |
| 26 | 29 | ||
| 30 | + SERVER_NOT_AVAILABLE("7100", "CIM server is not available, please try again later!"), | ||
| 31 | + | ||
| 27 | /** 登录信息不匹配 */ | 32 | /** 登录信息不匹配 */ |
| 28 | ACCOUNT_NOT_MATCH("9100", "The User information you have used is incorrect!"), | 33 | ACCOUNT_NOT_MATCH("9100", "The User information you have used is incorrect!"), |
| 29 | 34 | ||
| 30 | - /** 请求限流 */ | ||
| 31 | - REQUEST_LIMIT("6000", "请求限流"), | 35 | + |
| 36 | + | ||
| 32 | ; | 37 | ; |
| 33 | 38 | ||
| 34 | 39 |
| 1 | package com.crossoverjie.cim.common.route.algorithm.consistenthash; | 1 | package com.crossoverjie.cim.common.route.algorithm.consistenthash; |
| 2 | 2 | ||
| 3 | +import com.crossoverjie.cim.common.enums.StatusEnum; | ||
| 4 | +import com.crossoverjie.cim.common.exception.CIMException; | ||
| 5 | + | ||
| 3 | import java.util.SortedMap; | 6 | import java.util.SortedMap; |
| 4 | import java.util.TreeMap; | 7 | import java.util.TreeMap; |
| 5 | 8 | ||
| @@ -35,6 +38,9 @@ public class TreeMapConsistentHash extends AbstractConsistentHash { | @@ -35,6 +38,9 @@ public class TreeMapConsistentHash extends AbstractConsistentHash { | ||
| 35 | if (!last.isEmpty()) { | 38 | if (!last.isEmpty()) { |
| 36 | return last.get(last.firstKey()); | 39 | return last.get(last.firstKey()); |
| 37 | } | 40 | } |
| 41 | + if (treeMap.size() == 0){ | ||
| 42 | + throw new CIMException(StatusEnum.SERVER_NOT_AVAILABLE) ; | ||
| 43 | + } | ||
| 38 | return treeMap.firstEntry().getValue(); | 44 | return treeMap.firstEntry().getValue(); |
| 39 | } | 45 | } |
| 40 | } | 46 | } |
| @@ -3,12 +3,14 @@ package com.crossoverjie.cim.route.controller; | @@ -3,12 +3,14 @@ package com.crossoverjie.cim.route.controller; | ||
| 3 | import com.crossoverjie.cim.common.enums.StatusEnum; | 3 | import com.crossoverjie.cim.common.enums.StatusEnum; |
| 4 | import com.crossoverjie.cim.common.exception.CIMException; | 4 | import com.crossoverjie.cim.common.exception.CIMException; |
| 5 | import com.crossoverjie.cim.common.pojo.CIMUserInfo; | 5 | import com.crossoverjie.cim.common.pojo.CIMUserInfo; |
| 6 | +import com.crossoverjie.cim.common.pojo.RouteInfo; | ||
| 6 | import com.crossoverjie.cim.common.res.BaseResponse; | 7 | import com.crossoverjie.cim.common.res.BaseResponse; |
| 7 | import com.crossoverjie.cim.common.res.NULLBody; | 8 | import com.crossoverjie.cim.common.res.NULLBody; |
| 8 | import com.crossoverjie.cim.common.route.algorithm.RouteHandle; | 9 | import com.crossoverjie.cim.common.route.algorithm.RouteHandle; |
| 9 | import com.crossoverjie.cim.common.util.RouteInfoParseUtil; | 10 | import com.crossoverjie.cim.common.util.RouteInfoParseUtil; |
| 10 | import com.crossoverjie.cim.route.cache.ServerCache; | 11 | import com.crossoverjie.cim.route.cache.ServerCache; |
| 11 | import com.crossoverjie.cim.route.service.AccountService; | 12 | import com.crossoverjie.cim.route.service.AccountService; |
| 13 | +import com.crossoverjie.cim.route.service.CommonBizService; | ||
| 12 | import com.crossoverjie.cim.route.service.UserInfoCacheService; | 14 | import com.crossoverjie.cim.route.service.UserInfoCacheService; |
| 13 | import com.crossoverjie.cim.route.vo.req.ChatReqVO; | 15 | import com.crossoverjie.cim.route.vo.req.ChatReqVO; |
| 14 | import com.crossoverjie.cim.route.vo.req.LoginReqVO; | 16 | import com.crossoverjie.cim.route.vo.req.LoginReqVO; |
| @@ -50,6 +52,8 @@ public class RouteController { | @@ -50,6 +52,8 @@ public class RouteController { | ||
| 50 | @Autowired | 52 | @Autowired |
| 51 | private UserInfoCacheService userInfoCacheService ; | 53 | private UserInfoCacheService userInfoCacheService ; |
| 52 | 54 | ||
| 55 | + @Autowired | ||
| 56 | + private CommonBizService commonBizService ; | ||
| 53 | 57 | ||
| 54 | @Autowired | 58 | @Autowired |
| 55 | private RouteHandle routeHandle ; | 59 | private RouteHandle routeHandle ; |
| @@ -148,17 +152,19 @@ public class RouteController { | @@ -148,17 +152,19 @@ public class RouteController { | ||
| 148 | public BaseResponse<CIMServerResVO> login(@RequestBody LoginReqVO loginReqVO) throws Exception { | 152 | public BaseResponse<CIMServerResVO> login(@RequestBody LoginReqVO loginReqVO) throws Exception { |
| 149 | BaseResponse<CIMServerResVO> res = new BaseResponse(); | 153 | BaseResponse<CIMServerResVO> res = new BaseResponse(); |
| 150 | 154 | ||
| 155 | + // check server available | ||
| 156 | + String server = routeHandle.routeServer(serverCache.getAll(),String.valueOf(loginReqVO.getUserId())); | ||
| 157 | + RouteInfo routeInfo = RouteInfoParseUtil.parse(server); | ||
| 158 | + commonBizService.checkServerAvailable(routeInfo); | ||
| 159 | + | ||
| 151 | //登录校验 | 160 | //登录校验 |
| 152 | StatusEnum status = accountService.login(loginReqVO); | 161 | StatusEnum status = accountService.login(loginReqVO); |
| 153 | if (status == StatusEnum.SUCCESS) { | 162 | if (status == StatusEnum.SUCCESS) { |
| 154 | 163 | ||
| 155 | - String server = routeHandle.routeServer(serverCache.getAll(),String.valueOf(loginReqVO.getUserId())); | ||
| 156 | - String[] serverInfo = server.split(":"); | ||
| 157 | - CIMServerResVO vo = new CIMServerResVO(RouteInfoParseUtil.parse(server)); | ||
| 158 | - | ||
| 159 | //保存路由信息 | 164 | //保存路由信息 |
| 160 | accountService.saveRouteInfo(loginReqVO,server); | 165 | accountService.saveRouteInfo(loginReqVO,server); |
| 161 | 166 | ||
| 167 | + CIMServerResVO vo = new CIMServerResVO(routeInfo); | ||
| 162 | res.setDataBody(vo); | 168 | res.setDataBody(vo); |
| 163 | 169 | ||
| 164 | } | 170 | } |
| 1 | +package com.crossoverjie.cim.route.exception; | ||
| 2 | + | ||
| 3 | +import com.crossoverjie.cim.common.exception.CIMException; | ||
| 4 | +import com.crossoverjie.cim.common.res.BaseResponse; | ||
| 5 | +import org.slf4j.Logger; | ||
| 6 | +import org.slf4j.LoggerFactory; | ||
| 7 | +import org.springframework.web.bind.annotation.ControllerAdvice; | ||
| 8 | +import org.springframework.web.bind.annotation.ExceptionHandler; | ||
| 9 | +import org.springframework.web.bind.annotation.ResponseBody; | ||
| 10 | + | ||
| 11 | +/** | ||
| 12 | + * Function: | ||
| 13 | + * | ||
| 14 | + * @author crossoverJie | ||
| 15 | + * Date: 2020-04-12 22:13 | ||
| 16 | + * @since JDK 1.8 | ||
| 17 | + */ | ||
| 18 | +@ControllerAdvice | ||
| 19 | +public class ExceptionHandlingController { | ||
| 20 | + | ||
| 21 | + private static Logger logger = LoggerFactory.getLogger(ExceptionHandlingController.class) ; | ||
| 22 | + | ||
| 23 | + @ExceptionHandler(CIMException.class) | ||
| 24 | + @ResponseBody() | ||
| 25 | + public BaseResponse handleAllExceptions(CIMException ex) { | ||
| 26 | + logger.error("exception", ex); | ||
| 27 | + BaseResponse baseResponse = new BaseResponse(); | ||
| 28 | + baseResponse.setCode(ex.getErrorCode()); | ||
| 29 | + baseResponse.setMessage(ex.getMessage()); | ||
| 30 | + return baseResponse ; | ||
| 31 | + } | ||
| 32 | + | ||
| 33 | +} |
| 1 | +package com.crossoverjie.cim.route.service; | ||
| 2 | + | ||
| 3 | +import com.crossoverjie.cim.common.enums.StatusEnum; | ||
| 4 | +import com.crossoverjie.cim.common.exception.CIMException; | ||
| 5 | +import com.crossoverjie.cim.common.pojo.RouteInfo; | ||
| 6 | +import com.crossoverjie.cim.route.kit.NetAddressIsReachable; | ||
| 7 | +import org.springframework.stereotype.Component; | ||
| 8 | + | ||
| 9 | +/** | ||
| 10 | + * Function: | ||
| 11 | + * | ||
| 12 | + * @author crossoverJie | ||
| 13 | + * Date: 2020-04-12 21:40 | ||
| 14 | + * @since JDK 1.8 | ||
| 15 | + */ | ||
| 16 | +@Component | ||
| 17 | +public class CommonBizService { | ||
| 18 | + | ||
| 19 | + /** | ||
| 20 | + * check ip and port | ||
| 21 | + * @param routeInfo | ||
| 22 | + */ | ||
| 23 | + public void checkServerAvailable(RouteInfo routeInfo){ | ||
| 24 | + boolean reachable = NetAddressIsReachable.checkAddressReachable(routeInfo.getIp(), routeInfo.getCimServerPort(), 1000); | ||
| 25 | + if (!reachable) { | ||
| 26 | + throw new CIMException(StatusEnum.SERVER_NOT_AVAILABLE) ; | ||
| 27 | + } | ||
| 28 | + | ||
| 29 | + } | ||
| 30 | +} |
-
请 注册 或 登录 后发表评论