Graves
V2EX  ›  Java

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

  •  
  •   Graves · Apr 10, 2019 · 8870 views
    This topic created in 2770 days ago, the information mentioned may be changed or developed.
    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 replies  •  2019-04-30 10:00:37 +08:00
    Graves
    Graves
    OP
    Apr 10, 2019
    1
    感觉像是 netty 绑定端口的时候阻塞了主线程,导致 springboot 到达不了启动 tomcat 内置容器的那一步
    Graves
    Graves
    OP
    Apr 10, 2019
    2
    这里 netty 的作用我用来做 udp 服务器,用到 tomcat 是为了 websocket 服务,需求是有一个 NB 给我轮询发送数据,接受到数据要及时刷新网页渲染
    wbf1013
    wbf1013
    Apr 10, 2019 via Android
    3
    直接用 netty 做 websocket 服务不好吗?
    soloV2
    soloV2
    Apr 11, 2019 via iPhone
    4
    netty 做 websocket 和 http 服务的例子
    https://m.gitee.com/geliang/Umsp-netty
    sutra
    sutra
    Apr 11, 2019
    5
    把整个 org.spring....boot 的日志打开成 INFO 甚至 ALL 级别再看看日志。
    MoHen9
    MoHen9
    Apr 11, 2019 via Android
    6
    创建 Netty 服务时,在子线程创建,或者不调用 closeFuture 方法去阻塞
    hantsy
    hantsy
    Apr 11, 2019
    7
    直接用 Netty 做 HTTP 服务器就完事了,Spring 5 现在支持 Reactive 编程,Spring Boot 中添加 spring-boot-starter-reactive 依赖,默认使用 Netty。
    yuyizyk
    yuyizyk
    Apr 11, 2019
    8
    让你的 netty 在 mvc servlet runner 之后运行就可以了。至于阻塞主线程...至少我当时并不是这个问题
    IamNotShady
    IamNotShady
    Apr 11, 2019
    9
    升级 spring boot 版本到 2.X 呢?
    cnsoloer
    cnsoloer
    Apr 11, 2019
    10
    有像我一样直接用 Netty 做 HTTP,TCP,UDP,Websocket 服务器的吗。
    至于 REST,通过注解和正则表达式,做路径和参数匹配到方法。
    ala2008
    ala2008
    Apr 11, 2019
    11
    6 楼加一?
    dbpe
    dbpe
    Apr 11, 2019
    12
    直接用 vertx 不好么..../
    xinQing
    xinQing
    Apr 30, 2019
    13
    你是不是启动 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.");
    }

    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   974 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 55ms · UTC 19:36 · PVG 03:36 · LAX 11:36 · JFK 14:36
    ♥ Do have faith in what you're doing.