標籤:math java.net log 資料 ref ios event alibaba ext
網路編程 -- RPC實現原理 -- 目錄
啦啦啦
V2——Netty -- 使用序列化和還原序列化在網路上傳輸對象
只能傳輸( ByteBuf, FileRegion )兩種類型,因此必須將對象在發送之前進行序列化,放進ByteBuf中,用戶端接收到ByteBuf時,將位元組碼取出,還原序列化成對象。
Class : Server
package lime.pri.limeNio.netty.netty02.exercise;import java.net.InetSocketAddress;import java.util.Date;import com.alibaba.fastjson.JSON;import com.alibaba.fastjson.serializer.SerializerFeature;import io.netty.bootstrap.ServerBootstrap;import io.netty.buffer.ByteBuf;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelFutureListener;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;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.util.CharsetUtil;import lime.pri.limeNio.netty.netty03.entity.User;public class Server { public static void main(String[] args) throws Exception { ServerBootstrap serverBootstrap = new ServerBootstrap(); EventLoopGroup boss = new NioEventLoopGroup(); EventLoopGroup worker = new NioEventLoopGroup(); serverBootstrap.group(boss, worker); serverBootstrap.channel(NioServerSocketChannel.class); serverBootstrap.childHandler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new ChannelHandlerAdapter(){ @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf byteBuf = (ByteBuf) msg; String request = byteBuf.toString(CharsetUtil.UTF_8); System.out.println("用戶端請求資料:" + request); String response = JSON.toJSONString("請求參數不正確",SerializerFeature.WriteClassName); if("Query Date".equalsIgnoreCase(request)){ response = JSON.toJSONString("當前系統時間:" + new Date().toString(),SerializerFeature.WriteClassName); }else if("Query User".equalsIgnoreCase(request)){ response = JSON.toJSONString(new User(1,"lime",new Date()), SerializerFeature.WriteClassName); } byteBuf.clear(); byteBuf.writeBytes(response.getBytes(CharsetUtil.UTF_8)); ChannelFuture channelFuture = ctx.writeAndFlush(byteBuf); channelFuture.addListener(ChannelFutureListener.CLOSE); } }); } }); ChannelFuture channelFuture = serverBootstrap.bind(new InetSocketAddress(9999)).sync(); channelFuture.channel().closeFuture().sync(); boss.close(); worker.close(); }}
Class : Client
package lime.pri.limeNio.netty.netty02.exercise;import java.net.InetSocketAddress;import com.alibaba.fastjson.JSON;import io.netty.bootstrap.Bootstrap;import io.netty.buffer.ByteBuf;import io.netty.buffer.Unpooled;import io.netty.channel.ChannelFuture;import io.netty.channel.ChannelHandlerAdapter;import io.netty.channel.ChannelHandlerContext;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.NioSocketChannel;import io.netty.util.CharsetUtil;public class Client { public static void main(String[] args) throws Exception { for (int i = 0; i < 10; i++) { new Thread() { { setDaemon(false); } public void run() { try { client(); } catch (Exception e) { e.printStackTrace(); } }; }.start(); } } private static void client() throws Exception { Bootstrap bootstrap = new Bootstrap(); EventLoopGroup worker = new NioEventLoopGroup(); bootstrap.group(worker); bootstrap.channel(NioSocketChannel.class); bootstrap.handler(new ChannelInitializer<SocketChannel>() { @Override protected void initChannel(SocketChannel ch) throws Exception { ChannelPipeline pipeline = ch.pipeline(); pipeline.addLast(new ChannelHandlerAdapter() { /** * 預設只捕獲網路連接異常 */ @Override public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception { System.out.println(cause); } @Override public void channelActive(ChannelHandlerContext ctx) throws Exception { String request = null; switch ((int) (Math.random() * 10) % 3) { case 0: request = "Query Date"; break; case 1: request = "Query User"; break; default: request = "Query What?"; break; } ctx.writeAndFlush(Unpooled.buffer().writeBytes(request.getBytes(CharsetUtil.UTF_8))); } @Override public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception { ByteBuf byteBuf = (ByteBuf) msg; System.out.println("服務端響應資料 --> " + JSON.parse(byteBuf.toString(CharsetUtil.UTF_8))); } }); } }); ChannelFuture channelFuture; channelFuture = bootstrap.connect(new InetSocketAddress(9999)).sync(); channelFuture.channel().closeFuture().sync(); worker.close(); }}
Console : Server
用戶端請求資料:Query What?用戶端請求資料:Query User用戶端請求資料:Query User用戶端請求資料:Query Date用戶端請求資料:Query Date用戶端請求資料:Query What?用戶端請求資料:Query Date用戶端請求資料:Query Date用戶端請求資料:Query User用戶端請求資料:Query User
Console : Client
服務端響應資料 --> 當前系統時間:Sat Jun 24 18:21:40 CST 2017服務端響應資料 --> 請求參數不正確服務端響應資料 --> 當前系統時間:Sat Jun 24 18:21:40 CST 2017服務端響應資料 --> 請求參數不正確服務端響應資料 --> 當前系統時間:Sat Jun 24 18:21:40 CST 2017服務端響應資料 --> 當前系統時間:Sat Jun 24 18:21:40 CST 2017服務端響應資料 --> User [id=1, name=lime, birth=Sat Jun 24 18:21:40 CST 2017]服務端響應資料 --> User [id=1, name=lime, birth=Sat Jun 24 18:21:40 CST 2017]服務端響應資料 --> User [id=1, name=lime, birth=Sat Jun 24 18:21:40 CST 2017]服務端響應資料 --> User [id=1, name=lime, birth=Sat Jun 24 18:21:40 CST 2017]
啦啦啦
網路編程 -- RPC實現原理 -- Netty -- 迭代版本V2 -- 對象傳輸