Netty原理和使用
Netty是一个高性能 事件驱动的异步的非堵塞的IO(NIO)框架,用于建立TCP等底层的连接,基于Netty可以建立高性能的Http服务器。支持HTTP、 WebSocket 、Protobuf、 Binary TCP |和UDP,Netty已经被很多高性能项目作为其Socket底层基础,如HornetQ Infinispan Vert.x
Play Framework Finangle和 Cassandra。其竞争对手是:Apache MINA和 Grizzly。
传统堵塞的IO读取如下:
InputStream is = new FileInputStream("input.bin");
int byte = is.read(); // 当前线程等待结果到达直至错误
而使用NIO如下:
while (true) {
selector.select(); // 从多个通道请求事件
Iterator it = selector.selectedKeys().iterator();
while (it.hasNext()) {
SelectorKey key = (SelectionKey) it.next();
handleKey(key);
it.remove();
}
}
堵塞与非堵塞原理
传统硬件的堵塞如下,从内存中读取数据,然后写到磁盘,而CPU一直等到磁盘写完成,磁盘的写操作是慢的,这段时间CPU被堵塞不能发挥效率。
使用非堵塞的DMA如下图:CPU只是发出写操作这样的指令,做一些初始化工作,DMA具体执行,从内存中读取数据,然后写到磁盘,当完成写后发出一个中断事件给CPU。这段时间CPU是空闲的,可以做别的事情。这个原理称为Zero.copy零拷贝。
Netty底层基于上述Java NIO的零拷贝原理实现:
比较
- Tomcat是一个Web服务器,它是采取一个请求一个线程,当有1000客户端时,会耗费很多内存。通常一个线程将花费 256kb到1mb的stack空间。
- Node.js是一个线程服务于所有请求,在错误处理上有限制
- Netty是一个线程服务于很多请求,如下图,当从Java NIO获得一个Selector事件,将激活通道Channel。
演示
Netty的使用代码如下:
Channel channel = ...
ChannelFuture cf = channel.write(data);
cf.addListener(
new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if(!future.isSuccess() {
future.cause().printStacktrace();
...
}
...
}
});
...
cf.sync();
通过引入观察者监听,当有数据时,将自动激活监听者中的代码运行。
我们使用Netty建立一个服务器代码:
public class EchoServer {
private final int port;
public EchoServer(int port) {
this.port = port;
}
public void run() throws Exception {
// Configure the server.
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(bossGroup, workerGroup).channel(NioServerSocketChannel.class).option(ChannelOption.SO_BACKLOG, 100)
.handler(new LoggingHandler(LogLevel.INFO)).childHandler(newChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(
// new LoggingHandler(LogLevel.INFO),
new EchoServerHandler());
}
});
// Start the server.
ChannelFuture f = b.bind(port).sync();
// Wait until the server socket is closed.
f.channel().closeFuture().sync();
} finally {
// Shut down all event loops to terminate all threads.
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
这段代码调用:在9999端口启动
new EchoServer(9999).run();
我们需要完成的代码是EchoServerHandler:
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
private static final Logger logger = Logger.getLogger(EchoServerHandler.class.getName());
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ctx.write(msg);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
ctx.close();
}
}
原理
一个Netty服务器的原理如下:
图中每次请求的读取是通过UpStream来实现,然后激活我们的服务逻辑如EchoServerHandler,而服务器向外写数据,也就是响应是通过DownStream实现的。每个通道Channel包含一对UpStream和DownStream,以及我们的handlers(EchoServerHandler),如下图,这些都是通过channel pipeline封装起来的,数据流在管道里流动,每个Socket对应一个ChannelPipeline。
CHANNELPIPELINE是关键,它类似Unix的管道,有以下作用:
- 为每个Channel 保留 ChannelHandlers ,如EchoServerHandler
- 所有的事件都要通过它
- 不断地修改:类似unix的SH管道: echo "Netty is shit...." | sed -e 's/is /is the /'
- 一个Channel对应一个 ChannelPipeline
- 包含协议编码解码 安全验证SSL/TLS和应用逻辑
客户端代码
前面我们演示了服务器端代码,下面是客户端代码:
public class EchoClient {
private final String host;
private final int port;
private final int firstMessageSize;
public EchoClient(String host, int port, int firstMessageSize) {
this.host = host;
this.port = port;
this.firstMessageSize = firstMessageSize;
}
public void run() throws Exception {
// Configure the client.
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).option(ChannelOption.TCP_NODELAY, true).handler(new ChannelInitializer<SocketChannel>() {
@Override
public void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(
// new LoggingHandler(LogLevel.INFO),
new EchoClientHandler(firstMessageSize));
}
});
// Start the client.
ChannelFuture f = b.connect(host, port).sync();
// Wait until the connection is closed.
f.channel().closeFuture().sync();
} finally {
// Shut down the event loop to terminate all threads.
group.shutdownGracefully();
}
}
}
客户端的应用逻辑EchoClientHandler:
public class EchoClientHandler extends ChannelInboundHandlerAdapter {
private static final Logger logger = Logger.getLogger(EchoClientHandler.class.getName());
private final ByteBuf firstMessage;
/**
* Creates a client-side handler.
*/
public EchoClientHandler(int firstMessageSize) {
if (firstMessageSize <= 0) {
throw new IllegalArgumentException("firstMessageSize: " + firstMessageSize);
}
firstMessage = Unpooled.buffer(firstMessageSize);
for (int i = 0; i < firstMessage.capacity(); i++) {
firstMessage.writeByte((byte) i);
}
}
@Override
public void channelActive(ChannelHandlerContext ctx) {
ctx.writeAndFlush(firstMessage);
System.out.print("active");
}
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
ctx.write(msg);
System.out.print("read");
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
System.out.print("readok");
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
// Close the connection when an exception is raised.
logger.log(Level.WARNING, "Unexpected exception from downstream.", cause);
ctx.close();
}
}
from: https://www.jdon.com/concurrent/netty.html
Netty原理和使用相关推荐
- 精尽 Netty 原理与源码专栏( 已经完成 61+ 篇,预计总共 70+ 篇 )
只更新在笔者的知识星球,欢迎加入一起讨论 Netty 源码与实现. 目前已经有 1000+ 位球友加入- 进度:已经完成 60+ 篇,预计总共 70+ 篇,完成度 90% . 对应 Netty 版本号 ...
- (高级)Dubbo 第五章 Dubbo及RocketMQ底层-Netty原理
Netty原理 Netty 是一个高性能.异步事件驱动的NIO 框架,基于JAVA NIO 提供的API 实现.它提供了对TCP.UDP 和文件传输的支持,作为一个异步NIO 框架,Netty 的所有 ...
- netty 进程挂起_这可能是目前最透彻的Netty原理架构解析
本文基于 Netty 4.1 展开介绍相关理论模型,使用场景,基本组件.整体架构,知其然且知其所以然,希望给大家在实际开发实践.学习开源项目方面提供参考. Netty 是一个异步事件驱动的网络应用程序 ...
- Netty原理五:ChannelFuture、DefaultChannelPromise对象解析
文章目录 1. 前言 2. 原理解析 2.1 ChannelFuture 调用 sync() 的作用 2.2 Channel 调用的 closeFuture() 是什么 1. 前言 学习Netty的时 ...
- Netty原理三:NioEventLoop如何处理客户端连接
文章目录 前言 原理解析 总结 前言 Netty服务端存在类型为 NioEventLoopGroup 的 Boss 和 Worker,Boss 接收到客户端连接后,将客户端 Channel 注册到 W ...
- Netty原理一:ServerBootstrap启动过程全解析
文章目录 前言 原理解析 问题扩展 前言 Netty 体系庞大而复杂,所以会分成几篇文章进行讲解.深入理解需要有一定的基础和耐心,参照源码反复推敲才行 为了方便阅读,本篇的源码部分只保留核心部分,比较 ...
- Netty原理架构解析
本文基于 Netty 4.1 展开介绍相关理论模型,使用场景,基本组件.整体架构,知其然且知其所以然,希望给大家在实际开发实践.学习开源项目方面提供参考. Netty 是一个异步事件驱动的网络应用程序 ...
- 这可能是目前最透彻的Netty原理架构解析
转载自:https://www.toutiao.com/i6620280257846968840/?tt_from=weixin&utm_campaign=client_share&w ...
- 初探Netty:Netty原理、核心组件、数据容器以及运行机制
Netty 是一个异步事件驱动的通信框架,可用于搭建高性能协议服务器和客户端. 一.NIO Selector机制是NIO的核心: 当客户端请求时,就创建一个scoketChannel,并注册到Sele ...
最新文章
- 开局一段扯,数据全靠编?真被一篇“神论文”气到了
- Fiddler无法抓取HTTPS的问题,Fiddler证书无法安装终极解决方案,
- C/C++ 中的0长数组(柔性数组)
- 怎样快速识别 英文地址中包含非英文字符_[论文笔记]端到端的场景文本识别算法--CRNN 论文笔记...
- 机器学习第五篇:详解决策树-CART算法
- c语言程序设计的一般错误的是,《C语言程序设计》第十章 程序常见错误分析.pdf...
- codeforces C. Vanya and Scales
- 成绩出来,C++专业课没问题
- 【Android Fragment】解决Fragment多层嵌套时onActivityResult无法正确回调的问题
- 交叉编译iproute2
- from scipy.misc import imread 报错原因
- Domino 邮箱服务器接收不存在的邮箱账号的邮件
- ros路由器数据包工作流程
- 烽火2640路由器命令行手册-01-基础配置命令
- Java小游戏实操---大鱼吃小鱼 游戏开发
- ADODB.Recordset 错误 #x27;800a0bb9#x27; 参数类型不正确,或不在可以接受的范围之内,或与其他参数冲突。
- 华清远见上海中心22071班
- 这款Shadertoy转换器,太牛逼了!
- 28计算机表演赛获奖情况,20届无锡地区计算机表演赛获奖名单
- 硅谷码农35岁危机:Java之父也找不到工作
热门文章
- solr/lucence和关系数据库的混合使用
- Understanding Spring Web Application Architecture: The Classic Way--转载
- 金融风控实战——有监督分箱
- Python中最常用十大图像处理库详细介绍
- 进入正在运行的Docker容器的4种方式
- 源自Google、Facebook、Netflix和Cisco的10款开源安全工具很值得回味
- Java Review - 并发编程_Unsafe
- Android接收短信-createFromPdu
- Imageloader3-单例模式
- Android-上传图片(-)_HttpURLConnection