一开始我以为zuul支持不了websocket,报了很多错,我都追究在zuul上,其实zuul没问题,是zuul1不支持,我现在使用的是zuul2了,已经支持了,这也是后面搞着搞着突然想到我会不会用的就是zuul2,

经过我一番排查,果然没问题,先说一下问题在哪,在于spring security和https上

1.spring security会拦截websocket的请求,所以得在拦截器上放行:

2.如果项目设置了统一访问名入口,即:


 

则websocket连接地址必须在项目里写的websocket连接地址前面加上api:

要不然会报200

3.https连接下使用websocket

websocket utl的协议必须得从ws改为wss

且IP必须写成域名,不能加端口:

http协议:

var ip = "localhost:8090:";

websocket = new WebSocket("ws://" + ip + "/way/pp");

https协议:

var ip = "www.baidu.com";

websocket = new WebSocket("wss://" + ip + "/way/pp");

4.https连接下使用websocket一起都设置好了,但是报404

假如使用的是nginx反向代理,则在nginx.conf里面加一条匹配websocket连接的映射路径

# websockets
location /way/ {
    proxy_pass http://localhost:8913;           
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
	#设置连接时间 
	proxy_read_timeout 3600s;
}

注意,是加载server下面的,跟转发后台请求一样

5.https连接下使用websocket连接成功了没多久后台就报java.io.EOFException

因为使用了nginx代理websocket连接请求,nginx默认设置了一个连接时间为1分钟,所以连接成功1分钟之后前台就会断开websocket连接,后台自然报错

解决办法便是在nginx设置连接时间

在转发websocket连接请求的配置里加上

proxy_read_timeout 3600s; #为一个小时

# websockets
location /way/ {
    proxy_pass http://localhost:8913;           
    proxy_http_version 1.1;
    proxy_set_header Upgrade $http_upgrade;
    proxy_set_header Connection "Upgrade";
    proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    proxy_set_header X-Real-IP $remote_addr;
	#设置连接时间
	proxy_read_timeout 3600s;
}

 

Logo

北京人形旗下天工造物具身智能开源社区,聚焦具身天工与慧思开物两大平台

更多推荐