http服务器是什么,服务器组成部分介绍( 二 )


import io.netty.bootstrap.ServerBootstrap;import io.netty.buffer.Unpooled;import io.netty.channel.*;import io.netty.channel.nio.NioEventLoopGroup;import io.netty.channel.socket.ServerSocketChannel;import io.netty.channel.socket.nio.NioServerSocketChannel;import io.netty.channel.socket.nio.NioSocketChannel;import io.netty.handler.codec.http.*;import io.netty.util.concurrent.Future;import io.netty.util.concurrent.GenericFutureListener;import java.net.ContentHandler;import java.util.concurrent.ThreadFactory;public class HttpSimpleServer { //open 启动服务 public void openServer() { ServerBootstrap bootstrap = new ServerBootstrap(); bootstrap.channel(NioServerSocketChannel.class); EventLoopGroup boss = new NioEventLoopGroup(1); EventLoopGroup work = new NioEventLoopGroup(8); bootstrap.childHandler(new ChannelInitializer() { @Override protected void initChannel(NioSocketChannel ch) throws Exception { ch.pipeline().addLast("http-decoder", new HttpRequestDecoder()); ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536)); ch.pipeline().addLast("http-encoder", new HttpResponseEncoder()); ch.pipeline().addLast("http-server", new HttpServerHandler()); } }); bootstrap.group(boss, work); try { ChannelFuture future = bootstrap.bind(8080).sync(); System.out.println("服务启动:8080"); future.channel().closeFuture().sync(); } catch (InterruptedException e) { e.printStackTrace(); } finally { boss.shutdownGracefully(); work.shutdownGracefully(); } } private static class HttpServerHandler extends SimpleChannelInboundHandler { @Override protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception { FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html;charset=UTF-8"); String html = "n" + "n" + "n" + " n" + " hello idig8.comn" + "n" + "n" + "hello idig8.comn" + "n" + ""; response.content().writeBytes(html.getBytes("UTF-8")); ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE); } } public static void main(String[] args) { HttpSimpleServer simpleServer = new HttpSimpleServer(); simpleServer.openServer(); }}实施过程分析
建立连接读取消息流 解码Request 业务处理 编码Response 返回消息关闭连接和通道管道
1.频道:
【http服务器是什么,服务器组成部分介绍】a.通道
b.套接字通道
2.pipeline:一个管道依次包含多个ChandlerHandler 。
3.钱德勒汉德勒
A.HttpRequestDecode:解码请求
B.HttpResponseEncode:对返回的结果进行编码 。
在Netty中,每个通道只有一个通道管道,它们的组成如下:
一个通道包含一个ChannelPipeline,在ChannelPipeline中维护一个由ChannelHandlerContext组成的双向链表 。这个链表的头是HeadContext,链表的尾是TailContext,每个ChannelHandlerContext都与一个ChannelHandler相关联
bootstrap.childHandler(new ChannelInitializer() {@Overrideprotected void initChannel(NioSocketChannel ch) throws Exception {ch.pipeline().addLast("http-decoder", new HttpRequestDecoder());ch.pipeline().addLast("http-aggregator", new HttpObjectAggregator(65536));ch.pipeline().addLast("http-encoder", new HttpResponseEncoder());ch.pipeline().addLast("http-server", new HttpServerHandler());}});netty中http的表达结构
netty中HttpResponse的结构
PS:我讲了http协议,以及如何通过netty完成http服务 。


推荐阅读