一、什么是Nettyjava
Netty 是一个基于 JAVA NIO 类库的异步通讯框架,它的架构特色是:异步非阻塞、基于事件驱动、高性能、高可靠性和高可定制性。算法
二、Netty应用场景编程
1.分布式开源框架中dubbo、Zookeeper,RocketMQ底层rpc通信使用就是netty。json
2.游戏开发中,底层使用netty通信。bootstrap
三、为何选择netty数组
四、netty案例安全
服务端服务器
package com.zhang.netty; import io.netty.bootstrap.ServerBootstrap; import io.netty.channel.ChannelFuture; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; 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.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; public class HelloServer { /** * 服务端监听的端口地址 */ private static final int portNumber = 7878; public static void main(String[] args) throws InterruptedException { EventLoopGroup bossGroup = new NioEventLoopGroup(); EventLoopGroup workerGroup = new NioEventLoopGroup(); try { ServerBootstrap b = new ServerBootstrap(); b.group(bossGroup, workerGroup); b.channel(NioServerSocketChannel.class); b.childHandler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); // 以("\n")为结尾分割的 解码器 pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); // 字符串解码 和 编码 pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); // 本身的逻辑Handler pipeline.addLast("handler", new HelloServerHandler()); } }); // 服务器绑定端口监听 ChannelFuture f = b.bind(portNumber).sync(); // 监听服务器关闭监听 f.channel().closeFuture().sync(); // 能够简写为 /* b.bind(portNumber).sync().channel().closeFuture().sync(); */ } finally { bossGroup.shutdownGracefully(); workerGroup.shutdownGracefully(); } } }
package com.zhang.netty; import java.net.InetAddress; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; public class HelloServerHandler extends SimpleChannelInboundHandler<String> { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { // 收到消息直接打印输出 System.out.println(ctx.channel().remoteAddress() + " Say : " + msg); // 返回客户端消息 - 我已经接收到了你的消息 ctx.writeAndFlush("Received your message:"+msg+"\n"); } /* * 覆盖 channelActive 方法 在channel被启用的时候触发 (在创建链接的时候) * */ @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("RamoteAddress : " + ctx.channel().remoteAddress() + " active !"); ctx.writeAndFlush( "Welcome to " + InetAddress.getLocalHost().getHostName() + " service!\n"); super.channelActive(ctx); } }
客户端网络
package com.zhang.netty; import io.netty.bootstrap.Bootstrap; import io.netty.channel.Channel; import io.netty.channel.ChannelInitializer; import io.netty.channel.ChannelPipeline; import io.netty.channel.EventLoopGroup; import io.netty.channel.nio.NioEventLoopGroup; import io.netty.channel.socket.ServerSocketChannel; import io.netty.channel.socket.SocketChannel; import io.netty.channel.socket.nio.NioSocketChannel; import io.netty.handler.codec.DelimiterBasedFrameDecoder; import io.netty.handler.codec.Delimiters; import io.netty.handler.codec.string.StringDecoder; import io.netty.handler.codec.string.StringEncoder; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; public class HelloClient { public static String host = "127.0.0.1"; public static int port = 7878; /** * @param args * @throws InterruptedException * @throws IOException */ public static void main(String[] args) throws InterruptedException, IOException { EventLoopGroup group = new NioEventLoopGroup(); try { Bootstrap b = new Bootstrap(); b.group(group) .channel(NioSocketChannel.class) .handler(new ChannelInitializer<SocketChannel>() { protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); //这个地方的 必须和服务端对应上。不然没法正常解码和编码 pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter())); pipeline.addLast("decoder", new StringDecoder()); pipeline.addLast("encoder", new StringEncoder()); // 客户端的逻辑 pipeline.addLast("handler", new HelloClientHandler()); } }); // 链接服务端 Channel ch = b.connect(host, port).sync().channel(); // 控制台输入 BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); for (;;) { String line = in.readLine(); if (line == null) { continue; } /* * 向服务端发送在控制台输入的文本 并用"\r\n"结尾 * 之因此用\r\n结尾 是由于咱们在handler中添加了 DelimiterBasedFrameDecoder 帧解码。 * 这个解码器是一个根据\n符号位分隔符的解码器。因此每条消息的最后必须加上\n不然没法识别和解码 * */ ch.writeAndFlush(line + "\r\n"); } } finally { group.shutdownGracefully(); } } }
package com.zhang.netty; import io.netty.channel.ChannelHandlerContext; import io.netty.channel.SimpleChannelInboundHandler; public class HelloClientHandler extends SimpleChannelInboundHandler<String> { @Override protected void channelRead0(ChannelHandlerContext ctx, String msg) throws Exception { System.out.println("Server say : " + msg); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { System.out.println("Client active "); super.channelActive(ctx); } @Override public void channelInactive(ChannelHandlerContext ctx) throws Exception { System.out.println("Client close "); super.channelInactive(ctx); } }
五、什么是粘包/拆包多线程
一个完整的业务可能会被TCP拆分红多个包进行发送,也有可能把多个小的包封装成一个大的数据包发送,这个就是TCP的拆包和封包问题。下面能够看一张图,是客户端向服务端发送包:
第一种状况,Data1和Data2都分开发送到了Server端,没有产生粘包和拆包的状况。
第二种状况,Data1和Data2数据粘在了一块儿,打成了一个大的包发送到Server端,这个状况就是粘包。
第三种状况,Data2被分离成Data2_1和Data2_2,而且Data2_1在Data1以前到达了服务端,这种状况就产生了拆包。
因为网络的复杂性,可能数据会被分离成N多个复杂的拆包/粘包的状况,因此在作TCP服务器的时候就须要首先解决拆包
解决办法:
消息定长,报文大小固定长度,不够空格补全,发送和接收方遵循相同的约定,这样即便粘包了经过接收方编程实现获取定长报文也能区分。
sc.pipeline().addLast(new FixedLengthFrameDecoder(10));
包尾添加特殊分隔符,例如每条报文结束都添加回车换行符(例如FTP协议)或者指定特殊字符做为报文分隔符,接收方经过特殊分隔符切分报文区分。
//使用特殊字符 ByteBuf buf = Unpooled.copiedBuffer("ha".getBytes()); sc.pipeline().addLast(new DelimiterBasedFrameDecoder(1024, buf)); //使用\n pipeline.addLast("framer", new DelimiterBasedFrameDecoder(8192, Delimiters.lineDelimiter()));
六、序列化定义
序列化(serialization)就是将对象序列化为二进制形式(字节数组),通常也将序列化称为编码(Encode),主要用于网络传输、数据持久化等;
反序列化(deserialization)则是将从网络、磁盘等读取的字节数组还原成原始对象,以便后续业务的进行,通常也将反序列化称为解码(Decode),主要用于网络传输对象的解码,以便完成远程调用。
七、序列化协议
Java默认提供的序列化机制,须要序列化的Java对象只须要实现 Serializable / Externalizable 接口并生成序列化ID,这个类就可以经过 ObjectInput 和 ObjectOutput 序列化和反序列化。可是Java默认提供的序列化有不少问题,主要有如下几个缺点:
八、影响序列化性能的关键因素
九、几种流行的序列化协议比较
XML
XML(Extensible Markup Language)是一种经常使用的序列化和反序列化协议, 它历史悠久,从1998年的1.0版本被普遍使用至今。
优势:人机可读性好,可指定元素或特性的名称
缺点:
使用场景:当作配置文件存储数据,实时数据转换
JSON
JSON(JavaScript Object Notation, JS 对象标记) 是一种轻量级的数据交换格式。它基于 ECMAScript (w3c制定的js规范)的一个子集, JSON采用与编程语言无关的文本格式,可是也使用了类C语言(包括C, C++, C#, Java, JavaScript, Perl, Python等)的习惯,简洁和清晰的层次结构使得 JSON 成为理想的数据交换语言。
优势:
缺点:
适用场景(可替代XML):
Fastjson
Fastjson是一个Java语言编写的高性能功能完善的JSON库。它采用一种“假定有序快速匹配”的算法,把JSON Parse的性能提高到极致。
优势:
缺点:
适用场景:
Thrift
Thrift并不只仅是序列化协议,而是一个RPC框架。它可让你选择客户端与服务端之间传输通讯协议的类别,即文本(text)和二进制(binary)传输协议, 为节约带宽,提供传输效率,通常状况下使用二进制类型的传输协议。
优势:
缺点:
适用场景:分布式系统的RPC解决方案