网络编程 -- 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资源). ( ...
随机推荐
- unity3d 射线的原理,基础用法
射线:射线是3D世界中一个点向一个方向发射的一条无终点的线,在发射轨迹中与其他物体发生碰撞时,它将停止发射 . Ray射线类和RaycastHit射线投射信息类是射线中常用的两个工具类. 用途:射线多 ...
- ios 类别(category)
定义 类别(category)是Objective-C语言的新特性,为现有的类添加新方法的方式.局限性:1.无法添加新的实例变量.2.与类本身的方法名称冲突.当名称冲突时,类别具有更高的优先级.作用: ...
- Delphi 开发ActiveX控件(非ActiveForm)
Delphi 开发ActiveX控件(非ActiveForm) Q:为什么不采用ActiveForm工程?通过它可以快速开发带窗体控件,创建过程也非常简单(都不用考虑安全接口问题),很省事! A:如果 ...
- Python中文转拼音代码(支持全拼和首字母缩写)
本文的代码,从https://github.com/cleverdeng/pinyin.py升级得来,针对原文的代码,做了以下升级: 1 2 3 4 1.可以传入参数firstcode:如果为 ...
- nodejs sass安装报错一招解决
背景: 这个问题不是一天两天了,有时候是网速不行,有时候是被墙了,有时候是github把node-sass的包转移目录导致下载失败. Cannot download "https://git ...
- 【Javascript Demo】图片瀑布流实现
瀑布流就是像瀑布一样的网站——丰富的网站内容,特别是绚美的图片会让你流连忘返.你在浏览网站的时候只需要轻轻滑动一下鼠标滚轮,一切的美妙的图片精彩便可呈现在你面前.瀑布流网站是新兴的一种网站模式——她的 ...
- windows多线程同步--临界区
推荐参考博客:秒杀多线程第五篇 经典线程同步 关键段CS 关于临界区的观念,一般操作系统书上面都有. 适用范围:它只能同步一个进程中的线程,不能跨进程同步.一般用它来做单个进程内的代码快同步,效率 ...
- Windows 7用U盘安装CentOS 7
已经有Windows 7,准备再安装一个CentOS 7. 1. 划分磁盘空间 从磁盘上划分安装CentOS 7的安装空间.如果有多个硬盘的话,一定要在主硬盘上划分2G的空间(其实1G就够了)作为bo ...
- Android——SQLite/数据库 相关知识总结贴
android SQLite简介 http://www.apkbus.com/android-1780-1-1.html Android SQLite基础 http://www.apkbus.com/ ...
- CAS Maven
https://wiki.jasig.org/display/CASUM/Best+Practice+-+Setting+Up+CAS+Locally+using+the+Maven2+WAR+Ove ...