java-netty


netty常用API学习

netty简介

  • Netty是基于Java NIO的网络应用框架.
  • Netty是一个NIO client-server(客户端服务器)框架,使用Netty可以快速开发网络应用,例如服务器和客户端协议。Netty提供了一种新的方式来使开发网络应用程序,这种新的方式使得它很容易使用和有很强的扩展性。Netty的内部实现时很复杂的,但是Netty提供了简单易用的api从网络处理代码中解耦业务逻辑。Netty是完全基于NIO实现的,所以整个Netty都是异步的。
  • 网络应用程序通常需要有较高的可扩展性,无论是Netty还是其他的基于Java NIO的框架,都会提供可扩展性的解决方案。Netty中一个关键组成部分是它的异步特性.

netty的helloworld

下载netty包

服务端启动类

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.Channel;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.nio.NioServerSocketChannel;

/**
 * • 配置服务器功能,如线程、端口 • 实现服务器处理程序,它包含业务逻辑,决定当有一个请求连接或接收数据时该做什么
 *
 */
public class EchoServer {

    private final int port;

    public EchoServer(int port) {
        this.port = port;
    }

    public void start() throws Exception {
        EventLoopGroup eventLoopGroup = null;
        try {
            //创建ServerBootstrap实例来引导绑定和启动服务器
            ServerBootstrap serverBootstrap = new ServerBootstrap();
            //创建NioEventLoopGroup对象来处理事件,如接受新连接、接收数据、写数据等等
            eventLoopGroup = new NioEventLoopGroup();
            //指定通道类型为NioServerSocketChannel,设置InetSocketAddress让服务器监听某个端口已等待客户端连接。
            serverBootstrap.group(eventLoopGroup).channel(NioServerSocketChannel.class).localAddress("localhost",port).childHandler(new ChannelInitializer<Channel>() {
                //设置childHandler执行所有的连接请求
                @Override
                protected void initChannel(Channel ch) throws Exception {
                    ch.pipeline().addLast(new EchoServerHandler());
                }
                    });
            // 最后绑定服务器等待直到绑定完成,调用sync()方法会阻塞直到服务器完成绑定,然后服务器等待通道关闭,因为使用sync(),所以关闭操作也会被阻塞。
            ChannelFuture channelFuture = serverBootstrap.bind().sync();
            System.out.println("开始监听,端口为:" + channelFuture.channel().localAddress());
            channelFuture.channel().closeFuture().sync();
        } finally {
            eventLoopGroup.shutdownGracefully().sync();
        }
    }

    public static void main(String[] args) throws Exception {
        new EchoServer(20000).start();
    }
}

服务端回调方法

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.util.Date;

public class EchoServerHandler extends ChannelInboundHandlerAdapter {

    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg)
            throws Exception {
        System.out.println("server 读取数据……");
        //读取数据
        ByteBuf buf = (ByteBuf) msg;
        byte[] req = new byte[buf.readableBytes()];
        buf.readBytes(req);
        String body = new String(req, "UTF-8");
        System.out.println("接收客户端数据:" + body);
        //向客户端写数据
        System.out.println("server向client发送数据");
        String currentTime = new Date(System.currentTimeMillis()).toString();
        ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
        ctx.write(resp);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("server 读取数据完毕..");
        ctx.flush();//刷新后才将数据发出到SocketChannel
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
            throws Exception {
        cause.printStackTrace();
        ctx.close();
    }

}

客户端启动类


import io.netty.bootstrap.Bootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioSocketChannel;

import java.net.InetSocketAddress;

/**
 * • 连接服务器 • 写数据到服务器 • 等待接受服务器返回相同的数据 • 关闭连接
 *
 */
public class EchoClient {

    private final String host;
    private final int port;

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

    public void start() throws Exception {
        EventLoopGroup nioEventLoopGroup = null;
        try {
            //创建Bootstrap对象用来引导启动客户端
            Bootstrap bootstrap = new Bootstrap();
            //创建EventLoopGroup对象并设置到Bootstrap中,EventLoopGroup可以理解为是一个线程池,这个线程池用来处理连接、接受数据、发送数据
            nioEventLoopGroup = new NioEventLoopGroup();
            //创建InetSocketAddress并设置到Bootstrap中,InetSocketAddress是指定连接的服务器地址
            bootstrap.group(nioEventLoopGroup).channel(NioSocketChannel.class).remoteAddress(new InetSocketAddress(host, port))
                    .handler(new ChannelInitializer<SocketChannel>() {
                        //添加一个ChannelHandler,客户端成功连接服务器后就会被执行
                        @Override
                        protected void initChannel(SocketChannel ch)
                                throws Exception {
                            ch.pipeline().addLast(new EchoClientHandler());
                        }
                    });
            // • 调用Bootstrap.connect()来连接服务器
            ChannelFuture f = bootstrap.connect().sync();
            // • 最后关闭EventLoopGroup来释放资源
            f.channel().closeFuture().sync();
        } finally {
            nioEventLoopGroup.shutdownGracefully().sync();
        }
    }

    public static void main(String[] args) throws Exception {
        new EchoClient("localhost", 20000).start();
    }
}

netty中handler的执行顺序

  • Handler在netty中,无疑占据着非常重要的地位。Handler与Servlet中的filter很像,通过Handler可以完成通讯报文的解码编码、拦截指定的报文、统一对日志错误进行处理、统一对请求进行计数、控制Handler执行与否。一句话,没有它做不到的只有你想不到的。
  • Netty中的所有handler都实现自ChannelHandler接口。按照输出输出来分,分为ChannelInboundHandler、ChannelOutboundHandler两大类。ChannelInboundHandler对从客户端发往服务器的报文进行处理,一般用来执行解码、读取客户端数据、进行业务处理等;ChannelOutboundHandler对从服务器发往客户端的报文进行处理,一般用来进行编码、发送报文到客户端。
  • Netty中,可以注册多个handler。ChannelInboundHandler按照注册的先后顺序执行;ChannelOutboundHandler按照注册的先后顺序逆序执行,如下图所示,按照注册的先后顺序对Handler进行排序,request进入Netty后的执行顺序为
    • mark

代码示例

  • server

    • import io.netty.bootstrap.ServerBootstrap;
      import io.netty.channel.Channel;
      import io.netty.channel.ChannelFuture;
      import io.netty.channel.ChannelInitializer;
      import io.netty.channel.EventLoopGroup;
      import io.netty.channel.nio.NioEventLoopGroup;
      import io.netty.channel.socket.nio.NioServerSocketChannel;
      
      /**
       * • 配置服务器功能,如线程、端口 • 实现服务器处理程序,它包含业务逻辑,决定当有一个请求连接或接收数据时该做什么
       */
      public class EchoServer {
      
          private final int port;
      
          public EchoServer(int port) {
              this.port = port;
          }
      
          public void start() throws Exception {
              EventLoopGroup eventLoopGroup = null;
              try {
                  //server端引导类
                  ServerBootstrap serverBootstrap = new ServerBootstrap();
                  //连接池处理数据
                  eventLoopGroup = new NioEventLoopGroup();
                  serverBootstrap.group(eventLoopGroup)
                  .channel(NioServerSocketChannel.class)//指定通道类型为NioServerSocketChannel,一种异步模式,OIO阻塞模式为OioServerSocketChannel
                  .localAddress("localhost",port)//设置InetSocketAddress让服务器监听某个端口已等待客户端连接。
                  .childHandler(new ChannelInitializer<Channel>() {//设置childHandler执行所有的连接请求
                      @Override
                      protected void initChannel(Channel ch) throws Exception {
                          // 注册两个InboundHandler,执行顺序为注册顺序,所以应该是InboundHandler1 InboundHandler2
                          // 注册两个OutboundHandler,执行顺序为注册顺序的逆序,所以应该是OutboundHandler2 OutboundHandler1
                          ch.pipeline().addLast(new EchoInHandler1());
                          ch.pipeline().addLast(new EchoInHandler2());
                          ch.pipeline().addLast(new EchoOutHandler1());
                          ch.pipeline().addLast(new EchoOutHandler2()); 
                          
                      }
                          });
                  // 最后绑定服务器等待直到绑定完成,调用sync()方法会阻塞直到服务器完成绑定,然后服务器等待通道关闭,因为使用sync(),所以关闭操作也会被阻塞。
                  ChannelFuture channelFuture = serverBootstrap.bind().sync();
                  System.out.println("开始监听,端口为:" + channelFuture.channel().localAddress());
                  channelFuture.channel().closeFuture().sync();
              } finally {
                  eventLoopGroup.shutdownGracefully().sync();
              }
          }
      
          public static void main(String[] args) throws Exception {
              new EchoServer(20000).start();
          }
      }
      
  • EchoInHandler1

    • public class EchoInHandler1 extends ChannelInboundHandlerAdapter {
      
          @Override
          public void channelRead(ChannelHandlerContext ctx, Object msg)
                  throws Exception {
              System.out.println("in1");
               // 通知执行下一个InboundHandler
              ctx.fireChannelRead(msg);
          }
      
          @Override
          public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
              ctx.flush();//刷新后才将数据发出到SocketChannel
          }
      
          @Override
          public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
                  throws Exception {
              cause.printStackTrace();
              ctx.close();
          }
      
      }
      

  • EchoInHandler2

    • import io.netty.buffer.ByteBuf;
      import io.netty.buffer.Unpooled;
      import io.netty.channel.ChannelHandlerContext;
      import io.netty.channel.ChannelInboundHandlerAdapter;
      
      import java.util.Date;
      
      import cn.itcast_03_netty.sendobject.bean.Person;
      
      public class EchoInHandler2 extends ChannelInboundHandlerAdapter {
      
          @Override
          public void channelRead(ChannelHandlerContext ctx, Object msg)
                  throws Exception {
              System.out.println("in2");
              ByteBuf buf = (ByteBuf) msg;
              byte[] req = new byte[buf.readableBytes()];
              buf.readBytes(req);
              String body = new String(req, "UTF-8");
              System.out.println("接收客户端数据:" + body);
              //向客户端写数据
              System.out.println("server向client发送数据");
              String currentTime = new Date(System.currentTimeMillis()).toString();
              ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
              ctx.write(resp);
              
          }
      
          @Override
          public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
              ctx.flush();//刷新后才将数据发出到SocketChannel
          }
      
          @Override
          public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
                  throws Exception {
              cause.printStackTrace();
              ctx.close();
          }
      
      }
      

  • EchoOutHandler1

    • import java.util.Date;
      
      import io.netty.buffer.ByteBuf;
      import io.netty.buffer.Unpooled;
      import io.netty.channel.ChannelHandlerContext;
      import io.netty.channel.ChannelOutboundHandlerAdapter;
      import io.netty.channel.ChannelPromise;
      
      public class EchoOutHandler1 extends ChannelOutboundHandlerAdapter {
          @Override
          // 向client发送消息
          public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
              System.out.println("out1");
              /*System.out.println(msg);*/
              
              String currentTime = new Date(System.currentTimeMillis()).toString();
              ByteBuf resp = Unpooled.copiedBuffer(currentTime.getBytes());
              ctx.write(resp);
              ctx.flush();
             }
      }
      

  • EchoOutHandler2

    • import io.netty.channel.ChannelHandlerContext;
      import io.netty.channel.ChannelOutboundHandlerAdapter;
      import io.netty.channel.ChannelPromise;
      
      public class EchoOutHandler2 extends ChannelOutboundHandlerAdapter {
      
           @Override
              public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
                  System.out.println("out2");
                  // 执行下一个OutboundHandler
                  /*System.out.println("at first..msg = "+msg);
                  msg = "hi newed in out2";*/
                  super.write(ctx, msg, promise);
              }
      
      }
      

总结

  • 在使用Handler的过程中,需要注意:
    1. ChannelInboundHandler之间的传递,通过调用ctx.fireChannelRead(msg)实现;调用ctx.write(msg) 将传递到ChannelOutboundHandler。
    2. ctx.write()方法执行后,需要调用flush()方法才能令它立即执行。
    3. 流水线pipeline中outhandler不能放在最后,否则不生效
    4. Handler的消费处理放在最后一个处理。

netty发送对象

简介

  • Netty中,通讯的双方建立连接后,会把数据按照ByteBuf的方式进行传输,例如http协议中,就是通过HttpRequestDecoder对ByteBuf数据流进行处理,转换成http的对象。基于这个思路,我自定义一种通讯协议:Server和客户端直接传输java对象。
  • 实现的原理是通过Encoder把java对象转换成ByteBuf流进行传输,通过Decoder把ByteBuf转换成java对象进行处理,处理逻辑如下图所示:
    • mark

代码

  • bean

    • 
      import java.io.Serializable;
      
      public class Person implements Serializable {
          private static final long serialVersionUID = 1L;
          private String name;
          private String sex;
          private int age;
      
          public String toString() {
              return "name:" + name + " sex:" + sex + " age:" + age;
          }
      get/set...
      }
      
  • 序列化

    • import io.netty.buffer.ByteBuf;
      import io.netty.channel.ChannelHandlerContext;
      import io.netty.handler.codec.MessageToByteEncoder;
      import cn.itcast_03_netty.sendobject.bean.Person;
      import cn.itcast_03_netty.sendobject.utils.ByteObjConverter;
       /**
        * 序列化
        * 将object转换成Byte[]
        *
        */
      public class PersonEncoder extends MessageToByteEncoder<Person> {
       
          @Override
          protected void encode(ChannelHandlerContext ctx, Person msg, ByteBuf out) throws Exception {
              //工具类:将object转换为byte[]
              byte[] datas = ByteObjConverter.objectToByte(msg);
              out.writeBytes(datas);
              ctx.flush();
          }
      }
      
  • 反序列化

    • import io.netty.buffer.ByteBuf;
      import io.netty.channel.ChannelHandlerContext;
      import io.netty.handler.codec.ByteToMessageDecoder;
      
      import java.util.List;
      
      import cn.itcast_03_netty.sendobject.utils.ByteBufToBytes;
      import cn.itcast_03_netty.sendobject.utils.ByteObjConverter;
       
       /**
        * 反序列化
        * 将Byte[]转换为Object
        *
        */
      public class PersonDecoder extends ByteToMessageDecoder {
          @Override
          protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
              //工具类:将ByteBuf转换为byte[]
              ByteBufToBytes read = new ByteBufToBytes();
              byte[] bytes = read.read(in);
              //工具类:将byte[]转换为object
              Object obj = ByteObjConverter.byteToObject(bytes);
              out.add(obj);
          }
       
      }
      
  • 转换工具类

    • 
      import io.netty.buffer.ByteBuf;
      
      public class ByteBufToBytes {
          /**
           * 将ByteBuf转换为byte[]
           * @param datas
           * @return
           */
          public byte[] read(ByteBuf datas) {
              byte[] bytes = new byte[datas.readableBytes()];// 创建byte[]
              datas.readBytes(bytes);// 将ByteBuf转换为byte[]
              return bytes;
          }
      }
      
    • 
      import java.io.ByteArrayInputStream;
      import java.io.ByteArrayOutputStream;
      import java.io.IOException;
      import java.io.ObjectInputStream;
      import java.io.ObjectOutputStream;
      
      public class ByteObjConverter {
          /**
           * 使用IO的inputstream流将byte[]转换为object
           * @param bytes
           * @return
           */
          public static Object byteToObject(byte[] bytes) {
              Object obj = null;
              ByteArrayInputStream bi = new ByteArrayInputStream(bytes);
              ObjectInputStream oi = null;
              try {
                  oi = new ObjectInputStream(bi);
                  obj = oi.readObject();
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  try {
                      bi.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  try {
                      oi.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
              return obj;
          }
          /**
           * 使用IO的outputstream流将object转换为byte[]
           * @param bytes
           * @return
           */
          public static byte[] objectToByte(Object obj) {
              byte[] bytes = null;
              ByteArrayOutputStream bo = new ByteArrayOutputStream();
              ObjectOutputStream oo = null;
              try {
                  oo = new ObjectOutputStream(bo);
                  oo.writeObject(obj);
                  bytes = bo.toByteArray();
              } catch (Exception e) {
                  e.printStackTrace();
              } finally {
                  try {
                      bo.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
                  try {
                      oo.close();
                  } catch (IOException e) {
                      e.printStackTrace();
                  }
              }
              return bytes;
          }
      }
      
  • ServerHandler

    • 
      import io.netty.channel.ChannelHandlerContext;
      import io.netty.channel.ChannelInboundHandlerAdapter;
      import cn.itcast_03_netty.sendobject.bean.Person;
      
      public class EchoServerHandler extends ChannelInboundHandlerAdapter {
      
          @Override
          public void channelRead(ChannelHandlerContext ctx, Object msg)
                  throws Exception {
              Person person = (Person) msg;
              System.out.println(person.getName());
              System.out.println(person.getAge());
              System.out.println(person.getSex());
          }
      
          @Override
          public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
              System.out.println("server 读取数据完毕..");
              ctx.flush();//刷新后才将数据发出到SocketChannel
          }
      
          @Override
          public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
                  throws Exception {
              cause.printStackTrace();
              ctx.close();
          }
      
      }
      
  • server

    • import io.netty.bootstrap.ServerBootstrap;
      import io.netty.channel.Channel;
      import io.netty.channel.ChannelFuture;
      import io.netty.channel.ChannelInitializer;
      import io.netty.channel.EventLoopGroup;
      import io.netty.channel.nio.NioEventLoopGroup;
      import io.netty.channel.socket.nio.NioServerSocketChannel;
      import cn.itcast_03_netty.sendobject.coder.PersonDecoder;
      
      /**
       * • 配置服务器功能,如线程、端口 • 实现服务器处理程序,它包含业务逻辑,决定当有一个请求连接或接收数据时该做什么
       * 
       *
       */
      public class EchoServer {
      
          private final int port;
      
          public EchoServer(int port) {
              this.port = port;
          }
      
          public void start() throws Exception {
              EventLoopGroup eventLoopGroup = null;
              try {
                  //创建ServerBootstrap实例来引导绑定和启动服务器
                  ServerBootstrap serverBootstrap = new ServerBootstrap();
                  //创建NioEventLoopGroup对象来处理事件,如接受新连接、接收数据、写数据等等
                  eventLoopGroup = new NioEventLoopGroup();
                  //指定通道类型为NioServerSocketChannel,一种异步模式,OIO阻塞模式为OioServerSocketChannel
                  //设置InetSocketAddress让服务器监听某个端口已等待客户端连接。
                  serverBootstrap.group(eventLoopGroup).channel(NioServerSocketChannel.class).localAddress("localhost",port)
                  .childHandler(new ChannelInitializer<Channel>() {
                      //设置childHandler执行所有的连接请求
                      @Override
                      protected void initChannel(Channel ch) throws Exception {
                          //注册解码的handler
                          ch.pipeline().addLast(new PersonDecoder());  //IN1  反序列化
                          //添加一个入站的handler到ChannelPipeline  
                          ch.pipeline().addLast(new EchoServerHandler());   //IN2
                      }
                          });
                  // 最后绑定服务器等待直到绑定完成,调用sync()方法会阻塞直到服务器完成绑定,然后服务器等待通道关闭,因为使用sync(),所以关闭操作也会被阻塞。
                  ChannelFuture channelFuture = serverBootstrap.bind().sync();
                  System.out.println("开始监听,端口为:" + channelFuture.channel().localAddress());
                  channelFuture.channel().closeFuture().sync();
              } finally {
                  eventLoopGroup.shutdownGracefully().sync();
              }
          }
      
          public static void main(String[] args) throws Exception {
              new EchoServer(20000).start();
          }
      }
      
  • clientHandler

    • 
      import io.netty.buffer.ByteBuf;
      import io.netty.channel.ChannelHandlerContext;
      import io.netty.channel.SimpleChannelInboundHandler;
      import cn.itcast_03_netty.sendobject.bean.Person;
      
      public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {
          // 客户端连接服务器后被调用
          @Override
          public void channelActive(ChannelHandlerContext ctx) throws Exception {
               Person person = new Person();
                  person.setName("angelababy");
                  person.setSex("girl");
                  person.setAge(18);
                  ctx.write(person);
                  ctx.flush();
          }
      
          // • 从服务器接收到数据后调用
          @Override
          protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg)
                  throws Exception {
              System.out.println("client 读取server数据..");
              // 服务端返回消息后
              ByteBuf buf = (ByteBuf) msg;
              byte[] req = new byte[buf.readableBytes()];
              buf.readBytes(req);
              String body = new String(req, "UTF-8");
              System.out.println("服务端数据为 :" + body);
          }
      
          // • 发生异常时被调用
          @Override
          public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause)
                  throws Exception {
              System.out.println("client exceptionCaught..");
              // 释放资源
              ctx.close();
          }
      }
      
  • client

    • 
      import io.netty.bootstrap.Bootstrap;
      import io.netty.channel.ChannelFuture;
      import io.netty.channel.ChannelInitializer;
      import io.netty.channel.EventLoopGroup;
      import io.netty.channel.nio.NioEventLoopGroup;
      import io.netty.channel.socket.SocketChannel;
      import io.netty.channel.socket.nio.NioSocketChannel;
      
      import java.net.InetSocketAddress;
      
      import cn.itcast_03_netty.sendobject.coder.PersonEncoder;
      
      /**
       * • 连接服务器 • 写数据到服务器 • 等待接受服务器返回相同的数据 • 关闭连接
       *
       */
      public class EchoClient {
      
          private final String host;
          private final int port;
      
          public EchoClient(String host, int port) {
              this.host = host;
              this.port = port;
          }
      
          public void start() throws Exception {
              EventLoopGroup nioEventLoopGroup = null;
              try {
                  // 创建Bootstrap对象用来引导启动客户端
                  Bootstrap bootstrap = new Bootstrap();
                  // 创建EventLoopGroup对象并设置到Bootstrap中,EventLoopGroup可以理解为是一个线程池,这个线程池用来处理连接、接受数据、发送数据
                  nioEventLoopGroup = new NioEventLoopGroup();
                  // 创建InetSocketAddress并设置到Bootstrap中,InetSocketAddress是指定连接的服务器地址
                  bootstrap.group(nioEventLoopGroup)//
                          .channel(NioSocketChannel.class)//
                          .remoteAddress(new InetSocketAddress(host, port))//
                          .handler(new ChannelInitializer<SocketChannel>() {//
                                      // 添加一个ChannelHandler,客户端成功连接服务器后就会被执行
                                      @Override
                                      protected void initChannel(SocketChannel ch)
                                              throws Exception {
                                          // 注册编码的handler
                                          ch.pipeline().addLast(new PersonEncoder());  //out
                                          //注册处理消息的handler
                                          ch.pipeline().addLast(new EchoClientHandler());   //in
                                      }
                                  });
                  // • 调用Bootstrap.connect()来连接服务器
                  ChannelFuture f = bootstrap.connect().sync();
                  // • 最后关闭EventLoopGroup来释放资源
                  f.channel().closeFuture().sync();
              } finally {
                  nioEventLoopGroup.shutdownGracefully().sync();
              }
          }
      
          public static void main(String[] args) throws Exception {
              new EchoClient("localhost", 20000).start();
          }
      }
      
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,117评论 4 362
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,328评论 1 293
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 108,839评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,007评论 0 206
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,384评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,629评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,880评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,593评论 0 198
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,313评论 1 243
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,575评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,066评论 1 260
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,392评论 2 253
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,052评论 3 236
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,082评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,844评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,662评论 2 274
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,575评论 2 270

推荐阅读更多精彩内容

  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 134,100评论 18 139
  • 6.2 Channel实现 ![Netty_Channel类图][2] Channel的类图比较清晰。我们主要分析...
    Hypercube阅读 8,448评论 6 19
  • RPC框架远程调用的实现方式在原理上是比较简单的,即将调用的方法(接口名、方法名、参数类型、参数)序列化之后发送到...
    谜碌小孩阅读 3,052评论 0 13
  • 重新学习笑来老师《通往财富自由之路》笔记专题(2) ——你拥有最宝贵的财富是什么 一、讲了什么 这篇文章打磨的概念...
    从零再次起步阅读 213评论 0 0
  • 2014年毕业,至今为止已工作3年多差不多4年了,期间一直不知道自己到底想做什么,一直摸爬滚打的换了3份工作,一份...
    欣籽阅读 523评论 0 0