Netty 系列知识分享(三)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_38318622/article/details/85678365

Http服务器

前面了我们说了tcp服务器客户端的编写,今天我们来说一下http服务器的编写。那么没错也就是说我们的netty同样可以写web服务器。并且性能要略高于大家常用的什么Spring SpringBoot等。
下面我们开始愉快的写代码吧!

package netty.http.demo;

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 HttpServer {
    public static void main(String[] args) throws InterruptedException {
        EventLoopGroup bossgroup=new NioEventLoopGroup();
        EventLoopGroup workgroup=new NioEventLoopGroup();
        try {
            ServerBootstrap b=new ServerBootstrap();
            b.group(bossgroup,workgroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new HttpInitializer());
            ChannelFuture f=b.bind(8088).sync();
            f.channel().closeFuture().sync();
        }finally {
            bossgroup.shutdownGracefully();
            workgroup.shutdownGracefully();
        }
    }
}
package netty.http.demo;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpRequestDecoder;
import io.netty.handler.codec.http.HttpServerCodec;

public class HttpInitializer extends  ChannelInitializer<SocketChannel> {
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline=socketChannel.pipeline();
        pipeline.addLast(new HttpServerCodec());
        pipeline.addLast(new HttpServerHandler());
        pipeline.addLast("http-decoder", new HttpRequestDecoder());
    }
}

package netty.http.demo;

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;

public class HttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject msg) throws Exception {
        if (msg instanceof HttpRequest) {
            HttpRequest request = (HttpRequest) msg;
            URI uri = new URI(request.uri());
            String reqdata=uri.getPath().substring(1);
            System.err.println("请求是 :  " + reqdata);
            ByteBuf content = Unpooled.copiedBuffer("Netty Http Server Send to congratulatory message : Hello small browser", CharsetUtil.UTF_8);
            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());
            ctx.writeAndFlush(response);
        }

    }
}

这里呢我比较懒没有写注释,其实和上次的注释基本上都是一样的。我就不写了。下面我们看一下效果吧!

在这里插入图片描述
在这里插入图片描述
哈哈 我们看到了这里我们可以看到我们在浏览器得到了我们想要的结果,还有就只在后台我们也得到了对应的请求信息。

favicon.ico这是个什么鬼?
这里解释一下,favicon.ico 请求是怎么回事我们不是只发了一个请求吗??
好首先我们在浏览器上按下F12开启检查
在这里插入图片描述
下面我们选择到NetWork选项下,然后点击刷新。
在这里插入图片描述
额,这就奇怪了确实有两个请求啊,一个user是我们自己发的请求,一个就是favicon.ico了但是我们确确实实没有发送这个请求啊。这是怎么回事?
那么我们就来看一下这个请求是怎么回事吧?
在这里插入图片描述
我们可以看到这个请求的期望返回值是图片。而且这个请求是浏览器自动提交的,那么我们在这个系统里显然是分析不出来了这个到底是干嘛的,好我们去百度一下看看。看看百度是不是也有这个请求这个请求是干什么的呢??
在这里插入图片描述
在这里插入图片描述
现在我们可以看到原来他返回的是一个图片啊。就是在我们页面上那个标题栏上显示的那个小图片。所以大家不要纠结了,这个是是我们浏览器帮我们发送的一个请求,和我们没啥大关系。下面大家就敲起来吧,看看是否能实现像Neon一样的结果呢?
在这里插入图片描述
下篇文章我们不在分享通讯协议这列例子了,通讯协议我们就分享这么多,这也是http、tcp是主流的大家用的比较多的。下面我们将分享一下写Netty的常用编解码器,以及其自定义编解码器,自定义私有协议等等。

猜你喜欢

转载自blog.csdn.net/qq_38318622/article/details/85678365