Netty实战
一、Netty异步和事件驱动
1.Java网络编程回顾
socket.accept 阻塞
socket.setsockopt /非阻塞
2.NIO异步非阻塞
a).nio 非阻塞的关键时使用选择器(java.nio.channels.Selector)来实现;可以监控多个socket读写的完成状态来协调其他socket的读写,以提高资源使用率;
b).异步事件驱动,socket请求发起,立即响应,后端执行处理请求,处理完毕通知客户端;
c).Channel 链接实体的连接,请求和响应的载体;Netty通过回调来处理事件,时间触发->ChannelHandler->channelActive()来处理事件;
d).ChannelFuture通过ChannelFutureListener来监听事件提供通知处理完成的结果;Future是需要手动去获取结果;每个I/O操作都会立即返回一个ChannelFuture,不会阻塞,后台处理I/O,至于何时处理完成,异步时间驱动通知;
3.Nttey的异步变成模型建立在Future和回调的基础之上,再将时间派发到ChannelHandler之上进行处理;
二、Netty Demo
1.EchoServerHandler
package chap01; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.util.CharsetUtil; @ChannelHandler.Sharable
public class EchoServerHandler extends ChannelInboundHandlerAdapter {
@Override
public void channelRead(ChannelHandlerContext ctx,Object msg){
ByteBuf in = (ByteBuf) msg;
System.out.println("Server received: "+ in.toString(CharsetUtil.UTF_8));
ctx.write(in);
}
@Override
public void channelReadComplete(ChannelHandlerContext ctx){
ctx.writeAndFlush(Unpooled.EMPTY_BUFFER).addListener(ChannelFutureListener.CLOSE);
}
@Override
public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause){
cause.printStackTrace();
ctx.close();
}
}
2.EchoServer
package chap01; import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel; import java.net.InetSocketAddress; public class EchoServer {
private final int port; public EchoServer(int port){
this.port = port;
} public static void main(String[] args) throws Exception{
// if (args.length !=-1){
// System.err.println("Usage: " + EchoServer.class.getSimpleName()+"<port>");
// }
// int port = Integer.parseInt(args[0]);
// new EchoServer(port).start();
new EchoServer(9001).start();
} public void start() throws Exception{
final EchoServerHandler serverHandler = new EchoServerHandler();
EventLoopGroup group = new NioEventLoopGroup();
try {
ServerBootstrap b = new ServerBootstrap();
b.group(group).channel(NioServerSocketChannel.class).localAddress(new InetSocketAddress(port)).childHandler(new ChannelInitializer<SocketChannel>(){
@Override
public void initChannel(SocketChannel ch) throws Exception{
ch.pipeline().addLast(serverHandler);
}
});
ChannelFuture f = b.bind().sync();
f.channel().closeFuture().sync();
}finally {
group.shutdownGracefully().sync();
}
}
}
3.EchoClientHandler
package chap01; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil; public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
@Override
public void channelActive(ChannelHandlerContext ctx){
ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8));
} @Override
public void channelRead0(ChannelHandlerContext ctx,ByteBuf in){
System.out.println("Client received: "+in.toString(CharsetUtil.UTF_8));
} @Override
public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause){
cause.printStackTrace();
ctx.close();
} }
4.EchoClient
package chap01; import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel; import java.net.InetSocketAddress; public class EchoClient {
private final String host;
private final int port; public EchoClient(String host,int port){
this.host = host;
this.port = port;
} public void start() throws Exception{
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap b = new Bootstrap();
b.group(group).channel(NioSocketChannel.class).remoteAddress(new InetSocketAddress(host,port)).handler(new ChannelInitializer<SocketChannel>(){
@Override
public void initChannel(SocketChannel ch) throws Exception{
ch.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture f = b.connect().sync();
}finally {
group.shutdownGracefully().sync();
}
} public static void main(String[] args) throws Exception{
// if (args.length!=2){
// System.err.println("Usage: "+EchoClient.class.getSimpleName() + "<host><port>");
// return;
// }
// String host = args[0];
// int port = Integer.parseInt(args[1]);
// new EchoClient(host,port).start();
new EchoClient("127.0.0.1",9001).start();
}
}
三、Netty组件和设计
1.Channel、Event Loop和ChannelFuture
a).Channel 即Socket,提供基本的I/O操作,bind(),connet(),read(),write();Event Loop控制流的管理,和并发,Channel生命周期只和一个Event Loop进行绑定,多个EventLoop组成一个Event LoopGroup;Channel Future用以接收回调,Netty中所有的处理都是异步的,每个请求可能不会立即返回,addListener()方法会添加一个监听器ChannelFutureListener用来回调通知;
2.ChannelHandler、ChannelPipeLine
a).ChannelHandler 用于接受入站事件和数据;常用ChannelInBoundHandler;
b).ChannelPipeLine 提供了Channel Handler链的容器,一个Channel被创建时,她会被ChannelHandler安装到ChannelPipeLine中;
Netty实战的更多相关文章
- 《Netty实战》源码运行及本地环境搭建
1.源码路径: GitHub - zzzvvvxxxd/netty-in-action-cn: Netty In Action 中文版 ,中文唯一正版<Netty实战>的代码清单 下载后 ...
- 1.Netty 实战前言
1.参考文档:Netty实战精髓篇 2.Netty介绍: Netty是基于Java NIO的网络应用框架. Netty是一个NIO client-server(客户端服务器)框架,使用Nett ...
- 重磅!阿里P8费心整理Netty实战+指南+项目白皮书PDF,总计1.08G
前言 Netty是一款用于快速开发高性能的网络应用程序的Java框架.它封装了网络编程的复杂性,使网络编程和Web技术的最新进展能够被比以往更广泛的开发人员接触到. Netty不只是一个接口和类的集合 ...
- 1、Netty 实战入门详解
一.Netty 简介 Netty 是基于 Java NIO 的异步事件驱动的网络应用框架,使用 Netty 可以快速开发网络应用,Netty 提供了高层次的抽象来简化 TCP 和 UDP 服务器的编程 ...
- Netty实战入门详解——让你彻底记住什么是Netty(看不懂你来找我)
一.Netty 简介 Netty 是基于 Java NIO 的异步事件驱动的网络应用框架,使用 Netty 可以快速开发网络应用,Netty 提供了高层次的抽象来简化 TCP 和 UDP 服务器的编程 ...
- 「Netty实战 02」手把手教你实现自己的第一个 Netty 应用!新手也能搞懂!
大家好,我是 「后端技术进阶」 作者,一个热爱技术的少年. 很多小伙伴搞不清楚为啥要学习 Netty ,今天这篇文章开始之前,简单说一下自己的看法: @ 目录 服务端 创建服务端 自定义服务端 Cha ...
- Netty实战十四之案例研究(一)
1.Droplr——构建移动服务 Bruno de Carvalho,首席架构师 在Droplr,我们在我的基础设施的核心部分.从我们的API服务器到辅助服务的各个部分都使用了Netty. 这是一个关 ...
- Netty实战十三之使用UDP广播事件
1.UDP的基础知识 我们将会把重点放在一个无连接协议即用户数据报协议(UDP)上,它通常用在性能至关重要并且能够容忍一定的数据报丢失的情况下. 面向连接的传输(如TCP)管理了两个网络端点之间的连接 ...
- Netty实战十一之预置的ChannelHandler和编解码器
Netty为许多通用协议提供了编解码器和处理器,几乎可以开箱即用,这减少了你在那些相当繁琐的事务上本来会花费的时间与精力.我们将探讨这些工具以及它们所带来的好处,其中包括Netty对于SSL/TLS和 ...
- Netty实战十二之WebSocket
如果你有跟进Web技术的最新进展,你很可能就遇到过“实时Web”这个短语,这里并不是指所谓的硬实时服务质量(QoS),硬实时服务质量是保证计算结果将在指定的时间间隔内被递交.仅HTTP的请求/响应模式 ...
随机推荐
- MySQL索引,MySQL中索引的限制?
MySQL中索引的限制: 1.MyISAM存储引擎引键的长度综合不能超过1000字节: 2.BLOB和TEXT类型的列只能创建前缀索引: 3.MySQL目前不支持函数索引: 4.使用!= 或者< ...
- Delphi Modbus RTU CRC16校验码
function CheckCrc16(const ABuf; ALen: Integer): Boolean;var uwTemp: WORD; i, j: BYTE; P: PByte;begin ...
- xcode 6 exporting ipa 提示 Your account already has a valid iOS distribution certificate 的另一种解决方法
背景: 1. XCode 6.1 2. 证书:develop 证书 3. Scheme 为Device 操作: 在Product - Archive 包过程中,选择Save for Ad hoc De ...
- sed命令使用示例
sed -i '/mirrorlist/d' CentOS-Base-163.repo 把有mirrorlist的行删除sed -i '/\[addons\]/,/^$/d' CentOS-Base- ...
- apache2.2 到 2.4后配置文件需要更改的部分
参考: http://www.dotblogs.com.tw/maple ... e24_httpd_conf.aspx 1. 访问控制2.2 的时候Order deny,allowDeny fro ...
- ylb: 数据库操作方法基础
ylbtech-SQL Server:SQL Server-数据库操作方法基础 数据库操作方法基础. ylb: 数据库操作方法基础 返回顶部 ----------试图操作(view)--------- ...
- 如何给JQ的ajax方法中的success()传入参数?
当时在使用JQuery提供的Ajax技术的时候,我有个需求,就是要给它请求成功后调用的success()方法传入参数: 所以,我就直接这样子写了: <script> function ge ...
- django 修改默认的user表和默认的认证系统
django的功能非常强大,但是自带的user表很多情况下并不满足我们的需求,因此我们需要修改其默认的user表,并且把用username登录改成用email登录 第一步,创建自己的user表,在创建 ...
- java根据身份证号和获取用户年龄和性别的工具类
import java.text.SimpleDateFormat; import java.util.Date; import java.util.HashMap; import java.util ...
- es6 - array for-chrome
// 去重复 Array.from(new Set([1, 1, 2, 3])); // [1, 2, 3] console.log(Array.from(new Set([1, 1, 2, 3])) ...