编者注
由于json是字符进行传送,尤其是传送float参数,会直接导致数据传输量暴增。则需要使用二进制化的跨语言传输方法。编者找到Google Protocol Buffers,Apache Thrift,Apache avro。
Google Protocol Buffers
Google Protocol Buffers 官网 - 需要翻墙
Git地址
安装
通过Readme,安装包在如下url Protocol Compiler Installation
由于protobuf-all-x.x.x.tar.gz需要进行重新编译,则本作者下载的是
protoc-3.5.1-win32.zip
解压缩后,能够看到如下结构
│ readme.txt
│
├─bin
│ protoc.exe
│
└─include
└─google
└─protobuf
│ any.proto
│ api.proto
│ descriptor.proto
│ duration.proto
│ empty.proto
│ field_mask.proto
│ source_context.proto
│ struct.proto
│ timestamp.proto
│ type.proto
│ wrappers.proto
│
└─compiler
plugin.proto
可以看到最重要的是protoc.exe 这个程序,下列的*.proto是代码模板。
版本确认
#protoc.exe --version
libprotoc 3.5.1
Gradle Protobuf
build.gradle配置
// 引入插件
apply plugin: 'com.google.protobuf'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.google.protobuf:protobuf-gradle-plugin:0.8.3'
}
}
// protobuf 配置
protobuf {
// 代码创建目录,而不是生成在build/generated下
generatedFilesBaseDir = "$projectDir/src"
// protoc执行位置
protoc {
path = 'C:\\build\\protoc\\protoc-3.5.1-win32\\bin\\protoc'
}
generateProtoTasks {
all().each {
task ->
task.builtins {
remove java
remove python
}
// 多语言同时输出
task.builtins {
cpp {}
}
task.builtins {
java {}
}
task.builtins {
csharp {}
}
task.builtins {
python {}
}
}
}
}
通过执行generateProto
来生成代码
Java代码测试
序列化反序列化package
compile group: 'com.google.protobuf', name: 'protobuf-java', version: '3.5.1'
compile group: 'com.google.protobuf', name: 'protobuf-java-util', version: '3.5.1'
由此可以看到具有两个包,一个名字叫做Core,另一个叫做util。
IDEA插件安装
File->Settings->Plugins
搜索Protobuf Support
其他重要内容
支持树状结构嵌套
float长度为32bit,int会随着数值的大小变化而变化,但是很大的话会变成40bit
支持泛型
只能够生成一个java文件
Apache Thrift
下载
IDEA插件安装
File->Settings->Plugins
搜索Thrift Support
Thrift Types - thrift类型(翻译)
Thrift类型允许程序员使用尽可能多的本地类型,不管使用何种开发语言进行工作。请以Thrift白皮书作为基础依据。Thrift IDL提供类型的描述,在目标平台上生成的代码描述。
Base Types - 基础类型
我们选择的基础类型是以简朴为目标,而不是让类型变得丰富。重点关注所有编程预演的关键类型。
- bool:布尔类型
- byte:8位有符号整数
- i16:16位有符号整数
- i32:32位有符号整数
- i64:64位有符号整数
- double:64位浮点数
- string:UTF-8编码的字符串
注意:无符号整数类型的缺失。这是由于需要编程预演没有无符号整数类型。
Special Types - 特殊类型
binary:为编码的byte队列
注意:binary是字符串类型的特殊表示,增加与Java更好的交互性。
Struct - 结构
Thrift Struct定义对象 - 基本上等同于面向对象当中的类,但是没有继承。Struct具有一组强类型字段,每个字段都有唯一名称作为标识。字段可以有各种辅助描述(数字字段的ID,可选的默认值,等等)将会在Thrift IDL中描述。
Containers - 容器
容器类型是强类型,将会映射到开发语言的本地容器类型。
具备三种类型:
- list:有序元素列表。自动转换为:STL vector,Java ArrayList,脚本语言的本地数组
- set :无序不重复的元素列表。自动转换为:STL set,Java HashSet,Python的set等等。注意:PHP不支持sets,在PHP视为list
- map :严格不重复key与对应的value。自动转换为:STL map,Java HashMap,PHP associative array, Python/Ruby dictionary,等。虽然提供了默认值,但是类型映射并没有固定。添加自定义代码生成器指令,以允许在各种指定的语言中替换自定义类型。
容器的元素可是任何thrift有效类型。
注意:为了最大限度的兼容性,映射的key类型应该是基本类型,而不是struct和container类型。有些语言的本地化map实现不支持复杂的key类型。另外,JSON协议仅仅支持基础类型。
Exceptions - 异常
Exception在功能上等同于struct,比struct多出的部分是及那个会继承本地语言的异常部分。以便和任何预演的异常处理做无缝集成。
Services - 服务
与本地调研无关
Thrift 三种协议
TBinaryProtocol 二进制协议
TCompactProtocol 紧凑型协议
TDenseProtocol 稠密型协议
通过牺牲Thrift协议当中的字段顺序和数据类型来减少数据流量,提高速度
Apache Avro
原始类型长度
- null: no value
- boolean: a binary value
- int: 32-bit signed integer
- long: 64-bit signed integer
- float: single precision (32-bit) IEEE 754 floating-point number
- double: double precision (64-bit) IEEE 754 floating-point number
- bytes: sequence of 8-bit unsigned bytes string: unicode character sequence
IDEA插件安装
File->Settings->Plugins
搜索Apache Avro Support
附录
Protocol Buffer vs Thrift vs Avro
来源:oschina
链接:https://my.oschina.net/u/52678/blog/1610054