Netty 4.0 多端口不同协议每个端口 [英] Netty 4.0 multi port with difference protocol each port

查看:191
本文介绍了Netty 4.0 多端口不同协议每个端口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想 netty 是我所知道的最好的 Java 网络框架,在阅读并尝试了一些示例后,我有疑问:

I guess netty is best java networking framework ever i know, after reading and try some sample i have question:

1.使用 netty 4.0 为不同协议的多端口创建网络服务器的最佳方法是什么?

每个服务器创建:

EventLoopGroup bossGroup = new NioEventLoopGroup();//(1)

EventLoopGroup bossGroup = new NioEventLoopGroup(); // (1)

EventLoopGroup workerGroup = new NioEventLoopGroup();

EventLoopGroup workerGroup = new NioEventLoopGroup();

ServerBootstrap b = new ServerBootstrap();//(2)

ServerBootstrap b = new ServerBootstrap(); // (2)

在线程内运行的每个服务器

Each Server Running Inside Thread

这样对吗?

2.Websocket 服务器

如何为跨源案例保护 Websocket 服务器?我没有任何参考资料

How to securing Websocket Server for Cross origin case? I don't have any reference about it

非常感谢您的帮助,

问候

BC,

推荐答案

正如 Norman 所说,重要的是您需要共享事件循环组,这样您就不会创建太多线程.只要您共享事件循环组,您就可以根据需要创建任意数量的 ServerBootstrap:

As Norman said, the important thing is that you need to share the event loop groups so that you do not create way too many threads. As long as you share the event loop groups, you can create as many ServerBootstraps as you wish:

EventLoopGroup bossGroup = new NioEventLoopGroup(numBossThreads);
EventLoopGroup workerGroup = new NioEventLoopGroup(numWorkerThreads);

ServerBootstrap sb1 = new ServerBootstrap();
sb1.group(bossGroup, workerGroup);
...
sb1.bind();

ServerBootstrap sb2 = new ServerBootstrap();
sb2.group(bossGroup, workerGroup);
...
sb2.bind();

ServerBootstrap sb3 = new ServerBootstrap();
sb3.group(bossGroup, workerGroup);
...
sb3.bind();

bossGroup 用于接受传入的连接,workerGroup 用于处理bossGroup 接受的连接.请做一些性能测试并指定最佳的numBossThreadsnumWorkerThreads.

The bossGroup is used to accept the incoming connections, and the workerGroup is used to handle the connections accepted by the bossGroup. Please do some performance tests and specify the optimal numBossThreads and numWorkerThreads.

这篇关于Netty 4.0 多端口不同协议每个端口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆