Netty之HTTP协议应用开发

时间:2022-07-24
本文章向大家介绍Netty之HTTP协议应用开发,主要内容包括其使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。

1、HTTP协议

HTTP(超文本传输协议)是建立在TCP传输协议上的应用层协议,是一个属于应用层的面向对象的协议,也是目前Web开发主流的协议。

HTTP协议的特点如下:

(1)支持Client/Server模式

(2)简单,客户端向服务端发请求时,只需指定URL,携带必要的请求参数以及消息体。

(3)灵活,允许传输任意类型的数据对象,传输类型由请求头的Content-Type指定。

(4)无状态,协议对于事务处理没有记忆能力,如果后续需要处理之前的信息,必须进行重传。

2、Netty HTTP服务端开发

我们以获取请求参数为例,来开发HTTP服务端,客户端可以是浏览器,也可以是Postman工具。如果没有参数,则返回给客户端错误,如果有请求参数,则获取到请求参数返回给客户端。

服务端程序

public class HttpServer {

    public void start(int port) throws InterruptedException {
        NioEventLoopGroup boss = new NioEventLoopGroup();
        NioEventLoopGroup worker = new NioEventLoopGroup();

        ServerBootstrap server = new ServerBootstrap();

        server.group(boss,worker)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG,1024)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel ch) throws Exception {
                        ChannelPipeline pipeline = ch.pipeline();
                        //使用Netty自带的HTTP请求解码类
                        pipeline.addLast(new HttpRequestDecoder());
                        //参数的最大长度
                        pipeline.addLast(new HttpObjectAggregator(65535));
                        //使用Netty自带HTTP返回体消息编码类
                        pipeline.addLast(new HttpResponseEncoder());
                        //支持异步发送大的码流,比如文件,但不占用过多的内存
                        pipeline.addLast(new ChunkedWriteHandler());
                        pipeline.addLast(new HttpServerNewHandler());
                    }
                })
                .childOption(ChannelOption.SO_KEEPALIVE,true);

        ChannelFuture future = server.bind(port).sync();
        System.out.println("服务端已启动,端口号为:"+port);
        future.channel().closeFuture().sync();

    }

    public static void main(String[] args) throws InterruptedException {
        new HttpServer().start(8080);
    }
}

这边使用Netty自带的HttpRequestDecoder和HttpResponseEncoder类进行编解码。

HttpObjectAggregator解码器的作用是将多个消息转换成单一的FullHttpRequest或者是FullHttpResponse,原因是HTTP解码器在每个HTTP消息中会生成多个消息对象。

ChunkedWriteHandler的作用是支持异步发送大的码流,例如大文件传输,但不占用过多的内存,放置JVM内存溢出 。

然后看一下我们进行参数解析的自定义处理类:

public class HttpServerNewHandler extends SimpleChannelInboundHandler<FullHttpRequest> {

    @Override
    protected void messageReceived(ChannelHandlerContext ctx, FullHttpRequest request) throws Exception 
        //解码失败
        if (request.decoderResult().isFailure()) {
            sendResp(ctx,"请求解码失败",HttpResponseStatus.BAD_REQUEST);
        }
        //获取请求方法
        HttpMethod method = request.method();
        if (method == HttpMethod.GET) {
            String uri = request.uri();
            int index = 0;
            if ((index = uri.indexOf("?"))<=-1) {
                sendResp(ctx,"此请求没有参数,请求失败",HttpResponseStatus.BAD_REQUEST);
            } else {
                String param = uri.substring(index + 1);
                sendResp(ctx,"请求方式为GET,参数为:"+param,HttpResponseStatus.OK);
            }
        } else if (method == HttpMethod.POST) {
            ByteBuf buf = request.content();
            int length = buf.readableBytes();
            if (length>0) {
                byte[] param = new byte[length];
                buf.readBytes(param);
                String paramStr = new String(param);
                sendResp(ctx, "请求方式为POST,参数为:" + paramStr, HttpResponseStatus.OK);
            } else {
                sendResp(ctx, "此请求没有参数,请求失败" , HttpResponseStatus.BAD_REQUEST);
            }
        }
    }

    /**
     * 将消息封装成FullHttpResponse返回给客户端
     */
    private static void sendResp(ChannelHandlerContext ctx,String msg,HttpResponseStatus status) {
        ByteBuf byteBuf = Unpooled.copiedBuffer(msg.getBytes());
        FullHttpResponse response = new DefaultFullHttpResponse(HTTP_1_1, status);
        response.headers().set(CONTENT_TYPE, "text/html; charset=UTF-8");
        response.content().writeBytes(byteBuf);
        byteBuf.release();
        ctx.writeAndFlush(response).addListener(ChannelFutureListener.CLOSE);
    }
}

参数解析的处理类逻辑比较简单,如果Http请求解析失败,则直接返回给客户端,如果是GET请求,则直接获取url后面的参数,如果是POST请求,则获取到HttpRequest里面的content。

接下类启动服务端,然后利用PostMan进行Get请求和Post请求的测试。

GET请求不带参数

GET请求带参数

POST请求不带参数

POST请求带参数