我们知道, 现在很多浏览器都已经是直接支持 websocket 协议的了,  除此之外, sockjs, 也可以实现 websocket 功能.. 当然, 其原理是不同的.

一开始 websocket 已经做好了,  但是后面发现 websocket不能穿透zuul ,

出现

com.netflix.zuul.exception.ZuulException: Forwarding error ... 等问题.

找到了 https://blog.csdn.net/moshowgame/article/details/80511158

修改了ZuulFilter , 貌似行了, 结果仍然各种问题..

真是郁闷了.  网上的关于springboot + websocket 资料很多

https://blog.csdn.net/qq_39320953/article/details/77852126 基本可用

https://blog.csdn.net/jie873440996/article/details/70257558 非常详细

https://blog.csdn.net/haoyuyang/article/details/53364372 很好

https://segmentfault.com/a/1190000006617344 stomp介绍

https://blog.csdn.net/nongshuqiner/article/details/78792079 sockjs介绍

好像stomp能解决问题....  哦, 改stomp吧, 但很快遇到问题:

org.springframework.web.socket.sockjs.SockJsMessageDeliveryException: Failed to ..

原来js 也是需要改的..

参考 https://my.oschina.net/robinjiang/blog/898557

, 但是 zuul + websocket 的资料就少了.

其实也是找到了几个的, 但是呢, 不能完全拿过来用, 因为1 没有它们的源代码, 2 认证机制不一样..  怎么办呢? 升级到 zuul2 ? 使用 spring-cloud-gateway? spring-cloud-gateway 下载了一个示例, 基本是可行的.  但是, 这样的话, 需要整个升级, 改动也太大了..

终于找到了 一篇: https://github.com/mthizo247/spring-cloud-netflix-zuul-websocket

示例也亲测可用, 但是, 拿过来到我们项目就不行了,  出现各种问题,

首先是   认证机制不一样.. 我们登录都是基于token 的, mthizo247是基于spring security的, 发送点对点消息遇到各种问题:

annotation.support.MissingSessionUserException: No "user" header in message:

找到答案:

You are trying to subscribe to a user destination so the user must be authenticated.

If that is an anonymous user who want to subscribe to the topic, answer to this questionwill help.

You'll have to assign an anonymous identify to the user and there are two options:

  1. Configure a sub-class of DefaultHandshakeHandler that overrides determineUserand assigns some kind of identity to every WebSocketSession.

  2. The WebSocket session will fall back on the value returned from HttpServletRequest.getUserPrincipal on the handshake HTTP request. You could have a servlet Filter wrap the HttpServletRequest and decide what to return from that method. Or if you're using Spring Security which has the AnonymousAuthenticationFilter, override its createAuthentication method.

https://stackoverflow.com/questions/36616167/spring-websocket-missingsessionuserexception-no-user-header-in-message

http://www.itkeyword.com/doc/0662498046730811x767/spring-websocket-missingsessionuserexception-no-user-header-in-message

https://stackoverflow.com/questions/36847814/spring-websocket-send-message-from-a-queue-to-a-user-convertandsendtouser-not-wo

这个通过 stomp 建立连接时候 设置 nativeHeaders 解决了, nativeHeaders  也是很难获取到的, 后面 通过stomp 发出的event , 终于是解决了.

另外, mthizo247 群发消息会出现 重复消息的情况, 这就尴尬了..  这个问题比较难比较, 我仔细调试过mthizo247的源码, 发现里面session 的数量刚好出现了重复, 这样就导致了 前端接收到重复消息,,, 怎么办呢 ?

我后面想出了一个折中的解决方案:

https://github.com/mthizo247/spring-cloud-netflix-zuul-websocket/issues/4

问题好像解决了, 但是, convertAndSendToUser 好像不工作了??

simpMessagingTemplate convertAndSendToUser

哦, 仔细一看是代码bug, mthizo247  也需要配合修改..

问题好像解决了, 但是, 又,

Failed to send message to ExecutorSubscribableChannel[clientInboundChannel

...

NoClassDefFoundErrorc org/springframework/integration/channel/DirectChannel

...

分析了下, 好像是sleuth 出来捣乱, dependency 中先屏蔽它吧, 然后呢, 有发现一个NosuchMethodError 之类的错误, 仔细分析, 发现是 spring-message的版本不一致, 当然, 这个是由于spring-boot 版本不一致导致的...  改吧..

但是,  我们前后端是分离的, 前端除了浏览器, 还有nodejs 服务器呢..  浏览器刷新之后, stomp 连接就断开了, 但是 nodejs 服务器 和zuul 的连接确实没有断开的..

sockjs.js:3559 'protocols_whitelist' is DEPRECATED. Use 'transports' instead.

Opening Web Socket...
Web Socket Opened... >>> CONNECT
token:u1
accept-version:1.1,1.0
heart-beat:10000,10000 <<< CONNECTED
version:1.1
heart-beat:0,0
user-name:u1 connected to server undefined >>> SUBSCRIBE
id:sub-0
destination:/user/queue/notifications 浏览器刷新出现: >>> CONNECT
token:u1
accept-version:1.1,1.0
heart-beat:10000,10000 Whoops! Lost connection to http://10.10.10.76:9999/lk-websocket

因为nodejs 服务器的存在, 浏览器刷新 会进行重连,  然后导致重新建立连接, 但是, 当前用户的连接以及存在了, 所以就无法再建立了, 所以就  Lost connection ...   怎么办呢?  nodejs  端处理吧...    当连接断开的时候, nodejs 服务器检测到了, 然后也同样的像zuul 主动发起一个连接断开的请求..

spring cloud整合 websocket 的那些事的更多相关文章

  1. 【Java Web开发学习】Spring MVC整合WebSocket通信

    Spring MVC整合WebSocket通信 目录 ========================================================================= ...

  2. Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇

    Spring cloud系列教程第十篇- Spring cloud整合Eureka总结篇 本文主要内容: 1:spring cloud整合Eureka总结 本文是由凯哥(凯哥Java:kagejava ...

  3. Spring Cloud实战 | 第九篇:Spring Cloud整合Spring Security OAuth2认证服务器统一认证自定义异常处理

    本文完整代码下载点击 一. 前言 相信了解过我或者看过我之前的系列文章应该多少知道点我写这些文章包括创建 有来商城youlai-mall 这个项目的目的,想给那些真的想提升自己或者迷茫的人(包括自己- ...

  4. Spring Cloud 整合 Feign 的原理

    前言 在 上篇 介绍了 Feign 的核心实现原理,在文末也提到了会再介绍其和 Spring Cloud 的整合原理,Spring 具有很强的扩展性,会把一些常用的解决方案通过 starter 的方式 ...

  5. Spring Cloud 整合 nacos 实现动态配置中心

    上一篇文章讲解了Spring Cloud 整合 nacos 实现服务注册与发现,nacos除了有服务注册与发现的功能,还有提供动态配置服务的功能.本文主要讲解Spring Cloud 整合nacos实 ...

  6. spring boot整合websocket

    转载自:https://www.cnblogs.com/GoodHelper/p/7078381.html 一.WebSocket简单介绍 随着互联网的发展,传统的HTTP协议已经很难满足Web应用日 ...

  7. Spring Cloud都做了哪些事

    Spring Cloud是一系列框架的有序集合.它利用Spring Boot的开发便利性巧妙地简化了分布式系统基础设施的开发,如服务发现注册.配置中心.消息总线.负载均衡.断路器.数据监控等,都可以用 ...

  8. Spring容器整合WebSocket

    原链接:http://blog.csdn.net/canot/article/details/52575054 WebSocker是一个保持web客户端与服务器长链接的技术.这样在两者通信过程中如果服 ...

  9. Spring Cloud 整合分布式链路追踪系统Sleuth和ZipKin实战,分析系统瓶颈

    导读 微服务架构中,是否遇到过这种情况,服务间调用链过长,导致性能迟迟上不去,不知道哪里出问题了,巴拉巴拉....,回归正题,今天我们使用SpringCloud组件,来分析一下微服务架构中系统调用的瓶 ...

随机推荐

  1. centos 安装桌面系统

    yum -y groupinstall "Desktop" "Desktop Platform" "X Window System" &qu ...

  2. LINUX 录制屏幕制作gif动态图工具peek

    sudo add-apt-repository ppa:peek-developers/stable sudo apt update sudo apt install peek linux 压缩工具改 ...

  3. Problem E: 类的初体验(V)

    Description 定义一个类Data,只有一个int类型的属性和如下方法: 1.   缺省构造函数,将属性初始化为0,并输出"Data's default constructor.&q ...

  4. mysql中外键的创建与删除

    外键的创建 方法1:创建表的时候设置(外键名随机生成) 1.前提条件,必须要有一个主表,这里设为persons 2.主表中必须设置主键字段primary key,这里设为id_p //创建数据库dat ...

  5. IOS Block代码块的定义与使用

    代码块的本质是和其他的变量类似,不同的是,代码块存储的数据是一个函数体.使用代码块,你可以像调用其他标准函数一样的调用,可以传入参数,并得到返回值.     脱字符是代码块的语法标记.下图表示代码块的 ...

  6. 慢工出细活 JS 等待加载效果

    实例可以直接运行查看效果.很方便快捷 <html> <head> <meta http-equiv="content-Type" content=&q ...

  7. Restful Service 中 DateTime 在 url 中传递

    在C# url 中一旦包特殊字符,请求可能就无法送达.可以使用如下方法,最为便捷. 请求端: beginTime.Value.ToString("yyyyMMddHHmmss") ...

  8. PythonStudy——Python字典底层实现原理 The underlying implementation principle of Python dictionary

    在Python中,字典是通过散列表或说哈希表实现的.字典也被称为关联数组,还称为哈希数组等.也就是说,字典也是一个数组,但数组的索引是键经过哈希函数处理后得到的散列值.哈希函数的目的是使键均匀地分布在 ...

  9. 戴尔R710服务器安装系统——配置raid

    一,内存二,硬盘(分区,数据量大小)三,电源线,网络线四,raid(raid0,raid1,raid5) 从这里开始 1.进入系统时不用管,默认进入即可 2.在读完内存消息之后,开始读取磁盘消息,在出 ...

  10. Ubuntu 16.04出现:Problem executing scripts APT::Update::Post-Invoke-Success 'if /usr/bin/test -w /var/cache/app-info -a -e /usr/bin/appstreamcli; then appstreamcli refresh > /dev/null; fi'

    错误: Reading package lists... Done E: Problem executing scripts APT::Update::Post-Invoke-Success 'if ...