提交者
GitHub
Merge pull request #87 from crossoverJie/fix-issue-28
Fix issue 28
正在显示
20 个修改的文件
包含
257 行增加
和
44 行删除
| @@ -112,7 +112,7 @@ public class CIMClient { | @@ -112,7 +112,7 @@ public class CIMClient { | ||
| 112 | LOGGER.error("连接失败", e); | 112 | LOGGER.error("连接失败", e); |
| 113 | } | 113 | } |
| 114 | if (future.isSuccess()) { | 114 | if (future.isSuccess()) { |
| 115 | - echoService.echo("start cim client success!"); | 115 | + echoService.echo("Start cim client success!"); |
| 116 | LOGGER.info("启动 cim client 成功"); | 116 | LOGGER.info("启动 cim client 成功"); |
| 117 | } | 117 | } |
| 118 | channel = (SocketChannel) future.channel(); | 118 | channel = (SocketChannel) future.channel(); |
| @@ -158,7 +158,7 @@ public class CIMClient { | @@ -158,7 +158,7 @@ public class CIMClient { | ||
| 158 | .build(); | 158 | .build(); |
| 159 | ChannelFuture future = channel.writeAndFlush(login); | 159 | ChannelFuture future = channel.writeAndFlush(login); |
| 160 | future.addListener((ChannelFutureListener) channelFuture -> | 160 | future.addListener((ChannelFutureListener) channelFuture -> |
| 161 | - echoService.echo("registry cim server success!") | 161 | + echoService.echo("Registry cim server success!") |
| 162 | ); | 162 | ); |
| 163 | } | 163 | } |
| 164 | 164 |
| @@ -8,7 +8,7 @@ swagger.enable = true | @@ -8,7 +8,7 @@ swagger.enable = true | ||
| 8 | 8 | ||
| 9 | logging.level.root=error | 9 | logging.level.root=error |
| 10 | 10 | ||
| 11 | -#消息记录存放路径 | 11 | +# 消息记录存放路径 |
| 12 | cim.msg.logger.path = /opt/logs/cim/ | 12 | cim.msg.logger.path = /opt/logs/cim/ |
| 13 | 13 | ||
| 14 | 14 | ||
| @@ -45,7 +45,7 @@ cim.server.online.user.url=http://localhost:8083/onlineUser | @@ -45,7 +45,7 @@ cim.server.online.user.url=http://localhost:8083/onlineUser | ||
| 45 | cim.clear.route.request.url=http://localhost:8083/offLine | 45 | cim.clear.route.request.url=http://localhost:8083/offLine |
| 46 | 46 | ||
| 47 | # 客户端唯一ID | 47 | # 客户端唯一ID |
| 48 | -cim.user.id=1566914867344 | 48 | +cim.user.id=1586617710861 |
| 49 | cim.user.userName=zhangsan | 49 | cim.user.userName=zhangsan |
| 50 | 50 | ||
| 51 | # 回调线程队列大小 | 51 | # 回调线程队列大小 |
| @@ -14,21 +14,26 @@ public enum StatusEnum { | @@ -14,21 +14,26 @@ 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 | ||
| 21 | /** 重复登录 */ | 21 | /** 重复登录 */ |
| 22 | - REPEAT_LOGIN("5000", "账号重复登录,请退出一个账号!"), | 22 | + REPEAT_LOGIN("5000", "Repeat login, log out an account please!"), |
| 23 | + | ||
| 24 | + /** 请求限流 */ | ||
| 25 | + REQUEST_LIMIT("6000", "请求限流"), | ||
| 23 | 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", "登录信息不匹配!"), | 33 | + ACCOUNT_NOT_MATCH("9100", "The User information you have used is incorrect!"), |
| 34 | + | ||
| 35 | + | ||
| 29 | 36 | ||
| 30 | - /** 请求限流 */ | ||
| 31 | - REQUEST_LIMIT("6000", "请求限流"), | ||
| 32 | ; | 37 | ; |
| 33 | 38 | ||
| 34 | 39 |
| 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.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 | } |
| 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 | +} |
| @@ -17,7 +17,7 @@ public class RouteApplication implements CommandLineRunner{ | @@ -17,7 +17,7 @@ public class RouteApplication implements CommandLineRunner{ | ||
| 17 | 17 | ||
| 18 | public static void main(String[] args) { | 18 | public static void main(String[] args) { |
| 19 | SpringApplication.run(RouteApplication.class, args); | 19 | SpringApplication.run(RouteApplication.class, args); |
| 20 | - LOGGER.info("启动 route 成功"); | 20 | + LOGGER.info("Start cim route success!!!"); |
| 21 | } | 21 | } |
| 22 | 22 | ||
| 23 | @Override | 23 | @Override |
| @@ -3,11 +3,14 @@ package com.crossoverjie.cim.route.controller; | @@ -3,11 +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; |
| 10 | +import com.crossoverjie.cim.common.util.RouteInfoParseUtil; | ||
| 9 | import com.crossoverjie.cim.route.cache.ServerCache; | 11 | import com.crossoverjie.cim.route.cache.ServerCache; |
| 10 | import com.crossoverjie.cim.route.service.AccountService; | 12 | import com.crossoverjie.cim.route.service.AccountService; |
| 13 | +import com.crossoverjie.cim.route.service.CommonBizService; | ||
| 11 | import com.crossoverjie.cim.route.service.UserInfoCacheService; | 14 | import com.crossoverjie.cim.route.service.UserInfoCacheService; |
| 12 | import com.crossoverjie.cim.route.vo.req.ChatReqVO; | 15 | import com.crossoverjie.cim.route.vo.req.ChatReqVO; |
| 13 | import com.crossoverjie.cim.route.vo.req.LoginReqVO; | 16 | import com.crossoverjie.cim.route.vo.req.LoginReqVO; |
| @@ -49,6 +52,8 @@ public class RouteController { | @@ -49,6 +52,8 @@ public class RouteController { | ||
| 49 | @Autowired | 52 | @Autowired |
| 50 | private UserInfoCacheService userInfoCacheService ; | 53 | private UserInfoCacheService userInfoCacheService ; |
| 51 | 54 | ||
| 55 | + @Autowired | ||
| 56 | + private CommonBizService commonBizService ; | ||
| 52 | 57 | ||
| 53 | @Autowired | 58 | @Autowired |
| 54 | private RouteHandle routeHandle ; | 59 | private RouteHandle routeHandle ; |
| @@ -128,7 +133,7 @@ public class RouteController { | @@ -128,7 +133,7 @@ public class RouteController { | ||
| 128 | 133 | ||
| 129 | CIMUserInfo cimUserInfo = userInfoCacheService.loadUserInfoByUserId(groupReqVO.getUserId()); | 134 | CIMUserInfo cimUserInfo = userInfoCacheService.loadUserInfoByUserId(groupReqVO.getUserId()); |
| 130 | 135 | ||
| 131 | - LOGGER.info("下线用户[{}]", cimUserInfo.toString()); | 136 | + LOGGER.info("user [{}] offline!", cimUserInfo.toString()); |
| 132 | accountService.offLine(groupReqVO.getUserId()); | 137 | accountService.offLine(groupReqVO.getUserId()); |
| 133 | 138 | ||
| 134 | res.setCode(StatusEnum.SUCCESS.getCode()); | 139 | res.setCode(StatusEnum.SUCCESS.getCode()); |
| @@ -147,17 +152,19 @@ public class RouteController { | @@ -147,17 +152,19 @@ public class RouteController { | ||
| 147 | public BaseResponse<CIMServerResVO> login(@RequestBody LoginReqVO loginReqVO) throws Exception { | 152 | public BaseResponse<CIMServerResVO> login(@RequestBody LoginReqVO loginReqVO) throws Exception { |
| 148 | BaseResponse<CIMServerResVO> res = new BaseResponse(); | 153 | BaseResponse<CIMServerResVO> res = new BaseResponse(); |
| 149 | 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 | + | ||
| 150 | //登录校验 | 160 | //登录校验 |
| 151 | StatusEnum status = accountService.login(loginReqVO); | 161 | StatusEnum status = accountService.login(loginReqVO); |
| 152 | if (status == StatusEnum.SUCCESS) { | 162 | if (status == StatusEnum.SUCCESS) { |
| 153 | 163 | ||
| 154 | - String server = routeHandle.routeServer(serverCache.getAll(),String.valueOf(loginReqVO.getUserId())); | ||
| 155 | - String[] serverInfo = server.split(":"); | ||
| 156 | - CIMServerResVO vo = new CIMServerResVO(serverInfo[0], Integer.parseInt(serverInfo[1]),Integer.parseInt(serverInfo[2])); | ||
| 157 | - | ||
| 158 | //保存路由信息 | 164 | //保存路由信息 |
| 159 | accountService.saveRouteInfo(loginReqVO,server); | 165 | accountService.saveRouteInfo(loginReqVO,server); |
| 160 | 166 | ||
| 167 | + CIMServerResVO vo = new CIMServerResVO(routeInfo); | ||
| 161 | res.setDataBody(vo); | 168 | res.setDataBody(vo); |
| 162 | 169 | ||
| 163 | } | 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 | +} |
cim-forward-route/src/main/java/com/crossoverjie/cim/route/kit/NetAddressIsReachable.java
0 → 100644
| 1 | +package com.crossoverjie.cim.route.kit; | ||
| 2 | + | ||
| 3 | +import java.io.IOException; | ||
| 4 | +import java.net.InetSocketAddress; | ||
| 5 | +import java.net.Socket; | ||
| 6 | + | ||
| 7 | +/** | ||
| 8 | + * Function: | ||
| 9 | + * | ||
| 10 | + * @author crossoverJie | ||
| 11 | + * Date: 2020-04-12 20:32 | ||
| 12 | + * @since JDK 1.8 | ||
| 13 | + */ | ||
| 14 | +public class NetAddressIsReachable { | ||
| 15 | + | ||
| 16 | + /** | ||
| 17 | + * check ip and port | ||
| 18 | + * | ||
| 19 | + * @param address | ||
| 20 | + * @param port | ||
| 21 | + * @param timeout | ||
| 22 | + * @return True if connection successful | ||
| 23 | + */ | ||
| 24 | + public static boolean checkAddressReachable(String address, int port, int timeout) { | ||
| 25 | + Socket socket = new Socket() ; | ||
| 26 | + try { | ||
| 27 | + socket.connect(new InetSocketAddress(address, port), timeout); | ||
| 28 | + return true; | ||
| 29 | + } catch (IOException exception) { | ||
| 30 | + return false; | ||
| 31 | + } finally { | ||
| 32 | + try { | ||
| 33 | + socket.close(); | ||
| 34 | + } catch (IOException e) { | ||
| 35 | + return false ; | ||
| 36 | + } | ||
| 37 | + } | ||
| 38 | + } | ||
| 39 | +} |
| @@ -40,7 +40,7 @@ public class ZKit { | @@ -40,7 +40,7 @@ public class ZKit { | ||
| 40 | zkClient.subscribeChildChanges(path, new IZkChildListener() { | 40 | zkClient.subscribeChildChanges(path, new IZkChildListener() { |
| 41 | @Override | 41 | @Override |
| 42 | public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { | 42 | public void handleChildChange(String parentPath, List<String> currentChilds) throws Exception { |
| 43 | - logger.info("清除/更新本地缓存 parentPath=【{}】,currentChilds=【{}】", parentPath,currentChilds.toString()); | 43 | + logger.info("Clear or update local cache parentPath=[{}],currentChilds=[{}]", parentPath,currentChilds.toString()); |
| 44 | 44 | ||
| 45 | //更新所有缓存/先删除 再新增 | 45 | //更新所有缓存/先删除 再新增 |
| 46 | serverCache.updateCache(currentChilds) ; | 46 | serverCache.updateCache(currentChilds) ; |
| 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 | +} |
| @@ -38,8 +38,8 @@ public interface UserInfoCacheService { | @@ -38,8 +38,8 @@ public interface UserInfoCacheService { | ||
| 38 | 38 | ||
| 39 | 39 | ||
| 40 | /** | 40 | /** |
| 41 | - * | ||
| 42 | - * @return 获取所有在线用户 | 41 | + * query all online user |
| 42 | + * @return online user | ||
| 43 | */ | 43 | */ |
| 44 | Set<CIMUserInfo> onlineUser() ; | 44 | Set<CIMUserInfo> onlineUser() ; |
| 45 | } | 45 | } |
cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImpl.java
| @@ -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; |
| @@ -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() { |
| 1 | +import com.crossoverjie.cim.route.kit.NetAddressIsReachable; | ||
| 2 | +import org.junit.Test; | ||
| 3 | + | ||
| 4 | +/** | ||
| 5 | + * Function: | ||
| 6 | + * | ||
| 7 | + * @author crossoverJie | ||
| 8 | + * Date: 2020-04-12 18:38 | ||
| 9 | + * @since JDK 1.8 | ||
| 10 | + */ | ||
| 11 | +public class CommonTest { | ||
| 12 | + | ||
| 13 | + @Test | ||
| 14 | + public void test() { | ||
| 15 | + boolean reachable = NetAddressIsReachable.checkAddressReachable("127.0.0.1", 11211, 1000); | ||
| 16 | + System.out.println(reachable); | ||
| 17 | + } | ||
| 18 | + | ||
| 19 | + | ||
| 20 | + | ||
| 21 | +} |
| @@ -28,7 +28,7 @@ public class CIMServerApplication implements CommandLineRunner{ | @@ -28,7 +28,7 @@ public class CIMServerApplication implements CommandLineRunner{ | ||
| 28 | 28 | ||
| 29 | public static void main(String[] args) { | 29 | public static void main(String[] args) { |
| 30 | SpringApplication.run(CIMServerApplication.class, args); | 30 | SpringApplication.run(CIMServerApplication.class, args); |
| 31 | - LOGGER.info("启动 Server 成功"); | 31 | + LOGGER.info("Start cim server success!!!"); |
| 32 | } | 32 | } |
| 33 | 33 | ||
| 34 | @Override | 34 | @Override |
| @@ -49,7 +49,7 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto | @@ -49,7 +49,7 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto | ||
| 49 | //可能出现业务判断离线后再次触发 channelInactive | 49 | //可能出现业务判断离线后再次触发 channelInactive |
| 50 | CIMUserInfo userInfo = SessionSocketHolder.getUserId((NioSocketChannel) ctx.channel()); | 50 | CIMUserInfo userInfo = SessionSocketHolder.getUserId((NioSocketChannel) ctx.channel()); |
| 51 | if (userInfo != null){ | 51 | if (userInfo != null){ |
| 52 | - LOGGER.warn("[{}]触发 channelInactive 掉线!",userInfo.getUserName()); | 52 | + LOGGER.warn("[{}] trigger channelInactive offline!",userInfo.getUserName()); |
| 53 | userOffLine(userInfo, (NioSocketChannel) ctx.channel()); | 53 | userOffLine(userInfo, (NioSocketChannel) ctx.channel()); |
| 54 | ctx.channel().close(); | 54 | ctx.channel().close(); |
| 55 | } | 55 | } |
| @@ -77,7 +77,7 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto | @@ -77,7 +77,7 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto | ||
| 77 | * @throws IOException | 77 | * @throws IOException |
| 78 | */ | 78 | */ |
| 79 | private void userOffLine(CIMUserInfo userInfo, NioSocketChannel channel) throws IOException { | 79 | private void userOffLine(CIMUserInfo userInfo, NioSocketChannel channel) throws IOException { |
| 80 | - LOGGER.info("用户[{}]下线", userInfo.getUserName()); | 80 | + LOGGER.info("account [{}] offline!", userInfo.getUserName()); |
| 81 | SessionSocketHolder.remove(channel); | 81 | SessionSocketHolder.remove(channel); |
| 82 | SessionSocketHolder.removeSession(userInfo.getUserId()); | 82 | SessionSocketHolder.removeSession(userInfo.getUserId()); |
| 83 | 83 | ||
| @@ -118,13 +118,13 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto | @@ -118,13 +118,13 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto | ||
| 118 | 118 | ||
| 119 | @Override | 119 | @Override |
| 120 | protected void channelRead0(ChannelHandlerContext ctx, CIMRequestProto.CIMReqProtocol msg) throws Exception { | 120 | protected void channelRead0(ChannelHandlerContext ctx, CIMRequestProto.CIMReqProtocol msg) throws Exception { |
| 121 | - LOGGER.info("收到msg={}", msg.toString()); | 121 | + LOGGER.info("received msg=[{}]", msg.toString()); |
| 122 | 122 | ||
| 123 | if (msg.getType() == Constants.CommandType.LOGIN) { | 123 | if (msg.getType() == Constants.CommandType.LOGIN) { |
| 124 | //保存客户端与 Channel 之间的关系 | 124 | //保存客户端与 Channel 之间的关系 |
| 125 | SessionSocketHolder.put(msg.getRequestId(), (NioSocketChannel) ctx.channel()); | 125 | SessionSocketHolder.put(msg.getRequestId(), (NioSocketChannel) ctx.channel()); |
| 126 | SessionSocketHolder.saveSession(msg.getRequestId(), msg.getReqMsg()); | 126 | SessionSocketHolder.saveSession(msg.getRequestId(), msg.getReqMsg()); |
| 127 | - LOGGER.info("客户端[{}]上线成功", msg.getReqMsg()); | 127 | + LOGGER.info("client [{}] online success!!", msg.getReqMsg()); |
| 128 | } | 128 | } |
| 129 | 129 | ||
| 130 | //心跳更新时间 | 130 | //心跳更新时间 |
| @@ -42,7 +42,7 @@ public class RegistryZK implements Runnable { | @@ -42,7 +42,7 @@ public class RegistryZK implements Runnable { | ||
| 42 | if (appConfiguration.isZkSwitch()){ | 42 | if (appConfiguration.isZkSwitch()){ |
| 43 | String path = appConfiguration.getZkRoot() + "/ip-" + ip + ":" + cimServerPort + ":" + httpPort; | 43 | String path = appConfiguration.getZkRoot() + "/ip-" + ip + ":" + cimServerPort + ":" + httpPort; |
| 44 | zKit.createNode(path); | 44 | zKit.createNode(path); |
| 45 | - logger.info("注册 zookeeper 成功,msg=[{}]", path); | 45 | + logger.info("Registry zookeeper success, msg=[{}]", path); |
| 46 | } | 46 | } |
| 47 | 47 | ||
| 48 | 48 |
| @@ -61,7 +61,7 @@ public class CIMServer { | @@ -61,7 +61,7 @@ public class CIMServer { | ||
| 61 | 61 | ||
| 62 | ChannelFuture future = bootstrap.bind().sync(); | 62 | ChannelFuture future = bootstrap.bind().sync(); |
| 63 | if (future.isSuccess()) { | 63 | if (future.isSuccess()) { |
| 64 | - LOGGER.info("启动 cim server 成功"); | 64 | + LOGGER.info("Start cim server success!!!"); |
| 65 | } | 65 | } |
| 66 | } | 66 | } |
| 67 | 67 | ||
| @@ -73,7 +73,7 @@ public class CIMServer { | @@ -73,7 +73,7 @@ public class CIMServer { | ||
| 73 | public void destroy() { | 73 | public void destroy() { |
| 74 | boss.shutdownGracefully().syncUninterruptibly(); | 74 | boss.shutdownGracefully().syncUninterruptibly(); |
| 75 | work.shutdownGracefully().syncUninterruptibly(); | 75 | work.shutdownGracefully().syncUninterruptibly(); |
| 76 | - LOGGER.info("关闭 cim server 成功"); | 76 | + LOGGER.info("Close cim server success!!!"); |
| 77 | } | 77 | } |
| 78 | 78 | ||
| 79 | 79 | ||
| @@ -85,7 +85,7 @@ public class CIMServer { | @@ -85,7 +85,7 @@ public class CIMServer { | ||
| 85 | NioSocketChannel socketChannel = SessionSocketHolder.get(sendMsgReqVO.getUserId()); | 85 | NioSocketChannel socketChannel = SessionSocketHolder.get(sendMsgReqVO.getUserId()); |
| 86 | 86 | ||
| 87 | if (null == socketChannel) { | 87 | if (null == socketChannel) { |
| 88 | - throw new NullPointerException("客户端[" + sendMsgReqVO.getUserId() + "]不在线!"); | 88 | + LOGGER.error("client {} offline!", sendMsgReqVO.getUserId()); |
| 89 | } | 89 | } |
| 90 | CIMRequestProto.CIMReqProtocol protocol = CIMRequestProto.CIMReqProtocol.newBuilder() | 90 | CIMRequestProto.CIMReqProtocol protocol = CIMRequestProto.CIMReqProtocol.newBuilder() |
| 91 | .setRequestId(sendMsgReqVO.getUserId()) | 91 | .setRequestId(sendMsgReqVO.getUserId()) |
| @@ -95,6 +95,6 @@ public class CIMServer { | @@ -95,6 +95,6 @@ public class CIMServer { | ||
| 95 | 95 | ||
| 96 | ChannelFuture future = socketChannel.writeAndFlush(protocol); | 96 | ChannelFuture future = socketChannel.writeAndFlush(protocol); |
| 97 | future.addListener((ChannelFutureListener) channelFuture -> | 97 | future.addListener((ChannelFutureListener) channelFuture -> |
| 98 | - LOGGER.info("服务端手动发送 Google Protocol 成功={}", sendMsgReqVO.toString())); | 98 | + LOGGER.info("server push msg:[{}]", sendMsgReqVO.toString())); |
| 99 | } | 99 | } |
| 100 | } | 100 | } |
-
请 注册 或 登录 后发表评论