Spring Boot集成Websocket长连接,本地配置运行无问题。打包上传到服务器出现问题:无法启动、404。
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.server.standard.ServerEndpointExporter;
@Configuration
public class WebSocketConfig {
@Bean
public ServerEndpointExporter serverEndpointExporter() {
return new ServerEndpointExporter();
}
}
无法启动原因:由于SpringBoot内置tomcat需要以上代码,使用外置Tomcat需要注释。
404原因:使用nginx代理需要进行以下配置,否则会报404。
server
{
listen 80;
server_name api.xxx.com;
location / {
proxy_pass http://127.0.0.1:8083;
#websocket 配置
proxy_connect_timeout 4s;
proxy_read_timeout 7200s; #超过7200秒(两小时)内没通讯则断连
proxy_send_timeout 12s;
proxy_set_header Upgrade $http_upgrade;
proxy_set_header Connection "upgrade";
}
}