问题记录
问题场景,微服务中的一个子模块中,整合netty-socketIO,配置好的情况下,通过在主类上实现CommandLineRunner 实现run方法,来在springBoot启动完成之后启动socketIo服务,
原来的代码如下
@Slf4j
@RestController
@EnableFeignClients
@EnableDiscoveryClient
@Configuration
@SpringBootApplication
@RefreshScope
public class Application implements CommandLineRunner {
@AutoWired
SocketIoServer sockerIoServer;
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
@Override
public void run(String... args) throws Exception {
sockerIoServer.start();
log.info("socketIo 启动成功");
}
}
启动应用后发现启动失败,报了端口已被绑定。我之前已经检查过了,启动之前socketio服务的端口是没有被占用的。更换端口仍然报端口占用。
查看日志发现“socketIo 启动成功 ”打印了两遍。第二遍打印随后就报了端口占用,那肯定会因为启动了两次。
搞了好久好久,也没发现是哪里的原因。
直到百度到了一个信息@RefreshScope会导致CommandLineRunner 执行两次 -。-
真相大白了。
那就不能放在这里进行启动了
可以在没有标注@RefreshScope的类上使用CommandLineRunner
或者使用应用准备就绪事件监听。
@EventListstener(ApplicationRedayEvent.class)
public void start() {
//TODO
}
windows 查看端口占用
netstat -ano | findstr "8080"
taskkill -f -pid 1222