protobuf的簡單使用


操作系統 : CentOS7.3.1611_x64

gcc版本 :4.8.5

go 版本 : go1.8.3 linux/amd64

Python 版本 : 2.7.5

libprotoc : 2.5.0

Protobuf是Google開發一種數據描述語言,能夠將結構化數據序列化,可用於數據存儲、通信協議等方面。

首頁: https://developers.google.com/protocol-buffers/

文檔: https://developers.google.com/protocol-buffers/docs/overview

github地址: https://github.com/google/protobuf

這里記錄了C++、Go和Python語言的簡單使用示例,更多內容請參考官網手冊。

准備工作

安裝protobuf:

yum install protobuf protobuf-compiler protobuf-c-devel protobuf-devel

寫proto文件(addr.book.proto):

package addr;
message book
{
   required int32     id = 1;
   required string    str = 2;
   optional int32     opt = 3;
}

C++示例

寫文件操作(writer.cpp):

https://github.com/mike-zhang/cppExamples/blob/master/protobufOpt/example1/writer.cpp

讀文件操作(reader.cpp):

https://github.com/mike-zhang/cppExamples/blob/master/protobufOpt/example1/reader.cpp

Makefile文件:

CC = g++
CFLAGS = -g -O2 -Wall
SRC_DIR=.
DST_DIR=libs

all:
    make genproto
    make writer
    make reader

genproto:
    mkdir -p $(DST_DIR)
    protoc -I=$(SRC_DIR) --cpp_out=$(DST_DIR) addr.book.proto

writer:
    $(CC) -o writer writer.cpp $(DST_DIR)/addr.book.pb.cc -I$(DST_DIR) -lprotobuf

reader:
    $(CC) -o reader reader.cpp $(DST_DIR)/addr.book.pb.cc -I$(DST_DIR) -lprotobuf

clean:
    rm -rf $(DST_DIR)
    rm -f writer reader log
    rm -f *.o

運行效果:

[root@localhost proto1]# ./writer
[root@localhost proto1]# ./reader
101
book1
[root@localhost proto1]#

Go示例代碼

安裝goprotobuf:

yum install golang-googlecode-goprotobuf.x86_64

將proto文件轉換為go代碼(genPbCode.sh):

#! /bin/sh

mkdir -p src/addr
protoc -I=. --go_out=src/addr addr.book.proto

修改protobuf路徑:

vi src/addr/addr.book.pb.go

import proto "code.google.com/p/goprotobuf/proto"

修改為:

import proto "github.com/golang/protobuf/proto"

添加PATH(臨時使用時可以這么操作):

export GOPATH=$GOPATH:$PWD

寫文件操作(write.go):

https://github.com/mike-zhang/goExamples/blob/master/protobufOpt/protoTest1/write.go

讀文件操作(reader.go):

https://github.com/mike-zhang/goExamples/blob/master/protobufOpt/protoTest1/read.go

運行效果:

[root@localhost proto3]# go run write.go
[root@localhost proto3]# go run read.go
id:11 str:"testMsg11"
[root@localhost proto3]#

Python示例

需要安裝protobuf包:

pip install protobuf

將proto文件轉換為python代碼(genPbCode.sh):

#! /bin/sh

protoc -I=. --python_out=. addr.book.proto
touch addr/__init__.py

寫文件操作(write.py):

https://github.com/mike-zhang/pyExamples/blob/master/protobufOpt/example1/write.py

讀文件操作(reader.py):

https://github.com/mike-zhang/pyExamples/blob/master/protobufOpt/example1/reader.py

運行效果:

(py27env) [root@localhost proto2]# ./genPbCode.sh
(py27env) [root@localhost proto2]# python write.py
(py27env) [root@localhost proto2]# python reader.py
id: 1
str: "testMsg1"

(py27env) [root@localhost proto2]#

好,就這些了,希望對你有幫助。

本文github地址:

https://github.com/mike-zhang/mikeBlogEssays/blob/master/2018/20180201_protobuf的簡單使用.rst

歡迎補充


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM