作者 crossoverJie

:recycle: Refactoring code.联调成功2

正在显示 29 个修改的文件 包含 687 行增加100 行删除
@@ -4,7 +4,9 @@ import com.alibaba.fastjson.JSON; @@ -4,7 +4,9 @@ import com.alibaba.fastjson.JSON;
4 import com.crossoverjie.cim.client.init.CIMClientHandleInitializer; 4 import com.crossoverjie.cim.client.init.CIMClientHandleInitializer;
5 import com.crossoverjie.cim.client.service.RouteRequest; 5 import com.crossoverjie.cim.client.service.RouteRequest;
6 import com.crossoverjie.cim.client.vo.req.GoogleProtocolVO; 6 import com.crossoverjie.cim.client.vo.req.GoogleProtocolVO;
  7 +import com.crossoverjie.cim.client.vo.req.LoginReqVO;
7 import com.crossoverjie.cim.client.vo.res.CIMServerResVO; 8 import com.crossoverjie.cim.client.vo.res.CIMServerResVO;
  9 +import com.crossoverjie.cim.common.constant.Constants;
8 import com.crossoverjie.cim.common.pojo.CustomProtocol; 10 import com.crossoverjie.cim.common.pojo.CustomProtocol;
9 import com.crossoverjie.cim.common.protocol.CIMRequestProto; 11 import com.crossoverjie.cim.common.protocol.CIMRequestProto;
10 import io.netty.bootstrap.Bootstrap; 12 import io.netty.bootstrap.Bootstrap;
@@ -38,6 +40,12 @@ public class CIMClient { @@ -38,6 +40,12 @@ public class CIMClient {
38 40
39 private EventLoopGroup group = new NioEventLoopGroup(); 41 private EventLoopGroup group = new NioEventLoopGroup();
40 42
  43 + @Value("${cim.user.id}")
  44 + private long userId;
  45 +
  46 + @Value("${cim.user.userName}")
  47 + private String userName;
  48 +
41 49
42 @Value("${netty.server.port}") 50 @Value("${netty.server.port}")
43 private int nettyPort; 51 private int nettyPort;
@@ -52,10 +60,38 @@ public class CIMClient { @@ -52,10 +60,38 @@ public class CIMClient {
52 60
53 @PostConstruct 61 @PostConstruct
54 public void start() throws Exception { 62 public void start() throws Exception {
55 - //获取可以使用的服务器 ip+port  
56 - CIMServerResVO.ServerInfo cimServer = routeRequest.getCIMServer(); 63 + //登录 + 获取可以使用的服务器 ip+port
  64 + LoginReqVO loginReqVO = new LoginReqVO(userId,userName) ;
  65 + CIMServerResVO.ServerInfo cimServer = routeRequest.getCIMServer(loginReqVO);
57 LOGGER.info("cimServer=[{}]",cimServer.toString()); 66 LOGGER.info("cimServer=[{}]",cimServer.toString());
58 67
  68 + //启动客户端
  69 + startClient(cimServer);
  70 +
  71 + //向服务端注册
  72 + loginCIMServer();
  73 + }
  74 +
  75 + /**
  76 + * 向服务器注册
  77 + */
  78 + private void loginCIMServer() {
  79 + CIMRequestProto.CIMReqProtocol login = CIMRequestProto.CIMReqProtocol.newBuilder()
  80 + .setRequestId(userId)
  81 + .setReqMsg(userName)
  82 + .setType(Constants.CommandType.LOGIN)
  83 + .build();
  84 + ChannelFuture future = channel.writeAndFlush(login);
  85 + future.addListener((ChannelFutureListener) channelFuture ->
  86 + LOGGER.info("注册成功={}", login.toString()));
  87 + }
  88 +
  89 + /**
  90 + * 启动客户端
  91 + * @param cimServer
  92 + * @throws InterruptedException
  93 + */
  94 + private void startClient(CIMServerResVO.ServerInfo cimServer) throws InterruptedException {
59 Bootstrap bootstrap = new Bootstrap(); 95 Bootstrap bootstrap = new Bootstrap();
60 bootstrap.group(group) 96 bootstrap.group(group)
61 .channel(NioSocketChannel.class) 97 .channel(NioSocketChannel.class)
@@ -104,6 +140,7 @@ public class CIMClient { @@ -104,6 +140,7 @@ public class CIMClient {
104 CIMRequestProto.CIMReqProtocol protocol = CIMRequestProto.CIMReqProtocol.newBuilder() 140 CIMRequestProto.CIMReqProtocol protocol = CIMRequestProto.CIMReqProtocol.newBuilder()
105 .setRequestId(googleProtocolVO.getRequestId()) 141 .setRequestId(googleProtocolVO.getRequestId())
106 .setReqMsg(googleProtocolVO.getMsg()) 142 .setReqMsg(googleProtocolVO.getMsg())
  143 + .setType(Constants.CommandType.MSG)
107 .build(); 144 .build();
108 145
109 146
1 package com.crossoverjie.cim.client.config; 1 package com.crossoverjie.cim.client.config;
2 2
  3 +import com.crossoverjie.cim.common.constant.Constants;
3 import com.crossoverjie.cim.common.protocol.CIMRequestProto; 4 import com.crossoverjie.cim.common.protocol.CIMRequestProto;
4 import okhttp3.OkHttpClient; 5 import okhttp3.OkHttpClient;
5 import org.springframework.beans.factory.annotation.Value; 6 import org.springframework.beans.factory.annotation.Value;
@@ -18,8 +19,8 @@ import java.util.concurrent.TimeUnit; @@ -18,8 +19,8 @@ import java.util.concurrent.TimeUnit;
18 @Configuration 19 @Configuration
19 public class BeanConfig { 20 public class BeanConfig {
20 21
21 - @Value("${client.request.id}")  
22 - private int requestId; 22 + @Value("${cim.user.id}")
  23 + private long userId;
23 24
24 25
25 /** 26 /**
@@ -29,8 +30,9 @@ public class BeanConfig { @@ -29,8 +30,9 @@ public class BeanConfig {
29 @Bean(value = "heartBeat") 30 @Bean(value = "heartBeat")
30 public CIMRequestProto.CIMReqProtocol heartBeat() { 31 public CIMRequestProto.CIMReqProtocol heartBeat() {
31 CIMRequestProto.CIMReqProtocol heart = CIMRequestProto.CIMReqProtocol.newBuilder() 32 CIMRequestProto.CIMReqProtocol heart = CIMRequestProto.CIMReqProtocol.newBuilder()
32 - .setRequestId(requestId) 33 + .setRequestId(userId)
33 .setReqMsg("ping") 34 .setReqMsg("ping")
  35 + .setType(Constants.CommandType.PING)
34 .build(); 36 .build();
35 return heart; 37 return heart;
36 } 38 }
1 package com.crossoverjie.cim.client.service; 1 package com.crossoverjie.cim.client.service;
2 2
  3 +import com.crossoverjie.cim.client.vo.req.LoginReqVO;
3 import com.crossoverjie.cim.client.vo.res.CIMServerResVO; 4 import com.crossoverjie.cim.client.vo.res.CIMServerResVO;
4 5
5 /** 6 /**
@@ -23,5 +24,5 @@ public interface RouteRequest { @@ -23,5 +24,5 @@ public interface RouteRequest {
23 * @return 服务ip+port 24 * @return 服务ip+port
24 * @throws Exception 25 * @throws Exception
25 */ 26 */
26 - CIMServerResVO.ServerInfo getCIMServer() throws Exception; 27 + CIMServerResVO.ServerInfo getCIMServer(LoginReqVO loginReqVO) throws Exception;
27 } 28 }
@@ -3,6 +3,7 @@ package com.crossoverjie.cim.client.service.impl; @@ -3,6 +3,7 @@ package com.crossoverjie.cim.client.service.impl;
3 import com.alibaba.fastjson.JSON; 3 import com.alibaba.fastjson.JSON;
4 import com.alibaba.fastjson.JSONObject; 4 import com.alibaba.fastjson.JSONObject;
5 import com.crossoverjie.cim.client.service.RouteRequest; 5 import com.crossoverjie.cim.client.service.RouteRequest;
  6 +import com.crossoverjie.cim.client.vo.req.LoginReqVO;
6 import com.crossoverjie.cim.client.vo.res.CIMServerResVO; 7 import com.crossoverjie.cim.client.vo.res.CIMServerResVO;
7 import com.crossoverjie.cim.common.enums.StatusEnum; 8 import com.crossoverjie.cim.common.enums.StatusEnum;
8 import okhttp3.*; 9 import okhttp3.*;
@@ -56,9 +57,11 @@ public class RouteRequestImpl implements RouteRequest { @@ -56,9 +57,11 @@ public class RouteRequestImpl implements RouteRequest {
56 } 57 }
57 58
58 @Override 59 @Override
59 - public CIMServerResVO.ServerInfo getCIMServer() throws Exception { 60 + public CIMServerResVO.ServerInfo getCIMServer(LoginReqVO loginReqVO) throws Exception {
60 61
61 JSONObject jsonObject = new JSONObject(); 62 JSONObject jsonObject = new JSONObject();
  63 + jsonObject.put("userId",loginReqVO.getUserId());
  64 + jsonObject.put("userName",loginReqVO.getUserName());
62 RequestBody requestBody = RequestBody.create(mediaType,jsonObject.toString()); 65 RequestBody requestBody = RequestBody.create(mediaType,jsonObject.toString());
63 66
64 Request request = new Request.Builder() 67 Request request = new Request.Builder()
  1 +package com.crossoverjie.cim.client.vo.req;
  2 +
  3 +import com.crossoverjie.cim.common.req.BaseRequest;
  4 +
  5 +/**
  6 + * Function:
  7 + *
  8 + * @author crossoverJie
  9 + * Date: 2018/12/23 22:30
  10 + * @since JDK 1.8
  11 + */
  12 +public class LoginReqVO extends BaseRequest{
  13 + private Long userId ;
  14 + private String userName ;
  15 +
  16 + public LoginReqVO(Long userId, String userName) {
  17 + this.userId = userId;
  18 + this.userName = userName;
  19 + }
  20 +
  21 + public Long getUserId() {
  22 + return userId;
  23 + }
  24 +
  25 + public void setUserId(Long userId) {
  26 + this.userId = userId;
  27 + }
  28 +
  29 + public String getUserName() {
  30 + return userName;
  31 + }
  32 +
  33 + public void setUserName(String userName) {
  34 + this.userName = userName;
  35 + }
  36 +
  37 + @Override
  38 + public String toString() {
  39 + return "LoginReqVO{" +
  40 + "userId=" + userId +
  41 + ", userName='" + userName + '\'' +
  42 + "} " + super.toString();
  43 + }
  44 +}
@@ -15,10 +15,11 @@ logging.level.root=info @@ -15,10 +15,11 @@ logging.level.root=info
15 cim.group.route.request.url=http://localhost:8083/groupRoute 15 cim.group.route.request.url=http://localhost:8083/groupRoute
16 16
17 # 获取服务器ip+port 17 # 获取服务器ip+port
18 -cim.server.route.request.url=http://localhost:8083/getCIMServer 18 +cim.server.route.request.url=http://localhost:8083/login
19 19
20 # 客户端唯一ID 20 # 客户端唯一ID
21 -client.request.id=100 21 +cim.user.id=1545574841528
  22 +cim.user.userName=zhangsan
22 23
23 24
24 # 关闭健康检查权限 25 # 关闭健康检查权限
  1 +package com.crossoverjie.cim.server.test;
  2 +
  3 +import com.crossoverjie.cim.client.CIMClientApplication;
  4 +import com.crossoverjie.cim.client.service.RouteRequest;
  5 +import com.crossoverjie.cim.client.vo.req.LoginReqVO;
  6 +import com.crossoverjie.cim.client.vo.res.CIMServerResVO;
  7 +import org.junit.Test;
  8 +import org.junit.runner.RunWith;
  9 +import org.slf4j.Logger;
  10 +import org.slf4j.LoggerFactory;
  11 +import org.springframework.beans.factory.annotation.Autowired;
  12 +import org.springframework.beans.factory.annotation.Value;
  13 +import org.springframework.boot.test.context.SpringBootTest;
  14 +import org.springframework.test.context.junit4.SpringRunner;
  15 +
  16 +/**
  17 + * Function:
  18 + *
  19 + * @author crossoverJie
  20 + * Date: 2018/12/23 22:39
  21 + * @since JDK 1.8
  22 + */
  23 +@SpringBootTest(classes = CIMClientApplication.class)
  24 +@RunWith(SpringRunner.class)
  25 +public class RouteTest {
  26 +
  27 + private final static Logger LOGGER = LoggerFactory.getLogger(RouteTest.class);
  28 +
  29 + @Value("${cim.user.id}")
  30 + private long userId;
  31 +
  32 + @Value("${cim.user.userName}")
  33 + private String userName;
  34 +
  35 + @Autowired
  36 + private RouteRequest routeRequest ;
  37 +
  38 + @Test
  39 + public void test() throws Exception {
  40 + LoginReqVO vo = new LoginReqVO(userId,userName) ;
  41 + CIMServerResVO.ServerInfo cimServer = routeRequest.getCIMServer(vo);
  42 + LOGGER.info("cimServer=[{}]",cimServer.toString());
  43 + }
  44 +}
@@ -22,4 +22,23 @@ public class Constants { @@ -22,4 +22,23 @@ public class Constants {
22 public static final String COUNTER_CLIENT_PUSH_COUNT = "counter.client.push.count" ; 22 public static final String COUNTER_CLIENT_PUSH_COUNT = "counter.client.push.count" ;
23 23
24 24
  25 +
  26 +
  27 + public static class CommandType{
  28 + /**
  29 + * 登录
  30 + */
  31 + public static final int LOGIN = 1 ;
  32 + /**
  33 + * 业务消息
  34 + */
  35 + public static final int MSG = 2 ;
  36 +
  37 + /**
  38 + * 业务消息
  39 + */
  40 + public static final int PING = 3 ;
  41 + }
  42 +
  43 +
25 } 44 }
@@ -19,13 +19,13 @@ public final class CIMRequestProto { @@ -19,13 +19,13 @@ public final class CIMRequestProto {
19 com.google.protobuf.MessageOrBuilder { 19 com.google.protobuf.MessageOrBuilder {
20 20
21 /** 21 /**
22 - * <code>required int32 requestId = 2;</code> 22 + * <code>required int64 requestId = 2;</code>
23 */ 23 */
24 boolean hasRequestId(); 24 boolean hasRequestId();
25 /** 25 /**
26 - * <code>required int32 requestId = 2;</code> 26 + * <code>required int64 requestId = 2;</code>
27 */ 27 */
28 - int getRequestId(); 28 + long getRequestId();
29 29
30 /** 30 /**
31 * <code>required string reqMsg = 1;</code> 31 * <code>required string reqMsg = 1;</code>
@@ -40,6 +40,15 @@ public final class CIMRequestProto { @@ -40,6 +40,15 @@ public final class CIMRequestProto {
40 */ 40 */
41 com.google.protobuf.ByteString 41 com.google.protobuf.ByteString
42 getReqMsgBytes(); 42 getReqMsgBytes();
  43 +
  44 + /**
  45 + * <code>required int32 type = 3;</code>
  46 + */
  47 + boolean hasType();
  48 + /**
  49 + * <code>required int32 type = 3;</code>
  50 + */
  51 + int getType();
43 } 52 }
44 /** 53 /**
45 * Protobuf type {@code protocol.CIMReqProtocol} 54 * Protobuf type {@code protocol.CIMReqProtocol}
@@ -54,8 +63,9 @@ public final class CIMRequestProto { @@ -54,8 +63,9 @@ public final class CIMRequestProto {
54 super(builder); 63 super(builder);
55 } 64 }
56 private CIMReqProtocol() { 65 private CIMReqProtocol() {
57 - requestId_ = 0; 66 + requestId_ = 0L;
58 reqMsg_ = ""; 67 reqMsg_ = "";
  68 + type_ = 0;
59 } 69 }
60 70
61 @Override 71 @Override
@@ -97,7 +107,12 @@ public final class CIMRequestProto { @@ -97,7 +107,12 @@ public final class CIMRequestProto {
97 } 107 }
98 case 16: { 108 case 16: {
99 bitField0_ |= 0x00000001; 109 bitField0_ |= 0x00000001;
100 - requestId_ = input.readInt32(); 110 + requestId_ = input.readInt64();
  111 + break;
  112 + }
  113 + case 24: {
  114 + bitField0_ |= 0x00000004;
  115 + type_ = input.readInt32();
101 break; 116 break;
102 } 117 }
103 } 118 }
@@ -126,17 +141,17 @@ public final class CIMRequestProto { @@ -126,17 +141,17 @@ public final class CIMRequestProto {
126 141
127 private int bitField0_; 142 private int bitField0_;
128 public static final int REQUESTID_FIELD_NUMBER = 2; 143 public static final int REQUESTID_FIELD_NUMBER = 2;
129 - private int requestId_; 144 + private long requestId_;
130 /** 145 /**
131 - * <code>required int32 requestId = 2;</code> 146 + * <code>required int64 requestId = 2;</code>
132 */ 147 */
133 public boolean hasRequestId() { 148 public boolean hasRequestId() {
134 return ((bitField0_ & 0x00000001) == 0x00000001); 149 return ((bitField0_ & 0x00000001) == 0x00000001);
135 } 150 }
136 /** 151 /**
137 - * <code>required int32 requestId = 2;</code> 152 + * <code>required int64 requestId = 2;</code>
138 */ 153 */
139 - public int getRequestId() { 154 + public long getRequestId() {
140 return requestId_; 155 return requestId_;
141 } 156 }
142 157
@@ -182,6 +197,21 @@ public final class CIMRequestProto { @@ -182,6 +197,21 @@ public final class CIMRequestProto {
182 } 197 }
183 } 198 }
184 199
  200 + public static final int TYPE_FIELD_NUMBER = 3;
  201 + private int type_;
  202 + /**
  203 + * <code>required int32 type = 3;</code>
  204 + */
  205 + public boolean hasType() {
  206 + return ((bitField0_ & 0x00000004) == 0x00000004);
  207 + }
  208 + /**
  209 + * <code>required int32 type = 3;</code>
  210 + */
  211 + public int getType() {
  212 + return type_;
  213 + }
  214 +
185 private byte memoizedIsInitialized = -1; 215 private byte memoizedIsInitialized = -1;
186 public final boolean isInitialized() { 216 public final boolean isInitialized() {
187 byte isInitialized = memoizedIsInitialized; 217 byte isInitialized = memoizedIsInitialized;
@@ -196,6 +226,10 @@ public final class CIMRequestProto { @@ -196,6 +226,10 @@ public final class CIMRequestProto {
196 memoizedIsInitialized = 0; 226 memoizedIsInitialized = 0;
197 return false; 227 return false;
198 } 228 }
  229 + if (!hasType()) {
  230 + memoizedIsInitialized = 0;
  231 + return false;
  232 + }
199 memoizedIsInitialized = 1; 233 memoizedIsInitialized = 1;
200 return true; 234 return true;
201 } 235 }
@@ -206,7 +240,10 @@ public final class CIMRequestProto { @@ -206,7 +240,10 @@ public final class CIMRequestProto {
206 com.google.protobuf.GeneratedMessageV3.writeString(output, 1, reqMsg_); 240 com.google.protobuf.GeneratedMessageV3.writeString(output, 1, reqMsg_);
207 } 241 }
208 if (((bitField0_ & 0x00000001) == 0x00000001)) { 242 if (((bitField0_ & 0x00000001) == 0x00000001)) {
209 - output.writeInt32(2, requestId_); 243 + output.writeInt64(2, requestId_);
  244 + }
  245 + if (((bitField0_ & 0x00000004) == 0x00000004)) {
  246 + output.writeInt32(3, type_);
210 } 247 }
211 unknownFields.writeTo(output); 248 unknownFields.writeTo(output);
212 } 249 }
@@ -221,7 +258,11 @@ public final class CIMRequestProto { @@ -221,7 +258,11 @@ public final class CIMRequestProto {
221 } 258 }
222 if (((bitField0_ & 0x00000001) == 0x00000001)) { 259 if (((bitField0_ & 0x00000001) == 0x00000001)) {
223 size += com.google.protobuf.CodedOutputStream 260 size += com.google.protobuf.CodedOutputStream
224 - .computeInt32Size(2, requestId_); 261 + .computeInt64Size(2, requestId_);
  262 + }
  263 + if (((bitField0_ & 0x00000004) == 0x00000004)) {
  264 + size += com.google.protobuf.CodedOutputStream
  265 + .computeInt32Size(3, type_);
225 } 266 }
226 size += unknownFields.getSerializedSize(); 267 size += unknownFields.getSerializedSize();
227 memoizedSize = size; 268 memoizedSize = size;
@@ -249,6 +290,11 @@ public final class CIMRequestProto { @@ -249,6 +290,11 @@ public final class CIMRequestProto {
249 result = result && getReqMsg() 290 result = result && getReqMsg()
250 .equals(other.getReqMsg()); 291 .equals(other.getReqMsg());
251 } 292 }
  293 + result = result && (hasType() == other.hasType());
  294 + if (hasType()) {
  295 + result = result && (getType()
  296 + == other.getType());
  297 + }
252 result = result && unknownFields.equals(other.unknownFields); 298 result = result && unknownFields.equals(other.unknownFields);
253 return result; 299 return result;
254 } 300 }
@@ -262,12 +308,17 @@ public final class CIMRequestProto { @@ -262,12 +308,17 @@ public final class CIMRequestProto {
262 hash = (19 * hash) + getDescriptor().hashCode(); 308 hash = (19 * hash) + getDescriptor().hashCode();
263 if (hasRequestId()) { 309 if (hasRequestId()) {
264 hash = (37 * hash) + REQUESTID_FIELD_NUMBER; 310 hash = (37 * hash) + REQUESTID_FIELD_NUMBER;
265 - hash = (53 * hash) + getRequestId(); 311 + hash = (53 * hash) + com.google.protobuf.Internal.hashLong(
  312 + getRequestId());
266 } 313 }
267 if (hasReqMsg()) { 314 if (hasReqMsg()) {
268 hash = (37 * hash) + REQMSG_FIELD_NUMBER; 315 hash = (37 * hash) + REQMSG_FIELD_NUMBER;
269 hash = (53 * hash) + getReqMsg().hashCode(); 316 hash = (53 * hash) + getReqMsg().hashCode();
270 } 317 }
  318 + if (hasType()) {
  319 + hash = (37 * hash) + TYPE_FIELD_NUMBER;
  320 + hash = (53 * hash) + getType();
  321 + }
271 hash = (29 * hash) + unknownFields.hashCode(); 322 hash = (29 * hash) + unknownFields.hashCode();
272 memoizedHashCode = hash; 323 memoizedHashCode = hash;
273 return hash; 324 return hash;
@@ -397,10 +448,12 @@ public final class CIMRequestProto { @@ -397,10 +448,12 @@ public final class CIMRequestProto {
397 } 448 }
398 public Builder clear() { 449 public Builder clear() {
399 super.clear(); 450 super.clear();
400 - requestId_ = 0; 451 + requestId_ = 0L;
401 bitField0_ = (bitField0_ & ~0x00000001); 452 bitField0_ = (bitField0_ & ~0x00000001);
402 reqMsg_ = ""; 453 reqMsg_ = "";
403 bitField0_ = (bitField0_ & ~0x00000002); 454 bitField0_ = (bitField0_ & ~0x00000002);
  455 + type_ = 0;
  456 + bitField0_ = (bitField0_ & ~0x00000004);
404 return this; 457 return this;
405 } 458 }
406 459
@@ -433,6 +486,10 @@ public final class CIMRequestProto { @@ -433,6 +486,10 @@ public final class CIMRequestProto {
433 to_bitField0_ |= 0x00000002; 486 to_bitField0_ |= 0x00000002;
434 } 487 }
435 result.reqMsg_ = reqMsg_; 488 result.reqMsg_ = reqMsg_;
  489 + if (((from_bitField0_ & 0x00000004) == 0x00000004)) {
  490 + to_bitField0_ |= 0x00000004;
  491 + }
  492 + result.type_ = type_;
436 result.bitField0_ = to_bitField0_; 493 result.bitField0_ = to_bitField0_;
437 onBuilt(); 494 onBuilt();
438 return result; 495 return result;
@@ -483,6 +540,9 @@ public final class CIMRequestProto { @@ -483,6 +540,9 @@ public final class CIMRequestProto {
483 reqMsg_ = other.reqMsg_; 540 reqMsg_ = other.reqMsg_;
484 onChanged(); 541 onChanged();
485 } 542 }
  543 + if (other.hasType()) {
  544 + setType(other.getType());
  545 + }
486 this.mergeUnknownFields(other.unknownFields); 546 this.mergeUnknownFields(other.unknownFields);
487 onChanged(); 547 onChanged();
488 return this; 548 return this;
@@ -495,6 +555,9 @@ public final class CIMRequestProto { @@ -495,6 +555,9 @@ public final class CIMRequestProto {
495 if (!hasReqMsg()) { 555 if (!hasReqMsg()) {
496 return false; 556 return false;
497 } 557 }
  558 + if (!hasType()) {
  559 + return false;
  560 + }
498 return true; 561 return true;
499 } 562 }
500 563
@@ -517,34 +580,34 @@ public final class CIMRequestProto { @@ -517,34 +580,34 @@ public final class CIMRequestProto {
517 } 580 }
518 private int bitField0_; 581 private int bitField0_;
519 582
520 - private int requestId_ ; 583 + private long requestId_ ;
521 /** 584 /**
522 - * <code>required int32 requestId = 2;</code> 585 + * <code>required int64 requestId = 2;</code>
523 */ 586 */
524 public boolean hasRequestId() { 587 public boolean hasRequestId() {
525 return ((bitField0_ & 0x00000001) == 0x00000001); 588 return ((bitField0_ & 0x00000001) == 0x00000001);
526 } 589 }
527 /** 590 /**
528 - * <code>required int32 requestId = 2;</code> 591 + * <code>required int64 requestId = 2;</code>
529 */ 592 */
530 - public int getRequestId() { 593 + public long getRequestId() {
531 return requestId_; 594 return requestId_;
532 } 595 }
533 /** 596 /**
534 - * <code>required int32 requestId = 2;</code> 597 + * <code>required int64 requestId = 2;</code>
535 */ 598 */
536 - public Builder setRequestId(int value) { 599 + public Builder setRequestId(long value) {
537 bitField0_ |= 0x00000001; 600 bitField0_ |= 0x00000001;
538 requestId_ = value; 601 requestId_ = value;
539 onChanged(); 602 onChanged();
540 return this; 603 return this;
541 } 604 }
542 /** 605 /**
543 - * <code>required int32 requestId = 2;</code> 606 + * <code>required int64 requestId = 2;</code>
544 */ 607 */
545 public Builder clearRequestId() { 608 public Builder clearRequestId() {
546 bitField0_ = (bitField0_ & ~0x00000001); 609 bitField0_ = (bitField0_ & ~0x00000001);
547 - requestId_ = 0; 610 + requestId_ = 0L;
548 onChanged(); 611 onChanged();
549 return this; 612 return this;
550 } 613 }
@@ -624,6 +687,38 @@ public final class CIMRequestProto { @@ -624,6 +687,38 @@ public final class CIMRequestProto {
624 onChanged(); 687 onChanged();
625 return this; 688 return this;
626 } 689 }
  690 +
  691 + private int type_ ;
  692 + /**
  693 + * <code>required int32 type = 3;</code>
  694 + */
  695 + public boolean hasType() {
  696 + return ((bitField0_ & 0x00000004) == 0x00000004);
  697 + }
  698 + /**
  699 + * <code>required int32 type = 3;</code>
  700 + */
  701 + public int getType() {
  702 + return type_;
  703 + }
  704 + /**
  705 + * <code>required int32 type = 3;</code>
  706 + */
  707 + public Builder setType(int value) {
  708 + bitField0_ |= 0x00000004;
  709 + type_ = value;
  710 + onChanged();
  711 + return this;
  712 + }
  713 + /**
  714 + * <code>required int32 type = 3;</code>
  715 + */
  716 + public Builder clearType() {
  717 + bitField0_ = (bitField0_ & ~0x00000004);
  718 + type_ = 0;
  719 + onChanged();
  720 + return this;
  721 + }
627 public final Builder setUnknownFields( 722 public final Builder setUnknownFields(
628 final com.google.protobuf.UnknownFieldSet unknownFields) { 723 final com.google.protobuf.UnknownFieldSet unknownFields) {
629 return super.setUnknownFields(unknownFields); 724 return super.setUnknownFields(unknownFields);
@@ -687,10 +782,10 @@ public final class CIMRequestProto { @@ -687,10 +782,10 @@ public final class CIMRequestProto {
687 descriptor; 782 descriptor;
688 static { 783 static {
689 String[] descriptorData = { 784 String[] descriptorData = {
690 - "\n\026BaseRequestProto.proto\022\010protocol\"3\n\016CI" +  
691 - "MReqProtocol\022\021\n\trequestId\030\002 \002(\005\022\016\n\006reqMs" +  
692 - "g\030\001 \002(\tB7\n$com.crossoverjie.cim.common.p" +  
693 - "rotocolB\017CIMRequestProto" 785 + "\n\026BaseRequestProto.proto\022\010protocol\"A\n\016CI" +
  786 + "MReqProtocol\022\021\n\trequestId\030\002 \002(\003\022\016\n\006reqMs" +
  787 + "g\030\001 \002(\t\022\014\n\004type\030\003 \002(\005B7\n$com.crossoverji" +
  788 + "e.cim.common.protocolB\017CIMRequestProto"
694 }; 789 };
695 com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner = 790 com.google.protobuf.Descriptors.FileDescriptor.InternalDescriptorAssigner assigner =
696 new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() { 791 new com.google.protobuf.Descriptors.FileDescriptor. InternalDescriptorAssigner() {
@@ -709,7 +804,7 @@ public final class CIMRequestProto { @@ -709,7 +804,7 @@ public final class CIMRequestProto {
709 internal_static_protocol_CIMReqProtocol_fieldAccessorTable = new 804 internal_static_protocol_CIMReqProtocol_fieldAccessorTable = new
710 com.google.protobuf.GeneratedMessageV3.FieldAccessorTable( 805 com.google.protobuf.GeneratedMessageV3.FieldAccessorTable(
711 internal_static_protocol_CIMReqProtocol_descriptor, 806 internal_static_protocol_CIMReqProtocol_descriptor,
712 - new String[] { "RequestId", "ReqMsg", }); 807 + new String[] { "RequestId", "ReqMsg", "Type", });
713 } 808 }
714 809
715 // @@protoc_insertion_point(outer_class_scope) 810 // @@protoc_insertion_point(outer_class_scope)
@@ -19,13 +19,13 @@ public final class CIMResponseProto { @@ -19,13 +19,13 @@ public final class CIMResponseProto {
19 com.google.protobuf.MessageOrBuilder { 19 com.google.protobuf.MessageOrBuilder {
20 20
21 /** 21 /**
22 - * <code>required int32 responseId = 2;</code> 22 + * <code>required int64 responseId = 2;</code>
23 */ 23 */
24 boolean hasResponseId(); 24 boolean hasResponseId();
25 /** 25 /**
26 - * <code>required int32 responseId = 2;</code> 26 + * <code>required int64 responseId = 2;</code>
27 */ 27 */
28 - int getResponseId(); 28 + long getResponseId();
29 29
30 /** 30 /**
31 * <code>required string resMsg = 1;</code> 31 * <code>required string resMsg = 1;</code>
@@ -54,7 +54,7 @@ public final class CIMResponseProto { @@ -54,7 +54,7 @@ public final class CIMResponseProto {
54 super(builder); 54 super(builder);
55 } 55 }
56 private CIMResProtocol() { 56 private CIMResProtocol() {
57 - responseId_ = 0; 57 + responseId_ = 0L;
58 resMsg_ = ""; 58 resMsg_ = "";
59 } 59 }
60 60
@@ -97,7 +97,7 @@ public final class CIMResponseProto { @@ -97,7 +97,7 @@ public final class CIMResponseProto {
97 } 97 }
98 case 16: { 98 case 16: {
99 bitField0_ |= 0x00000001; 99 bitField0_ |= 0x00000001;
100 - responseId_ = input.readInt32(); 100 + responseId_ = input.readInt64();
101 break; 101 break;
102 } 102 }
103 } 103 }
@@ -126,17 +126,17 @@ public final class CIMResponseProto { @@ -126,17 +126,17 @@ public final class CIMResponseProto {
126 126
127 private int bitField0_; 127 private int bitField0_;
128 public static final int RESPONSEID_FIELD_NUMBER = 2; 128 public static final int RESPONSEID_FIELD_NUMBER = 2;
129 - private int responseId_; 129 + private long responseId_;
130 /** 130 /**
131 - * <code>required int32 responseId = 2;</code> 131 + * <code>required int64 responseId = 2;</code>
132 */ 132 */
133 public boolean hasResponseId() { 133 public boolean hasResponseId() {
134 return ((bitField0_ & 0x00000001) == 0x00000001); 134 return ((bitField0_ & 0x00000001) == 0x00000001);
135 } 135 }
136 /** 136 /**
137 - * <code>required int32 responseId = 2;</code> 137 + * <code>required int64 responseId = 2;</code>
138 */ 138 */
139 - public int getResponseId() { 139 + public long getResponseId() {
140 return responseId_; 140 return responseId_;
141 } 141 }
142 142
@@ -206,7 +206,7 @@ public final class CIMResponseProto { @@ -206,7 +206,7 @@ public final class CIMResponseProto {
206 com.google.protobuf.GeneratedMessageV3.writeString(output, 1, resMsg_); 206 com.google.protobuf.GeneratedMessageV3.writeString(output, 1, resMsg_);
207 } 207 }
208 if (((bitField0_ & 0x00000001) == 0x00000001)) { 208 if (((bitField0_ & 0x00000001) == 0x00000001)) {
209 - output.writeInt32(2, responseId_); 209 + output.writeInt64(2, responseId_);
210 } 210 }
211 unknownFields.writeTo(output); 211 unknownFields.writeTo(output);
212 } 212 }
@@ -221,7 +221,7 @@ public final class CIMResponseProto { @@ -221,7 +221,7 @@ public final class CIMResponseProto {
221 } 221 }
222 if (((bitField0_ & 0x00000001) == 0x00000001)) { 222 if (((bitField0_ & 0x00000001) == 0x00000001)) {
223 size += com.google.protobuf.CodedOutputStream 223 size += com.google.protobuf.CodedOutputStream
224 - .computeInt32Size(2, responseId_); 224 + .computeInt64Size(2, responseId_);
225 } 225 }
226 size += unknownFields.getSerializedSize(); 226 size += unknownFields.getSerializedSize();
227 memoizedSize = size; 227 memoizedSize = size;
@@ -262,7 +262,8 @@ public final class CIMResponseProto { @@ -262,7 +262,8 @@ public final class CIMResponseProto {
262 hash = (19 * hash) + getDescriptor().hashCode(); 262 hash = (19 * hash) + getDescriptor().hashCode();
263 if (hasResponseId()) { 263 if (hasResponseId()) {
264 hash = (37 * hash) + RESPONSEID_FIELD_NUMBER; 264 hash = (37 * hash) + RESPONSEID_FIELD_NUMBER;
265 - hash = (53 * hash) + getResponseId(); 265 + hash = (53 * hash) + com.google.protobuf.Internal.hashLong(
  266 + getResponseId());
266 } 267 }
267 if (hasResMsg()) { 268 if (hasResMsg()) {
268 hash = (37 * hash) + RESMSG_FIELD_NUMBER; 269 hash = (37 * hash) + RESMSG_FIELD_NUMBER;
@@ -397,7 +398,7 @@ public final class CIMResponseProto { @@ -397,7 +398,7 @@ public final class CIMResponseProto {
397 } 398 }
398 public Builder clear() { 399 public Builder clear() {
399 super.clear(); 400 super.clear();
400 - responseId_ = 0; 401 + responseId_ = 0L;
401 bitField0_ = (bitField0_ & ~0x00000001); 402 bitField0_ = (bitField0_ & ~0x00000001);
402 resMsg_ = ""; 403 resMsg_ = "";
403 bitField0_ = (bitField0_ & ~0x00000002); 404 bitField0_ = (bitField0_ & ~0x00000002);
@@ -517,34 +518,34 @@ public final class CIMResponseProto { @@ -517,34 +518,34 @@ public final class CIMResponseProto {
517 } 518 }
518 private int bitField0_; 519 private int bitField0_;
519 520
520 - private int responseId_ ; 521 + private long responseId_ ;
521 /** 522 /**
522 - * <code>required int32 responseId = 2;</code> 523 + * <code>required int64 responseId = 2;</code>
523 */ 524 */
524 public boolean hasResponseId() { 525 public boolean hasResponseId() {
525 return ((bitField0_ & 0x00000001) == 0x00000001); 526 return ((bitField0_ & 0x00000001) == 0x00000001);
526 } 527 }
527 /** 528 /**
528 - * <code>required int32 responseId = 2;</code> 529 + * <code>required int64 responseId = 2;</code>
529 */ 530 */
530 - public int getResponseId() { 531 + public long getResponseId() {
531 return responseId_; 532 return responseId_;
532 } 533 }
533 /** 534 /**
534 - * <code>required int32 responseId = 2;</code> 535 + * <code>required int64 responseId = 2;</code>
535 */ 536 */
536 - public Builder setResponseId(int value) { 537 + public Builder setResponseId(long value) {
537 bitField0_ |= 0x00000001; 538 bitField0_ |= 0x00000001;
538 responseId_ = value; 539 responseId_ = value;
539 onChanged(); 540 onChanged();
540 return this; 541 return this;
541 } 542 }
542 /** 543 /**
543 - * <code>required int32 responseId = 2;</code> 544 + * <code>required int64 responseId = 2;</code>
544 */ 545 */
545 public Builder clearResponseId() { 546 public Builder clearResponseId() {
546 bitField0_ = (bitField0_ & ~0x00000001); 547 bitField0_ = (bitField0_ & ~0x00000001);
547 - responseId_ = 0; 548 + responseId_ = 0L;
548 onChanged(); 549 onChanged();
549 return this; 550 return this;
550 } 551 }
@@ -688,7 +689,7 @@ public final class CIMResponseProto { @@ -688,7 +689,7 @@ public final class CIMResponseProto {
688 static { 689 static {
689 String[] descriptorData = { 690 String[] descriptorData = {
690 "\n\027BaseResponseProto.proto\022\010protocol\"4\n\016C" + 691 "\n\027BaseResponseProto.proto\022\010protocol\"4\n\016C" +
691 - "IMResProtocol\022\022\n\nresponseId\030\002 \002(\005\022\016\n\006res" + 692 + "IMResProtocol\022\022\n\nresponseId\030\002 \002(\003\022\016\n\006res" +
692 "Msg\030\001 \002(\tB8\n$com.crossoverjie.cim.common" + 693 "Msg\030\001 \002(\tB8\n$com.crossoverjie.cim.common" +
693 ".protocolB\020CIMResponseProto" 694 ".protocolB\020CIMResponseProto"
694 }; 695 };
@@ -13,7 +13,7 @@ public class ProtocolUtil { @@ -13,7 +13,7 @@ public class ProtocolUtil {
13 13
14 public static void main(String[] args) throws InvalidProtocolBufferException { 14 public static void main(String[] args) throws InvalidProtocolBufferException {
15 CIMRequestProto.CIMReqProtocol protocol = CIMRequestProto.CIMReqProtocol.newBuilder() 15 CIMRequestProto.CIMReqProtocol protocol = CIMRequestProto.CIMReqProtocol.newBuilder()
16 - .setRequestId(123) 16 + .setRequestId(123L)
17 .setReqMsg("你好啊") 17 .setReqMsg("你好啊")
18 .build(); 18 .build();
19 19
@@ -35,6 +35,11 @@ @@ -35,6 +35,11 @@
35 </exclusions> 35 </exclusions>
36 </dependency> 36 </dependency>
37 37
  38 + <dependency>
  39 + <groupId>org.springframework.boot</groupId>
  40 + <artifactId>spring-boot-starter-data-redis</artifactId>
  41 + </dependency>
  42 +
38 43
39 <dependency> 44 <dependency>
40 <groupId>io.springfox</groupId> 45 <groupId>io.springfox</groupId>
@@ -7,6 +7,10 @@ import org.I0Itec.zkclient.ZkClient; @@ -7,6 +7,10 @@ import org.I0Itec.zkclient.ZkClient;
7 import org.springframework.beans.factory.annotation.Autowired; 7 import org.springframework.beans.factory.annotation.Autowired;
8 import org.springframework.context.annotation.Bean; 8 import org.springframework.context.annotation.Bean;
9 import org.springframework.context.annotation.Configuration; 9 import org.springframework.context.annotation.Configuration;
  10 +import org.springframework.data.redis.connection.RedisConnectionFactory;
  11 +import org.springframework.data.redis.core.RedisTemplate;
  12 +import org.springframework.data.redis.core.StringRedisTemplate;
  13 +import org.springframework.data.redis.serializer.StringRedisSerializer;
10 14
11 /** 15 /**
12 * Function: 16 * Function:
@@ -36,4 +40,19 @@ public class BeanConfig { @@ -36,4 +40,19 @@ public class BeanConfig {
36 } 40 }
37 }); 41 });
38 } 42 }
  43 +
  44 +
  45 + /**
  46 + * Redis bean
  47 + * @param factory
  48 + * @return
  49 + */
  50 + @Bean
  51 + public RedisTemplate<String, String> redisTemplate(RedisConnectionFactory factory) {
  52 + StringRedisTemplate redisTemplate = new StringRedisTemplate(factory);
  53 + redisTemplate.setKeySerializer(new StringRedisSerializer());
  54 + redisTemplate.setValueSerializer(new StringRedisSerializer());
  55 + redisTemplate.afterPropertiesSet();
  56 + return redisTemplate;
  57 + }
39 } 58 }
  1 +package com.crossoverjie.cim.route.constant;
  2 +
  3 +/**
  4 + * Function:
  5 + *
  6 + * @author crossoverJie
  7 + * Date: 2018/9/10 14:07
  8 + * @since JDK 1.8
  9 + */
  10 +public final class Constant {
  11 +
  12 +
  13 + /**
  14 + * 账号前缀
  15 + */
  16 + public final static String ACCOUNT_PREFIX = "cim-account:";
  17 +
  18 +
  19 +}
@@ -4,9 +4,13 @@ import com.crossoverjie.cim.common.enums.StatusEnum; @@ -4,9 +4,13 @@ import com.crossoverjie.cim.common.enums.StatusEnum;
4 import com.crossoverjie.cim.common.res.BaseResponse; 4 import com.crossoverjie.cim.common.res.BaseResponse;
5 import com.crossoverjie.cim.common.res.NULLBody; 5 import com.crossoverjie.cim.common.res.NULLBody;
6 import com.crossoverjie.cim.route.cache.ServerCache; 6 import com.crossoverjie.cim.route.cache.ServerCache;
7 -import com.crossoverjie.cim.route.vo.req.GroupRequest;  
8 -import com.crossoverjie.cim.route.vo.req.P2PRequest; 7 +import com.crossoverjie.cim.route.service.AccountService;
  8 +import com.crossoverjie.cim.route.vo.req.GroupReqVO;
  9 +import com.crossoverjie.cim.route.vo.req.LoginReqVO;
  10 +import com.crossoverjie.cim.route.vo.req.P2PReqVO;
  11 +import com.crossoverjie.cim.route.vo.req.RegisterInfoReqVO;
9 import com.crossoverjie.cim.route.vo.res.CIMServerResVO; 12 import com.crossoverjie.cim.route.vo.res.CIMServerResVO;
  13 +import com.crossoverjie.cim.route.vo.res.RegisterInfoResVO;
10 import io.swagger.annotations.ApiOperation; 14 import io.swagger.annotations.ApiOperation;
11 import org.slf4j.Logger; 15 import org.slf4j.Logger;
12 import org.slf4j.LoggerFactory; 16 import org.slf4j.LoggerFactory;
@@ -32,13 +36,16 @@ public class RouteController { @@ -32,13 +36,16 @@ public class RouteController {
32 @Autowired 36 @Autowired
33 private ServerCache serverCache ; 37 private ServerCache serverCache ;
34 38
  39 + @Autowired
  40 + private AccountService accountService;
  41 +
35 @ApiOperation("群聊 API") 42 @ApiOperation("群聊 API")
36 @RequestMapping(value = "groupRoute",method = RequestMethod.POST) 43 @RequestMapping(value = "groupRoute",method = RequestMethod.POST)
37 @ResponseBody() 44 @ResponseBody()
38 - public BaseResponse<NULLBody> groupRoute(@RequestBody GroupRequest groupRequest){ 45 + public BaseResponse<NULLBody> groupRoute(@RequestBody GroupReqVO groupReqVO){
39 BaseResponse<NULLBody> res = new BaseResponse(); 46 BaseResponse<NULLBody> res = new BaseResponse();
40 47
41 - LOGGER.info("msg=[{}]",groupRequest.toString()); 48 + LOGGER.info("msg=[{}]", groupReqVO.toString());
42 49
43 res.setCode(StatusEnum.SUCCESS.getCode()) ; 50 res.setCode(StatusEnum.SUCCESS.getCode()) ;
44 res.setMessage(StatusEnum.SUCCESS.getMessage()) ; 51 res.setMessage(StatusEnum.SUCCESS.getMessage()) ;
@@ -54,7 +61,7 @@ public class RouteController { @@ -54,7 +61,7 @@ public class RouteController {
54 @ApiOperation("私聊 API") 61 @ApiOperation("私聊 API")
55 @RequestMapping(value = "p2pRoute",method = RequestMethod.POST) 62 @RequestMapping(value = "p2pRoute",method = RequestMethod.POST)
56 @ResponseBody() 63 @ResponseBody()
57 - public BaseResponse<NULLBody> p2pRoute(@RequestBody P2PRequest p2pRequest){ 64 + public BaseResponse<NULLBody> p2pRoute(@RequestBody P2PReqVO p2pRequest){
58 BaseResponse<NULLBody> res = new BaseResponse(); 65 BaseResponse<NULLBody> res = new BaseResponse();
59 66
60 res.setCode(StatusEnum.SUCCESS.getCode()) ; 67 res.setCode(StatusEnum.SUCCESS.getCode()) ;
@@ -66,12 +73,15 @@ public class RouteController { @@ -66,12 +73,15 @@ public class RouteController {
66 * 获取一台 CIM server 73 * 获取一台 CIM server
67 * @return 74 * @return
68 */ 75 */
69 - @ApiOperation("获取服务器")  
70 - @RequestMapping(value = "getCIMServer",method = RequestMethod.POST) 76 + @ApiOperation("登录并获取服务器")
  77 + @RequestMapping(value = "login",method = RequestMethod.POST)
71 @ResponseBody() 78 @ResponseBody()
72 - public BaseResponse<CIMServerResVO> getCIMServer(){ 79 + public BaseResponse<CIMServerResVO> login(@RequestBody LoginReqVO loginReqVO) throws Exception {
73 BaseResponse<CIMServerResVO> res = new BaseResponse(); 80 BaseResponse<CIMServerResVO> res = new BaseResponse();
74 81
  82 + //登录校验
  83 + boolean login = accountService.login(loginReqVO);
  84 + if (login){
75 String server = serverCache.selectServer(); 85 String server = serverCache.selectServer();
76 String[] serverInfo = server.split(":"); 86 String[] serverInfo = server.split(":");
77 CIMServerResVO vo = new CIMServerResVO(serverInfo[0],Integer.parseInt(serverInfo[1])) ; 87 CIMServerResVO vo = new CIMServerResVO(serverInfo[0],Integer.parseInt(serverInfo[1])) ;
@@ -79,6 +89,31 @@ public class RouteController { @@ -79,6 +89,31 @@ public class RouteController {
79 res.setDataBody(vo); 89 res.setDataBody(vo);
80 res.setCode(StatusEnum.SUCCESS.getCode()) ; 90 res.setCode(StatusEnum.SUCCESS.getCode()) ;
81 res.setMessage(StatusEnum.SUCCESS.getMessage()) ; 91 res.setMessage(StatusEnum.SUCCESS.getMessage()) ;
  92 + }else {
  93 + res.setCode(StatusEnum.FAIL.getCode()) ;
  94 + res.setMessage(StatusEnum.FAIL.getMessage()) ;
  95 + }
  96 +
  97 + return res ;
  98 + }
  99 +
  100 + /**
  101 + * 注册账号
  102 + * @return
  103 + */
  104 + @ApiOperation("注册账号")
  105 + @RequestMapping(value = "registerAccount",method = RequestMethod.POST)
  106 + @ResponseBody()
  107 + public BaseResponse<RegisterInfoResVO> registerAccount(@RequestBody RegisterInfoReqVO registerInfoReqVO) throws Exception {
  108 + BaseResponse<RegisterInfoResVO> res = new BaseResponse();
  109 +
  110 + long userId = System.currentTimeMillis();
  111 + RegisterInfoResVO info = new RegisterInfoResVO(userId,registerInfoReqVO.getUserName()) ;
  112 + info = accountService.registerAccount(info);
  113 +
  114 + res.setDataBody(info);
  115 + res.setCode(StatusEnum.SUCCESS.getCode()) ;
  116 + res.setMessage(StatusEnum.SUCCESS.getMessage()) ;
82 return res ; 117 return res ;
83 } 118 }
84 119
  1 +package com.crossoverjie.cim.route.service;
  2 +
  3 +import com.crossoverjie.cim.route.vo.req.LoginReqVO;
  4 +import com.crossoverjie.cim.route.vo.res.RegisterInfoResVO;
  5 +
  6 +/**
  7 + * Function: 账户服务
  8 + *
  9 + * @author crossoverJie
  10 + * Date: 2018/12/23 21:57
  11 + * @since JDK 1.8
  12 + */
  13 +public interface AccountService {
  14 +
  15 + /**
  16 + * 注册用户
  17 + * @param info 用户信息
  18 + * @return
  19 + * @throws Exception
  20 + */
  21 + RegisterInfoResVO registerAccount(RegisterInfoResVO info) throws Exception;
  22 +
  23 + /**
  24 + * 登录服务
  25 + * @param loginReqVO 登录信息
  26 + * @return true 成功 false 失败
  27 + * @throws Exception
  28 + */
  29 + boolean login(LoginReqVO loginReqVO) throws Exception ;
  30 +}
  1 +package com.crossoverjie.cim.route.service.impl;
  2 +
  3 +import com.crossoverjie.cim.route.service.AccountService;
  4 +import com.crossoverjie.cim.route.vo.req.LoginReqVO;
  5 +import com.crossoverjie.cim.route.vo.res.RegisterInfoResVO;
  6 +import org.slf4j.Logger;
  7 +import org.slf4j.LoggerFactory;
  8 +import org.springframework.beans.factory.annotation.Autowired;
  9 +import org.springframework.data.redis.core.RedisTemplate;
  10 +import org.springframework.stereotype.Service;
  11 +
  12 +import static com.crossoverjie.cim.route.constant.Constant.ACCOUNT_PREFIX;
  13 +
  14 +/**
  15 + * Function:
  16 + *
  17 + * @author crossoverJie
  18 + * Date: 2018/12/23 21:58
  19 + * @since JDK 1.8
  20 + */
  21 +@Service
  22 +public class AccountServiceRedisImpl implements AccountService {
  23 + private final static Logger LOGGER = LoggerFactory.getLogger(AccountServiceRedisImpl.class);
  24 +
  25 + @Autowired
  26 + private RedisTemplate<String, String> redisTemplate;
  27 +
  28 + @Override
  29 + public RegisterInfoResVO registerAccount(RegisterInfoResVO info) {
  30 + String key = ACCOUNT_PREFIX + info.getUserId();
  31 +
  32 + String name = redisTemplate.opsForValue().get(info.getUserName()) ;
  33 + if (null == name){
  34 + //为了方便查询,冗余存一份
  35 + redisTemplate.opsForValue().set(key, info.getUserName());
  36 + redisTemplate.opsForValue().set(info.getUserName(),key);
  37 + }else {
  38 + long userId = Long.parseLong(name.split(":")[1]);
  39 + info.setUserId(userId);
  40 + info.setUserName(info.getUserName());
  41 + }
  42 +
  43 + return info ;
  44 + }
  45 +
  46 + @Override
  47 + public boolean login(LoginReqVO loginReqVO) throws Exception {
  48 + String key = ACCOUNT_PREFIX + loginReqVO.getUserId();
  49 + String userName = redisTemplate.opsForValue().get(key);
  50 + if (null == userName){
  51 + return false ;
  52 + }
  53 +
  54 + if (!userName.equals(loginReqVO.getUserName())){
  55 + return false ;
  56 + }
  57 + return true ;
  58 + }
  59 +}
@@ -12,7 +12,7 @@ import javax.validation.constraints.NotNull; @@ -12,7 +12,7 @@ import javax.validation.constraints.NotNull;
12 * Date: 2018/05/21 15:56 12 * Date: 2018/05/21 15:56
13 * @since JDK 1.8 13 * @since JDK 1.8
14 */ 14 */
15 -public class GroupRequest extends BaseRequest { 15 +public class GroupReqVO extends BaseRequest {
16 16
17 @NotNull(message = "msg 不能为空") 17 @NotNull(message = "msg 不能为空")
18 @ApiModelProperty(required = true, value = "msg", example = "hello") 18 @ApiModelProperty(required = true, value = "msg", example = "hello")
  1 +package com.crossoverjie.cim.route.vo.req;
  2 +
  3 +import com.crossoverjie.cim.common.req.BaseRequest;
  4 +
  5 +/**
  6 + * Function:
  7 + *
  8 + * @author crossoverJie
  9 + * Date: 2018/12/23 22:30
  10 + * @since JDK 1.8
  11 + */
  12 +public class LoginReqVO extends BaseRequest{
  13 + private Long userId ;
  14 + private String userName ;
  15 +
  16 + public Long getUserId() {
  17 + return userId;
  18 + }
  19 +
  20 + public void setUserId(Long userId) {
  21 + this.userId = userId;
  22 + }
  23 +
  24 + public String getUserName() {
  25 + return userName;
  26 + }
  27 +
  28 + public void setUserName(String userName) {
  29 + this.userName = userName;
  30 + }
  31 +
  32 + @Override
  33 + public String toString() {
  34 + return "LoginReqVO{" +
  35 + "userId=" + userId +
  36 + ", userName='" + userName + '\'' +
  37 + "} " + super.toString();
  38 + }
  39 +}
@@ -12,7 +12,7 @@ import javax.validation.constraints.NotNull; @@ -12,7 +12,7 @@ import javax.validation.constraints.NotNull;
12 * Date: 2018/05/21 15:56 12 * Date: 2018/05/21 15:56
13 * @since JDK 1.8 13 * @since JDK 1.8
14 */ 14 */
15 -public class P2PRequest extends BaseRequest { 15 +public class P2PReqVO extends BaseRequest {
16 16
17 @NotNull(message = "msg 不能为空") 17 @NotNull(message = "msg 不能为空")
18 @ApiModelProperty(required = true, value = "msg", example = "hello") 18 @ApiModelProperty(required = true, value = "msg", example = "hello")
  1 +package com.crossoverjie.cim.route.vo.req;
  2 +
  3 +import com.crossoverjie.cim.common.req.BaseRequest;
  4 +import io.swagger.annotations.ApiModelProperty;
  5 +
  6 +import javax.validation.constraints.NotNull;
  7 +
  8 +/**
  9 + * Function:
  10 + *
  11 + * @author crossoverJie
  12 + * Date: 2018/12/23 22:04
  13 + * @since JDK 1.8
  14 + */
  15 +public class RegisterInfoReqVO extends BaseRequest {
  16 +
  17 + @NotNull(message = "用户名不能为空")
  18 + @ApiModelProperty(required = true, value = "userName", example = "zhangsan")
  19 + private String userName ;
  20 +
  21 + public String getUserName() {
  22 + return userName;
  23 + }
  24 +
  25 + public void setUserName(String userName) {
  26 + this.userName = userName;
  27 + }
  28 +
  29 + @Override
  30 + public String toString() {
  31 + return "RegisterInfoReqVO{" +
  32 + "userName='" + userName + '\'' +
  33 + "} " + super.toString();
  34 + }
  35 +}
  1 +package com.crossoverjie.cim.route.vo.res;
  2 +
  3 +import java.io.Serializable;
  4 +
  5 +/**
  6 + * Function:
  7 + *
  8 + * @author crossoverJie
  9 + * Date: 2018/12/23 21:54
  10 + * @since JDK 1.8
  11 + */
  12 +public class RegisterInfoResVO implements Serializable{
  13 + private Long userId ;
  14 + private String userName ;
  15 +
  16 + public RegisterInfoResVO(Long userId, String userName) {
  17 + this.userId = userId;
  18 + this.userName = userName;
  19 + }
  20 +
  21 + public Long getUserId() {
  22 + return userId;
  23 + }
  24 +
  25 + public void setUserId(Long userId) {
  26 + this.userId = userId;
  27 + }
  28 +
  29 + public String getUserName() {
  30 + return userName;
  31 + }
  32 +
  33 + public void setUserName(String userName) {
  34 + this.userName = userName;
  35 + }
  36 +
  37 + @Override
  38 + public String toString() {
  39 + return "RegisterInfo{" +
  40 + "userId=" + userId +
  41 + ", userName='" + userName + '\'' +
  42 + '}';
  43 + }
  44 +}
@@ -18,3 +18,13 @@ app.zk.addr=47.98.194.60:2181 @@ -18,3 +18,13 @@ app.zk.addr=47.98.194.60:2181
18 # zk 注册根节点 18 # zk 注册根节点
19 app.zk.root=/route 19 app.zk.root=/route
20 20
  21 +
  22 +
  23 +# Redis 配置
  24 +spring.redis.host=47.98.194.60
  25 +spring.redis.port=6379
  26 +spring.redis.pool.max-active=100
  27 +spring.redis.pool.max-idle=100
  28 +spring.redis.pool.max-wait=1000
  29 +spring.redis.pool.min-idle=10
  30 +
  1 +import com.crossoverjie.cim.route.RouteApplication;
  2 +import org.junit.Test;
  3 +import org.junit.runner.RunWith;
  4 +import org.springframework.beans.factory.annotation.Autowired;
  5 +import org.springframework.boot.test.context.SpringBootTest;
  6 +import org.springframework.data.redis.core.RedisTemplate;
  7 +import org.springframework.test.context.junit4.SpringRunner;
  8 +
  9 +/**
  10 + * Function:
  11 + *
  12 + * @author crossoverJie
  13 + * Date: 2018/12/23 21:40
  14 + * @since JDK 1.8
  15 + */
  16 +@SpringBootTest(classes = RouteApplication.class)
  17 +@RunWith(SpringRunner.class)
  18 +public class RedisTest {
  19 +
  20 + @Autowired
  21 + private RedisTemplate<String,String> redisTemplate ;
  22 +
  23 + @Test
  24 + public void test(){
  25 + redisTemplate.opsForValue().set("test","test") ;
  26 + String test = redisTemplate.opsForValue().get("test");
  27 + System.out.println("====" + test);
  28 + }
  29 +}
1 package com.crossoverjie.cim.server.endpoint; 1 package com.crossoverjie.cim.server.endpoint;
2 2
3 -import com.crossoverjie.cim.server.util.NettySocketHolder; 3 +import com.crossoverjie.cim.server.util.SessionSocketHolder;
4 import io.netty.channel.socket.nio.NioSocketChannel; 4 import io.netty.channel.socket.nio.NioSocketChannel;
5 import org.springframework.boot.actuate.endpoint.AbstractEndpoint; 5 import org.springframework.boot.actuate.endpoint.AbstractEndpoint;
6 6
@@ -27,6 +27,6 @@ public class CustomEndpoint extends AbstractEndpoint<Map<Long,NioSocketChannel>> @@ -27,6 +27,6 @@ public class CustomEndpoint extends AbstractEndpoint<Map<Long,NioSocketChannel>>
27 27
28 @Override 28 @Override
29 public Map<Long, NioSocketChannel> invoke() { 29 public Map<Long, NioSocketChannel> invoke() {
30 - return NettySocketHolder.getMAP(); 30 + return SessionSocketHolder.getMAP();
31 } 31 }
32 } 32 }
1 package com.crossoverjie.cim.server.handle; 1 package com.crossoverjie.cim.server.handle;
2 2
  3 +import com.crossoverjie.cim.common.constant.Constants;
3 import com.crossoverjie.cim.common.protocol.CIMRequestProto; 4 import com.crossoverjie.cim.common.protocol.CIMRequestProto;
4 -import com.crossoverjie.cim.common.protocol.CIMResponseProto;  
5 -import com.crossoverjie.cim.server.util.NettySocketHolder; 5 +import com.crossoverjie.cim.server.util.SessionSocketHolder;
6 import io.netty.channel.ChannelHandler; 6 import io.netty.channel.ChannelHandler;
7 import io.netty.channel.ChannelHandlerContext; 7 import io.netty.channel.ChannelHandlerContext;
8 import io.netty.channel.SimpleChannelInboundHandler; 8 import io.netty.channel.SimpleChannelInboundHandler;
@@ -30,28 +30,22 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto @@ -30,28 +30,22 @@ public class CIMServerHandle extends SimpleChannelInboundHandler<CIMRequestProto
30 */ 30 */
31 @Override 31 @Override
32 public void channelInactive(ChannelHandlerContext ctx) throws Exception { 32 public void channelInactive(ChannelHandlerContext ctx) throws Exception {
33 - LOGGER.info("客户端断开");  
34 - NettySocketHolder.remove((NioSocketChannel) ctx.channel()); 33 + String userName = SessionSocketHolder.getUserName((NioSocketChannel) ctx.channel());
  34 + LOGGER.info("用户[{}]断开",userName);
  35 + SessionSocketHolder.remove((NioSocketChannel) ctx.channel());
35 } 36 }
36 37
37 - @Override  
38 - public void channelActive(ChannelHandlerContext ctx) throws Exception {  
39 - LOGGER.info("有客户端连上来了。。");  
40 - }  
41 38
42 @Override 39 @Override
43 protected void channelRead0(ChannelHandlerContext ctx, CIMRequestProto.CIMReqProtocol msg) throws Exception { 40 protected void channelRead0(ChannelHandlerContext ctx, CIMRequestProto.CIMReqProtocol msg) throws Exception {
44 - LOGGER.info("收到msg={}", msg.getReqMsg());  
45 -  
46 - if (999 == msg.getRequestId()){  
47 - CIMResponseProto.CIMResProtocol responseProtocol = CIMResponseProto.CIMResProtocol.newBuilder()  
48 - .setResponseId(1000)  
49 - .setResMsg("服务端响应")  
50 - .build();  
51 - ctx.writeAndFlush(responseProtocol) ;  
52 - } 41 + LOGGER.info("收到msg={}", msg.toString());
53 42
  43 + if (msg.getType() == Constants.CommandType.LOGIN){
54 //保存客户端与 Channel 之间的关系 44 //保存客户端与 Channel 之间的关系
55 - NettySocketHolder.put((long) msg.getRequestId(),(NioSocketChannel)ctx.channel()) ; 45 + SessionSocketHolder.put(msg.getRequestId(),(NioSocketChannel)ctx.channel()) ;
  46 + SessionSocketHolder.saveSession(msg.getRequestId(),msg.getReqMsg());
  47 + LOGGER.info("客户端[{}]注册成功",msg.getReqMsg());
  48 + }
  49 +
56 } 50 }
57 } 51 }
@@ -4,7 +4,7 @@ import com.alibaba.fastjson.JSON; @@ -4,7 +4,7 @@ import com.alibaba.fastjson.JSON;
4 import com.crossoverjie.cim.common.pojo.CustomProtocol; 4 import com.crossoverjie.cim.common.pojo.CustomProtocol;
5 import com.crossoverjie.cim.common.protocol.CIMRequestProto; 5 import com.crossoverjie.cim.common.protocol.CIMRequestProto;
6 import com.crossoverjie.cim.server.init.CIMServerInitializer; 6 import com.crossoverjie.cim.server.init.CIMServerInitializer;
7 -import com.crossoverjie.cim.server.util.NettySocketHolder; 7 +import com.crossoverjie.cim.server.util.SessionSocketHolder;
8 import com.crossoverjie.cim.server.vo.req.SendMsgReqVO; 8 import com.crossoverjie.cim.server.vo.req.SendMsgReqVO;
9 import io.netty.bootstrap.ServerBootstrap; 9 import io.netty.bootstrap.ServerBootstrap;
10 import io.netty.buffer.Unpooled; 10 import io.netty.buffer.Unpooled;
@@ -86,7 +86,7 @@ public class CIMServer { @@ -86,7 +86,7 @@ public class CIMServer {
86 * @param customProtocol 86 * @param customProtocol
87 */ 87 */
88 public void sendMsg(CustomProtocol customProtocol) { 88 public void sendMsg(CustomProtocol customProtocol) {
89 - NioSocketChannel socketChannel = NettySocketHolder.get(customProtocol.getId()); 89 + NioSocketChannel socketChannel = SessionSocketHolder.get(customProtocol.getId());
90 90
91 if (null == socketChannel) { 91 if (null == socketChannel) {
92 throw new NullPointerException("没有[" + customProtocol.getId() + "]的socketChannel"); 92 throw new NullPointerException("没有[" + customProtocol.getId() + "]的socketChannel");
@@ -102,7 +102,7 @@ public class CIMServer { @@ -102,7 +102,7 @@ public class CIMServer {
102 * @param sendMsgReqVO 消息 102 * @param sendMsgReqVO 消息
103 */ 103 */
104 public void sendGoogleProtoMsg(SendMsgReqVO sendMsgReqVO){ 104 public void sendGoogleProtoMsg(SendMsgReqVO sendMsgReqVO){
105 - NioSocketChannel socketChannel = NettySocketHolder.get(sendMsgReqVO.getId()); 105 + NioSocketChannel socketChannel = SessionSocketHolder.get(sendMsgReqVO.getId());
106 106
107 if (null == socketChannel) { 107 if (null == socketChannel) {
108 throw new NullPointerException("没有[" + sendMsgReqVO.getId() + "]的socketChannel"); 108 throw new NullPointerException("没有[" + sendMsgReqVO.getId() + "]的socketChannel");
1 package com.crossoverjie.cim.server.util; 1 package com.crossoverjie.cim.server.util;
2 2
3 -import io.netty.channel.socket.SocketChannel;  
4 -import io.netty.channel.socket.nio.NioServerSocketChannel;  
5 import io.netty.channel.socket.nio.NioSocketChannel; 3 import io.netty.channel.socket.nio.NioSocketChannel;
6 4
7 import java.util.Map; 5 import java.util.Map;
@@ -14,22 +12,46 @@ import java.util.concurrent.ConcurrentHashMap; @@ -14,22 +12,46 @@ import java.util.concurrent.ConcurrentHashMap;
14 * Date: 22/05/2018 18:33 12 * Date: 22/05/2018 18:33
15 * @since JDK 1.8 13 * @since JDK 1.8
16 */ 14 */
17 -public class NettySocketHolder {  
18 - private static final Map<Long, NioSocketChannel> MAP = new ConcurrentHashMap<>(16); 15 +public class SessionSocketHolder {
  16 + private static final Map<Long, NioSocketChannel> CHANNEL_MAP = new ConcurrentHashMap<>(16);
  17 + private static final Map<Long, String> SESSION_MAP = new ConcurrentHashMap<>(16);
  18 +
  19 + public static void saveSession(Long userId,String userName){
  20 + SESSION_MAP.put(userId, userName);
  21 + }
19 22
20 public static void put(Long id, NioSocketChannel socketChannel) { 23 public static void put(Long id, NioSocketChannel socketChannel) {
21 - MAP.put(id, socketChannel); 24 + CHANNEL_MAP.put(id, socketChannel);
22 } 25 }
23 26
24 public static NioSocketChannel get(Long id) { 27 public static NioSocketChannel get(Long id) {
25 - return MAP.get(id); 28 + return CHANNEL_MAP.get(id);
26 } 29 }
27 30
28 public static Map<Long, NioSocketChannel> getMAP() { 31 public static Map<Long, NioSocketChannel> getMAP() {
29 - return MAP; 32 + return CHANNEL_MAP;
30 } 33 }
31 34
32 public static void remove(NioSocketChannel nioSocketChannel) { 35 public static void remove(NioSocketChannel nioSocketChannel) {
33 - MAP.entrySet().stream().filter(entry -> entry.getValue() == nioSocketChannel).forEach(entry -> MAP.remove(entry.getKey())); 36 + CHANNEL_MAP.entrySet().stream().filter(entry -> entry.getValue() == nioSocketChannel).forEach(entry -> CHANNEL_MAP.remove(entry.getKey()));
  37 + }
  38 +
  39 + /**
  40 + * 获取注册用户信息
  41 + * @param nioSocketChannel
  42 + * @return
  43 + */
  44 + public static String getUserName(NioSocketChannel nioSocketChannel){
  45 + for (Map.Entry<Long, NioSocketChannel> entry : CHANNEL_MAP.entrySet()) {
  46 + NioSocketChannel value = entry.getValue();
  47 + if (nioSocketChannel == value){
  48 + Long key = entry.getKey();
  49 + String userName = SESSION_MAP.get(key);
  50 + return userName ;
34 } 51 }
  52 + }
  53 +
  54 + return null;
  55 + }
  56 +
35 } 57 }
@@ -11,7 +11,7 @@ @@ -11,7 +11,7 @@
11 11
12 12
13 <properties> 13 <properties>
14 - <junit.version>4.11</junit.version> 14 + <junit.version>4.12</junit.version>
15 <netty.version>4.1.21.Final</netty.version> 15 <netty.version>4.1.21.Final</netty.version>
16 <logback.version>1.0.13</logback.version> 16 <logback.version>1.0.13</logback.version>
17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 17 <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>