在 SpringBoot 中使用 Netty 的思路,偷梁换柱之计

2023-04-15 21:53:07 +08:00
 wellR

项目中准备用 netty 做些定制化的功能,自然而然用上了 SpringBoot 这套,招不在老管用就好。思路就是不启动 tomcat ,在主线程启动 netty 。

	public static void main(String[] args) {
        StopWatch stopWatch = new StopWatch();
        stopWatch.start("start app...");
        ConfigurableApplicationContext context = SpringApplication.run(NettyServer.class, args);
        //get bean
        SecondHandler handler = context.getBean(SecondHandler.class);
        int port = context.getEnvironment().getProperty("server.port", Integer.class, 9000);
        EventLoopGroup bossGroup = new NioEventLoopGroup(1);
        EventLoopGroup workerGroup = new NioEventLoopGroup(16);
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .localAddress(new InetSocketAddress(port))
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch) {
                            ch.pipeline()
                                    .addLast("codec", new HttpServerCodec())
                                    .addLast("compressor", new HttpContentCompressor())
                                    .addLast("aggregator", new HttpObjectAggregator(65536))
                                    .addLast("handler", handler);
                        }
                    })
                    .childOption(ChannelOption.SO_KEEPALIVE, true);
            ChannelFuture f = b.bind().sync();
            stopWatch.stop();
            log.info("Netty Server started ,Listening on {}, info={}", port, stopWatch.prettyPrint());
            f.channel().closeFuture().sync();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

项目链接

3670 次点击
所在节点    Java
30 条回复
liuhai233
2023-04-15 21:56:52 +08:00
不是可以选 reactive 嘛,默认就是 netty 了吧
wellR
2023-04-15 22:03:08 +08:00
@liuhai233 哦 webflux 没在生产上用过,相对来说 netty 更好做定制化开发
chendy
2023-04-15 22:06:08 +08:00
不启动 tomcat 可以直接不要 tomcat ,留个啥东西不让 main 退出就行…
wellR
2023-04-15 22:14:00 +08:00
@chendy 是这样的,一直以来用 SpringBoot 搞 web 开发都形成思维定势了,这次是打开思路了,SpringBoot 还可以来做 webMvc 开发之外的东西
wellR
2023-04-15 22:16:57 +08:00
SpringBoot 用了这么久了,才知道 SpringApplication.run 返回了 ApplicationContext ,哈
fzdwx
2023-04-15 23:32:46 +08:00
以前我也写过一个类似的哈哈 https://github.com/fzdwx/sky
lsk569937453
2023-04-15 23:35:57 +08:00
不要加 springboot-web 的 starter,直接用 springboot 的 CommandLineRunner 里运行 netty 即可啊
silentsky
2023-04-16 00:29:13 +08:00
这么巧 今天我也在整这个
zhiyu1998
2023-04-16 01:26:52 +08:00
学到了,感谢分享
dreamlike
2023-04-16 03:14:20 +08:00
🤔你这样写其实没有把 server 的生命周期托管给 spring 看起来,
wellR
2023-04-16 08:21:43 +08:00
@dreamlike 是的,这是野路子
niuroumian
2023-04-16 08:28:14 +08:00
没必要折腾,https://armeria.dev/
cnzjl
2023-04-16 09:06:45 +08:00
用 Smartlifecycle ,不添加 web 依赖
dreamlike
2023-04-16 11:58:31 +08:00
等下 我看了你这个代码 其实就是搞了一个基于 netty 的 http 服务器,我建议直接换 vertx or quarkus 吧
LeegoYih
2023-04-16 12:04:29 +08:00
这样写不就好了?

SpringApplication application = new SpringApplication(NettyServer.class);
application.setApplicationContextFactory(ApplicationContextFactory.ofContextClass(AnnotationConfigApplicationContext.class));
application.run(args);
LeegoYih
2023-04-16 12:16:29 +08:00
new SpringApplicationBuilder(ChatServerApplication.class)
.web(WebApplicationType.NONE)
.run(args);
WispZhan
2023-04-16 16:37:20 +08:00
不理解,为什么不直接用 vert.x 之类的?
wellR
2023-04-16 18:21:23 +08:00
@dreamlike 是的,要做一个异步实时响应的服务。webflux 不好上手,请问 vertx 的使用体验如何,与 webflux 比怎么样
wellR
2023-04-16 18:29:15 +08:00
大体流程:
1 、客户端发送请求,参数中有业务数据、超时时间与回调接口;
2 、服务 A 收到请求,初步处理 task 再发 mq
3 、服务 B 消费 mq 进行一系列比较耗时的操作之后将处理结果发到 mq 广播消息
4 、服务 A 消费 mq 响应客户端
5 、若上述处理流程中超时了,则立即返回客户端,后台 task 跑完了会调客户端的回调
dreamlike
2023-04-16 19:15:44 +08:00
@wellR 我看这个需求很简单 也没有什么复杂的逻辑 直接 vertx 上吧 又轻又快 用好 eventbus ,开发速度应该是爆杀 webflux 的,开 n 个 vertical 跑满核心就行了

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

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

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

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

© 2021 V2EX