反应器应用的网络编程
写在前面
在netty使用了reactor的线程模型(或者叫做工作模式)。本文就一起来看下其是如何使用的。
1:不同的rector对应的不同的编码方式
首先是rector的单线程模型,对应到netty中的编码方式如下:
// 这里的1,就是rector的单线程模型中一个线程的"1"
NioEventLoopGroup eventExecutors = new NioEventLoopGroup(1);
// 定义启动类,并将rector模型设置到启动类中
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(eventExecutors);
非主从多线程版本:
// 这里不设置线程数,线程数会自动根据核数指定(一般多核,所以肯定大于1),就是rector的多线程模型了,当然你可以显式指定线程数量
NioEventLoopGroup eventExecutors = new NioEventLoopGroup();
// 定义启动类,并将rector模型设置到启动类中
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(eventExecutors);
主从多线程版本:
// reactor主从模式中的主
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
// reactor主从模式中的从
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
// 定义启动类,并将rector模型设置到启动类中
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup);
2:netty对reactor主从模式支持源码分析
netty想要支持reactor,需要做的工作其实就是将主reactor绑定到ServerSocketChannel,将从reactor绑定到SocketChannel中就行了,即让主reactor负责基于ServerSocketChannel的接收连接的工作,而让从reactor负责基于SocketChannel的数据读写工作。
debug使用的代码:
// reactor主从模式中的主
NioEventLoopGroup bossGroup = new NioEventLoopGroup();
// reactor主从模式中的从
NioEventLoopGroup workerGroup = new NioEventLoopGroup();
// 定义启动类,并将rector模型设置到启动类中
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup);
在serverBootstrap.group(bossGroup, workerGroup);
中完成绑定的工作。
2.1:主reactor绑定到ServerSocketChannel
// io.netty.bootstrap.ServerBootstrap#group(io.netty.channel.EventLoopGroup, io.netty.channel.EventLoopGroup)
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
// 调用父类group方法
super.group(parentGroup);
...
return this;
}
// io.netty.bootstrap.AbstractBootstrap#group(io.netty.channel.EventLoopGroup)
public B group(EventLoopGroup group) {
...
// volatile EventLoopGroup group; 设置到局部变量
this.group = group;
return self();
}
io.netty.bootstrap.AbstractBootstrap#group()
// 该方法负责读取到上一步设置的局部变量
public final EventLoopGroup group() {
return group;
}
// io.netty.bootstrap.AbstractBootstrap#initAndRegister
final ChannelFuture initAndRegister() {
...
ChannelFuture regFuture = config().group().register(channel);
...
return regFuture;
}
ChannelFuture regFuture = config().group().register(channel);
这里的channel类是netty的channel底层就是Java nio的ServerSocketChannel了,通过register方法也就完成了绑定。
2.2:从reactor绑定到SocketChannel
从reactor绑定到SocketChannel需要依赖于ServerSocketChannel,因为SocketChannel的创建是由ServerSocketChannel来完成的,首先看代码:
// io.netty.bootstrap.ServerBootstrap#group(io.netty.channel.EventLoopGroup, io.netty.channel.EventLoopGroup)
public ServerBootstrap group(EventLoopGroup parentGroup, EventLoopGroup childGroup) {
...
// 赋值从reactor到childGroup
this.childGroup = childGroup;
return this;
}
// io.netty.bootstrap.ServerBootstrap#init
void init(Channel channel) throws Exception {
...
// 又换了个名字!
final EventLoopGroup currentChildGroup = childGroup;
p.addLast(new ChannelInitializer<Channel>() {
@Override
public void initChannel(final Channel ch) throws Exception {
...
ch.eventLoop().execute(new Runnable() {
@Override
public void run() {
// 这里创建ServerBootstrapAcceptor,将从reactor作为参数传了进去
pipeline.addLast(new ServerBootstrapAcceptor(
ch, currentChildGroup, currentChildHandler, currentChildOptions, currentChildAttrs));
}
});
}
// io.netty.bootstrap.ServerBootstrap.ServerBootstrapAcceptor#channelRead
public void channelRead(ChannelHandlerContext ctx, Object msg) {
// 这里的msg就是socketchannel,所以这里可以强转
final Channel child = (Channel) msg;
childGroup.register(child).addListener(new ChannelFutureListener() {
...
}
childGroup.register(child)
这里就完成绑定了,需要注意,只有在第一次读取客户端数据时才会执行到这里。
3:main reactor为什么只会用到线程组中的一个线程
前面我们分析了main rector是如何帮i当道ServerSocketChannel中的,在如下位置:
可以看到这个绑定是在bind端口时,即代码b.bind(PORT).sync();
执行的,因为只会绑定到一个端口,所以使用一个线程也就足够了,多了也没有什么意义。所以只会用到线程组中一个线程的原因是只会绑定一个端口号。所以啊,就要从这一组线程中选出一个线程来,如何选的呢?接着绑定逻辑来看源码:
// io.netty.channel.MultithreadEventLoopGroup#register(io.netty.channel.Channel)
public ChannelFuture register(Channel channel) {
return next().register(channel);
}
这里的next方法就是来完成选择工作的:
// io.netty.util.concurrent.MultithreadEventExecutorGroup#next
public EventExecutor next() {
// private final EventExecutorChooserFactory.EventExecutorChooser chooser
return chooser.next();
}
choose有两个实现类:
private static final class PowerOfTwoEventExecutorChooser implements EventExecutorChooser {
private final AtomicInteger idx = new AtomicInteger();
private final EventExecutor[] executors;
PowerOfTwoEventExecutorChooser(EventExecutor[] executors) {
this.executors = executors;
}
@Override
public EventExecutor next() {
// 这种与的方式选择一个效率要由于取余的方式,一般我们都是采用取余的方式,但netty追求极致性能
// 但要求executors总数是2的次幂(另外要注意,-的优先级高于&,我觉得写成这样子更清晰)
// executors[idx.getAndIncrement() & (executors.length - 1)]
return executors[idx.getAndIncrement() & executors.length - 1];
}
}
private static final class GenericEventExecutorChooser implements EventExecutorChooser {
private final AtomicInteger idx = new AtomicInteger();
private final EventExecutor[] executors;
GenericEventExecutorChooser(EventExecutor[] executors) {
this.executors = executors;
}
@Override
public EventExecutor next() {
// 取余选择,效率就要低于位运算的方式了
return executors[Math.abs(idx.getAndIncrement() % executors.length)];
}
}
PowerOfTwoEventExecutorChooser采用位运算方式选出一个来效率更高,计算如下:
executors总数4(注意-的优先级高于&)
0 & 4 - 1 = 00000000 & 00000011 = 0(十进制)
1 & 4 - 1 = 00000001 & 00000011 = 1(十进制)
2 & 4 - 1 = 00000010 & 00000011 = 2(十进制)
3 & 4 - 1 = 00000011 & 00000011 = 3(十进制)
4 & 4 - 1 = 00000100 & 00000011 = 0(十进制)
循环了。。。
那么,直接让用户设置一个不就好了吗?为什么还要设置线程组呢?我想这是因为netty为了降低编码的复杂度,从而使得主reactor和从reactor使用相同的编码方式,而底层的差异性就由netty来解决了,所以不得不说netty是一个很优秀的框架啊!
写在后面
参考文章列表
什么是reactor以及其三种版本 。
下一篇: JavaScript 高频
推荐阅读
-
反应器应用的网络编程
-
一个简单的网络摄像头应用程序 6
-
网络编程 (5) - 仿真伪闭包实现连接的安全回收
-
网络爬虫是做什么的?有哪些应用场景?
-
分析Tars-Java的网络编程源码
-
Java 8新特性探究(十三)JavaFX 8新特性以及开发2048游戏-JavaFX历史## 跟java在服务器端和web端成绩相比,桌面一直是java的软肋,于是Sun公司在2008年推出JavaFX,弥补桌面软件的缺陷,请看下图JavaFX一路走过来的改进 从上图看出,一开始推出时候,开发者需使用一种名为JavaFX Script的静态的、声明式的编程语言来开发JavaFX应用程序。因为JavaFX Script将会被编译为Java bytecode,程序员可以使用Java代码代替。 JavaFX 2.0之后的版本摒弃了JavaFX Script语言,而作为一个Java API来使用。因此使用JavaFX平台实现的应用程序将直接通过标准Java代码来实现。 JavaFX 2.0 包含非常丰富的 UI 控件、图形和多媒体特性用于简化可视化应用的开发,WebView可直接在应用中嵌入网页;另外 2.0 版本允许使用 FXML 进行 UI 定义,这是一个脚本化基于 XML 的标识语言。 从JDK 7u6开始,JavaFx就与JDK捆绑在一起了,JavaFX团队称,下一个版本将是8.0,目前所有的工作都已经围绕8.0库进行。这是因为JavaFX将捆绑在Java 8中,因此该团队决定跳过几个版本号,迎头赶上Java 8。 ##JavaFx8的新特性 ## ###全新现代主题:Modena 新的Modena主题来替换原来的Caspian主题。不过在Application的start方法中,可以通过setUserAgentStylesheet(STYLESHEET_CASPIAN)来继续使用Caspian主题。 参考http://fxexperience.com/2013/03/modena-theme-update/ ###JavaFX 3D 在JavaFX8中提供了3D图像处理API,包括Shape3D (Box, Cylinder, MeshView, Sphere子类),SubScene, Material, PickResult, LightBase (AmbientLight 和PointLight子类),SceneAntialiasing等。Camera类也得到了更新。从JavaDoc中可以找到更多信息。 ###富文本 强化了富文本的支持 ###TreeTableView ###日期控件DatePicker 增加日期控件 ###用于 CSS 结构的公共 API
-
构建自己的应用: JAVA编程思想导论(猜字谜游戏1.0)- 系列三
-
基于FNN网络的电影评论情感分析在自然语言处理(NLP)中的应用
-
使用Matlab进行LSTM-Adaboost-ABKDE集成学习:长短期记忆神经网络与自适应带宽核密度估计在多变量回归中的区间预测应用
-
黄金法则:在Java编程中的应用