网络编程 -- RPC实现原理 -- Netty -- 迭代版本V2 -- 对象传输
啦啦啦
V2——Netty -- 使用序列化和反序列化在网络上传输对象:需要实现 java.io.Serializable 接口
只能传输( 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 -- 对象传输的更多相关文章
- 网络编程 -- RPC实现原理 -- Netty -- 迭代版本V1 -- 入门应用
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V1——Netty入门应用 Class : NIOServerBootStrap package lime.pri.limeNio.netty.ne ...
- 网络编程 -- RPC实现原理 -- Netty -- 迭代版本V3 -- 编码解码
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——Netty -- pipeline.addLast(io.netty.handler.codec.MessageToMessageCodec ...
- 网络编程 -- RPC实现原理 -- Netty -- 迭代版本V4 -- 粘包拆包
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——Netty -- new LengthFieldPrepender(2) : 设置数据包 2 字节的特征码 new LengthFieldB ...
- 网络编程 -- RPC实现原理 -- 目录
-- 啦啦啦 -- 网络编程 -- RPC实现原理 -- NIO单线程 网络编程 -- RPC实现原理 -- NIO多线程 -- 迭代版本V1 网络编程 -- RPC实现原理 -- NIO多线程 -- ...
- 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V1 -- 本地方法调用
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V1——RPC -- 本地方法调用:不通过网络 入门 1. RPCObjectProxy rpcObjectProxy = new RPCObjec ...
- 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V2 -- 本地方法调用 整合 Spring
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V2——RPC -- 本地方法调用 + Spring 1. 配置applicationContext.xml文件 注入 bean 及 管理 bean ...
- 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V3 -- 远程方法调用 整合 Spring
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V3——RPC -- 远程方法调用 及 null的传输 + Spring 服务提供商: 1. 配置 rpc03_server.xml 注入 服务提供 ...
- 网络编程 -- RPC实现原理 -- RPC -- 迭代版本V4 -- 远程方法调用 整合 Spring 自动注册
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V4——RPC -- 远程方法调用 + Spring 自动注册 服务提供商: 1. 配置 rpc04_server.xml 注入 服务提供商 rpc ...
- 网络编程 -- RPC实现原理 -- NIO多线程 -- 迭代版本V1
网络编程 -- RPC实现原理 -- 目录 啦啦啦 V1——设置标识变量selectionKey.attach(true);只处理一次(会一直循环遍历selectionKeys,占用CPU资源). ( ...
随机推荐
- svn与git操作对比 (未来有空做一个 svn与git实战对比 )
svn是集中式的,git是分布式的,但是我们日常使用的都是按照集中式唯一服务器仓库的方式来去做的,最终我们的代码都要提交到一个唯一仓库中. 他们最大的区别是本地工作拷贝的工作方式不同, 一.svn本地 ...
- IIS6 2.0 4.0 冲突解决 'c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\xxx' -- 'Access is denied. '
今天在阿里云虚拟机上部署新站点后出现下面的错误: Compiler Error Message: CS0016: Could not write to output file 'c:\Windows\ ...
- DbContext SQLite配置文件
<?xml version="1.0" encoding="utf-8"?> <configuration> <configSec ...
- 微软BI 之SSIS 系列 - 通过 OLE DB 连接访问 Excel 2013 以及对不同 Sheet 页的数据处理
文章更新历史 2014年9月7日 - 加入了部分更新内容,在文章最后提到了关于不同 Office Excel 版本间的连接问题. 开篇介绍 这篇文章主要总结在 SSIS 中访问和处理 Excel 数据 ...
- Googlenet 中1*1 卷积核分析
一种简单的解释是用来降维. For example, an image of 200*200 with 50 features on convolution with 20 filters of 1* ...
- asp.net core2->2.1 webapi 进行了重大变更
传统的在 启动时候 使用Mvc路由的配置不再有效.而是基于Attribute的声明标注进行配置路由.
- OpenCV 学习笔记 05 人脸检测和识别 AttributeError: module 'cv2' has no attribute 'face'
1 环境设置: win10 python 3.6.8 opencv 4.0.1 2 尝试的方法 在学习人脸识别中,遇到了没有 cv2 中没有 face 属性.在网上找了几个方法,均没有成功解决掉该问题 ...
- Chrome F12 温故而知新 :因为重定向导致清空Network信息
虽然我以前都是用Fiddler 4来作为解决方案.但实际上可以勾选 [Preserve log]来保存日志 这样就不担心因为页面重定向导致清空了日志了
- (Java编程思想)Thinking in Java
1. 为什么突然想去研读<Thinking in Java>? 最近终于下定决心撸了一本<Thinking in Java>第四版,虽然在此之前我就久闻这本书的大名,但一直未曾 ...
- Socket网络编程--聊天程序(6)
这一小节将增加一个用户的结构体,用于保存用户的用户名和密码,然后发给服务器,然后在服务器进行判断验证.这里就有一个问题,以前讲的就是发送字符串是使用char类型进行传输,然后在服务器进行用同样是字符串 ...