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

完工 记录一下

相关推荐
try2find几秒前
移动conda虚拟环境的安装目录
linux·运维·conda
码农101号23 分钟前
Linux中容器文件操作和数据卷使用以及目录挂载
linux·运维·服务器
PanZonghui41 分钟前
Centos项目部署之Nginx 的安装与卸载
linux·nginx
PanZonghui1 小时前
Centos项目部署之安装数据库MySQL8
linux·后端·mysql
PanZonghui1 小时前
Centos项目部署之运行SpringBoot打包后的jar文件
linux·spring boot
PanZonghui1 小时前
Centos项目部署之Java安装与配置
java·linux
程序员弘羽1 小时前
Linux进程管理:从基础到实战
linux·运维·服务器
PanZonghui1 小时前
Centos项目部署之常用操作命令
linux
JeffersonZU1 小时前
Linux/Unix进程概念及基本操作(PID、内存布局、虚拟内存、环境变量、fork、exit、wait、exec、system)
linux·c语言·unix·gnu
大熊程序猿1 小时前
netcore PowerShell 安装-linux
linux·运维