2.0.2 springboot項目集成socketIo實現實時推送功能出現異常 Caused by: java.net.BindException: 地址已在使用


信息: Using a shared selector for servlet write/read
2019-12-15 13:57:42,380 ERROR (SocketIOServer.java:157) - SocketIO server start failed at port: 3703!
2019-12-15 13:57:42,395 ERROR (SpringApplication.java:842) - Application run failed
java.lang.IllegalStateException: Failed to execute CommandLineRunner

.......
Caused by: java.net.BindException: 地址已在使用
at sun.nio.ch.Net.bind0(Native Method) ~[?:1.8.0_212]
at sun.nio.ch.Net.bind(Net.java:433) ~[?:1.8.0_212]
at sun.nio.ch.Net.bind(Net.java:425) ~[?:1.8.0_212]
at sun.nio.ch.ServerSocketChannelImpl.bind(ServerSocketChannelImpl.java:223) ~[?:1.8.0_212]
at io.netty.channel.socket.nio.NioServerSocketChannel.doBind(NioServerSocketChannel.java:130) ~[netty-transport-4.1.30.Final.jar!/:4.1.30.Final]
at io.netty.channel.AbstractChannel$AbstractUnsafe.bind(AbstractChannel.java:558) ~[netty-transport-

“java.net.BindException: Address already in use”

出現這個問題的是有客戶端在使用,項目重啓後,socket的端口在短時間內沒有完全釋放,就會出現上述提示,端口被佔用,但是用netstat命令查看又沒有

開發作者給的意見:

Some developers don't know how to solve this problem, so I think this is a good idea to set the default value of SocketConfig.reuseAddress to true. It can avoid the BindException.

解決辦法:

Configuration configuration = new Configuration();
 SocketConfig socketConfig = configuration.getSocketConfig();
 socketConfig.setReuseAddress(true);
項目使用:
Configuration config = new Configuration();
config.setHostname(host);
config.setPort(port);
SocketConfig socketConfig=config.getSocketConfig();
if(!socketConfig.isReuseAddress()){
    socketConfig.setReuseAddress(true);
    System.out.println("是否綁定了: "+socketConfig.isReuseAddress());
}
config.setSocketConfig(socketConfig);

經驗證是有效的,項目重啓後再沒有端口占用問題

參考連接:
https://blog.csdn.net/zhouzuixin/article/details/26502597

發佈了13 篇原創文章 · 獲贊 1 · 訪問量 3萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章