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

完工 记录一下

相关推荐
Tender_光1 小时前
iptables实验
运维·服务器
szxinmai主板定制专家2 小时前
【飞腾AI加固服务器】全国产化飞腾+昇腾310+PCIe Switch的AI大模型服务器解决方案
运维·服务器·arm开发·人工智能·fpga开发
点击查询2 小时前
怎么把自己电脑设置成服务器?
运维·服务器
阿里云大数据AI技术2 小时前
ES Serverless 8.17王牌发布:向量检索「火力全开」,智能扩缩「秒级响应」!
大数据·运维·serverless
yzx9910132 小时前
Linux 系统中的算法技巧与性能优化
linux·算法·性能优化
fengyehongWorld2 小时前
Linux Docker的简介
linux·docker
wanhengidc2 小时前
服务器中日志分析的作用都有哪些
运维·服务器
Mikhail_G3 小时前
Python应用变量与数据类型
大数据·运维·开发语言·python·数据分析
曹瑞曹瑞3 小时前
VMware导入vmdk文件
linux·运维·服务器
Johny_Zhao3 小时前
2025年6月Docker镜像加速失效终极解决方案
linux·网络·网络安全·docker·信息安全·kubernetes·云计算·containerd·yum源·系统运维