Hello Netty

通过java进行网络编程,netty可以提高网络通信的开发效率的同时大大提高网络通信的效率。下面来看下如何使用netty进行高效编程。

引入依赖

<dependency>
    <groupId>io.netty</gro
    <artifactId>netty-all</artifactId>
    <version>4.0.25.Final</version>
</dependency>

netty3和netty4在编程api上有一定的区别,本篇是通过netty4进行实践的。

服务端句柄对象io.netty.bootstrap.ServerBootstrap

netty服务端和客户端的创建都是依赖于Bootstrap句柄对象,下面我们看下服务端是如何通过ServerBootstrap创建服务的。

serverBootstrap.group(new NioEventLoopGroup(), new NioEventLoopGroup()).channel(NioServerSocketChannel.class)
    .localAddress(new InetSocketAddress(nettyServerConfig.getListenPort()))
    .childHandler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(
                    new NettyEncoder(),
                    new NettyDecoder(),
                    new NettyConnetManageHandler(),
                    new EchoServerHandler());
        }
    });

ChannelFuture f = serverBootstrap.bind().sync();

大家去看源码就可以知道上面都是在初始化ServerBootstrap对象的属性:

  • io.netty.bootstrap.ServerBootstrap#group(io.netty.channel.EventLoopGroup, io.netty.channel.EventLoopGroup)第一个参数是用于接收请求的EventLoopGroup,第二个参数是处理请求的EventLoopGroup。

  • io.netty.bootstrap.AbstractBootstrap#channel用于初始化channelFactory,channel创建为NioServerSocketChannel的类型。

  • io.netty.bootstrap.AbstractBootstrap#localAddress(java.net.SocketAddress)这个没什么好说的设置监听的地址。

  • io.netty.bootstrap.ServerBootstrap#childHandler数据流的handler处理器,上面我们设置了四个handler,分别有数据流出和流进是待处理的handler链。

  • 上面都是在初始化句柄接下来,serverBootstrap.bind().sync()同步开启服务。

客户端句柄对象io.netty.bootstrap.Bootstrap

bootstrap.group(new NioEventLoopGroup()).channel(NioSocketChannel.class)
    .handler(new ChannelInitializer<SocketChannel>() {
        @Override
        public void initChannel(SocketChannel ch) throws Exception {
            ch.pipeline().addLast(
                    new NettyEncoder(),
                    new NettyDecoder(),
                    new NettyClientHandler());
        }
    });

bootstrap.connect(new InetSocketAddress(ip, port));

客户端句柄初始化相对来说简单,初始化处理EventLoopGroup,channel factory,handler处理链,最后connect就可以连接到netty的服务端了。

handler实现

1、NettyEncoder

public class NettyEncoder extends MessageToByteEncoder<NettyCommand> {

    @Override
    public void encode(ChannelHandlerContext ctx, NettyCommand msg, ByteBuf out)
            throws Exception {

        out.writeBytes(msg.encode());
    }
}

2、NettyDecoder

public class NettyDecoder extends ByteToMessageDecoder {

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
        byte[] tmpBuf = new byte[in.readableBytes()];
        in.readBytes(tmpBuf);
        NettyCommand command = new NettyCommand();
        out.add(command.decode(tmpBuf));
    }
}

传输对象的encode和decode都依赖于自定义的NettyCommand进行定义。

3、EchoServerHandler

class EchoServerHandler extends ChannelInboundHandlerAdapter {

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            System.out.println("phase: channelReadComplete");
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx,
                                    Throwable cause) {
            System.out.println("phase: exceptionCaught");
            cause.printStackTrace();
            ctx.close();
        }

        @Override
        public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
            NettyCommand requestCommand = (NettyCommand) msg;
            System.out.println("Server received: " + new String(requestCommand.getBody()));

            processMessageReceive(ctx, requestCommand);
        }

    }

4、NettyClientHandler

class NettyClientHandler extends SimpleChannelInboundHandler<NettyCommand> {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            System.out.println("channelActive");
        }

        @Override
        protected void channelRead0(ChannelHandlerContext ctx, NettyCommand msg)
                throws Exception {
            NettyCommand command = (NettyCommand) msg;
            processMessageReceive(ctx, command);
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx,
                                    Throwable cause) {                    //4
            System.out.println("exceptionCaught");
            cause.printStackTrace();
            ctx.close();
        }

        @Override
        public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
            System.out.println("channelReadComplete");
        }
    }

到这里一个简单的hello netty就实现完毕了。

后记

到这里整个脑袋还不是太清晰,可能是因为初次使用netty,很多深入的原理性东西还没有充分的认识到,后面不断学习升华。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值