diff --git a/README.md b/README.md index b7d1a701..aff3edbb 100644 --- a/README.md +++ b/README.md @@ -129,7 +129,7 @@ nohup java -jar /root/work/route0/cim-forward-route-1.0.0-SNAPSHOT.jar --app.zk ```shell cp /cim/cim-client/target/cim-client-1.0.0-SNAPSHOT.jar /xx/work/route0/ cd /xx/work/route0/ -java -jar cim-client-1.0.0-SNAPSHOT.jar --server.port=8084 --cim.user.id=唯一客户端ID --cim.user.userName=用户名 --cim.group.route.request.url=http://路由服务器:8083/groupRoute --cim.server.route.request.url=http://路由服务器:8083/login +java -jar cim-client-1.0.0-SNAPSHOT.jar --server.port=8084 --cim.user.id=唯一客户端ID --cim.user.userName=用户名 --cim.route.url=http://路由服务器:8083/ ``` ![](https://ws2.sinaimg.cn/large/006tNbRwly1fylgxjgshfj31vo04m7p9.jpg) @@ -167,7 +167,7 @@ curl -X POST --header 'Content-Type: application/json' --header 'Accept: applica # 启动本地客户端 cp /cim/cim-client/target/cim-client-1.0.0-SNAPSHOT.jar /xx/work/route0/ cd /xx/work/route0/ -java -jar cim-client-1.0.0-SNAPSHOT.jar --server.port=8084 --cim.user.id=上方返回的userId --cim.user.userName=用户名 --cim.group.route.request.url=http://路由服务器:8083/groupRoute --cim.server.route.request.url=http://路由服务器:8083/login +java -jar cim-client-1.0.0-SNAPSHOT.jar --server.port=8084 --cim.user.id=上方返回的userId --cim.user.userName=用户名 --cim.route.url=http://路由服务器:8083/ ``` ## 客户端内置命令 diff --git a/cim-client/pom.xml b/cim-client/pom.xml index 66c5b1a3..b8603bb4 100644 --- a/cim-client/pom.xml +++ b/cim-client/pom.xml @@ -93,6 +93,11 @@ 5.0.0 + + com.crossoverjie.netty + cim-rout-api + + diff --git a/cim-client/src/main/java/com/crossoverjie/cim/client/service/impl/RouteRequestImpl.java b/cim-client/src/main/java/com/crossoverjie/cim/client/service/impl/RouteRequestImpl.java index f8ca23e4..c7345705 100644 --- a/cim-client/src/main/java/com/crossoverjie/cim/client/service/impl/RouteRequestImpl.java +++ b/cim-client/src/main/java/com/crossoverjie/cim/client/service/impl/RouteRequestImpl.java @@ -1,7 +1,6 @@ package com.crossoverjie.cim.client.service.impl; import com.alibaba.fastjson.JSON; -import com.alibaba.fastjson.JSONObject; import com.crossoverjie.cim.client.config.AppConfiguration; import com.crossoverjie.cim.client.service.EchoService; import com.crossoverjie.cim.client.service.RouteRequest; @@ -11,17 +10,20 @@ import com.crossoverjie.cim.client.vo.req.P2PReqVO; import com.crossoverjie.cim.client.vo.res.CIMServerResVO; import com.crossoverjie.cim.client.vo.res.OnlineUsersResVO; +import com.crossoverjie.cim.common.core.proxy.ProxyManager; import com.crossoverjie.cim.common.enums.StatusEnum; import com.crossoverjie.cim.common.exception.CIMException; import com.crossoverjie.cim.common.res.BaseResponse; -import okhttp3.*; +import com.crossoverjie.cim.route.api.RouteApi; +import com.crossoverjie.cim.route.api.vo.req.ChatReqVO; +import okhttp3.OkHttpClient; +import okhttp3.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; -import java.io.IOException; import java.util.List; /** @@ -39,19 +41,8 @@ public class RouteRequestImpl implements RouteRequest { @Autowired private OkHttpClient okHttpClient ; - private MediaType mediaType = MediaType.parse("application/json"); - - @Value("${cim.group.route.request.url}") - private String groupRouteRequestUrl; - - @Value("${cim.p2p.route.request.url}") - private String p2pRouteRequestUrl; - - @Value("${cim.server.route.request.url}") - private String serverRouteLoginUrl; - - @Value("${cim.server.online.user.url}") - private String onlineUserUrl; + @Value("${cim.route.url}") + private String routeUrl ; @Autowired private EchoService echoService ; @@ -62,22 +53,13 @@ public class RouteRequestImpl implements RouteRequest { @Override public void sendGroupMsg(GroupReqVO groupReqVO) throws Exception { - - JSONObject jsonObject = new JSONObject(); - jsonObject.put("msg",groupReqVO.getMsg()); - jsonObject.put("userId",groupReqVO.getUserId()); - RequestBody requestBody = RequestBody.create(mediaType,jsonObject.toString()); - - Request request = new Request.Builder() - .url(groupRouteRequestUrl) - .post(requestBody) - .build(); - - Response response = okHttpClient.newCall(request).execute() ; + RouteApi routeApi = new ProxyManager<>(RouteApi.class, routeUrl, okHttpClient).getInstance(); + ChatReqVO chatReqVO = new ChatReqVO(groupReqVO.getUserId(), groupReqVO.getMsg()) ; + Response response = null; try { - if (!response.isSuccessful()){ - throw new IOException("Unexpected code " + response); - } + response = (Response)routeApi.groupRoute(chatReqVO); + }catch (Exception e){ + LOGGER.error("exception",e); }finally { response.body().close(); } @@ -85,58 +67,43 @@ public void sendGroupMsg(GroupReqVO groupReqVO) throws Exception { @Override public void sendP2PMsg(P2PReqVO p2PReqVO) throws Exception { - JSONObject jsonObject = new JSONObject(); - jsonObject.put("msg",p2PReqVO.getMsg()); - jsonObject.put("userId",p2PReqVO.getUserId()); - jsonObject.put("receiveUserId",p2PReqVO.getReceiveUserId()); - RequestBody requestBody = RequestBody.create(mediaType,jsonObject.toString()); - - Request request = new Request.Builder() - .url(p2pRouteRequestUrl) - .post(requestBody) - .build(); - - Response response = okHttpClient.newCall(request).execute() ; - if (!response.isSuccessful()){ - throw new IOException("Unexpected code " + response); - } + RouteApi routeApi = new ProxyManager<>(RouteApi.class, routeUrl, okHttpClient).getInstance(); + com.crossoverjie.cim.route.api.vo.req.P2PReqVO vo = new com.crossoverjie.cim.route.api.vo.req.P2PReqVO() ; + vo.setMsg(p2PReqVO.getMsg()); + vo.setReceiveUserId(p2PReqVO.getReceiveUserId()); + vo.setUserId(p2PReqVO.getUserId()); - ResponseBody body = response.body(); + Response response = null; try { - String json = body.string() ; + response = (Response) routeApi.p2pRoute(vo); + String json = response.body().string() ; BaseResponse baseResponse = JSON.parseObject(json, BaseResponse.class); - //选择的账号不存在 + // account offline. if (baseResponse.getCode().equals(StatusEnum.OFF_LINE.getCode())){ LOGGER.error(p2PReqVO.getReceiveUserId() + ":" + StatusEnum.OFF_LINE.getMessage()); } + }catch (Exception e){ + LOGGER.error("exception",e); }finally { - body.close(); + response.body().close(); } } @Override public CIMServerResVO.ServerInfo getCIMServer(LoginReqVO loginReqVO) throws Exception { - JSONObject jsonObject = new JSONObject(); - jsonObject.put("userId",loginReqVO.getUserId()); - jsonObject.put("userName",loginReqVO.getUserName()); - RequestBody requestBody = RequestBody.create(mediaType,jsonObject.toString()); + RouteApi routeApi = new ProxyManager<>(RouteApi.class, routeUrl, okHttpClient).getInstance(); + com.crossoverjie.cim.route.api.vo.req.LoginReqVO vo = new com.crossoverjie.cim.route.api.vo.req.LoginReqVO() ; + vo.setUserId(loginReqVO.getUserId()); + vo.setUserName(loginReqVO.getUserName()); - Request request = new Request.Builder() - .url(serverRouteLoginUrl) - .post(requestBody) - .build(); - - Response response = okHttpClient.newCall(request).execute() ; - if (!response.isSuccessful()){ - throw new IOException("Unexpected code " + response); - } - CIMServerResVO cimServerResVO ; - ResponseBody body = response.body(); + Response response = null; + CIMServerResVO cimServerResVO = null; try { - String json = body.string(); + response = (Response) routeApi.login(vo); + String json = response.body().string(); cimServerResVO = JSON.parseObject(json, CIMServerResVO.class); //重复失败 @@ -152,40 +119,30 @@ public CIMServerResVO.ServerInfo getCIMServer(LoginReqVO loginReqVO) throws Exce System.exit(-1); } + }catch (Exception e){ + LOGGER.error("exception",e); }finally { - body.close(); + response.body().close(); } - - return cimServerResVO.getDataBody(); } @Override public List onlineUsers() throws Exception{ + RouteApi routeApi = new ProxyManager<>(RouteApi.class, routeUrl, okHttpClient).getInstance(); - JSONObject jsonObject = new JSONObject(); - RequestBody requestBody = RequestBody.create(mediaType,jsonObject.toString()); - - Request request = new Request.Builder() - .url(onlineUserUrl) - .post(requestBody) - .build(); - - Response response = okHttpClient.newCall(request).execute() ; - if (!response.isSuccessful()){ - throw new IOException("Unexpected code " + response); - } - - - ResponseBody body = response.body(); - OnlineUsersResVO onlineUsersResVO ; + Response response = null; + OnlineUsersResVO onlineUsersResVO = null; try { - String json = body.string() ; + response = (Response) routeApi.onlineUser(); + String json = response.body().string() ; onlineUsersResVO = JSON.parseObject(json, OnlineUsersResVO.class); + }catch (Exception e){ + LOGGER.error("exception",e); }finally { - body.close(); + response.body().close(); } return onlineUsersResVO.getDataBody(); @@ -193,20 +150,12 @@ public List onlineUsers() throws Exception{ @Override public void offLine() { - JSONObject jsonObject = new JSONObject(); - jsonObject.put("userId", appConfiguration.getUserId()); - jsonObject.put("msg", "offLine"); - RequestBody requestBody = RequestBody.create(mediaType, jsonObject.toString()); - - Request request = new Request.Builder() - .url(appConfiguration.getClearRouteUrl()) - .post(requestBody) - .build(); - + RouteApi routeApi = new ProxyManager<>(RouteApi.class, routeUrl, okHttpClient).getInstance(); + ChatReqVO vo = new ChatReqVO(appConfiguration.getUserId(), "offLine") ; Response response = null; try { - response = okHttpClient.newCall(request).execute(); - } catch (IOException e) { + response = (Response) routeApi.offLine(vo); + } catch (Exception e) { LOGGER.error("exception",e); } finally { response.body().close(); diff --git a/cim-client/src/main/resources/application.properties b/cim-client/src/main/resources/application.properties index 8c5a2253..1a13ea6d 100644 --- a/cim-client/src/main/resources/application.properties +++ b/cim-client/src/main/resources/application.properties @@ -13,36 +13,11 @@ cim.msg.logger.path = /opt/logs/cim/ ###=======生产模拟======### -## 群发消息 -#cim.group.route.request.url=http://45.78.28.220:8083/groupRoute -# -## 私聊消息 -#cim.p2p.route.request.url=http://45.78.28.220:8083/p2pRoute -# -## 登录并获取服务器ip+port -#cim.server.route.request.url=http://45.78.28.220:8083/login -# -## 在线用户 -#cim.server.online.user.url=http://45.78.28.220:8083/onlineUser -# -## 清除路由信息 -#cim.clear.route.request.url=http://45.78.28.220:8083/offLine +#cim.route.url = http://45.78.28.220:8083/ ###=======本地模拟======### -# 群发消息 -cim.group.route.request.url=http://localhost:8083/groupRoute - -# 私聊消息 -cim.p2p.route.request.url=http://localhost:8083/p2pRoute - -# 登录并获取服务器ip+port -cim.server.route.request.url=http://localhost:8083/login - -# 在线用户 -cim.server.online.user.url=http://localhost:8083/onlineUser - -# 清除路由信息 -cim.clear.route.request.url=http://localhost:8083/offLine +# route url suggested that this is Nginx address +cim.route.url = http://localhost:8083/ # 客户端唯一ID cim.user.id=1586617710861 diff --git a/cim-common/pom.xml b/cim-common/pom.xml index ad162e70..af5f5d45 100644 --- a/cim-common/pom.xml +++ b/cim-common/pom.xml @@ -49,5 +49,9 @@ netty-all ${netty.version} + + com.alibaba + fastjson + \ No newline at end of file diff --git a/cim-common/src/main/java/com/crossoverjie/cim/common/core/proxy/ProxyManager.java b/cim-common/src/main/java/com/crossoverjie/cim/common/core/proxy/ProxyManager.java new file mode 100644 index 00000000..e380d9fb --- /dev/null +++ b/cim-common/src/main/java/com/crossoverjie/cim/common/core/proxy/ProxyManager.java @@ -0,0 +1,77 @@ +package com.crossoverjie.cim.common.core.proxy; + +import com.alibaba.fastjson.JSONObject; +import com.crossoverjie.cim.common.exception.CIMException; +import com.crossoverjie.cim.common.util.HttpClient; +import okhttp3.OkHttpClient; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.lang.reflect.Field; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; + +import static com.crossoverjie.cim.common.enums.StatusEnum.VALIDATION_FAIL; + +/** + * Function: + * + * @author crossoverJie + * Date: 2020-04-25 00:18 + * @since JDK 1.8 + */ +public final class ProxyManager { + + private final static Logger LOGGER = LoggerFactory.getLogger(ProxyManager.class); + + private Class clazz; + + private String url; + + private OkHttpClient okHttpClient; + + /** + * + * @param clazz Proxied interface + * @param url server provider url + * @param okHttpClient http client + */ + public ProxyManager(Class clazz, String url, OkHttpClient okHttpClient) { + this.clazz = clazz; + this.url = url; + this.okHttpClient = okHttpClient; + } + + /** + * Get proxy instance of api. + * @return + */ + public T getInstance() { + return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), new Class[]{clazz}, new ProxyInvocation()); + } + + + private class ProxyInvocation implements InvocationHandler { + + @Override + public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { + JSONObject jsonObject = new JSONObject(); + String serverUrl = url + "/" + method.getName() ; + + if (args != null && args.length > 1) { + throw new CIMException(VALIDATION_FAIL); + } + + if (method.getParameterTypes().length > 0){ + Object para = args[0]; + Class parameterType = method.getParameterTypes()[0]; + for (Field field : parameterType.getDeclaredFields()) { + field.setAccessible(true); + jsonObject.put(field.getName(), field.get(para)); + } + } + return HttpClient.call(okHttpClient, jsonObject.toString(), serverUrl); + } + } +} diff --git a/cim-common/src/main/java/com/crossoverjie/cim/common/enums/StatusEnum.java b/cim-common/src/main/java/com/crossoverjie/cim/common/enums/StatusEnum.java index 990e4265..d19afbd7 100644 --- a/cim-common/src/main/java/com/crossoverjie/cim/common/enums/StatusEnum.java +++ b/cim-common/src/main/java/com/crossoverjie/cim/common/enums/StatusEnum.java @@ -16,7 +16,7 @@ public enum StatusEnum { /** 参数校验失败**/ VALIDATION_FAIL("3000", "invalid argument"), /** 失败 */ - FAIL("4000", "失败"), + FAIL("4000", "Failure"), /** 重复登录 */ REPEAT_LOGIN("5000", "Repeat login, log out an account please!"), @@ -30,7 +30,6 @@ public enum StatusEnum { SERVER_NOT_AVAILABLE("7100", "cim server is not available, please try again later!"), RECONNECT_FAIL("7200", "Reconnect fail, continue to retry!"), - /** 登录信息不匹配 */ ACCOUNT_NOT_MATCH("9100", "The User information you have used is incorrect!"), diff --git a/cim-common/src/main/java/com/crossoverjie/cim/common/util/HttpClient.java b/cim-common/src/main/java/com/crossoverjie/cim/common/util/HttpClient.java new file mode 100644 index 00000000..3a80a1d7 --- /dev/null +++ b/cim-common/src/main/java/com/crossoverjie/cim/common/util/HttpClient.java @@ -0,0 +1,37 @@ +package com.crossoverjie.cim.common.util; + +import okhttp3.MediaType; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.RequestBody; +import okhttp3.Response; + +import java.io.IOException; + +/** + * Function: + * + * @author crossoverJie + * Date: 2020-04-25 00:39 + * @since JDK 1.8 + */ +public final class HttpClient { + + private static MediaType mediaType = MediaType.parse("application/json"); + + public static Response call(OkHttpClient okHttpClient, String params, String url) throws IOException { + RequestBody requestBody = RequestBody.create(mediaType, params); + + Request request = new Request.Builder() + .url(url) + .post(requestBody) + .build(); + + Response response = okHttpClient.newCall(request).execute(); + if (!response.isSuccessful()) { + throw new IOException("Unexpected code " + response); + } + + return response; + } +} diff --git a/cim-common/src/test/java/com/crossoverjie/cim/common/util/HttpClientTest.java b/cim-common/src/test/java/com/crossoverjie/cim/common/util/HttpClientTest.java new file mode 100644 index 00000000..a809eab9 --- /dev/null +++ b/cim-common/src/test/java/com/crossoverjie/cim/common/util/HttpClientTest.java @@ -0,0 +1,33 @@ +package com.crossoverjie.cim.common.util; + +import com.alibaba.fastjson.JSONObject; +import okhttp3.OkHttpClient; +import org.junit.Before; +import org.junit.Test; + +import java.io.IOException; +import java.util.concurrent.TimeUnit; + +public class HttpClientTest { + + private OkHttpClient okHttpClient ; + + @Before + public void before(){ + OkHttpClient.Builder builder = new OkHttpClient.Builder(); + builder.connectTimeout(30, TimeUnit.SECONDS) + .readTimeout(10, TimeUnit.SECONDS) + .writeTimeout(10,TimeUnit.SECONDS) + .retryOnConnectionFailure(true); + okHttpClient = builder.build(); + } + + @Test + public void call() throws IOException { + JSONObject jsonObject = new JSONObject(); + jsonObject.put("msg", "hello"); + jsonObject.put("userId", 1586617710861L); + + HttpClient.call(okHttpClient,jsonObject.toString(),"http://127.0.0.1:8081/sendMsg") ; + } +} \ No newline at end of file diff --git a/cim-forward-route/pom.xml b/cim-forward-route/pom.xml index 99a1223a..2cb9f925 100644 --- a/cim-forward-route/pom.xml +++ b/cim-forward-route/pom.xml @@ -35,6 +35,16 @@ + + com.crossoverjie.netty + cim-rout-api + + + + com.crossoverjie.netty + cim-server-api + + org.springframework.boot spring-boot-starter-data-redis diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/controller/RouteController.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/controller/RouteController.java index 20562c99..4a8dce36 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/controller/RouteController.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/controller/RouteController.java @@ -8,16 +8,17 @@ 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.api.RouteApi; +import com.crossoverjie.cim.route.api.vo.req.ChatReqVO; +import com.crossoverjie.cim.route.api.vo.req.LoginReqVO; +import com.crossoverjie.cim.route.api.vo.req.P2PReqVO; +import com.crossoverjie.cim.route.api.vo.req.RegisterInfoReqVO; +import com.crossoverjie.cim.route.api.vo.res.CIMServerResVO; +import com.crossoverjie.cim.route.api.vo.res.RegisterInfoResVO; 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; -import com.crossoverjie.cim.route.vo.req.P2PReqVO; -import com.crossoverjie.cim.route.vo.req.RegisterInfoReqVO; -import com.crossoverjie.cim.route.vo.res.CIMServerResVO; -import com.crossoverjie.cim.route.vo.res.RegisterInfoResVO; import io.swagger.annotations.ApiOperation; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -40,7 +41,7 @@ */ @Controller @RequestMapping("/") -public class RouteController { +public class RouteController implements RouteApi { private final static Logger LOGGER = LoggerFactory.getLogger(RouteController.class); @Autowired @@ -61,6 +62,7 @@ public class RouteController { @ApiOperation("群聊 API") @RequestMapping(value = "groupRoute", method = RequestMethod.POST) @ResponseBody() + @Override public BaseResponse groupRoute(@RequestBody ChatReqVO groupReqVO) throws Exception { BaseResponse res = new BaseResponse(); @@ -70,7 +72,7 @@ public BaseResponse groupRoute(@RequestBody ChatReqVO groupReqVO) thro Map serverResVOMap = accountService.loadRouteRelated(); for (Map.Entry cimServerResVOEntry : serverResVOMap.entrySet()) { Long userId = cimServerResVOEntry.getKey(); - CIMServerResVO value = cimServerResVOEntry.getValue(); + CIMServerResVO cimServerResVO = cimServerResVOEntry.getValue(); if (userId.equals(groupReqVO.getUserId())){ //过滤掉自己 CIMUserInfo cimUserInfo = userInfoCacheService.loadUserInfoByUserId(groupReqVO.getUserId()); @@ -79,10 +81,8 @@ public BaseResponse groupRoute(@RequestBody ChatReqVO groupReqVO) thro } //推送消息 - String url = "http://" + value.getIp() + ":" + value.getHttpPort() + "/sendMsg" ; ChatReqVO chatVO = new ChatReqVO(userId,groupReqVO.getMsg()) ; - - accountService.pushMsg(url,groupReqVO.getUserId(),chatVO); + accountService.pushMsg(cimServerResVO ,groupReqVO.getUserId(),chatVO); } @@ -101,18 +101,17 @@ public BaseResponse groupRoute(@RequestBody ChatReqVO groupReqVO) thro @ApiOperation("私聊 API") @RequestMapping(value = "p2pRoute", method = RequestMethod.POST) @ResponseBody() + @Override public BaseResponse p2pRoute(@RequestBody P2PReqVO p2pRequest) throws Exception { BaseResponse res = new BaseResponse(); try { //获取接收消息用户的路由信息 CIMServerResVO cimServerResVO = accountService.loadRouteRelatedByUserId(p2pRequest.getReceiveUserId()); - //推送消息 - String url = "http://" + cimServerResVO.getIp() + ":" + cimServerResVO.getHttpPort() + "/sendMsg" ; //p2pRequest.getReceiveUserId()==>消息接收者的 userID ChatReqVO chatVO = new ChatReqVO(p2pRequest.getReceiveUserId(),p2pRequest.getMsg()) ; - accountService.pushMsg(url,p2pRequest.getUserId(),chatVO); + accountService.pushMsg(cimServerResVO ,p2pRequest.getUserId(),chatVO); res.setCode(StatusEnum.SUCCESS.getCode()); res.setMessage(StatusEnum.SUCCESS.getMessage()); @@ -128,6 +127,7 @@ public BaseResponse p2pRoute(@RequestBody P2PReqVO p2pRequest) throws @ApiOperation("客户端下线") @RequestMapping(value = "offLine", method = RequestMethod.POST) @ResponseBody() + @Override public BaseResponse offLine(@RequestBody ChatReqVO groupReqVO) throws Exception { BaseResponse res = new BaseResponse(); @@ -149,6 +149,7 @@ public BaseResponse offLine(@RequestBody ChatReqVO groupReqVO) throws @ApiOperation("登录并获取服务器") @RequestMapping(value = "login", method = RequestMethod.POST) @ResponseBody() + @Override public BaseResponse login(@RequestBody LoginReqVO loginReqVO) throws Exception { BaseResponse res = new BaseResponse(); @@ -184,6 +185,7 @@ public BaseResponse login(@RequestBody LoginReqVO loginReqVO) th @ApiOperation("注册账号") @RequestMapping(value = "registerAccount", method = RequestMethod.POST) @ResponseBody() + @Override public BaseResponse registerAccount(@RequestBody RegisterInfoReqVO registerInfoReqVO) throws Exception { BaseResponse res = new BaseResponse(); @@ -205,6 +207,7 @@ public BaseResponse registerAccount(@RequestBody RegisterInfo @ApiOperation("获取所有在线用户") @RequestMapping(value = "onlineUser", method = RequestMethod.POST) @ResponseBody() + @Override public BaseResponse> onlineUser() throws Exception { BaseResponse> res = new BaseResponse(); diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/AccountService.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/AccountService.java index b860fdd4..dd78b419 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/AccountService.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/AccountService.java @@ -1,10 +1,10 @@ package com.crossoverjie.cim.route.service; import com.crossoverjie.cim.common.enums.StatusEnum; -import com.crossoverjie.cim.route.vo.req.ChatReqVO; -import com.crossoverjie.cim.route.vo.req.LoginReqVO; -import com.crossoverjie.cim.route.vo.res.CIMServerResVO; -import com.crossoverjie.cim.route.vo.res.RegisterInfoResVO; +import com.crossoverjie.cim.route.api.vo.req.ChatReqVO; +import com.crossoverjie.cim.route.api.vo.req.LoginReqVO; +import com.crossoverjie.cim.route.api.vo.res.CIMServerResVO; +import com.crossoverjie.cim.route.api.vo.res.RegisterInfoResVO; import java.util.Map; @@ -45,7 +45,7 @@ public interface AccountService { * 加载所有用户的路有关系 * @return 所有的路由关系 */ - Map loadRouteRelated() ; + Map loadRouteRelated() ; /** * 获取某个用户的路有关系 @@ -57,12 +57,12 @@ public interface AccountService { /** * 推送消息 - * @param url url + * @param cimServerResVO * @param groupReqVO 消息 * @param sendUserId 发送者的ID * @throws Exception */ - void pushMsg(String url,long sendUserId ,ChatReqVO groupReqVO) throws Exception; + void pushMsg(CIMServerResVO cimServerResVO, long sendUserId , ChatReqVO groupReqVO) throws Exception; /** * 用户下线 diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImpl.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImpl.java index 4731c4ad..56b64fd6 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImpl.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImpl.java @@ -1,17 +1,20 @@ package com.crossoverjie.cim.route.service.impl; -import com.alibaba.fastjson.JSONObject; +import com.crossoverjie.cim.common.core.proxy.ProxyManager; 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.api.vo.req.ChatReqVO; +import com.crossoverjie.cim.route.api.vo.req.LoginReqVO; +import com.crossoverjie.cim.route.api.vo.res.CIMServerResVO; +import com.crossoverjie.cim.route.api.vo.res.RegisterInfoResVO; import com.crossoverjie.cim.route.service.AccountService; import com.crossoverjie.cim.route.service.UserInfoCacheService; -import com.crossoverjie.cim.route.vo.req.ChatReqVO; -import com.crossoverjie.cim.route.vo.req.LoginReqVO; -import com.crossoverjie.cim.route.vo.res.CIMServerResVO; -import com.crossoverjie.cim.route.vo.res.RegisterInfoResVO; -import okhttp3.*; +import com.crossoverjie.cim.server.api.ServerApi; +import com.crossoverjie.cim.server.api.vo.req.SendMsgReqVO; +import okhttp3.OkHttpClient; +import okhttp3.Response; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; @@ -50,8 +53,6 @@ public class AccountServiceRedisImpl implements AccountService { @Autowired private OkHttpClient okHttpClient; - private MediaType mediaType = MediaType.parse("application/json"); - @Override public RegisterInfoResVO register(RegisterInfoResVO info) { String key = ACCOUNT_PREFIX + info.getUserId(); @@ -148,24 +149,17 @@ private void parseServerInfo(Map routes, String key) { @Override - public void pushMsg(String url, long sendUserId, ChatReqVO groupReqVO) throws Exception { + public void pushMsg(CIMServerResVO cimServerResVO, long sendUserId, ChatReqVO groupReqVO) throws Exception { CIMUserInfo cimUserInfo = userInfoCacheService.loadUserInfoByUserId(sendUserId); - JSONObject jsonObject = new JSONObject(); - jsonObject.put("msg", cimUserInfo.getUserName() + ":" + groupReqVO.getMsg()); - jsonObject.put("userId", groupReqVO.getUserId()); - RequestBody requestBody = RequestBody.create(mediaType, jsonObject.toString()); - - Request request = new Request.Builder() - .url(url) - .post(requestBody) - .build(); - - Response response = okHttpClient.newCall(request).execute(); + String url = "http://" + cimServerResVO.getIp() + ":" + cimServerResVO.getHttpPort(); + ServerApi serverApi = new ProxyManager<>(ServerApi.class, url, okHttpClient).getInstance(); + SendMsgReqVO vo = new SendMsgReqVO(cimUserInfo.getUserName() + ":" + groupReqVO.getMsg(), groupReqVO.getUserId()); + Response response = null; try { - if (!response.isSuccessful()) { - throw new IOException("Unexpected code " + response); - } + response = (Response) serverApi.sendMsg(vo); + } catch (Exception e) { + LOGGER.error("Exception", e); } finally { response.body().close(); } diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java index 8eaf6a98..ba2fa8d2 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java +++ b/cim-forward-route/src/main/java/com/crossoverjie/cim/route/service/impl/UserInfoCacheServiceImpl.java @@ -25,7 +25,7 @@ public class UserInfoCacheServiceImpl implements UserInfoCacheService { /** - * 本地缓存,为了防止内存撑爆,后期可换为 LRU。 + * todo 本地缓存,为了防止内存撑爆,后期可换为 LRU。 */ private final static Map USER_INFO_MAP = new ConcurrentHashMap<>(64) ; diff --git a/cim-forward-route/src/test/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImplTest.java b/cim-forward-route/src/test/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImplTest.java index f7a924f8..dd48d9a1 100644 --- a/cim-forward-route/src/test/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImplTest.java +++ b/cim-forward-route/src/test/java/com/crossoverjie/cim/route/service/impl/AccountServiceRedisImplTest.java @@ -2,8 +2,8 @@ import com.alibaba.fastjson.JSON; import com.crossoverjie.cim.route.RouteApplication; +import com.crossoverjie.cim.route.api.vo.res.CIMServerResVO; import com.crossoverjie.cim.route.service.AccountService; -import com.crossoverjie.cim.route.vo.res.CIMServerResVO; import org.junit.Test; import org.junit.runner.RunWith; import org.slf4j.Logger; diff --git a/cim-rout-api/pom.xml b/cim-rout-api/pom.xml new file mode 100644 index 00000000..3c05f959 --- /dev/null +++ b/cim-rout-api/pom.xml @@ -0,0 +1,33 @@ + + + + cim + com.crossoverjie.netty + 1.0.0-SNAPSHOT + + 4.0.0 + + cim-rout-api + 1.0.1-SNAPSHOT + + + + + com.crossoverjie.netty + cim-common + + + log4j + log4j + + + + + + org.springframework.boot + spring-boot-starter-web + + + \ No newline at end of file diff --git a/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/RouteApi.java b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/RouteApi.java new file mode 100644 index 00000000..90b65c3d --- /dev/null +++ b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/RouteApi.java @@ -0,0 +1,70 @@ +package com.crossoverjie.cim.route.api; + +import com.crossoverjie.cim.common.res.BaseResponse; +import com.crossoverjie.cim.route.api.vo.req.ChatReqVO; +import com.crossoverjie.cim.route.api.vo.req.LoginReqVO; +import com.crossoverjie.cim.route.api.vo.req.P2PReqVO; +import com.crossoverjie.cim.route.api.vo.req.RegisterInfoReqVO; +import com.crossoverjie.cim.route.api.vo.res.RegisterInfoResVO; + +/** + * Function: Route Api + * + * @author crossoverJie + * Date: 2020-04-24 23:43 + * @since JDK 1.8 + */ +public interface RouteApi { + + /** + * group chat + * + * @param groupReqVO + * @return + * @throws Exception + */ + Object groupRoute(ChatReqVO groupReqVO) throws Exception; + + /** + * Point to point chat + * @param p2pRequest + * @return + * @throws Exception + */ + Object p2pRoute(P2PReqVO p2pRequest) throws Exception; + + + /** + * Offline account + * + * @param groupReqVO + * @return + * @throws Exception + */ + Object offLine(ChatReqVO groupReqVO) throws Exception; + + /** + * Login account + * @param loginReqVO + * @return + * @throws Exception + */ + Object login(LoginReqVO loginReqVO) throws Exception; + + /** + * Register account + * + * @param registerInfoReqVO + * @return + * @throws Exception + */ + BaseResponse registerAccount(RegisterInfoReqVO registerInfoReqVO) throws Exception; + + /** + * Get all online users + * + * @return + * @throws Exception + */ + Object onlineUser() throws Exception; +} diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/ChatReqVO.java b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/ChatReqVO.java similarity index 96% rename from cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/ChatReqVO.java rename to cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/ChatReqVO.java index 2801668a..eb9ec2fa 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/ChatReqVO.java +++ b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/ChatReqVO.java @@ -1,4 +1,4 @@ -package com.crossoverjie.cim.route.vo.req; +package com.crossoverjie.cim.route.api.vo.req; import com.crossoverjie.cim.common.req.BaseRequest; import io.swagger.annotations.ApiModelProperty; diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/LoginReqVO.java b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/LoginReqVO.java similarity index 94% rename from cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/LoginReqVO.java rename to cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/LoginReqVO.java index 1821fdd8..fcd97d8f 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/LoginReqVO.java +++ b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/LoginReqVO.java @@ -1,4 +1,4 @@ -package com.crossoverjie.cim.route.vo.req; +package com.crossoverjie.cim.route.api.vo.req; import com.crossoverjie.cim.common.req.BaseRequest; diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/P2PReqVO.java b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/P2PReqVO.java similarity index 97% rename from cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/P2PReqVO.java rename to cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/P2PReqVO.java index e7dd642b..e24182bb 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/P2PReqVO.java +++ b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/P2PReqVO.java @@ -1,4 +1,4 @@ -package com.crossoverjie.cim.route.vo.req; +package com.crossoverjie.cim.route.api.vo.req; import com.crossoverjie.cim.common.req.BaseRequest; import io.swagger.annotations.ApiModelProperty; diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/RegisterInfoReqVO.java b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/RegisterInfoReqVO.java similarity index 94% rename from cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/RegisterInfoReqVO.java rename to cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/RegisterInfoReqVO.java index c3da2b21..9b1d14c5 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/RegisterInfoReqVO.java +++ b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/RegisterInfoReqVO.java @@ -1,4 +1,4 @@ -package com.crossoverjie.cim.route.vo.req; +package com.crossoverjie.cim.route.api.vo.req; import com.crossoverjie.cim.common.req.BaseRequest; import io.swagger.annotations.ApiModelProperty; diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/SendMsgReqVO.java b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/SendMsgReqVO.java similarity index 94% rename from cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/SendMsgReqVO.java rename to cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/SendMsgReqVO.java index 10820388..3f51fca7 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/req/SendMsgReqVO.java +++ b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/req/SendMsgReqVO.java @@ -1,4 +1,4 @@ -package com.crossoverjie.cim.route.vo.req; +package com.crossoverjie.cim.route.api.vo.req; import com.crossoverjie.cim.common.req.BaseRequest; import io.swagger.annotations.ApiModelProperty; diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/res/CIMServerResVO.java b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/res/CIMServerResVO.java similarity index 95% rename from cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/res/CIMServerResVO.java rename to cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/res/CIMServerResVO.java index af6679a7..cec52bfb 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/res/CIMServerResVO.java +++ b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/res/CIMServerResVO.java @@ -1,4 +1,4 @@ -package com.crossoverjie.cim.route.vo.res; +package com.crossoverjie.cim.route.api.vo.res; import com.crossoverjie.cim.common.pojo.RouteInfo; diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/res/RegisterInfoResVO.java b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/res/RegisterInfoResVO.java similarity index 94% rename from cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/res/RegisterInfoResVO.java rename to cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/res/RegisterInfoResVO.java index 908bbe64..3cfcdb96 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/res/RegisterInfoResVO.java +++ b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/res/RegisterInfoResVO.java @@ -1,4 +1,4 @@ -package com.crossoverjie.cim.route.vo.res; +package com.crossoverjie.cim.route.api.vo.res; import java.io.Serializable; diff --git a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/res/SendMsgResVO.java b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/res/SendMsgResVO.java similarity index 85% rename from cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/res/SendMsgResVO.java rename to cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/res/SendMsgResVO.java index 5ac1de4d..364d5d81 100644 --- a/cim-forward-route/src/main/java/com/crossoverjie/cim/route/vo/res/SendMsgResVO.java +++ b/cim-rout-api/src/main/java/com/crossoverjie/cim/route/api/vo/res/SendMsgResVO.java @@ -1,4 +1,4 @@ -package com.crossoverjie.cim.route.vo.res; +package com.crossoverjie.cim.route.api.vo.res; /** * Function: diff --git a/cim-server-api/pom.xml b/cim-server-api/pom.xml new file mode 100644 index 00000000..d406bef0 --- /dev/null +++ b/cim-server-api/pom.xml @@ -0,0 +1,33 @@ + + + + cim + com.crossoverjie.netty + 1.0.0-SNAPSHOT + + 4.0.0 + + cim-server-api + 1.0.1-SNAPSHOT + + + + com.crossoverjie.netty + cim-common + + + log4j + log4j + + + + + + org.springframework.boot + spring-boot-starter-web + + + + \ No newline at end of file diff --git a/cim-server-api/src/main/java/com/crossoverjie/cim/server/api/ServerApi.java b/cim-server-api/src/main/java/com/crossoverjie/cim/server/api/ServerApi.java new file mode 100644 index 00000000..9e1be26e --- /dev/null +++ b/cim-server-api/src/main/java/com/crossoverjie/cim/server/api/ServerApi.java @@ -0,0 +1,21 @@ +package com.crossoverjie.cim.server.api; + +import com.crossoverjie.cim.server.api.vo.req.SendMsgReqVO; + +/** + * Function: + * + * @author crossoverJie + * Date: 2020-04-25 14:23 + * @since JDK 1.8 + */ +public interface ServerApi { + + /** + * Push msg to client + * @param sendMsgReqVO + * @return + * @throws Exception + */ + Object sendMsg(SendMsgReqVO sendMsgReqVO) throws Exception; +} diff --git a/cim-server/src/main/java/com/crossoverjie/cim/server/vo/req/SendMsgReqVO.java b/cim-server-api/src/main/java/com/crossoverjie/cim/server/api/vo/req/SendMsgReqVO.java similarity index 84% rename from cim-server/src/main/java/com/crossoverjie/cim/server/vo/req/SendMsgReqVO.java rename to cim-server-api/src/main/java/com/crossoverjie/cim/server/api/vo/req/SendMsgReqVO.java index 9ff92bd2..86dce450 100644 --- a/cim-server/src/main/java/com/crossoverjie/cim/server/vo/req/SendMsgReqVO.java +++ b/cim-server-api/src/main/java/com/crossoverjie/cim/server/api/vo/req/SendMsgReqVO.java @@ -1,4 +1,4 @@ -package com.crossoverjie.cim.server.vo.req; +package com.crossoverjie.cim.server.api.vo.req; import com.crossoverjie.cim.common.req.BaseRequest; import io.swagger.annotations.ApiModelProperty; @@ -22,6 +22,14 @@ public class SendMsgReqVO extends BaseRequest { @ApiModelProperty(required = true, value = "userId", example = "11") private Long userId ; + public SendMsgReqVO() { + } + + public SendMsgReqVO(String msg, Long userId) { + this.msg = msg; + this.userId = userId; + } + public String getMsg() { return msg; } diff --git a/cim-server/src/main/java/com/crossoverjie/cim/server/vo/res/SendMsgResVO.java b/cim-server-api/src/main/java/com/crossoverjie/cim/server/api/vo/res/SendMsgResVO.java similarity index 85% rename from cim-server/src/main/java/com/crossoverjie/cim/server/vo/res/SendMsgResVO.java rename to cim-server-api/src/main/java/com/crossoverjie/cim/server/api/vo/res/SendMsgResVO.java index 4de76588..b5fed290 100644 --- a/cim-server/src/main/java/com/crossoverjie/cim/server/vo/res/SendMsgResVO.java +++ b/cim-server-api/src/main/java/com/crossoverjie/cim/server/api/vo/res/SendMsgResVO.java @@ -1,4 +1,4 @@ -package com.crossoverjie.cim.server.vo.res; +package com.crossoverjie.cim.server.api.vo.res; /** * Function: diff --git a/cim-server/pom.xml b/cim-server/pom.xml index d0ad5680..42a167aa 100644 --- a/cim-server/pom.xml +++ b/cim-server/pom.xml @@ -36,6 +36,15 @@ + + com.crossoverjie.netty + cim-rout-api + + + + com.crossoverjie.netty + cim-server-api + org.springframework.boot diff --git a/cim-server/src/main/java/com/crossoverjie/cim/server/config/AppConfiguration.java b/cim-server/src/main/java/com/crossoverjie/cim/server/config/AppConfiguration.java index 6215e74d..0ba33499 100644 --- a/cim-server/src/main/java/com/crossoverjie/cim/server/config/AppConfiguration.java +++ b/cim-server/src/main/java/com/crossoverjie/cim/server/config/AppConfiguration.java @@ -25,8 +25,16 @@ public class AppConfiguration { @Value("${cim.server.port}") private int cimServerPort; - @Value("${cim.clear.route.request.url}") - private String clearRouteUrl ; + @Value("${cim.route.url}") + private String routeUrl ; + + public String getRouteUrl() { + return routeUrl; + } + + public void setRouteUrl(String routeUrl) { + this.routeUrl = routeUrl; + } @Value("${cim.heartbeat.time}") private long heartBeatTime ; @@ -37,14 +45,6 @@ public class AppConfiguration { public int getZkConnectTimeout() { return zkConnectTimeout; } - - public String getClearRouteUrl() { - return clearRouteUrl; - } - - public void setClearRouteUrl(String clearRouteUrl) { - this.clearRouteUrl = clearRouteUrl; - } public String getZkRoot() { return zkRoot; diff --git a/cim-server/src/main/java/com/crossoverjie/cim/server/controller/IndexController.java b/cim-server/src/main/java/com/crossoverjie/cim/server/controller/IndexController.java index 6a1d7612..260067f7 100644 --- a/cim-server/src/main/java/com/crossoverjie/cim/server/controller/IndexController.java +++ b/cim-server/src/main/java/com/crossoverjie/cim/server/controller/IndexController.java @@ -1,11 +1,12 @@ package com.crossoverjie.cim.server.controller; +import com.crossoverjie.cim.common.constant.Constants; import com.crossoverjie.cim.common.enums.StatusEnum; import com.crossoverjie.cim.common.res.BaseResponse; -import com.crossoverjie.cim.server.vo.req.SendMsgReqVO; -import com.crossoverjie.cim.common.constant.Constants; +import com.crossoverjie.cim.server.api.ServerApi; +import com.crossoverjie.cim.server.api.vo.req.SendMsgReqVO; +import com.crossoverjie.cim.server.api.vo.res.SendMsgResVO; import com.crossoverjie.cim.server.server.CIMServer; -import com.crossoverjie.cim.server.vo.res.SendMsgResVO; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.actuate.metrics.CounterService; @@ -24,7 +25,7 @@ */ @Controller @RequestMapping("/") -public class IndexController { +public class IndexController implements ServerApi { @Autowired private CIMServer cimServer; @@ -37,11 +38,12 @@ public class IndexController { private CounterService counterService; /** - * 向服务端发消息 + * * @param sendMsgReqVO * @return */ - @ApiOperation("服务端发送消息") + @Override + @ApiOperation("Push msg to client") @RequestMapping(value = "sendMsg",method = RequestMethod.POST) @ResponseBody public BaseResponse sendMsg(@RequestBody SendMsgReqVO sendMsgReqVO){ diff --git a/cim-server/src/main/java/com/crossoverjie/cim/server/handle/CIMServerHandle.java b/cim-server/src/main/java/com/crossoverjie/cim/server/handle/CIMServerHandle.java index f2c33f3f..54b43e84 100644 --- a/cim-server/src/main/java/com/crossoverjie/cim/server/handle/CIMServerHandle.java +++ b/cim-server/src/main/java/com/crossoverjie/cim/server/handle/CIMServerHandle.java @@ -1,13 +1,12 @@ package com.crossoverjie.cim.server.handle; -import com.alibaba.fastjson.JSONObject; import com.crossoverjie.cim.common.constant.Constants; import com.crossoverjie.cim.common.exception.CIMException; import com.crossoverjie.cim.common.kit.HeartBeatHandler; import com.crossoverjie.cim.common.pojo.CIMUserInfo; import com.crossoverjie.cim.common.protocol.CIMRequestProto; import com.crossoverjie.cim.common.util.NettyAttrUtil; -import com.crossoverjie.cim.server.config.AppConfiguration; +import com.crossoverjie.cim.server.kit.RouteHandler; import com.crossoverjie.cim.server.kit.ServerHeartBeatHandlerImpl; import com.crossoverjie.cim.server.util.SessionSocketHolder; import com.crossoverjie.cim.server.util.SpringBeanFactory; @@ -18,12 +17,9 @@ import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.timeout.IdleState; import io.netty.handler.timeout.IdleStateEvent; -import okhttp3.*; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import java.io.IOException; - /** * Function: * @@ -36,7 +32,6 @@ public class CIMServerHandle extends SimpleChannelInboundHandler(RouteApi.class, configuration.getRouteUrl(), okHttpClient).getInstance(); Response response = null; + ChatReqVO vo = new ChatReqVO(userInfo.getUserId(), userInfo.getUserName()); try { - response = okHttpClient.newCall(request).execute(); - if (!response.isSuccessful()) { - throw new IOException("Unexpected code " + response); - } - } finally { + response = (Response) routeApi.offLine(vo); + } catch (Exception e){ + LOGGER.error("Exception",e); + }finally { response.body().close(); } } diff --git a/cim-server/src/main/java/com/crossoverjie/cim/server/server/CIMServer.java b/cim-server/src/main/java/com/crossoverjie/cim/server/server/CIMServer.java index 9b12bacc..69baf42b 100644 --- a/cim-server/src/main/java/com/crossoverjie/cim/server/server/CIMServer.java +++ b/cim-server/src/main/java/com/crossoverjie/cim/server/server/CIMServer.java @@ -2,9 +2,9 @@ import com.crossoverjie.cim.common.constant.Constants; import com.crossoverjie.cim.common.protocol.CIMRequestProto; +import com.crossoverjie.cim.server.api.vo.req.SendMsgReqVO; import com.crossoverjie.cim.server.init.CIMServerInitializer; import com.crossoverjie.cim.server.util.SessionSocketHolder; -import com.crossoverjie.cim.server.vo.req.SendMsgReqVO; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelFutureListener; @@ -78,7 +78,7 @@ public void destroy() { /** - * 发送 Google Protocol 编码消息 + * Push msg to client. * @param sendMsgReqVO 消息 */ public void sendMsg(SendMsgReqVO sendMsgReqVO){ diff --git a/cim-server/src/main/resources/application.properties b/cim-server/src/main/resources/application.properties index 947d6993..6ef76771 100644 --- a/cim-server/src/main/resources/application.properties +++ b/cim-server/src/main/resources/application.properties @@ -33,8 +33,8 @@ app.zk.connect.timeout=15000 # zk 注册根节点 app.zk.root=/route -# 清除路由信息 -cim.clear.route.request.url=http://localhost:8083/offLine +# route url suggested that this is Nginx address +cim.route.url=http://localhost:8083/ # 检测多少秒没有收到客户端心跳后服务端关闭连接 单位秒 cim.heartbeat.time = 30 \ No newline at end of file diff --git a/pom.xml b/pom.xml index 70c4e78c..5242b3e7 100644 --- a/pom.xml +++ b/pom.xml @@ -31,7 +31,9 @@ cim-client cim-common cim-forward-route - + cim-rout-api + cim-server-api + @@ -54,6 +56,18 @@ 1.0.1-SNAPSHOT + + com.crossoverjie.netty + cim-rout-api + 1.0.1-SNAPSHOT + + + + com.crossoverjie.netty + cim-server-api + 1.0.1-SNAPSHOT + + com.google.protobuf protobuf-java