簡介
Google Protocol Buffer( 簡稱 Protobuf) 是 Google 公司內(nèi)部的混合語言數(shù)據(jù)標(biāo)準(zhǔn)躲惰,目前已經(jīng)正在使用的有超過 48,162 種報(bào)文格式定義和超過 12,183 個(gè) .proto 文件书斜。他們用于 RPC 系統(tǒng)和持續(xù)數(shù)據(jù)存儲(chǔ)系統(tǒng)寿谴。
Protocol Buffers 是一種輕便高效的結(jié)構(gòu)化數(shù)據(jù)存儲(chǔ)格式屋厘,可以用于結(jié)構(gòu)化數(shù)據(jù)串行化,或者說序列化高诺。它很適合做數(shù)據(jù)存儲(chǔ)或 RPC 數(shù)據(jù)交換格式踱讨。可用于通訊協(xié)議持偏、數(shù)據(jù)存儲(chǔ)等領(lǐng)域的語言無關(guān)驼卖、平臺(tái)無關(guān)、可擴(kuò)展的序列化結(jié)構(gòu)數(shù)據(jù)格式鸿秆。目前提供了 C++酌畜、Java、Python 三種語言的 API卿叽。
Protocol buffers are a flexible, efficient, automated mechanism for serializing structured data – think XML, but smaller, faster, and simpler. You define how you want your data to be structured once, then you can use special generated source code to easily write and read your structured data to and from a variety of data streams and using a variety of languages. You can even update your data structure without breaking deployed programs that are compiled against the "old" format.
protoc 命令使用
使用步驟如下:
- 下載:https://github.com/protocolbuffers/protobuf/releases/tag/v3.6.1
- 解壓: 解壓到任意目錄
-
配置path: 在windows中配置對應(yīng)的path路徑桥胞,如下圖所示windows path路徑配置
- 執(zhí)行protoc命令:
protoc --proto_path=IMPORT_PATH1 --proto_path=IMPORT_PATH2 --cpp_out=DST_DIR --java_out=DST_DIR --python_out=DST_DIR --go_out=DST_DIR --ruby_out=DST_DIR --objc_out=DST_DIR --csharp_out=DST_DIR path/to/file.proto
命令行執(zhí)行:F:\protoc-3.6.1-win32\bin>protoc.exe -I=F:\protoc-3.6.1-win32\include -I=. --ja
a_out=. addressbook.proto
配置對應(yīng)的include環(huán)境變量
未了避免每次都要指定protocol buffer對應(yīng)的include路徑贩虾,可以在windows 環(huán)境變量中新建一個(gè)環(huán)境變量:PROTOC_INCLUDE,其值指向protocol buffer對應(yīng)的include目錄沥阱,如我的就是F:\protoc-3.6.1-win32\include缎罢,對應(yīng)配置截圖如下:bat文件運(yùn)行
新建一個(gè)bat文件protoc.bat,內(nèi)容如下所示:
@echo off
@echo off
protoc.exe -I=%PROTOC_INCLUDE% %*
像protoc.exe一樣使用protoc.bat喳钟,唯一的好處就是不用指定protocol buffer對應(yīng)的include目錄了屁使,運(yùn)行命令截圖如下所示: