V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Graves
V2EX  ›  Java

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

  •  
  •   Graves · 2019-04-10 21:15:30 +08:00 · 7403 次点击
    这是一个创建于 1814 天前的主题,其中的信息可能已经有所发展或是发生改变。
    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
    13 条回复    2019-04-30 10:00:37 +08:00
    Graves
        1
    Graves  
    OP
       2019-04-10 22:16:03 +08:00
    感觉像是 netty 绑定端口的时候阻塞了主线程,导致 springboot 到达不了启动 tomcat 内置容器的那一步
    Graves
        2
    Graves  
    OP
       2019-04-10 22:18:05 +08:00
    这里 netty 的作用我用来做 udp 服务器,用到 tomcat 是为了 websocket 服务,需求是有一个 NB 给我轮询发送数据,接受到数据要及时刷新网页渲染
    wbf1013
        3
    wbf1013  
       2019-04-10 23:21:14 +08:00 via Android
    直接用 netty 做 websocket 服务不好吗?
    geliang0120
        4
    geliang0120  
       2019-04-11 01:18:12 +08:00 via iPhone
    netty 做 websocket 和 http 服务的例子
    https://m.gitee.com/geliang/Umsp-netty
    sutra
        5
    sutra  
       2019-04-11 07:09:44 +08:00
    把整个 org.spring....boot 的日志打开成 INFO 甚至 ALL 级别再看看日志。
    MoHen9
        6
    MoHen9  
       2019-04-11 07:49:43 +08:00 via Android
    创建 Netty 服务时,在子线程创建,或者不调用 closeFuture 方法去阻塞
    hantsy
        7
    hantsy  
       2019-04-11 08:00:43 +08:00
    直接用 Netty 做 HTTP 服务器就完事了,Spring 5 现在支持 Reactive 编程,Spring Boot 中添加 spring-boot-starter-reactive 依赖,默认使用 Netty。
    yuyizyk
        8
    yuyizyk  
       2019-04-11 09:13:22 +08:00
    让你的 netty 在 mvc servlet runner 之后运行就可以了。至于阻塞主线程...至少我当时并不是这个问题
    IamNotShady
        9
    IamNotShady  
       2019-04-11 09:22:10 +08:00
    升级 spring boot 版本到 2.X 呢?
    cnsoloer
        10
    cnsoloer  
       2019-04-11 10:31:31 +08:00
    有像我一样直接用 Netty 做 HTTP,TCP,UDP,Websocket 服务器的吗。
    至于 REST,通过注解和正则表达式,做路径和参数匹配到方法。
    ala2008
        11
    ala2008  
       2019-04-11 11:22:56 +08:00
    6 楼加一?
    dbpe
        12
    dbpe  
       2019-04-11 13:23:10 +08:00
    直接用 vertx 不好么..../
    xinQing
        13
    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.");
    }

    }
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   975 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 32ms · UTC 20:19 · PVG 04:19 · LAX 13:19 · JFK 16:19
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.