学习这件事,不在乎有没有人督促你,最重要的是在于你自己有没有觉悟和恒心。
温习中,会以笔记的形式记录下自我学习的过程.
文章部分图片来源于视频笔记!!非我自画!!
netty的自我学习(一)—BIO、NIO、AIO的简单介绍netty的自我学习(四)—NIO的Selector(选择器)
异步模型
-
表示异步的执行结果, 可以通过它提供的方法来检测执行是否完成,比如检索计算等等.
-
ChannelFuture 是一个接口 : public interface ChannelFuture extends Future我们可以添加监听器,当监听的事件发生时,就会通知到监听器。
在使用 Netty 进行编程时,拦截操作和转换出入站数据只需要您提供 callback 或利用future 即可。这使得链式操作简单、高效, 并有利于编写可重用的、通用的代码。
Future-Listener 机制
-
当 Future 对象刚刚创建时,处于非完成状态,调用者可以通过返回的 ChannelFuture 来获取操作执行的状态,注册监听函数来执行完成后的操作。
-
常见有如下操作
通过 isDone 方法来判断当前操作是否完成; 通过 isSuccess 方法来判断已完成的当前操作是否成功; 通过 getCause 方法来获取已完成的当前操作失败的原因; 通过 isCancelled 方法来判断已完成的当前操作是否被取消; 通过 addListener 方法来注册监听器,当操作已完成(isDone 方法返回完成),将会通知指定的监听器;如果 Future 对象已完成,则通知指定的监听器
//绑定一个端口并且同步, 生成了一个 ChannelFuture 对象
//启动服务器(并绑定端口)
ChannelFuture cf = bootstrap.bind(6668).sync();
//给cf 注册监听器,监控我们关心的事件
cf.addListener(new ChannelFutureListener() {
@Override
public void operationComplete(ChannelFuture future) throws Exception {
if (cf.isSuccess()) {
System.out.println("监听端口 6668 成功");
} else {
System.out.println("监听端口 6668 失败");
}
}
});
小结
相比传统阻塞 I/O,执行 I/O 操作后线程会被阻塞住, 直到操作完成;异步处理的好处是不会造成线程阻塞,线程在 I/O 操作期间可以执行别的程序,在高并发情形下会更稳定和更高的吞吐量
http服务
要求:
- Netty 服务器在 8888 端口监听,浏览器发出请求 "http://localhost:8888/ "
- 服务器可以回复消息给客户端 "Hello! 我是服务器 5 " , 并对特定请求资源进行过滤.
package netty.netty.http;
import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;
public class TestServer {
public static void main(String[] args) throws Exception {
EventLoopGroup bossGroup = new NioEventLoopGroup(1);
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap serverBootstrap = new ServerBootstrap();
serverBootstrap.group(bossGroup, workerGroup).
channel(NioServerSocketChannel.class).
childHandler(new TestServerInitializer());
ChannelFuture channelFuture = serverBootstrap.bind(8888).sync();
channelFuture.channel().closeFuture().sync();
}finally {
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
}
package netty.netty.http;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpServerCodec;
public class TestServerInitializer extends ChannelInitializer<SocketChannel> {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
//得到管道,向管道加入处理器
ChannelPipeline pipeline = ch.pipeline();
//加入一个netty 提供的httpServerCodec codec =>[coder - decoder]
//HttpServerCodec 说明
//1. HttpServerCodec 是netty 提供的处理http的 编-解码器
pipeline.addLast("MyHttpServerCodec",new HttpServerCodec());
//2. 增加一个自定义的handler
pipeline.addLast("MyTestHttpServerHandler", new TestHttpServerHandler());
System.out.println("ok~~~~");
}
}
package netty.netty.http;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.handler.codec.http.*;
import io.netty.util.CharsetUtil;
import java.net.URI;
/*
说明
1. SimpleChannelInboundHandler 是 ChannelInboundHandlerAdapter
2. HttpObject 客户端和服务器端相互通讯的数据被封装成 HttpObject
*/
public class TestHttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {
//channelRead0 读取客户端数据
@Override
protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
System.out.println("对应的channel=" + ctx.channel() + " pipeline=" + ctx.pipeline() + " 通过pipeline获取channel" + ctx.pipeline().channel());
System.out.println("当前ctx的handler=" + ctx.handler());
//判断 msg 是不是 httprequest请求
if(msg instanceof HttpRequest) {
// 1 HttpObject 的真实类型 是DefaultHttpRequest
System.out.println("ctx 类型="+ctx.getClass());
System.out.println("pipeline hashcode" + ctx.pipeline().hashCode() + " TestHttpServerHandler hash=" + this.hashCode());
System.out.println("msg 类型=" + msg.getClass());
System.out.println("客户端地址" + ctx.channel().remoteAddress());
//获取到
HttpRequest httpRequest = (HttpRequest) msg;
/**********************注意 ********************************/
/**
* 如果不加此过滤,我们在浏览器访问 http://localhost:8888/ 的时候 会发送2次请求
* 那么我们服务端就会收到2次请求,其中一次是 http://localhost:8888/favicon.ico 的网站图标请求
* 这个请求,对于我们来说是多余的,所以需要过滤掉这个特定资源的响应
*/
URI uri = new URI(httpRequest.uri());
if("/favicon.ico".equals(uri.getPath())) {
System.out.println("请求了 favicon.ico, 不做响应");
return;
}
/******************* 结束***********************************/
//回复信息给浏览器 [http协议]
ByteBuf content = Unpooled.copiedBuffer("hello, 我是服务器", CharsetUtil.UTF_8);
//构造一个http的相应,即 httpresponse
FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK, content);
response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/plain");
response.headers().set(HttpHeaderNames.CONTENT_LENGTH, content.readableBytes());
//将构建好 response返回
ctx.writeAndFlush(response);
}
}
}
打印结果如下:
ok~~~~
ok~~~~
对应的channel=[id: 0x46ba7e64, L:/0:0:0:0:0:0:0:1:8888 - R:/0:0:0:0:0:0:0:1:56766] pipeline=DefaultChannelPipeline{(MyHttpServerCodec = io.netty.handler.codec.http.HttpServerCodec), (MyTestHttpServerHandler = netty.netty.http.TestHttpServerHandler)} 通过pipeline获取channel[id: 0x46ba7e64, L:/0:0:0:0:0:0:0:1:8888 - R:/0:0:0:0:0:0:0:1:56766]
当前ctx的handler=netty.netty.http.TestHttpServerHandler@22dc040a
ctx 类型=class io.netty.channel.DefaultChannelHandlerContext
pipeline hashcode365619740 TestHttpServerHandler hash=584844298
msg 类型=class io.netty.handler.codec.http.DefaultHttpRequest
客户端地址/0:0:0:0:0:0:0:1:56766
对应的channel=[id: 0x46ba7e64, L:/0:0:0:0:0:0:0:1:8888 - R:/0:0:0:0:0:0:0:1:56766] pipeline=DefaultChannelPipeline{(MyHttpServerCodec = io.netty.handler.codec.http.HttpServerCodec), (MyTestHttpServerHandler = netty.netty.http.TestHttpServerHandler)} 通过pipeline获取channel[id: 0x46ba7e64, L:/0:0:0:0:0:0:0:1:8888 - R:/0:0:0:0:0:0:0:1:56766]
当前ctx的handler=netty.netty.http.TestHttpServerHandler@22dc040a
对应的channel=[id: 0x46ba7e64, L:/0:0:0:0:0:0:0:1:8888 - R:/0:0:0:0:0:0:0:1:56766] pipeline=DefaultChannelPipeline{(MyHttpServerCodec = io.netty.handler.codec.http.HttpServerCodec), (MyTestHttpServerHandler = netty.netty.http.TestHttpServerHandler)} 通过pipeline获取channel[id: 0x46ba7e64, L:/0:0:0:0:0:0:0:1:8888 - R:/0:0:0:0:0:0:0:1:56766]
当前ctx的handler=netty.netty.http.TestHttpServerHandler@22dc040a
ctx 类型=class io.netty.channel.DefaultChannelHandlerContext
pipeline hashcode365619740 TestHttpServerHandler hash=584844298
msg 类型=class io.netty.handler.codec.http.DefaultHttpRequest
客户端地址/0:0:0:0:0:0:0:1:56766
请求了 favicon.ico, 不做响应
对应的channel=[id: 0x46ba7e64, L:/0:0:0:0:0:0:0:1:8888 - R:/0:0:0:0:0:0:0:1:56766] pipeline=DefaultChannelPipeline{(MyHttpServerCodec = io.netty.handler.codec.http.HttpServerCodec), (MyTestHttpServerHandler = netty.netty.http.TestHttpServerHandler)} 通过pipeline获取channel[id: 0x46ba7e64, L:/0:0:0:0:0:0:0:1:8888 - R:/0:0:0:0:0:0:0:1:56766]
当前ctx的handler=netty.netty.http.TestHttpServerHandler@22dc040a
简单说明:
-
HttpObject 是是DefaultHttpRequest 类型的
-
浏览器请求的时候会生成一个对应hanlder,如果另外一个浏览器请求的时候,也会生成对应的一个handler,2个浏览器的请求生成不同的handler。 但是同一个浏览器,2个请求窗口也是不同的handler. 以下是同一个浏览器,不同窗口请求的
hashcode 值是一样的。