Commit 78b7841a by giaogiao

优化代码

parent 90722cb6
......@@ -68,12 +68,15 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
}
private void execute(ChannelHandlerContext ctx, String data) {
// Long userIdByChannel = appUserChannelsService.getUserIdByChannel(ctx);
//
// log.info("appWS收到" + userIdByChannel + ":" + data + ",channelId:" + ctx.channel().id().asLongText());
log.info("WS收到:" + data);
String appKey = ctx.channel().attr(MangerChannelService.APP_KEY).get();
String clientId = ctx.channel().attr(MangerChannelService.CLIENT_ID).get();
try {
readWsData.convertModel(data, ctx, appKey, clientId);
} catch (Exception e) {
log.error("系统繁忙data:" + data + ",appKey:" + appKey + ",clientId:" + clientId +
",channelId:" + ctx.channel().id().asShortText(), e);
}
readWsData.convertModel(data, ctx);
}
......@@ -87,13 +90,13 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
// @Override
// public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// String userIdByChannel = mangerChannelService.getInfoByChannel(ctx);
// log.info("uid:" + userIdByChannel + ",ws异常,channelId:" + ctx.channel().id().asLongText(), cause);
// log.info("uid:" + userIdByChannel + ",ws异常,channelId:" + ctx.channel().id().asShortText(), cause);
// }
@Override
public void handlerAdded(ChannelHandlerContext ctx) {
String userIdByChannel = mangerChannelService.getInfoByChannel(ctx);
log.info("连接WS成功handlerAdded,uid:" + userIdByChannel + "," + ",channelId:" + ctx.channel().id().asLongText());
log.info("连接WS成功handlerAdded,uid:" + userIdByChannel + "," + ",channelId:" + ctx.channel().id().asShortText());
}
......@@ -106,7 +109,7 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
// @Override
// public void channelInactive(ChannelHandlerContext ctx) {
// String userIdByChannel = mangerChannelService.getInfoByChannel(ctx);
// log.info("uid:" + userIdByChannel + "," + "channelInactive" + ",channelId:" + ctx.channel().id().asLongText());
// log.info("uid:" + userIdByChannel + "," + "channelInactive" + ",channelId:" + ctx.channel().id().asShortText());
// }
/**
......@@ -115,7 +118,7 @@ public class WsReadHandler extends SimpleChannelInboundHandler<TextWebSocketFram
@Override
public void handlerRemoved(ChannelHandlerContext ctx) {
String userIdByChannel = mangerChannelService.getInfoByChannel(ctx);
log.info("uid:" + userIdByChannel + "," + "handlerRemoved" + ",channelId:" + ctx.channel().id().asLongText());
log.info("uid:" + userIdByChannel + "," + "handlerRemoved" + ",channelId:" + ctx.channel().id().asShortText());
// 关掉连接
ctx.close();
}
......
package com.wecloud.im.ws.receive;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.json.JsonMapper;
import com.wecloud.im.ws.enums.WsRequestCmdEnum;
import com.wecloud.im.ws.model.request.ReceiveModel;
import com.wecloud.im.ws.service.MangerChannelService;
import com.wecloud.im.ws.service.WriteDataService;
import com.wecloud.im.ws.strategy.AbstractReceiveStrategy;
import com.wecloud.im.ws.strategy.ReceiveStrategyContext;
import io.geekidea.springbootplus.framework.common.exception.BusinessException;
import io.netty.channel.ChannelHandlerContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
......@@ -29,6 +28,8 @@ public class ReadWsData {
@Resource
private ReceiveStrategyContext receiveStrategyContext;
private static JsonMapper jsonMapper = new JsonMapper();
@Resource
private WriteDataService writeDataService;
......@@ -40,42 +41,25 @@ public class ReadWsData {
* @param data
* @throws Exception
*/
public void convertModel(String data, ChannelHandlerContext ctx) {
String appKey = ctx.channel().attr(MangerChannelService.APP_KEY).get();
String clientId = ctx.channel().attr(MangerChannelService.CLIENT_ID).get();
public void convertModel(String data, ChannelHandlerContext ctx, String appKey, String clientId) throws Exception {
log.info("appWS收到data:" + data + "\nappKey+clientId:" + appKey + ":" + clientId +
",channelId:" + ctx.channel().id().asShortText());
if (PING.equals(data)) {
log.info("收到心跳" + clientId);
log.info("收到心跳clientId:" + clientId);
return;
}
String language = ctx.channel().attr(MangerChannelService.LANGUAGE).get();
// ReceiveModel requestModel = JSON.parseObject(data, ReceiveModel.class);
JsonMapper jsonMapper = new JsonMapper();
ReceiveModel receiveModel = null;
try {
receiveModel = jsonMapper.readValue(data, ReceiveModel.class);
} catch (JsonProcessingException e) {
e.printStackTrace();
}
// 解析json
ReceiveModel receiveModel = jsonMapper.readValue(data, ReceiveModel.class);
if (null == receiveModel || null == receiveModel.getCmd()) {
return;
throw new BusinessException("null == receiveModel || null == receiveModel.getCmd()");
}
try {
WsRequestCmdEnum wsRequestUriPathEnum = WsRequestCmdEnum.getByCode(receiveModel.getCmd());
// 使用策略模式, 根据不同类型请求调用不同实现类
AbstractReceiveStrategy receiveStrategy = receiveStrategyContext.getStrategy(wsRequestUriPathEnum);
receiveStrategy.process(receiveModel, language, ctx, data);
} catch (Exception e) {
log.error("系统繁忙:" + data + ",appKey:" + appKey + ",clientId:" + clientId, e);
// writeDataService.nullDataSuccess(requestModel, ResultStatus.SYS_BUSY, userId, language);
}
WsRequestCmdEnum wsRequestUriPathEnum = WsRequestCmdEnum.getByCode(receiveModel.getCmd());
// 使用策略模式, 根据不同类型请求调用不同实现类
AbstractReceiveStrategy receiveStrategy = receiveStrategyContext.getStrategy(wsRequestUriPathEnum);
receiveStrategy.process(receiveModel, ctx, data, appKey, clientId);
}
......
......@@ -17,8 +17,7 @@ public abstract class AbstractReceiveStrategy {
* 处理业务流程
*
* @param requestModel
* @param language
* @throws Exception
*/
abstract public void process(ReceiveModel requestModel, String language, ChannelHandlerContext ctx, String data) throws JsonProcessingException;
abstract public void process(ReceiveModel requestModel, ChannelHandlerContext ctx, String data, String appKey, String clientId) throws JsonProcessingException;
}
......@@ -20,7 +20,6 @@ import com.wecloud.im.ws.enums.WsRequestCmdEnum;
import com.wecloud.im.ws.model.ResponseModel;
import com.wecloud.im.ws.model.request.ReceiveModel;
import com.wecloud.im.ws.sender.PushTask;
import com.wecloud.im.ws.service.MangerChannelService;
import com.wecloud.im.ws.service.WriteDataService;
import com.wecloud.im.ws.strategy.AbstractReceiveStrategy;
import io.geekidea.springbootplus.framework.common.api.ApiCode;
......@@ -75,10 +74,9 @@ public class ImConcreteReceiveStrategy extends AbstractReceiveStrategy {
private PushTask pushTask;
@Override
public void process(ReceiveModel receiveModel, String language, ChannelHandlerContext ctx, String data) throws JsonProcessingException {
public void process(ReceiveModel receiveModel, ChannelHandlerContext ctx, String data, String appKey, String clientId) throws JsonProcessingException {
String appKey = ctx.channel().attr(MangerChannelService.APP_KEY).get();
String clientUniId = ctx.channel().attr(MangerChannelService.CLIENT_ID).get();
// String language = ctx.channel().attr(MangerChannelService.LANGUAGE).get();
// 查询imApplication
ImApplication imApplication = imApplicationService.getOneByAppKey(appKey);
......@@ -88,7 +86,7 @@ public class ImConcreteReceiveStrategy extends AbstractReceiveStrategy {
}
// 查询发送者client
ImClient imClientSender = getClientSender(clientUniId, imApplication);
ImClient imClientSender = getClientSender(clientId, imApplication);
if (imClientSender == null) {
return;
}
......@@ -131,7 +129,7 @@ public class ImConcreteReceiveStrategy extends AbstractReceiveStrategy {
// 判断为单聊
if (membersList.size() == 1) {
// 拉黑逻辑
if (black(receiveModel, appKey, clientUniId, imClientSender, membersList)) {
if (black(receiveModel, appKey, clientId, imClientSender, membersList)) {
return;
}
}
......@@ -145,7 +143,7 @@ public class ImConcreteReceiveStrategy extends AbstractReceiveStrategy {
ImMessageOnlineSend imMessageOnlineSend = new ImMessageOnlineSend();
BeanUtils.copyProperties(imMessage, imMessageOnlineSend);
imMessageOnlineSend.setMsgId(imMessage.getId());
imMessageOnlineSend.setSender(clientUniId);
imMessageOnlineSend.setSender(clientId);
imMessageOnlineSend.setContent(receiveModel.getData());
imMessageOnlineSend.setConversationId(toConversationId);
......@@ -197,7 +195,7 @@ public class ImConcreteReceiveStrategy extends AbstractReceiveStrategy {
responseModel.setData(stringHashMap);
responseModel.setReqId(receiveModel.getReqId());
// 响应发送方
writeDataService.write(responseModel, appKey, clientUniId);
writeDataService.write(responseModel, appKey, clientId);
}
private ImClient getClientSender(String clientUniId, ImApplication imApplication) {
......
......@@ -43,23 +43,23 @@ public class PrintApplicationInfo {
tip.append("===========================================================================================\n");
tip.append(" \n");
tip.append(" !!!准备工作!!! \n");
tip.append(" 1.导入SQL初始化脚本:docs/db,根据不同数据库导入对应SQL脚本并修改链接等信息配置\n");
tip.append(" 2.启动Redis服务,必要条件\n");
tip.append(" 3.启动SpringBootAdmin Server,可选操作,admin模块中,启动SpringBootPlusAdminApplication\n");
tip.append(" 4.根据项目需要,修改项目配置,请先查看官网配置文档:https://springboot.plus/config/\n");
tip.append(" 5.项目模块说明:\n");
tip.append(" admin: SpringBootAdmin Server启动模块\n");
// tip.append(" 1.导入SQL初始化脚本:docs/db,根据不同数据库导入对应SQL脚本并修改链接等信息配置\n");
// tip.append(" 2.启动Redis服务,必要条件\n");
// tip.append(" 3.启动SpringBootAdmin Server,可选操作,admin模块中,启动SpringBootPlusAdminApplication\n");
// tip.append(" 4.根据项目需要,修改项目配置,请先查看官网配置文档:https://springboot.plus/config/\n");
// tip.append(" 5.项目模块说明:\n");
// tip.append(" admin: SpringBootAdmin Server启动模块\n");
tip.append(" bootstrap: 项目启动模块\n");
tip.append(" config: 项目配置模块\n");
tip.append(" distribution:项目打包模块,打包时,请先选中Maven Profiles中的release和对应环境\n");
tip.append(" example: 业务自定义模块,自己的业务代码可在example下进行,也可以再创建模块\n");
// tip.append(" distribution:项目打包模块,打包时,请先选中Maven Profiles中的release和对应环境\n");
// tip.append(" example: 业务自定义模块,自己的业务代码可在example下进行,也可以再创建模块\n");
tip.append(" framework: 项目核心框架模块\n");
tip.append(" generator: 代码生成模块,启动类:SpringBootPlusGenerator,请根据实际情况进行配置\n");
tip.append(" scheduled: 任务调度模块\n");
tip.append(" system: 系统管理模块\n");
tip.append(" 6.FAQ:https://springboot.plus/faq\n");
tip.append(" 7.如开发中遇到bug及问题,欢迎提交ISSUES:https://github.com/geekidea/spring-boot-plus/issues\n");
tip.append(" 8.QQ:625301326,进群答案:springboot.plus\n");
// tip.append(" scheduled: 任务调度模块\n");
// tip.append(" system: 系统管理模块\n");
// tip.append(" 6.FAQ:https://springboot.plus/faq\n");
// tip.append(" 7.如开发中遇到bug及问题,欢迎提交ISSUES:https://github.com/geekidea/spring-boot-plus/issues\n");
// tip.append(" 8.QQ:625301326,进群答案:springboot.plus\n");
tip.append(" \n");
tip.append("===========================================================================================\n");
if ("dev".equals(profileActive)) {
......@@ -107,16 +107,16 @@ public class PrintApplicationInfo {
String homeUrl = "http://" + serverIp + ":" + port + contextPath;
String swaggerUrl = "http://" + serverIp + ":" + port + contextPath + "/swagger-ui.html";
String knife4jUrl = "http://" + serverIp + ":" + port + contextPath + "/doc.html";
log.info("Admin: {}", springBootAdminServerUrl);
// log.info("Admin: {}", springBootAdminServerUrl);
log.info("Home: {}", homeUrl);
log.info("Knife4j: {}", knife4jUrl);
log.info("Swagger: {}", swaggerUrl);
// log.info("Swagger: {}", swaggerUrl);
log.info("spring-boot-plus project start success...........");
if ("dev".equals(profileActive)) {
log.info("\n{}", AnsiUtil.getAnsi(Ansi.Color.BLUE, startSuccess));
} else {
log.info("\n{}", startSuccess);
}
// if ("dev".equals(profileActive)) {
// log.info("\n{}", AnsiUtil.getAnsi(Ansi.Color.BLUE, startSuccess));
// } else {
// log.info("\n{}", startSuccess);
// }
}
}
......@@ -19,7 +19,6 @@ aaaaa3
--
-- 会话
1邀请2
1427910060675305472
......@@ -57,4 +56,23 @@ aaaaa3
"b":"attrs 阿道夫123123是用来213存储用户自定义的一些键值对,ttrs 阿道夫123123是用来213存储用户自定义的一些键值对"
}
}
}
\ No newline at end of file
}
## 集群配置
### AWS服务器内部获取公网IP地址 等元数据
curl http://instance-data/latest/meta-data/public-ipv4
### 华为
查询弹性云服务器的网络信息 Network data(OpenStack元数据API)
文档:https://support.huaweicloud.com/usermanual-ecs/ecs_03_0166.html
Linux操作系统:
curl http://169.254.169.254/latest/meta-data/public-ipv4
Windows操作系统:
Invoke-RestMethod http://169.254.169.254/latest/meta-data/public-ipv4
配置文件中配置当前服务器的运营商 可以从运营商处获取公网IP, 如果获取不到则走IP138等接口获取公网IP
服务器负载均衡配置
load-blance:
服务器配置 Local,AWS,AlibabaCloud,HuaweiCloud
server-type: Local
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment