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

完工 记录一下

相关推荐
Akshsjsjenjd1 小时前
Linux 服务部署:自签 CA 证书构建 HTTPS 及动态 Web 集成
linux·前端·https
为什么要内卷,摆烂不香吗2 小时前
kubernetes(4) 微服务
linux·运维·微服务·容器·kubernetes
conkl7 小时前
Linux 零基础万字入门指南(进阶详解版)
linux·运维·服务器·ssh·文件管理·shell·linux基础
一水鉴天10 小时前
认知系统的架构: 认知残余三角形、认知主体意识 和认知演进金字塔(腾讯元宝)
运维·服务器
三体世界10 小时前
Mysql基本使用语句(一)
linux·开发语言·数据库·c++·sql·mysql·主键
TT-Kun10 小时前
Linux 上手 UDP Socket 程序编写(含完整具体demo)
linux·计算机网络·udp
一川风絮千片雪10 小时前
【环境配置】Linux/Ubuntu24.04 无图形界面安装显卡驱动
linux·运维·服务器
Danileaf_Guo11 小时前
Ubuntu 18.04快速配置WireGuard互联
linux·运维·服务器·ubuntu
柳鲲鹏11 小时前
Ubuntu下载、安装、编译指定版本python
运维·服务器·ubuntu
快乐就是哈哈哈11 小时前
从零部署 MySQL 数据库:Linux 安装与防火墙策略全解析
linux·mysql