你好,netty!

你好,netty!

最近对netty这个框架热情高涨,今天晚上也是刚刚把netty源码fork到本地打算追一追,最近在逐渐完善一个简易RPC项目,我直接把netty拿来使用,而没有使用之前的NIO,作为一个牛逼的框架,netty不仅可以保证很高的性能,而且使用起来也是比较简练,一般的问题都是100行代码之内就可以解决,netty社区活跃,作者态度认真严谨,很多牛逼框架底层的网络通信使用的都是netty,例如Spark、Dubbo、RocketMQ等等,所以,值得深入学习一下,下面就是一个入门级别的hello-world,逐渐揭开神秘面纱,打开网络编程的大门!

TestServer.java

package 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;

/**
 * @Author : hadoo
 * @Date : 2020/9/2 21:50
 */
public class TestServer {
    public static void main(String[] args) throws Exception{
        //初始化两个事件循环组,那种死循环轮询的方式
        //boss一般负责接受连接,worker负责之后的逻辑
        EventLoopGroup boss = new NioEventLoopGroup();
        EventLoopGroup worker = new NioEventLoopGroup();

        try {
            //启动器
            //每一个channel可以对应若干的handler,若干个handler组合在一起成为管道pipeline
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            serverBootstrap.group(boss,worker).channel(NioServerSocketChannel.class).
                    childHandler(new TestServerInitializer());
            //同步绑定端口
            ChannelFuture channelFuture = serverBootstrap.bind(8899).sync();
            channelFuture.channel().closeFuture().sync();
        } finally {
            //优雅关闭方式
            boss.shutdownGracefully();
            worker.shutdownGracefully();
        }
    }
}

TestServerInitializer.java

package demo;

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

/**
 * @Author : hadoo
 * @Date : 2020/9/2 21:54
 */
//channel建好之后进行初始化,可以绑定若干handler,既可以使用系统已经存在的,也可以自定义
public class TestServerInitializer extends ChannelInitializer<SocketChannel> {
    @Override
    protected void initChannel(SocketChannel socketChannel) throws Exception {
        ChannelPipeline pipeline = socketChannel.pipeline();
        pipeline.addLast("httpServerCodec",new HttpServerCodec());
        //自定义handler
        pipeline.addLast("testHttpServerHandler",new TestHttpServerHandler());
    }
}

TestHttpServerHandler.java

package 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.nio.charset.Charset;

/**
 * @Author : hadoo
 * @Date : 2020/9/2 22:01
 */
//对请求做出响应,并返回
public class TestHttpServerHandler extends SimpleChannelInboundHandler {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, Object o) throws Exception {
        ByteBuf content = Unpooled.copiedBuffer("Hello World~", 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());

        channelHandlerContext.writeAndFlush(response);
    }
}

image-20200903122022240

Success~

接下来会开一个netty专栏,专门用来总结相关原理知识,敬请期待!