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

完工 记录一下

相关推荐
草莓熊Lotso5 小时前
C++ 抽象类与多态原理深度解析:从纯虚函数到虚表机制(附高频面试题)
java·运维·服务器·开发语言·c++·人工智能·笔记
q***96585 小时前
如何在 Ubuntu 22.04 上安装 MySQL
linux·mysql·ubuntu
4t4run5 小时前
16、Linux常用命令-vim 文本编辑
linux·vim
剑动山河5 小时前
ubuntu 22.04 升级openssh默认版本8.9p1 到10.1p1
linux·ubuntu
huangyuchi.5 小时前
【Linux网络】基于UDP的Socket编程,实现简单聊天室
linux·网络·udp·网络通信·c/c++·socket编程·简单聊天室
egoist20235 小时前
[linux仓库]多线程同步:基于POSIX信号量实现生产者-消费者模型[线程·柒]
linux·运维·生产者消费者模型·环形队列·system v信号量
DeeplyMind5 小时前
linux drm子系统专栏介绍
linux·驱动开发·ai·drm·amdgpu·kfd
艾莉丝努力练剑5 小时前
【Linux基础开发工具 (二)】详解Linux文本编辑器:Vim从入门到精通——完整教程与实战指南(上)
linux·运维·服务器·人工智能·ubuntu·centos·vim
拾光Ծ5 小时前
Linux高效编程与实战:自动化构建工具“make/Makefile”和第一个系统程序——进度条
linux·运维·自动化·gcc
wazmlp0018873696 小时前
第六章,主从服务器
运维·服务器