记录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连接

完工 记录一下

相关推荐
Cat_Rocky5 分钟前
利用Packet Tracer网络实验
linux·运维·服务器
嵌入式×边缘AI:打怪升级日志23 分钟前
Linux 驱动实战:SR501 人体红外传感器驱动开发与调试全记录
linux·运维·驱动开发
正点原子30 分钟前
【正点原子Linux连载】第三章 U-Boot使用 摘自【正点原子】ATK-DLRK3568嵌入式Linux驱动开发指南
linux·运维·驱动开发
Qbw20041 小时前
【Linux】进程地址空间
linux·c++
忍冬行者1 小时前
MongoDB 三节点副本集离线部署运维手册
运维·数据库·mongodb
爱学习的小囧1 小时前
ESXi VMkernel 端口 MTU 最佳设置详解
运维·服务器·网络·php·虚拟化
jamon_tan2 小时前
linux下lvgl8.3动态库编译
linux
Elastic 中国社区官方博客3 小时前
Elastic Security、Observability 和 Search 现在在你的 AI 工具中提供交互式 UI
大数据·运维·人工智能·elasticsearch·搜索引擎·安全威胁分析·可用性测试
星辰_mya4 小时前
OSI 七层模型之“跨国诈骗集团”深度讲解
运维·服务器·后端·面试·架构师
贝锐4 小时前
如何破解商用安卓无人值守运维痛点,向日葵赋能数字化高效转型
运维