Netty--Google Protobuf编解码
Google Protobuf是一种轻便高效的结构化数据存储格式,可以用于结构化数据序列化。它很适合做数据存储或 RPC 数据交换格式。可用于通讯协议、数据存储等领域的语言无关、平台无关、可扩展的序列化结构数据格式。
编译安装:
下载Java版 https://github.com/google/protobuf/releases
tar -zxvf xxx.tar.gz
./configure
make
make install
protoc --version
mvn install
mvn package
使用:
Google Protobuf支持复杂的POJO对象的编解码,而这些代码是自动生成的。
首先写文件Person.proto ,定义程序中需要处理的结构化数据,在 protobuf 的中,结构化数据被称为 Message。
syntax="proto3";
package com.luangeng.netty.protobuf; message Person {
string username=1; //
int32 age=2; //
string sex=3; //
}
执行命令
protoc -I=. --java_out=. Person.proto
格式: protoc -I=$SRC_DIR --cpp_out=$DST_DIR $SRC_DIR/***.proto
即在当前目录下生成了Java文件PersonOutClass.java, 将它引入到工程并需要添加POM依赖
<dependency>
<groupId>com.google.protobuf</groupId>
<artifactId>protobuf-java</artifactId>
<version>3.5.0</version>
</dependency>
--
然后做一个简单的编解码测试:
public class TestProtoBuf { //编码为byte数组
private static byte[] encoder(PersonOuterClass.Person p) {
return p.toByteArray();
} //从byte数组解码
private static PersonOuterClass.Person decoder(byte[] b) throws InvalidProtocolBufferException {
return PersonOuterClass.Person.parseFrom(b);
} //使用builder实例来设置属性
private static PersonOuterClass.Person create(){
PersonOuterClass.Person.Builder builder = PersonOuterClass.Person.newBuilder();
builder.setAge(10);
builder.setSex("man");
builder.setUsername("luangeng");
return builder.build();
} public static void main(String[] args) throws InvalidProtocolBufferException {
PersonOuterClass.Person p = create();
Q.p("before----------\n" + p.toString()); PersonOuterClass.Person p2 = decoder(encoder(p));
Q.p("afetr-----------\n" + p2.toString());
} }
---
输出结果:
before----------
username: "luangeng"
age: 10
sex: "man"
afetr-----------
username: "luangeng"
age: 10
sex: "man"
在Netty中使用Google Protobuf实例:
Server端:
public class EchoServer {
public static void main(String[] args) {
new EchoServer().bind(8080);
} public void bind(int port) {
EventLoopGroup bossGroup = new NioEventLoopGroup();
EventLoopGroup workerGroup = new NioEventLoopGroup();
try {
ServerBootstrap bootstrap = new ServerBootstrap();
bootstrap.group(bossGroup, workerGroup)
.channel(NioServerSocketChannel.class)
.option(ChannelOption.SO_BACKLOG, 100)
.childHandler(new ChannelInitializer<SocketChannel>() {
@Override
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
ch.pipeline().addLast(new ProtobufDecoder(PersonOuterClass.Person.getDefaultInstance()));
ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
ch.pipeline().addLast(new ProtobufEncoder());
ch.pipeline().addLast(new EchoServerHandler());
}
});
ChannelFuture future = bootstrap.bind(port).sync();
System.out.println("server started");
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("server shuting down");
bossGroup.shutdownGracefully();
workerGroup.shutdownGracefully();
}
}
} public class EchoServerHandler extends ChannelInboundHandlerAdapter {
int count = 0; @Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
PersonOuterClass.Person p = (PersonOuterClass.Person) msg;
System.out.println("Server received " + count++ + " :\n" + p.toString());
ctx.writeAndFlush(msg);
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
---
Client端:
public class EchoClient { public static void main(String[] args) {
new EchoClient().connect("127.0.0.1", 8080);
} public void connect(String host, int port) {
EventLoopGroup group = new NioEventLoopGroup();
try {
Bootstrap bootstrap = new Bootstrap();
bootstrap.group(group).channel(NioSocketChannel.class)
.option(ChannelOption.TCP_NODELAY, true)
.handler(new ChannelInitializer<SocketChannel>() {
protected void initChannel(SocketChannel ch) throws Exception {
ch.pipeline().addLast(new ProtobufVarint32FrameDecoder());
ch.pipeline().addLast(new ProtobufDecoder(PersonOuterClass.Person.getDefaultInstance()));
ch.pipeline().addLast(new ProtobufVarint32LengthFieldPrepender());
ch.pipeline().addLast(new ProtobufEncoder());
ch.pipeline().addLast(new EchoClientHandler());
}
});
ChannelFuture future = bootstrap.connect(host, port).sync();
System.out.println("client started");
future.channel().closeFuture().sync();
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
System.out.println("client shuting down");
group.shutdownGracefully();
}
}
} public class EchoClientHandler extends ChannelInboundHandlerAdapter { private int count = 0; private static PersonOuterClass.Person create(int i) {
PersonOuterClass.Person.Builder builder = PersonOuterClass.Person.newBuilder();
builder.setAge(i);
builder.setSex("man");
builder.setUsername("luangeng" + i);
return builder.build();
} @Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
for (int i = 0; i < 20; i++) {
ctx.writeAndFlush(create(i));
}
} //服务端返回应答信息后调用
@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {
PersonOuterClass.Person p = (PersonOuterClass.Person) msg;
Q.p("Client get " + count++ + " :\n" + p.toString());
} @Override
public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
ctx.flush();
} @Override
public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
cause.printStackTrace();
ctx.close();
}
}
---
执行结果:
client started
Client get 0 :
username: "luangeng0"
sex: "man"
Client get 1 :
username: "luangeng1"
age: 1
sex: "man"
Client get 2 :
username: "luangeng2"
age: 2
sex: "man"
Client get 3 :
username: "luangeng3"
age: 3
sex: "man"
Client get 4 :
username: "luangeng4"
age: 4
sex: "man"
Client get 5 :
username: "luangeng5"
age: 5
sex: "man"
Client get 6 :
username: "luangeng6"
age: 6
sex: "man"
Client get 7 :
username: "luangeng7"
age: 7
sex: "man"
Client get 8 :
username: "luangeng8"
age: 8
sex: "man"
Client get 9 :
username: "luangeng9"
age: 9
sex: "man"
Client get 10 :
username: "luangeng10"
age: 10
sex: "man"
Client get 11 :
username: "luangeng11"
age: 11
sex: "man"
Client get 12 :
username: "luangeng12"
age: 12
sex: "man"
Client get 13 :
username: "luangeng13"
age: 13
sex: "man"
Client get 14 :
username: "luangeng14"
age: 14
sex: "man"
Client get 15 :
username: "luangeng15"
age: 15
sex: "man"
end
Netty--Google Protobuf编解码的更多相关文章
- (中级篇 NettyNIO编解码开发)第八章-Google Protobuf 编解码-2
8.1.2 Protobuf编解码开发 Protobuf的类库使用比较简单,下面我们就通过对SubscrjbeReqProto进行编解码来介绍Protobuf的使用. 8-1 Protob ...
- (中级篇 NettyNIO编解码开发)第八章-Google Protobuf 编解码-1
Google的Protobuf在业界非常流行,很多商业项目选择Protobuf作为编解码框架,这里一起回顾一下Protobuf 的优点.(1)在谷歌内部长期使用,产品成熟度高:(2)跨语言,支持 ...
- Netty游戏服务器之四protobuf编解码和黏包处理
我们还没讲客户端怎么向服务器发送消息,服务器怎么接受消息. 在讲这个之前我们先要了解一点就是tcp底层存在粘包和拆包的机制,所以我们在进行消息传递的时候要考虑这个问题. 看了netty权威这里处理的办 ...
- Netty对常用编解码的支持
参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! Netty对编解码的支持 打开Netty的源码,它对很多的编码器都提 ...
- 编解码-protobuf
Google的Protobuf在业界非常流行,很多商业项目选择Protobuf作为编解码框架,Protobuf的优点. (1)在谷歌内部长期使用,产品成熟度高: (2)跨语言,支持多种语言,包括C++ ...
- 【转】Netty系列之Netty编解码框架分析
http://www.infoq.com/cn/articles/netty-codec-framework-analyse/ 1. 背景 1.1. 编解码技术 通常我们也习惯将编码(Encode)称 ...
- Netty系列之Netty编解码框架分析
1. 背景 1.1. 编解码技术 通常我们也习惯将编码(Encode)称为序列化(serialization),它将对象序列化为字节数组,用于网络传输.数据持久化或者其它用途. 反之,解码(Decod ...
- 《精通并发与Netty》学习笔记(04 - Google Protobuf介绍)
一 .Google Protobuf 介绍 protobuf是google团队开发的用于高效存储和读取结构化数据的工具,是Google的编解码技术,在业界十分流行,通过代码生成工具可以生成不同语言版本 ...
- netty源码解解析(4.0)-19 ChannelHandler: codec--常用编解码实现
数据包编解码过程中主要的工作就是:在编码过程中进行序列化,在解码过程中从Byte流中分离出数据包然后反序列化.在MessageToByteEncoder中,已经解决了序列化之后的问题,ByteToMe ...
随机推荐
- 031——VUE中表单控件处理之使用vue控制input和textarea表单项
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 使用游标、存储过程、pivot 三种方法导入数据
--使用游标循环 if (exists (select * from sys.objects where name = 'Base_RecordTend_Test')) drop proc Base_ ...
- New Concept English there (1)Typing speed exercise
Today,I start learn new concept english there,Mainly for listening practice and typing speed exercis ...
- vue.js 源代码学习笔记 ----- 工具方法 env
/* @flow */ /* globals MutationObserver */ import { noop } from 'shared/util' // can we use __proto_ ...
- azure最佳实践系列1-自我修复的设计
如何设计你的应用,能够在系统错误时做到自我修复?在分布式系统中,会经常遇到错误.硬件也会遇到异常情况.网络有时会出现短暂的错误.整个地区出现了服务中断.即便如此,关于这些问题的方案也是要提前规划的.因 ...
- chrome浏览器使用记录
出现错误 net::ERR_BLOCKED_BY_CLIENT 出现这个错误一般是因为chrome安装了adblocker等这样的插件,这些插件会把路径及文件名中包含广告字样的文字禁止掉,比如:adv ...
- 如何预编译ASP.Net程序
打开Developer Command Prompt,执行命令 aspnet_compiler -v \Target -p 源文件夹地址 -f 目标文件夹地址
- python 怎么画图
1 安装matplotlib: 安装方法:http://www.2cto.com/os/201309/246928.html(其中,安装过程中,tar解压怎么解都有问题.然后就删掉再下载一遍) 2 使 ...
- I.MX6 Linux eGTouch TouchScreen porting
I.MX6 Linux eGTouch TouchScreen porting 一.Download Driver: http://www.eeti.com.tw/drivers_Linux.html ...
- Windows 10 四月更新,文件夹名称也能区分大小写?
Windows 向来是不区分文件和文件夹大小写的,但是从 NTFS 开始却又支持区分文件夹大小写.而 Linux/Mac OS 一向都是区分文件和文件夹大小写的. 本文将推荐 Windows 10 四 ...