Netty笔记1----第一个netty例子

Netty笔记1----第一个netty例子

1.maven 依赖

<dependency>
  <groupId>io.netty</groupId>
  <artifactId>netty-all</artifactId>
  <version>4.1.10.Final</version>
  <scope>compile</scope>
</dependency>

2.第一个netty例子

  • 服务端:

import java.net.InetSocketAddress;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

public class Server {
   public void bind(int port) throws Exception {
      /**
       * 配置服务端的NIO线程组
       * 实际上EventLoopGroup就是Reactor线程组
       * 两个Reactor一个用于服务端接收客户端的连接,另一个用于进行SocketChannel的网络读写
       **/
      EventLoopGroup bossGroup = new NioEventLoopGroup();
      EventLoopGroup workerGroup = new NioEventLoopGroup();
      
      try{
         /**
          * 由于我们使用在 NIO 传输,我们
            已指定 NioEventLoopGroup接受和处理新连接,指定 NioServerSocketChannel
            为信道类型。在此之后,我们设置本地地址是 InetSocketAddress 与所选择的端口(6)如。
            服务器将绑定到此地址来监听新的连接请求。
            ServerBootstrap对象是Netty用于启动NIO服务端的辅助启动类,目的是降低服务端开发的复杂度
          */
         ServerBootstrap b = new ServerBootstrap();
         //Set the EventLoopGroup for the parent (acceptor) and the child (client). 
         b.group(bossGroup, workerGroup)
            .channel(NioServerSocketChannel.class)
            .localAddress(new InetSocketAddress(port))
            //配置NioServerSocketChannel的TCP参数
            .option(ChannelOption.SO_BACKLOG, 1024)
            //绑定I/O事件的处理类ChildChannelHandler,作用类似于Reactor模式中的Handler类
            //主要用于处理网络I/O事件,例如记录日志,对消息进行编解码等
            .childHandler(new ChannelInitializer<SocketChannel>(){
            //添加TimeServerHandler到Channel的ChannelPipeline
               //通过TimeServerHandler给每一个新来的Channel初始化
               @Override
               protected void initChannel(SocketChannel ch) throws Exception {
                  ch.pipeline().addLast(new ServerHandler());
               }
            });
         //绑定监听端口,调用sync同步阻塞方法等待绑定操作完成,完成后返回ChannelFuture类似于JDK中Future
         ChannelFuture f = b.bind(port).sync();
         //使用sync方法进行阻塞,等待服务端链路关闭之后Main函数才退出
         f.channel().closeFuture().sync();
      }finally {
         //优雅退出,释放线程池资源
         bossGroup.shutdownGracefully();
         workerGroup.shutdownGracefully();
      }
   }
   
   
   private class ServerHandler extends ChannelInboundHandlerAdapter {
      //每个信息入站都会调用
      @Override
      public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
         ByteBuf buf = (ByteBuf) msg;
         System.out.println("Server receive message !");
         ctx.write(buf);
      }
      
      //通知处理器最后的channelread()是当前批处理中的最后一条消息时调用
      @Override
      public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
         ctx.flush();
      }

      //读操作时捕获到异常时调用
      @Override
      public void exceptionCaught (ChannelHandlerContext ctx, Throwable cause) {
         ctx.close();
      }
   }

   public static void main(String[] args) throws Exception {
      int port = 10000;
      new Server().bind(port);
   }
}
  • 客户端:

import io.netty.bootstrap.Bootstrap;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.*;
import io.netty.channel.ChannelHandler.Sharable;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;
import io.netty.util.CharsetUtil;

import java.net.InetSocketAddress;

public class Client {
    private final String host;
    private final int port;

    public Client(String host, int port) {
        this.host = host;
        this.port = port;
    }

    public void start() throws Exception {
        EventLoopGroup group = new NioEventLoopGroup();
        try {
            Bootstrap b = new Bootstrap();
            b.group(group)
                    .channel(NioSocketChannel.class)
                    .remoteAddress(new InetSocketAddress(host, port))
                    .handler(new ChannelInitializer<SocketChannel>() {
                        @Override
                        public void initChannel(SocketChannel ch)
                                throws Exception {
                            ch.pipeline().addLast(new ClientHandler());
                        }
                    });

            ChannelFuture f = b.connect().sync();

            f.channel().closeFuture().sync();
        } finally {
            group.shutdownGracefully().sync();
        }
    }

    public static void main(String[] args) throws Exception {
        final String host = "127.0.0.1";
        final int port = 10000;
        new Client(host, port).start();
    }

    private class ClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

        @Override
        public void channelActive(ChannelHandlerContext ctx) {
            for(int i = 0; i < 10; i++){
                try {
                    Thread.sleep(1000);
                }catch (InterruptedException e){

                }
                ctx.writeAndFlush(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8));
            }
        }

        @Override
        protected void channelRead0(ChannelHandlerContext channelHandlerContext, ByteBuf byteBuf) throws Exception {
            System.out.println("Client received: " + byteBuf.toString(CharsetUtil.UTF_8));
        }

        @Override
        public void exceptionCaught(ChannelHandlerContext ctx,
                                    Throwable cause) {
            cause.printStackTrace();
            ctx.close();
        }
    }
}
  • Netty和普通nio相比,方便,简单。

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值