springboot 集成了 netty,启动后内置 tomcat 没有启动?

2019-04-10 21:15:30 +08:00
 Graves
https://i.loli.net/2019/04/10/5cadec18acacd.jpg

只有说 initialized,并没有 started on
stackoverflow 也搜到一个同样的问题,但是没有解答,实在没有头绪,有人遇到过吗
https://stackoverflow.com/questions/48730172/spring-boot-tomcat-initialized-successfully-but-not-listening-port-found
7454 次点击
所在节点    Java
13 条回复
Graves
2019-04-10 22:16:03 +08:00
感觉像是 netty 绑定端口的时候阻塞了主线程,导致 springboot 到达不了启动 tomcat 内置容器的那一步
Graves
2019-04-10 22:18:05 +08:00
这里 netty 的作用我用来做 udp 服务器,用到 tomcat 是为了 websocket 服务,需求是有一个 NB 给我轮询发送数据,接受到数据要及时刷新网页渲染
wbf1013
2019-04-10 23:21:14 +08:00
直接用 netty 做 websocket 服务不好吗?
geliang0120
2019-04-11 01:18:12 +08:00
netty 做 websocket 和 http 服务的例子
https://m.gitee.com/geliang/Umsp-netty
sutra
2019-04-11 07:09:44 +08:00
把整个 org.spring....boot 的日志打开成 INFO 甚至 ALL 级别再看看日志。
MoHen9
2019-04-11 07:49:43 +08:00
创建 Netty 服务时,在子线程创建,或者不调用 closeFuture 方法去阻塞
hantsy
2019-04-11 08:00:43 +08:00
直接用 Netty 做 HTTP 服务器就完事了,Spring 5 现在支持 Reactive 编程,Spring Boot 中添加 spring-boot-starter-reactive 依赖,默认使用 Netty。
yuyizyk
2019-04-11 09:13:22 +08:00
让你的 netty 在 mvc servlet runner 之后运行就可以了。至于阻塞主线程...至少我当时并不是这个问题
IamNotShady
2019-04-11 09:22:10 +08:00
升级 spring boot 版本到 2.X 呢?
cnsoloer
2019-04-11 10:31:31 +08:00
有像我一样直接用 Netty 做 HTTP,TCP,UDP,Websocket 服务器的吗。
至于 REST,通过注解和正则表达式,做路径和参数匹配到方法。
ala2008
2019-04-11 11:22:56 +08:00
6 楼加一?
dbpe
2019-04-11 13:23:10 +08:00
直接用 vertx 不好么..../
xinQing
2019-04-30 10:00:37 +08:00
你是不是启动 netty 阻塞了 main 啊,用一个新的线程启动 netty 就好了。我之前在 spring boot 中集成 netty 做 websocket 聊天,是在 spring 容器刷新后,再用新线程启动 netty 服务。下面的事例代码可以参考下:

/**
* spring 容器刷新时启动 netty 的 WebSocket 服务
*
* Created by xuan on 2018/3/5
*/
@Component
public class ApplicationRefreshListener implements ApplicationListener<ContextStartedEvent> {

private static final Logger LOG = LoggerFactory.getLogger(ApplicationRefreshListener.class);

private ExecutorService webSocketSinglePool;

@PostConstruct
public void setup() {
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder()
.setNameFormat("webSocketSinglePool-%d").build();
webSocketSinglePool = new ThreadPoolExecutor(1, 1, 0L,
TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(1024),
namedThreadFactory, new ThreadPoolExecutor.AbortPolicy());
LOG.info("webSocketSinglePool init.");
}

@Override
public void onApplicationEvent(ContextRefreshedEvent event) {
runWebSocketServer(event.getApplicationContext());
}

private void runWebSocketServer(ApplicationContext applicationContext) {
final WebSocketServer webSocketServer = applicationContext.getBean(WebSocketServer.class);
webSocketSinglePool.execute(() -> {
try {
webSocketServer.listenAndServe();
} catch (Exception e) {
LOG.error("webSocket listen and serve error.", e);
}
});
}

@PreDestroy
public void cleanup() {
webSocketSinglePool.shutdown();
LOG.info("webSocketSinglePool destroyed.");
}

}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/553865

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX