Netty构建HTTP服务

1. 众所周知,Http协议的上层是TCP,Netty作为非阻塞框架的大佬,完全有能力承担高并发,高可用的角色.先上车,后解释,
2. 可以用Netty创建一个TCP服务\color{red}{(测试Http的底层为TCP)},用浏览器请求,看能否收到请求,只要响应的是Http响应头,浏览器就可以解析

1.HttpServer.java

public class HttpServer {
    public void start(int port) throws Exception {
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();
        try {
            ServerBootstrap b = new ServerBootstrap();
            b.group(bossGroup, workerGroup)
                    .handler(new LoggingHandler(LogLevel.DEBUG))
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch)
                                throws Exception {
                            // server端发送的是httpResponse,所以要使用HttpResponseEncoder进行编码
                            ch.pipeline().addLast(
                                    new HttpResponseEncoder());
                            // server端接收到的是httpRequest,所以要使用HttpRequestDecoder进行解码
                            ch.pipeline().addLast(
                                    new HttpRequestDecoder());
                            ch.pipeline().addLast(
                                    new HttpObjectAggregator(512 * 1024));
                            ch.pipeline().addLast(
                                    new HttpServerHandler());
                            //增加自定义实现的Handler
                            ch.pipeline().addLast(new HttpServerCodec());
                        }
                    }).option(ChannelOption.SO_BACKLOG, 128)
                    .childOption(ChannelOption.SO_KEEPALIVE, true);
            ChannelFuture f = b.bind(port).sync();
            f.channel().closeFuture().sync();
        } finally {
            workerGroup.shutdownGracefully();
            bossGroup.shutdownGracefully();
        }
    }

    public static void main(String[] args) throws Exception {
        HttpServer server = new HttpServer();
        server.start(8080);
    }
public class HttpServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) {
        if (msg instanceof HttpRequest) {
            HttpRequest httpRequest = (HttpRequest) msg;
            System.out.println(Thread.currentThread().getId() + "->");
            System.out.println(httpRequest.uri() + ":" + ctx.channel().remoteAddress().toString());
            String uri = httpRequest.uri();
            if (msg instanceof HttpContent) {
                if(HttpPostRequestDecoder.isMultipart(httpRequest)){
                    // 是POST请求
                    HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(httpRequest);
                    decoder.offer((HttpContent)httpRequest);
                    List<InterfaceHttpData> parmList = decoder.getBodyHttpDatas();
                    System.out.println("form conent : \r\n" + parmList);
                }else{
                    HttpContent content = (HttpContent) msg;
                    ByteBuf buf = content.content();;
                    String requestString = buf.toString(io.netty.util.CharsetUtil.UTF_8);
                    buf.release();
                    System.out.println("json conent : \r\n" + requestString);
                }


            }
            Map<String, String> resMap = new HashMap<>();
            resMap.put("method", httpRequest.method().name());
            Set<Cookie> cookies = ServerCookieDecoder.LAX.decode(httpRequest.headers().get(HttpHeaderNames.COOKIE));
            resMap.put("uri", uri);
            // String text = "<html><head><title>test</title></head><body>你请求 port:"+ctx.channel().remoteAddress().toString()+" uri为:" + uri + ":" + httpRequest.headers().get(HttpHeaderNames.COOKIE) + "</body></html>";
            String text = "<html><head><title>test</title></head><body>你请求 port:" + ctx.channel().remoteAddress().toString() + " uri为:" + uri + "</body></html>";
            // 创建http响应
            FullHttpResponse response = new DefaultFullHttpResponse(
                    HttpVersion.HTTP_1_1,
                    HttpResponseStatus.OK,
                    Unpooled.copiedBuffer(text, CharsetUtil.UTF_8));
            // 设置头信息
            response.headers().set(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode("JSESESSION", "1234"));
            response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
            // 将html write到客户端
            ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);

        }

    }


    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        ctx.flush();
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        String text = "<html><head><title>test</title></head><body>你请求uri为:" + cause.toString() + "</body></html>";

        FullHttpResponse response = new DefaultFullHttpResponse(
                HttpVersion.HTTP_1_1,
                HttpResponseStatus.OK,
                Unpooled.copiedBuffer(text, CharsetUtil.UTF_8));
        // 设置头信息
        response.headers().set(HttpHeaderNames.SET_COOKIE, ServerCookieEncoder.STRICT.encode("JSESESSION", "1234"));
        response.headers().set(HttpHeaderNames.CONTENT_TYPE, "text/html; charset=UTF-8");
        // 将html write到客户端
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
        ctx.close();
    }

线程组:

 EventLoopGroup bossGroup = new NioEventLoopGroup();//主线程
 EventLoopGroup workerGroup = new NioEventLoopGroup();// 工作线程

主线程用来接受tcp请求并分发,工作线程用来处理请求
解码器(解析tcp请求过来的数据)
HttpRequestDecoder() 
HttpPostRequestDecoder.isMultipart(httpRequest)
用来区分是表单还是Json
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 并不是 每一辆公车 十分钟 就会准点来一班 正如并不是 每一段爱情 一两年 就会准点来一趟 但在间隔里耐心静候 终...
    大囍Daisy阅读 187评论 0 1
  • 年纪大了,熬夜熬不住了。
    吨吨农场主阅读 86评论 0 0
  • 最近一周遇到两件比较奇葩的事—微信好友被对方删除。本来这事也挺正常,现在一言不合即分,一语投机即婚的事也不少,何况...
    你的气质阅读 609评论 0 1
  • 《匆匆》 作者:魏城 匆匆 是上班族脚步的节奏 匆匆 是车轮与铁轨摩擦的声音 匆匆 是燕翅掠过的光影 匆匆 是岁月...
    魏城阅读 218评论 1 2
  • 作为一个父亲, 那天在万达影城我热泪盈眶, (身边的儿子似乎面无表情) 当时钢铁侠哄女儿入睡, 说我爱你宝贝, 她...
    A南星学生教育中心林老师阅读 567评论 0 1