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

** 既然是入门,那我们就在这里写一个简单的Demo,客户端发送一个字符串到服务器端,服务器端接收字符串后再发送回客户端。**

2.1、配置开发环境

1.安装JDK

2.去官网下载jar包

(或者通过pom构建)

2.2、认识下Netty的Client和Server

一个Netty应用模型,如下图所示,但需要明白一点的是,我们写的Server会自动处理多客户端请求,理论上讲,处理并发的能力决定于我们的系统配置及JDK的极限。

image
  1. Client连接到Server端

  2. 建立链接发送/接收数据

  3. Server端处理所有Client请求

    这里有一个形象的比喻来形容Netty客户端和服务器端的交互模式,比如把你比作一个Client,把山比作一个Server,你走到山旁,就是和山建立了链接,你向山大喊了一声,就代表向山发送了数据,你的喊声经过山的反射形成了回声,这个回声就是服务器的响应数据。如果你离开,就代表断开了链接,当然你也可以再回来。好多人可以同时向山大喊,他们的喊声也一定会得到山的回应。

2.3 写一个Netty Server

一个NettyServer程序主要由两部分组成:

  • BootsTrapping:配置服务器端基本信息
  • ServerHandler:真正的业务逻辑处理
    2.3.1 BootsTrapping的过程:
  
import io.netty.bootstrap.ServerBootstrap;  
import io.netty.channel.ChannelFuture;  
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;  
  
/** 
 *  
 * Netty中,通讯的双方建立连接后,会把数据按照ByteBuf的方式进行传输, 
 * 例如http协议中,就是通过HttpRequestDecoder对ByteBuf数据流进行处理,转换成http的对象。 
 *  
 */  
public class SimpleServer {  
    private int port;  
  
    public SimpleServer(int port) {  
        this.port = port;  
    }  
  
    public void run() throws Exception {  
        //EventLoopGroup是用来处理IO操作的多线程事件循环器  
        //bossGroup 用来接收进来的连接  
        EventLoopGroup bossGroup = new NioEventLoopGroup();   
        //workerGroup 用来处理已经被接收的连接  
        EventLoopGroup workerGroup = new NioEventLoopGroup();  
        try {  
            //启动 NIO 服务的辅助启动类  
            ServerBootstrap b = new ServerBootstrap();   
             b.group(bossGroup,workerGroup);
            b.channel(NioServerSocketChannel.class);// 设置nio类型的channel  
            b.localAddress(new InetSocketAddress(port));// 设置监听端口  
            b.childHandler(new ChannelInitializer<SocketChannel>() {//有连接到达时会创建一个channel  
                        protected void initChannel(SocketChannel ch) throws Exception {  
                            // pipeline管理channel中的Handler,在channel队列中添加一个handler来处理业务  
                            ch.pipeline().addLast("myHandler", new SimpleServerHandler());  
                        }  
                    });  
            ChannelFuture f = b.bind().sync();// 配置完成,开始绑定server,通过调用sync同步方法阻塞直到绑定成功  
            System.out.println(EchoServer.class.getName() + " started and listen on " + f.channel().localAddress());  
            f.channel().closeFuture().sync();// 应用程序会一直等待,直到channel关闭  
        } finally {  
            workerGroup.shutdownGracefully();  
            bossGroup.shutdownGracefully();  
        }  
    }  
      
    public static void main(String[] args) throws Exception {  
        new SimpleServer(9999).run();  
    }  
}  
  1. 创建一个ServerBootstrap实例
  2. 创建一个EventLoopGroup来处理各种事件,如处理链接请求,发送接收数据等。
  3. 定义本地InetSocketAddress( port)好让Server绑定
  4. 创建childHandler来处理每一个链接请求
  5. 所有准备好之后调用ServerBootstrap.bind()方法绑定Server

2.3.2 业务逻辑ServerHandler:

package com.yingjun.netty.server;  
  
import io.netty.buffer.ByteBuf;  
import io.netty.channel.ChannelHandlerContext;  
import io.netty.channel.ChannelInboundHandlerAdapter;  
  
public class SimpleServerHandler extends ChannelInboundHandlerAdapter {  
  
    @Override  
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {  
        System.out.println("SimpleServerHandler.channelRead");  
        ByteBuf result = (ByteBuf) msg;  
        byte[] result1 = new byte[result.readableBytes()];  
        // msg中存储的是ByteBuf类型的数据,把数据读取到byte[]中  
        result.readBytes(result1);  
        String resultStr = new String(result1);  
        // 接收并打印客户端的信息  
        System.out.println("Client said:" + resultStr);  
        // 释放资源,这行很关键  
        result.release();  
  
        // 向客户端发送消息  
        String response = "hello client!";  
        // 在当前场景下,发送的数据必须转换成ByteBuf数组  
        ByteBuf encoded = ctx.alloc().buffer(4 * response.length());  
        encoded.writeBytes(response.getBytes());  
        ctx.write(encoded);  
        ctx.flush();  
    }  
  
    @Override  
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {  
        // 当出现异常就关闭连接  
        cause.printStackTrace();  
        ctx.close();  
    }  
  
    @Override  
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {  
        ctx.flush();  
    }  
} 

2.4 写一个Netty Client

一般一个简单的Client会扮演如下角色:
连接到Server
向Server写数据
等待Server返回数据
关闭连接

package com.yingjun.netty.server;  
  
import io.netty.bootstrap.Bootstrap;  
import io.netty.bootstrap.ServerBootstrap;  
import io.netty.channel.ChannelFuture;  
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;  
import io.netty.channel.socket.nio.NioSocketChannel;  
  
public class SimpleClient {  
      
    public void connect(String host, int port) throws Exception {  
        EventLoopGroup workerGroup = new NioEventLoopGroup();  
  
        try {  
            Bootstrap b = new Bootstrap();  
            b.group(workerGroup);  
            b.channel(NioSocketChannel.class);  
            b.option(ChannelOption.SO_KEEPALIVE, true);  
            b.handler(new ChannelInitializer<SocketChannel>() {  
                @Override  
                public void initChannel(SocketChannel ch) throws Exception {  
                    ch.pipeline().addLast(new SimpleClientHandler());  
                }  
            });  
  
            // Start the client.  
            ChannelFuture f = b.connect(host, port).sync();  
  
            // Wait until the connection is closed.  
            f.channel().closeFuture().sync();  
        } finally {  
            workerGroup.shutdownGracefully();  
        }  
    }  
      
    public static void main(String[] args) throws Exception {  
        SimpleClient client=new SimpleClient();  
        client.connect("127.0.0.1", 9999);  
    }  
}  
  1. 创建一个ServerBootstrap实例
  2. 创建一个EventLoopGroup来处理各种事件,如处理链接请求,发送接收数据等。
  3. 定义一个远程InetSocketAddress好让客户端连接
  4. 当连接完成之后,Handler会被执行一次
  5. 所有准备好之后调用ServerBootstrap.connect()方法连接Server
package com.yingjun.netty.server;  
  
import io.netty.buffer.ByteBuf;  
import io.netty.channel.ChannelHandlerContext;  
import io.netty.channel.ChannelInboundHandlerAdapter;  
  
public class SimpleClientHandler extends ChannelInboundHandlerAdapter {  
  
    @Override  
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {  
        System.out.println("SimpleClientHandler.channelRead");    
        ByteBuf result = (ByteBuf) msg;    
        byte[] result1 = new byte[result.readableBytes()];    
        result.readBytes(result1);    
        System.out.println("Server said:" + new String(result1));    
        result.release();    
    }  
  
    @Override  
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {  
        // 当出现异常就关闭连接  
        cause.printStackTrace();  
        ctx.close();  
    }  
  
      
    // 连接成功后,向server发送消息    
    @Override    
    public void channelActive(ChannelHandlerContext ctx) throws Exception {    
        String msg = "hello Server!";    
        ByteBuf encoded = ctx.alloc().buffer(4 * msg.length());    
        encoded.writeBytes(msg.getBytes());    
        ctx.write(encoded);    
        ctx.flush();    
    }    
}  

运行结果:

1.  SimpleClientHandler.channelRead  
2.  Server said:hello client!  
3.  ------------------------------------------  
4.  SimpleServerHandler.channelRead  
5.  Client said:hello Server!  
6.  SimpleServerHandler.channelRead  
7.  Client said:hello Server!

参考:
https://blog.csdn.net/zzy7075/article/details/52095852
https://blog.csdn.net/suifeng3051/article/details/25238243

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。