2.0.2 springboot项目集成socketIo实现实时推送功能出现异常 Caused by: java.net.BindException: 地址已在使用

霸气de小男生 提交于 2019-12-15 15:57:48

.   ____          _            __ _ _
/\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/  ___)| |_)| | | | | || (_| |  ) ) ) )
'  |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot ::        (v2.0.2.RELEASE)
信息: 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
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:800) [spring-boot-2.0.2.RELEASE.jar!/:2.0.2.RELEASE]
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:781) [spring-boot-2.0.2.RELEASE.jar!/:2.0.2.RELEASE]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:335) [spring-boot-2.0.2.RELEASE.jar!/:2.0.2.RELEASE]
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.8.0_212]
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[?:1.8.0_212]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_212]
at java.lang.reflect.Method.invoke(Method.java:498) ~[?:1.8.0_212]
at org.springframework.boot.loader.MainMethodRunner.run(MainMethodRunner.java:48) [iip.sdrdatapushserver.plugin-2.2-SNAPSHOT.jar:2.2-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:87) [iip.sdrdatapushserver.plugin-2.2-SNAPSHOT.jar:2.2-SNAPSHOT]
at org.springframework.boot.loader.Launcher.launch(Launcher.java:50) [iip.sdrdatapushserver.plugin-2.2-SNAPSHOT.jar:2.2-SNAPSHOT]
at org.springframework.boot.loader.PropertiesLauncher.main(PropertiesLauncher.java:593) [iip.sdrdatapushserver.plugin-2.2-SNAPSHOT.jar:2.2-SNAPSHOT]
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-4.1.30.Final.jar!/:4.1.30.Final]
at io.netty.channel.DefaultChannelPipeline$HeadContext.bind(DefaultChannelPipeline.java:1358) ~[netty-transport-4.1.30.Final.jar!/:4.1.30.Final]
at io.netty.channel.AbstractChannelHandlerContext.invokeBind(AbstractChannelHandlerContext.java:501) ~[netty-transport-4.1.30.Final.jar!/:4.1.30.Final]
at io.netty.channel.AbstractChannelHandlerContext.bind(AbstractChannelHandlerContext.java:486) ~[netty-transport-4.1.30.Final.jar!/:4.1.30.Final]
at io.netty.channel.DefaultChannelPipeline.bind(DefaultChannelPipeline.java:1019) ~[netty-transport-4.1.30.Final.jar!/:4.1.30.Final]
at io.netty.channel.AbstractChannel.bind(AbstractChannel.java:254) ~[netty-transport-4.1.30.Final.jar!/:4.1.30.Final]
at io.netty.bootstrap.AbstractBootstrap$2.run(AbstractBootstrap.java:366) ~[netty-transport-4.1.30.Final.jar!/:4.1.30.Final]
at io.netty.util.concurrent.AbstractEventExecutor.safeExecute(AbstractEventExecutor.java:163) ~[netty-common-4.1.30.Final.jar!/:4.1.30.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor.runAllTasks(SingleThreadEventExecutor.java:404) ~[netty-common-4.1.30.Final.jar!/:4.1.30.Final]
at io.netty.channel.nio.NioEventLoop.run(NioEventLoop.java:462) ~[netty-transport-4.1.30.Final.jar!/:4.1.30.Final]
at io.netty.util.concurrent.SingleThreadEventExecutor$5.run(SingleThreadEventExecutor.java:897) ~[netty-common-4.1.30.Final.jar!/:4.1.30.Final]
at io.netty.util.concurrent.FastThreadLocalRunnable.run(FastThreadLocalRunnable.java:30) ~[netty-common-4.1.30.Final.jar!/:4.1.30.Final]
at java.lang.Thread.run(Thread.java:748) ~[?:1.8.0_212]
org.apache.coyote.AbstractProtocol pause

“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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!