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编解码的更多相关文章

  1. (中级篇 NettyNIO编解码开发)第八章-Google Protobuf 编解码-2

    8.1.2    Protobuf编解码开发 Protobuf的类库使用比较简单,下面我们就通过对SubscrjbeReqProto进行编解码来介绍Protobuf的使用. 8-1    Protob ...

  2. (中级篇 NettyNIO编解码开发)第八章-Google Protobuf 编解码-1

    Google的Protobuf在业界非常流行,很多商业项目选择Protobuf作为编解码框架,这里一起回顾一下Protobuf    的优点.(1)在谷歌内部长期使用,产品成熟度高:(2)跨语言,支持 ...

  3. Netty游戏服务器之四protobuf编解码和黏包处理

    我们还没讲客户端怎么向服务器发送消息,服务器怎么接受消息. 在讲这个之前我们先要了解一点就是tcp底层存在粘包和拆包的机制,所以我们在进行消息传递的时候要考虑这个问题. 看了netty权威这里处理的办 ...

  4. Netty对常用编解码的支持

    参考文献:极客时间傅健老师的<Netty源码剖析与实战>Talk is cheap.show me the code! Netty对编解码的支持 打开Netty的源码,它对很多的编码器都提 ...

  5. 编解码-protobuf

    Google的Protobuf在业界非常流行,很多商业项目选择Protobuf作为编解码框架,Protobuf的优点. (1)在谷歌内部长期使用,产品成熟度高: (2)跨语言,支持多种语言,包括C++ ...

  6. 【转】Netty系列之Netty编解码框架分析

    http://www.infoq.com/cn/articles/netty-codec-framework-analyse/ 1. 背景 1.1. 编解码技术 通常我们也习惯将编码(Encode)称 ...

  7. Netty系列之Netty编解码框架分析

    1. 背景 1.1. 编解码技术 通常我们也习惯将编码(Encode)称为序列化(serialization),它将对象序列化为字节数组,用于网络传输.数据持久化或者其它用途. 反之,解码(Decod ...

  8. 《精通并发与Netty》学习笔记(04 - Google Protobuf介绍)

    一 .Google Protobuf 介绍 protobuf是google团队开发的用于高效存储和读取结构化数据的工具,是Google的编解码技术,在业界十分流行,通过代码生成工具可以生成不同语言版本 ...

  9. netty源码解解析(4.0)-19 ChannelHandler: codec--常用编解码实现

    数据包编解码过程中主要的工作就是:在编码过程中进行序列化,在解码过程中从Byte流中分离出数据包然后反序列化.在MessageToByteEncoder中,已经解决了序列化之后的问题,ByteToMe ...

随机推荐

  1. hdu4453

    题解: splay模板 删除,翻转等等 代码: #include<cstdio> #include<cstring> #include<cmath> #includ ...

  2. 《APUE》第7章 进程环境-读书笔记

    一.main函数. main函数的原型如下.argc是命令行参数的数目,argv是指向参数的各个指针所构成的数组. int main(int argc, char *argv[]) 当内核执行C程序时 ...

  3. SpringInAction-- 配置Profile Bean

    Profile Bean 使用场景描述: 在开发软件的时候,在数据库方面,往往不是一个库就能解决的,一般分为开发库.测试库.生产库,在这些库设置链接的时候,也会配置其对应的数据. 现有一种方式,就是单 ...

  4. Linux系统在启动过程中mbr主引导程序被破坏的解决方案

    首先,mbr主引导程序被破坏是指系统在启动过程中,磁头找不到/boot分区(windows的启动分区在c盘). 1)下面我们模拟主引导分区被破坏的情况:(在启动分区划分446M的存储大小) 2)重启( ...

  5. Mybatis的执行过程

    1.Mybatis的作用 Mybatis的主要作用可以用下面的一段代码解释 Class.forName("com.mysql.jdbc.Driver"); Connection c ...

  6. Happy Swifting!

    Happy Swifting! 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 ...

  7. 在Golang中获取系统的磁盘空间内存占用

    获取磁盘占用情况(Linux/Mac下有效) import ( "syscall" ) type DiskStatus struct { All uint64 `json:&quo ...

  8. CSS Div固定在网页顶部、底部、左侧、右侧

    Div固定在网页顶部 .header { width:100%; height:80px; background-color:#FAFAFA; position:fixed; top:; left:; ...

  9. Eclipse web工程 部署 三种方式 1

    Eclipse web工程 部署 三种方式 1.run on 前提: 安装 好 eclipse.jdk.tomcat 然后 新建一个 web工程 注意此处Default output folder 最 ...

  10. BZOJ2687 交与并/BZOJ2369 区间【决策单调性优化DP】【分治】

    Description 对于一个区间集合 {A1,A2--Ak}(K>1,Ai不等于Aj(i不等于J),定义其权值 S=|A1∪A2∪--AK|*|A1∩A2--∩Ak| 即它们的交区间的长度乘 ...