记录linux websocket握手时间过长问题

1 握手拦截器 打印时间过长

bash 复制代码
@Service
@Slf4j
public class SocketInterceptor implements HandshakeInterceptor {

    /**
     * socket握手拦截
     *
     * @param request
     * @param response
     * @param wsHandler
     * @param attributes
     * @return boolean
     * @throws Exception
     */
    @Override
    public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Map<String, Object> attributes) throws Exception {
//        if(!SystemConstants.SYSTEM_INIT_SUCCESS){
//            return false;
//        }
        log.error("开始握手 {}", LocalDateTime.now());
        HttpServletRequest servletRequest = ((ServletServerHttpRequest) request).getServletRequest();
        //获取token
        String token = servletRequest.getHeader("authorization");
        if (StringUtils.isBlank(token)){
            return false;
        }
        //检查是否登录了
        SystemUserDetails userTokenInfoVo = AuthUtil.getLoginUser();
        if(userTokenInfoVo==null){
            return false;
        }
        //终端类型
        String clientType = SocketUtil.getClientTypeByRequest(servletRequest);
        //当前会话token
        attributes.put("authorization", token);
        //请求终端id
        attributes.put(SocketUtil.USER_ID, userTokenInfoVo.getUserId());
        //当前链接客户端类型
        attributes.put(SocketUtil.CLIENT_TYPE, StringUtils.isBlank(clientType)? "SYSTEM_ADMIN":clientType);
        //获取客户端ip
        String clientIp = IpUtils.getIpAddr(servletRequest);
        attributes.put(SocketUtil.CLIENT_IP, clientIp);
        log.error("开始握手完毕 {}", LocalDateTime.now());
        return true;
    }

    @Override
    public void afterHandshake(ServerHttpRequest request, ServerHttpResponse response, WebSocketHandler wsHandler, Exception exception) {
        log.error("afterHandshake {}", LocalDateTime.now());
    }
}

2 解决方案

原因分析:

找到服务器上的进程名

jps -l

使用jstack 进程号

查看自己服务堆栈的线程

3 解决方案

在系统hosts文件中增加本地ip地址与主机名的对应项

  1. ifconfig 查看本机 ip

  2. 修改hosts文件 sudo vim /etc/hosts

  3. 在文件中追加 192.168.0.224 myserver --myserver 这个随便起名

  4. 测试 ping myserve

再次进行websocket连接

完工 记录一下

相关推荐
YMWM_11 分钟前
ubuntu24.04自带录屏软件使用指南
linux·具身智能
JEECG低代码平台16 分钟前
开源远程桌面 PP远程 v0.1.9 发布:点对点直连,服务器不经手画面
运维·服务器·开源软件
Brain 博览记34 分钟前
脑科学前沿 | 聚焦:TMS 单神经元机制、癫痫网络、感觉交叉可塑性、记忆巩固、EEG 自动化
运维·开发语言·网络·论文阅读·学习·自动化·php
SYHC_CSDN43 分钟前
RTL 仿真自动化新思路:HDLHorse 一键完成用例生成‑仿真执行‑报告输出
运维·fpga开发·自动化
qetfw1 小时前
Debian 磁盘实验:RAID5、LVM 与 LUKS 分层及挂载验证
linux·运维·debian·raid
木心术11 小时前
蜂窝基站 vs 非蜂窝基站:多维度技术对比与选型指南
运维·网络
IT小小猪1 小时前
录音转文字做成流水线:一套规范 + 质检脚本 + 自动排版,34 分钟音频出 25 页 PDF
运维·人工智能·python
卓豪终端管理1 小时前
MDM 落地实战:一台设备从注册到退役的四道关
运维·网络·windows·安全
沫璃染墨2 小时前
从零入门计算机网络系列(三十三):Linux I/O 多路复用详解——epoll 核心接口、底层原理与事件通知机制
linux·运维·服务器·c++·驱动开发·系统架构·硬件架构
Rabitebla2 小时前
【Linux】文件系统(四):目录、路径解析与 dentry 缓存
linux·数据结构·c++·笔记·学习·算法·缓存