Netty4详解二:开发第一个Netty应用程序

来源:互联网 发布:搜索引擎数据库设计 编辑:程序博客网 时间:2024/04/30 09:12
既然是入门,那我们就在这里写一个简单的Demo,客户端发送一个字符串到服务器端,服务器端接收字符串后再发送回客户端。

2.1、配置开发环境

1.安装JDK
2.去官网下载jar包
(或者通过pom构建)
2.2、认识下Netty的Client和Server
     一个Netty应用模型,如下图所示,但需要明白一点的是,我们写的Server会自动处理多客户端请求,理论上讲,处理并发的能力决定于我们的系统配置及JDK的极限。   

  1. Client连接到Server端
  2. 建立链接发送/接收数据
  3. Server端处理所有Client请求
     这里有一个形象的比喻来形容Netty客户端和服务器端的交互模式,比如把你比作一个Client,把山比作一个Server,你走到山旁,就是和山建立了链接,你向山大喊了一声,就代表向山发送了数据,你的喊声经过山的反射形成了回声,这个回声就是服务器的响应数据。如果你离开,就代表断开了链接,当然你也可以再回来。好多人可以同时向山大喊,他们的喊声也一定会得到山的回应。
2.3 写一个Netty Server
     一个NettyServer程序主要由两部分组成:
  • BootsTrapping:配置服务器端基本信息
  • ServerHandler:真正的业务逻辑处理
2.3.1 BootsTrapping的过程:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package NettyDemo.echo.server;  
  2.   
  3. import io.netty.bootstrap.ServerBootstrap;  
  4. import io.netty.channel.ChannelFuture;  
  5. import io.netty.channel.ChannelInitializer;  
  6. import io.netty.channel.EventLoopGroup;  
  7. import io.netty.channel.nio.NioEventLoopGroup;  
  8. import io.netty.channel.socket.SocketChannel;  
  9. import io.netty.channel.socket.nio.NioServerSocketChannel;  
  10. import java.net.InetSocketAddress;  
  11. import NettyDemo.echo.handler.EchoServerHandler;  
  12. public class EchoServer {  
  13.     private static final int port = 8080;  
  14.     public void start() throws InterruptedException {  
  15.         ServerBootstrap b = new ServerBootstrap();// 引导辅助程序  
  16.         EventLoopGroup group = new NioEventLoopGroup();// 通过nio方式来接收连接和处理连接  
  17.         try {  
  18.             b.group(group);  
  19.             b.channel(NioServerSocketChannel.class);// 设置nio类型的channel  
  20.             b.localAddress(new InetSocketAddress(port));// 设置监听端口  
  21.             b.childHandler(new ChannelInitializer<SocketChannel>() {//有连接到达时会创建一个channel  
  22.                         protected void initChannel(SocketChannel ch) throws Exception {  
  23.                             // pipeline管理channel中的Handler,在channel队列中添加一个handler来处理业务  
  24.                             ch.pipeline().addLast("myHandler"new EchoServerHandler());  
  25.                         }  
  26.                     });  
  27.             ChannelFuture f = b.bind().sync();// 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功  
  28.             System.out.println(EchoServer.class.getName() + " started and listen on " + f.channel().localAddress());  
  29.             f.channel().closeFuture().sync();// 应用程序会一直等待,直到channel关闭  
  30.         } catch (Exception e) {  
  31.             e.printStackTrace();  
  32.         } finally {  
  33.             group.shutdownGracefully().sync();//关闭EventLoopGroup,释放掉所有资源包括创建的线程  
  34.         }  
  35.     }  
  36.     public static void main(String[] args) {  
  37.         try {  
  38.             new EchoServer().start();  
  39.         } catch (InterruptedException e) {  
  40.             e.printStackTrace();  
  41.         }  
  42.     }  
  43. }  
     1. 创建一个ServerBootstrap实例
     2. 创建一个EventLoopGroup来处理各种事件,如处理链接请求,发送接收数据等。
     3. 定义本地InetSocketAddress( port)好让Server绑定
     4. 创建childHandler来处理每一个链接请求   
     5. 所有准备好之后调用ServerBootstrap.bind()方法绑定Server

2.3.2 业务逻辑ServerHandler:

     要想处理接收到的数据,我们必须继承ChannelInboundHandlerAdapter接口,重写里面的MessageReceive方法,每当有数据到达,此方法就会被调用(一般是Byte类型数组),我们就在这里写我们的业务逻辑:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package NettyDemo.echo.handler;  
  2.   
  3. import io.netty.buffer.Unpooled;  
  4. import io.netty.channel.ChannelFutureListener;  
  5. import io.netty.channel.ChannelHandlerContext;  
  6. import io.netty.channel.ChannelInboundHandlerAdapter;  
  7. import io.netty.channel.ChannelHandler.Sharable;  
  8. /** 
  9.  * Sharable表示此对象在channel间共享 
  10.  * handler类是我们的具体业务类 
  11.  * */  
  12. @Sharable//注解@Sharable可以让它在channels间共享  
  13. public class EchoServerHandler extends ChannelInboundHandlerAdapter{  
  14.     public void channelRead(ChannelHandlerContext ctx, Object msg) {   
  15.         System.out.println("server received data :" + msg);   
  16.         ctx.write(msg);//写回数据,  
  17.     }   
  18.     public void channelReadComplete(ChannelHandlerContext ctx) {   
  19.         ctx.writeAndFlush(Unpooled.EMPTY_BUFFER) //flush掉所有写回的数据  
  20.         .addListener(ChannelFutureListener.CLOSE); //当flush完成后关闭channel  
  21.     }   
  22.     public void exceptionCaught(ChannelHandlerContext ctx,Throwable cause) {   
  23.         cause.printStackTrace();//捕捉异常信息  
  24.         ctx.close();//出现异常时关闭channel   
  25.     }     
  26. }  

2.3.3关于异常处理:

     我们在上面程序中也重写了exceptionCaught方法,这里就是对当异常出现时的处理。

2.4 写一个Netty Client

一般一个简单的Client会扮演如下角色:
  1. 连接到Server
  2. 向Server写数据
  3. 等待Server返回数据
  4. 关闭连接
4.4.1 BootsTrapping的过程:

     和Server端类似,只不过Client端要同时指定连接主机的IP和Port。
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package NettyDemo.echo.client;  
  2.   
  3. import io.netty.bootstrap.Bootstrap;  
  4. import io.netty.channel.ChannelFuture;  
  5. import io.netty.channel.ChannelFutureListener;  
  6. import io.netty.channel.ChannelInitializer;  
  7. import io.netty.channel.EventLoopGroup;  
  8. import io.netty.channel.nio.NioEventLoopGroup;  
  9. import io.netty.channel.socket.SocketChannel;  
  10. import io.netty.channel.socket.nio.NioSocketChannel;  
  11.   
  12. import java.net.InetSocketAddress;  
  13.   
  14. import NettyDemo.echo.handler.EchoClientHandler;  
  15.   
  16. public class EchoClient {  
  17.     private final String host;  
  18.     private final int port;  
  19.   
  20.     public EchoClient(String host, int port) {  
  21.         this.host = host;  
  22.         this.port = port;  
  23.     }  
  24.   
  25.     public void start() throws Exception {  
  26.         EventLoopGroup group = new NioEventLoopGroup();  
  27.         try {  
  28.             Bootstrap b = new Bootstrap();  
  29.             b.group(group);  
  30.             b.channel(NioSocketChannel.class);  
  31.             b.remoteAddress(new InetSocketAddress(host, port));  
  32.             b.handler(new ChannelInitializer<SocketChannel>() {  
  33.   
  34.                 public void initChannel(SocketChannel ch) throws Exception {  
  35.                     ch.pipeline().addLast(new EchoClientHandler());  
  36.                 }  
  37.             });  
  38.             ChannelFuture f = b.connect().sync();  
  39.             f.addListener(new ChannelFutureListener() {  
  40.                   
  41.                 public void operationComplete(ChannelFuture future) throws Exception {  
  42.                     if(future.isSuccess()){  
  43.                         System.out.println("client connected");  
  44.                     }else{  
  45.                         System.out.println("server attemp failed");  
  46.                         future.cause().printStackTrace();  
  47.                     }  
  48.                       
  49.                 }  
  50.             });  
  51.             f.channel().closeFuture().sync();  
  52.         } finally {  
  53.             group.shutdownGracefully().sync();  
  54.         }  
  55.     }  
  56.   
  57.     public static void main(String[] args) throws Exception {  
  58.       
  59.         new EchoClient("127.0.0.1"3331).start();  
  60.     }  
  61. }  
     1. 创建一个ServerBootstrap实例
     2. 创建一个EventLoopGroup来处理各种事件,如处理链接请求,发送接收数据等。
     3. 定义一个远程InetSocketAddress好让客户端连接
     4. 当连接完成之后,Handler会被执行一次   
     5. 所有准备好之后调用ServerBootstrap.connect()方法连接Server
4.4.2 业务逻辑ClientHandler:

  我们同样继承一个SimpleChannelInboundHandler来实现我们的Client,我们需要重写其中的三个方法:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. package NettyDemo.echo.handler;  
  2.   
  3. import io.netty.buffer.ByteBuf;  
  4. import io.netty.buffer.ByteBufUtil;  
  5. import io.netty.buffer.Unpooled;  
  6. import io.netty.channel.ChannelHandlerContext;  
  7. import io.netty.channel.SimpleChannelInboundHandler;  
  8. import io.netty.channel.ChannelHandler.Sharable;  
  9. import io.netty.util.CharsetUtil;  
  10.   
  11. @Sharable  
  12. public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {  
  13.     /** 
  14.      *此方法会在连接到服务器后被调用  
  15.      * */  
  16.     public void channelActive(ChannelHandlerContext ctx) {  
  17.         ctx.write(Unpooled.copiedBuffer("Netty rocks!", CharsetUtil.UTF_8));  
  18.     }  
  19.     /** 
  20.      *此方法会在接收到服务器数据后调用  
  21.      * */  
  22.     public void channelRead0(ChannelHandlerContext ctx, ByteBuf in) {  
  23.         System.out.println("Client received: " + ByteBufUtil.hexDump(in.readBytes(in.readableBytes())));  
  24.     }  
  25.     /** 
  26.      *捕捉到异常  
  27.      * */  
  28.     public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {  
  29.         cause.printStackTrace();  
  30.         ctx.close();  
  31.     }  
  32.   
  33. }  

    其中需要注意的是channelRead0()方法,此方法接收到的可能是一些数据片段,比如服务器发送了5个字节数据,Client端不能保证一次全部收到,比如第一次收到3个字节,第二次收到2个字节。我们可能还会关心收到这些片段的顺序是否可发送顺序一致,这要看具体是什么协议,比如基于TCP协议的字节流是能保证顺序的。
    还有一点,在Client端我们的业务Handler继承的是SimpleChannelInboundHandler,而在服务器端继承的是ChannelInboundHandlerAdapter,那么这两个有什么区别呢?最主要的区别就是SimpleChannelInboundHandler在接收到数据后会自动release掉数据占用的Bytebuffer资源(自动调用Bytebuffer.release())。而为何服务器端不能用呢,因为我们想让服务器把客户端请求的数据发送回去,而服务器端有可能在channelRead方法返回前还没有写完数据,因此不能让它自动release。
0 0
原创粉丝点击