Java网络编程:Netty框架学习(五)---写一个Netty

2021-02-25  本文已影响0人  singleZhang2010

概述

前面基本分析了一下Netty框架学习的一些前置概念和基础,其实netty都为我们封装好了,现在我们来写一个示例来加深印象

开始实践

项目在之前的https://gitee.com/kaixinshow/java-nionetty-learning基础上创建一个netty包

1.创建一个服务器端:HttpServer

/**
 * @ClassName HttpServer
 * @Description //HttpServer
 * @Author singleZhang
 * @Email 405780096@qq.com
 * @Date 2021/2/25 0025 上午 11:40
 **/
public class HttpServer {

    public static void main(String[] args) {

        // 构造两个线程组
        EventLoopGroup bossGroup = new NioEventLoopGroup();
        EventLoopGroup workerGroup = new NioEventLoopGroup();

        try{

            //服务端启动辅助类
            ServerBootstrap bootstrap = new ServerBootstrap();

            bootstrap.group(bossGroup,workerGroup)
                    .channel(NioServerSocketChannel.class)
                    .childHandler(new HttpServerInitializer());

            //启动
            ChannelFuture channelFuture = bootstrap.bind(8088).sync();

            //等待服务端口关闭
            channelFuture.channel().closeFuture().sync();
        }catch (InterruptedException e){
            e.printStackTrace();
        }finally {
            // 优雅退出,释放线程池资源
            bossGroup.shutdownGracefully();
            workerGroup.shutdownGracefully();
        }

    }
}

2.创建自定义通道初始化器:HttpServerInitializer 继承ChannelInitializer

/**
 * @ClassName HttpServerInitializer
 * @Description //HttpServerInitializer
 * @Author singleZhang
 * @Email 405780096@qq.com
 * @Date 2021/2/25 0025 上午 11:44
 **/
public class HttpServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline ch = socketChannel.pipeline();
        //HttpServerCodec可以被HttpResponseEncoder、HttpRequestDecoder两个替代
        ch.addLast("httpServerCodec",new HttpServerCodec())
                .addLast("httpServerHandler",new HttpServerHandler());
    }
}

3.创建自定义的 ChannelHandler 组件,处理自定义的业务逻辑:HttpServerHandler

/**
 * @ClassName HttpServerHandler
 * @Description //HttpServerHandler
 * @Author singleZhang
 * @Email 405780096@qq.com
 * @Date 2021/2/25 0025 上午 11:49
 **/
public class HttpServerHandler extends SimpleChannelInboundHandler<HttpObject> {

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, HttpObject  msg) throws Exception {
        if(msg instanceof HttpRequest){
            HttpRequest request = (HttpRequest) msg;

            System.out.println(request.method().name());

            System.out.println(request.uri());
        }

        if(msg instanceof HttpContent){
            HttpContent content = (HttpContent) msg;

            ByteBuf buf = content.content();
            System.out.println(buf.toString(CharsetUtil.UTF_8));

            ByteBuf resBuf = Unpooled.copiedBuffer("hello,server test!",CharsetUtil.UTF_8);
            FullHttpResponse response = new DefaultFullHttpResponse(HttpVersion.HTTP_1_1,HttpResponseStatus.OK,resBuf);
            response.headers().add(HttpHeaderNames.CONTENT_TYPE,"text/plain");
            response.headers().add(HttpHeaderNames.CONTENT_LENGTH,resBuf.readableBytes());

            ctx.writeAndFlush(response);
        }
    }
}

这样就完成了一个简单的服务器端程序,用来处理http请求/响应,启动程序并用postman测试一下:

postman server
  1. 写一个客户端:HttpClient
/**
 * @ClassName HttpClient
 * @Description //HttpClient
 * @Author singleZhang
 * @Email 405780096@qq.com
 * @Date 2021/2/25 0025 下午 2:52
 **/
public class HttpClient {

    public static void main(String[] args) {
        String host = "localhost";
        int port = 8088;

        EventLoopGroup group = new NioEventLoopGroup();

        try{

            Bootstrap bootstrap = new Bootstrap();
            bootstrap.group(group)
            .channel(NioSocketChannel.class)
            .handler(new ChannelInitializer<SocketChannel>() {

                @Override
                protected void initChannel(SocketChannel channel) throws Exception {

                    ChannelPipeline cp = channel.pipeline();
                    cp.addLast(new HttpClientCodec())
                            //HttpObjectAggregator将请求转为FullHttpRequest
                            .addLast("aggregator", new HttpObjectAggregator(65536))
                            .addLast(new HttpClientHandler());
                }
            });

            // 启动客户端.
            ChannelFuture f = bootstrap.connect(host, port).sync();
            f.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            group.shutdownGracefully();
        }
    }
}

5.创建自定义的 ChannelHandler 组件,处理自定义的业务逻辑:HttpClientHandler

/**
 * @ClassName HttpClientHandler
 * @Description //HttpClientHandler
 * @Author singleZhang
 * @Email 405780096@qq.com
 * @Date 2021/2/25 0025 下午 3:00
 **/
public class HttpClientHandler extends SimpleChannelInboundHandler<FullHttpResponse> {

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {

        URI uri = new URI("http://localhost:8080");
        String msg ="hi,client test!";
        FullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1,HttpMethod.GET,uri.toASCIIString(),
                Unpooled.wrappedBuffer(msg.getBytes("UTF-8")));

        ctx.channel().writeAndFlush(request);
    }

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, FullHttpResponse msg) throws Exception {

        FullHttpResponse response = msg;
        response.headers().get(HttpHeaderNames.CONTENT_TYPE);
        ByteBuf buf = response.content();

        System.out.println(buf.toString(io.netty.util.CharsetUtil.UTF_8));
    }
}

这样启动服务器端和客户端,显示的结果如下:


server client

总结

在这个示例程序里,我们使用了Netty中的一些类,如:NioEventLoopGroup、ServerBootstrap、Bootstrap、Channel、NioSocketChannel、ChannelInitializer、ChannelPipeline、SimpleChannelInboundHandler等等。

上一篇 下一篇

猜你喜欢

热点阅读