一、protobuf应用场景

protobuf 在Java中的应用场景可以是序列化和反序列化,流可以通过文件或者通过网络TCP/UDP等方式传输。
新建一个.proto文件

syntax = "proto3";
option java_package = "com.test.proto";
option java_outer_classname = "RequestInfo";
message SearchRequest {
repeated Param query_param = 1;
int32 page_number = 2;
int32 result_per_page = 3;
} message Param {
int32 age = 1;
string name = 2;
}

二、注意事项
1、使用proto3,必须显示申明:syntax = "proto3" ,否则使用proto2
2、生成的Java类的包可添加: option java_package = "com.test.proto";
3、生成的Java类的类名可添加:option java_outer_classname = "RequestInfo";

三、序列化和反序列化测试

通过idea插件生成Java类RequestInfo.java(生成方式详见上篇文章:Google Protocol Buffer 的使用(一)),将数据序列化到本地文件。

1、序列化测试:

/**
* 序列化
* @author monkjavaer
* @date 2019/01/05 09:27
*/
public class WriteTest { public static void main(String[] args) throws IOException {
//构造的RequestInfo信息
RequestInfo.SearchRequest.Builder searchRequest = RequestInfo.SearchRequest.newBuilder();
searchRequest.setPageNumber(1);
searchRequest.setResultPerPage(10);
RequestInfo.Param.Builder param = RequestInfo.Param.newBuilder();
param.setAge(25);
param.setName("Lee");
searchRequest.addQueryParam(param);
RequestInfo.SearchRequest request = searchRequest.build();
//写文件
FileOutputStream outputStream = new FileOutputStream(new File("F:\\request.pro"));
request.writeTo(outputStream);
outputStream.close();
}
}

2、 反序列化测试:

/**
* 反序列化
* @author monkjavaer
* @date 2019/01/06 09:43
*/
public class ReadTest {
public static void main(String[] args) throws IOException {
FileInputStream in = new FileInputStream(new File("F:\\request.pro"));
RequestInfo.SearchRequest request = RequestInfo.SearchRequest.parseFrom(in);
System.out.println(request.toString());
}
}

  

Google Protocol Buffer 的使用(二)的更多相关文章

  1. Google Protocol Buffer的安装与.proto文件的定义

    什么是protocol Buffer呢? Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准. 我理解的就是:它是一种轻便高效的结构 ...

  2. Google Protocol Buffer 的编码方式

    Google Protocol Buffer 使用到了两种编码方式:Varints 和 zigzag. 一 Varints 编码 每个 byte 只用 7bit 表示数字,最高位 bit作为标志位,如 ...

  3. Google Protocol Buffer的安装与.proto文件的定义(转)

    转自(https://www.cnblogs.com/yinheyi/p/6080244.html) 什么是protocol Buffer呢? Google Protocol Buffer( 简称 P ...

  4. Google Protocol Buffer 的使用(一)

    一.什么是Google Protocol Buffer下面是官网给的解释:Protocol buffers are a language-neutral, platform-neutral exten ...

  5. Netty使用Google Protocol Buffer完成服务器高性能数据传输

    一.什么是Google Protocol Buffer(protobuf官方网站) 下面是官网给的解释: Protocol buffers are a language-neutral, platfo ...

  6. Google Protocol Buffer 的使用和原理[转]

    本文转自: http://www.ibm.com/developerworks/cn/linux/l-cn-gpb/ Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构 ...

  7. Google Protocol Buffer 的使用

    简介 Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标准,目前已经正在使用的有超过 48,162 种报文格式定义和超过 12,183 ...

  8. 学习Google Protocol buffer之概述

    XML这种属于非常强大的一种格式,能存储任何你想存的数据,而且编辑起来还是比较方便的.致命的缺陷在于比较庞大,在某些情况下,序列化和解析都会成为瓶颈.这种对于实时性很强的应用来说,就不太适合了,想象下 ...

  9. Google Protocol Buffer 的使用和原理

    Google Protocol Buffer 的使用和原理 Protocol Buffers 是一种轻便高效的结构化数据存储格式,可以用于结构化数据串行化,很适合做数据存储或 RPC 数据交换格式.它 ...

随机推荐

  1. 机器学习-随机梯度下降(Stochastic gradient descent)和 批量梯度下降(Batch gradient descent )

    梯度下降(GD)是最小化风险函数.损失函数的一种常用方法,随机梯度下降和批量梯度下降是两种迭代求解思路,下面从公式和实现的角度对两者进行分析,如有哪个方面写的不对,希望网友纠正. 下面的h(x)是要拟 ...

  2. git --删除文件、重命名

    修改最后一次提交 git commit --amend -m “” 删除文件:. git rm <需要删除的文件> 只是删除当前工作目录和暂存区的文件,也就是取消跟踪.在下次提交时不纳入版 ...

  3. 阿里云服务器安装ss使用

    下载安装服务器版shadowsocks yum install epel-release yum update yum install python-setuptools m2crypto super ...

  4. matlab中数据类型

    在MATLAB中有15种基本数据类型,分别是8种整型数据.单精度浮点型.双精度浮点型.逻辑型.字符串型.单元数组.结构体类型和函数句柄.这15种基本数据类型具体如下. 有符号整数型:int8,int1 ...

  5. CREATE SCHEMA - 定义一个新的模式

    SYNOPSIS CREATE SCHEMA schemaname [ AUTHORIZATION username ] [ schema_element [ ... ] ] CREATE SCHEM ...

  6. CAD参数绘制圆(网页版)

    CAD绘制图像的过程中,画圆的情况是非常常见的,用户可以设置圆的圆心位置及半径属性. 主要用到函数说明: _DMxDrawX::DrawCircle 绘制一个圆.详细说明如下: 参数 说明 DOUBL ...

  7. 使用github作为maven仓库

    本文介绍的这种使用 github 作为 maven 仓库的思路主要为: github的项目上创建mvn-repo分支,使用mvn-repo分支作为maven仓库 配 置 pom.xml 使用 targ ...

  8. SQLServer:查询所有外键关联表信息

     --从左到右分别是: 外键约束名,子表名,外键列名,父表名 --use demodtcms--外键信息select fk.name fkname , ftable.name ftablename, ...

  9. ORACLE in与exists语句的区别(一)

    select * from Awhere id in(select id from B) 以上查询使用了in语句,in()只执行一次,它查出B表中的所有id字段并缓存起来.之后,检查A表的id是否与B ...

  10. SCOI2013 密码

    题目描述: Fish是一条生活在海里的鱼.有一天他很无聊,就到处去寻宝.他找到了位于海底深处的宫殿,但是一扇带有密码锁的大门却阻止了他的前进. 通过翻阅古籍,Fish 得知了这个密码的相关信息: 该密 ...